C++ 单链表操作
来源:互联网 发布:风险矩阵图的横纵 编辑:程序博客网 时间:2024/05/22 00:33
include //printf() scanf()
include //srand() rand()
include //malloc() NULL
typedef struct Node
{
int data;
struct Node* next;
}Node,*PNode;
//随机建立产生n个元素的值,建立带表头结点的单链线性表,尾插法
//这个链表的头结点就是head,也可以看作这个链表的名字
PNode CreateList( int n ){ PNode head,p; head=(PNode)malloc(sizeof(Node)); head->data=NULL; p=head; PNode temp; srand((int)time(0)); int tt=rand(); while( n-- ){ temp=(PNode)malloc(sizeof(Node)); temp->data=rand(); p->next=temp; p=temp; } p->next=NULL; return head;}
// 链表的元素个数
int LengthList( PNode head ){ PNode p; p=head; int num=0; while( p->next!=NULL ){ num++; p=p->next; } return num;}
//打印链表
void PrintList(PNode head){ int n=LengthList( head ); PNode p; p=head->next; while( n-- ){ printf("%d\n",p->data); p=p->next; }}
//删除第i个结点,并用e返回它的值
bool DeleteList(PNode head,int i,int &e){ PNode p,q; p=head; int j=1; while( p->next && j<i ){ //找第i-1个结点 p=p->next; j++; } if( !(p->next) || j>i ) return false; q=p->next; e=q->data; p->next=q->next; free(q); return true;}
//在链表第i个位置之前插入新的数据元素e
bool InsertList( PNode head,int i,int e ){ PNode p,q; int j=1; int len=LengthList(head); if( i>len || i <j) return false; p=head; while( p->next && j<i ){ p=p->next; j++; } if( !(p->next)||j>i ) return false; q=(PNode)malloc(sizeof(Node)); q->data=e; q->next=p->next; p->next=q; return true;}
//冒泡法实现单链表的排序
//PNode SortList( PNode head)
void swap( PNode p,PNode q ){ int temp; temp=p->data; p->data=q->data; q->data=temp;}Node* SortList( Node* head){ Node *p,*q; p=head->next; int len=LengthList(head); int i,j; if( len==1 ) return head; for( i=0;i<len-1;i++ ){ p=head->next; for( j=0;j<len-i;j++ ){ if( p->next ){ q=p->next; if( p->data > q->data ){ swap( p,q ); } p=p->next; }else continue; } } return head;}
//实现单链表的逆置
Node * ReverseList(Node * head){ Node *pre,*mid,*next; if( head==NULL || head->next==NULL ) return head; pre=head->next; mid=pre->next; pre->next=NULL; while( mid ){ next=mid->next; pre=mid->next; pre=mid; mid=next; } head->next=pre; return pre;}
//逆置函数2
Node *reverse(Node *head) { Node *p1,*p2,*p3; if (NULL == head || NULL == head->next) { return head; } p1 = head; p2 = p1->next; while (NULL != p2->next) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3; } p2->next = p1; p1 = p2; head->next->next = NULL; head->next = p1; return head; }
//主函数
int main(){ PNode head; head=CreateList(10); int len=LengthList(head); printf("%s%d\n","The length of the List is: ",len); PrintList(head); int e; bool status=DeleteList(head,2,e); if( status ) printf( "The deleted element is %d\n",e ); printf("The length of the List is: %d\n",LengthList(head)); PrintList(head); bool status1=InsertList(head,2,10000); if( status1 ) printf( "The insert element is %d\n",e ); printf("The length of the List is: %d\n",LengthList(head)); PrintList(head); printf("The sorted List is: %d\n",LengthList(head)); head=SortList(head); PrintList(head); printf("The reversed List is: %d\n",LengthList(head)); head=reverse(head); PrintList(head); head=ReverseList(head); printf("The reversed List is: %d\n",LengthList(head)); PrintList(head); system("pause"); return 0;}
阅读全文
0 0
- 单链表的操作(C#)
- c 单链表操作
- c操作单链表
- C单链表操作
- C数据结构 单链表操作
- c语言单链表操作
- C++-----单链表基本操作
- C单链表的操作
- C单链表操作
- 单链表的操作C语言版
- c数据结构之单链表操作
- C语言单链表常见操作
- 【C++】单链表的基本操作
- c语言数据结构--单链表操作
- C语言单链表基本操作
- C++/C移位操作
- [C/C++]文件操作
- C语言操作之单链表操作之删除
- Tomcat优化
- loadrunner Web_类函数之web_button()
- ♪ ♩ ♫ 海的声音-《这个世界不是因为你而存在-谦》
- linux安装配置zookeeper-3.4.10
- Unity3D 5.x 启动过程中 License Error 解决办法
- C++ 单链表操作
- JDBC详细总结
- C语言 if条件判断中包含语句块
- 全排列、子集
- js中Event 对象 target 与 currentTarget的应用
- 【ZJOI2016&&BZOJ4574】【NOIP模拟】作弊(DP&&随机数据)
- 第二章 Java内存区域与内存溢出异常
- 网易疯狂队列
- 2.计算机组成原理笔记二发展应用及相关知识