嵌入式-头插法插入节点
#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 * InsertFromHead(struct Test * head) { struct Test * new; new=(struct Test*)malloc(sizeof(struct Test)); printf("input your data\n"); scanf("%d",&(new->data)); if(head==NULL) { head=new; return head; } else { new->next=head; head=new; } return head; } int main() { struct Test * head=NULL; head=InsertFromHead(head); head=InsertFromHead(head); head=InsertFromHead(head); printf("after insert data\n"); printfLink(head); return 0; }
输出结果:
input your data
1
input your data
2
input your data
3
after insert data
3
2
1
4556