嵌入式-链表尾插法插入
链表通过尾部插入
#include<stdio.h> #include<stdlib.h> struct Test { int data; struct Test * next; }; printfLink(struct Test * head) { while(head!=NULL) { printf("%d\n",head->data); head=head->next; } putchar('\n'); } struct Test * InsertEndNode(struct Test* head,struct Test * new) { struct Test * p=head; if(head==NULL) { head=new; return head; } while(p->next!=NULL) { p=p->next; } p->next=new; return head; } int main() { struct Test *head=(struct Test *)malloc(sizeof(struct Test)); head->data=1; struct Test new1={2,NULL}; struct Test new2={3,NULL}; struct Test new3={4,NULL}; struct Test new4={5,NULL}; struct Test * hp1=InsertEndNode(head,&new1); struct Test * hp2=InsertEndNode(hp1,&new2); struct Test * hp3=InsertEndNode(hp2,&new3); printfLink(hp3); return 0; }
输出结果:
1
2
3
4
4556