码迷,mamicode.com
首页 > 编程语言 > 详细

C语言 链表部分

时间:2017-11-11 20:48:45      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:[]   free   logs   产生   str   lib   .com   code   continue   

技术分享技术分享
⒂链表 ①数据转链表(栈转堆) #include
<stdlib.h> struct node { int data; struct node*next; }; void main() { struct node*n1 = (struct node*)malloc(sizeof(struct node)); struct node*n2 = (struct node*)malloc(sizeof(struct node)); struct node*n3 = (struct node*)malloc(sizeof(struct node)); struct node*p=n1; n1->data=20; n1->next=n2; n2->data=30; n2->next=n3; n3->data=40; n3->next=NULL; while(p!=NULL) { printf("%d\n",p->data); p=p->next; } } ②遍历 #include<stdlib.h> typedef struct //结构体起名node { int data; struct node*next; }node; void printALL(node*head)//头结点 { node*p=head; while(p!=NULL) { printf("%d\n",p->data); p=p->next; } } void main() { node*n1 = ( node*)malloc(sizeof( node)); node*n2 = ( node*)malloc(sizeof( node)); node*n3 = ( node*)malloc(sizeof( node)); n1->data=20; n1->next=n2; n2->data=30; n2->next=n3; n3->data=40; n3->next=NULL; printALL(n1); } ③数组转链表 #include<stdlib.h> typedef struct //结构体起名Node { int data; struct node*next; }Node; void printALL(Node*head)//头结点 { Node*p=head; while(p!=NULL) { printf("%d\n",p->data); p=p->next; } } Node*arrToList(int *arr, int len)//传数组首地址和长度,返回值是一个指针即首地址int arr[] { Node*head=NULL; int i; for(i=0;i<len;i++) { printf("%d\n",arr[i]); } return head; } void main() { int myArr[]={3,4,6,8,0,9,1}; int mylen=sizeof(myArr)/sizeof(myArr[0]);//求数组长度 Node*myhead =arrToList(myArr,mylen);//Node指针接收头结点指针 } ④增、删、改、查 #include<stdlib.h> typedef struct node//结构体起名 { int data; struct node*next; }Node; void printALL(Node*head)//头结点 { Node*p=head; while(p!=NULL) { printf("%d\n",p->data); p = p->next; } } Node*arrToList(int* arr, int len) { Node*head=NULL; Node*tail=NULL;//记录最后一个节点便于插入 Node*newNode=NULL; int i; for(i=0;i<len;i++)//循环遍历数组中元素把每一个元素封装成一个节点 { newNode=(Node*)malloc(sizeof(Node));//创建节点 newNode->data=arr[i];//把节点元素赋成数组里相应位置的元素//填入数据 newNode->next=NULL; if(i==0)//第一次创建,第一个节点 { head=newNode; tail=newNode; continue;//防止产生循环链表 } tail->next=newNode;//如不是把此节点放到尾节点之后 tail=newNode;//新建节点作为新的尾节点 } return head;//返回链表头 } void addNode(Node*head, int poslval, int val)//要插入的链表(给链表头即可)、位置(那个值后面插入)、要插入的节点值 { Node*p=head; Node*newNode=(Node*)malloc(sizeof(Node)); while(p!=NULL&&p->data!=poslval) { p=p->next; } if(p==NULL) { printf("无你要找的位置!\n"); return; } newNode->data=val; newNode->next=NULL; newNode->next =p->next;//新建节点指向原有节点的下一节点 p->next =newNode;//新建节点放到P后 } void delNode(Node*head,int delval)//那个链表、删那个节点 { Node*p=head; Node*q=NULL; while(p->next !=NULL&&p->next->data!=delval) { p=p->next; } if(p->next==NULL) { printf("你胡乱删什么\n"); return; } q=p->next; p->next=p->next->next;//p->next=q->next; free(q); q=NULL; } void updateNode(Node* head,int oldval,int newval) { Node*p=head; while(p!=NULL&&p->data!=oldval) { p=p->next; } if(p==NULL) { printf("没有找到待修改节点\n"); return; } p->data=newval; } int queryNode(Node*head,int val)//用0表示不存在,1表示存在 { Node*p=head; while(p!=NULL) { if(p->data=val) { return 1; } p=p->next; } return 0; } void main() { int myArr[]={3,4,6,8,0,9,1}; int mylen=sizeof(myArr)/sizeof(myArr[0]); Node*myHead =arrToList(myArr,mylen); printALL(myHead); addNode(myHead, 6, 888);// delNode(myHead,10);//无待删除节点和有待删除节点;删 printf("__________\n"); updateNode(myHead,6,999);// printALL(myHead); } /*if (queryNode(myHead,4)) printf("存在\n"); else printf("不存在\n"); }*/

 

C语言 链表部分

标签:[]   free   logs   产生   str   lib   .com   code   continue   

原文地址:http://www.cnblogs.com/zmy-149/p/7819766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!