链表——头标法插入
作者:互联网
#include<stdio.h>
#include<stdlib.h>
struct Test { int data; struct Test* next; }; void printList(struct Test* head) { struct Test* point; point = head; while(point != NULL) { printf("%d ",point->data); point = point->next; } } struct Test* InsertfromHead(struct Test* head,struct Test* new) {//头标法插入 if(head == NULL) { head = new; }else { new->next = head; head = new; }
return head;
} struct Test* CreadList(struct Test* head) {//创建结点 struct Test *new = NULL; while(1) { new = (struct Test*)malloc(sizeof(struct Test)); printf("Please you input data to list\n"); scanf("%d",&(new->data)); if(new->data == 0) { printf("you input 0 quit!\n"); free(new); return head; }
head = InsertfromHead(head,new);//将链表头,和要插入的数据传到头标法中 }
} int main() { struct Test* head = NULL;
head = CreadList(head);
printList(head);
return 0; }
struct Test { int data; struct Test* next; }; void printList(struct Test* head) { struct Test* point; point = head; while(point != NULL) { printf("%d ",point->data); point = point->next; } } struct Test* InsertfromHead(struct Test* head,struct Test* new) {//头标法插入 if(head == NULL) { head = new; }else { new->next = head; head = new; }
return head;
} struct Test* CreadList(struct Test* head) {//创建结点 struct Test *new = NULL; while(1) { new = (struct Test*)malloc(sizeof(struct Test)); printf("Please you input data to list\n"); scanf("%d",&(new->data)); if(new->data == 0) { printf("you input 0 quit!\n"); free(new); return head; }
head = InsertfromHead(head,new);//将链表头,和要插入的数据传到头标法中 }
} int main() { struct Test* head = NULL;
head = CreadList(head);
printList(head);
return 0; }
标签:head,struct,point,链表,插入,new,Test,头标,data 来源: https://www.cnblogs.com/ightningmcqueen/p/16693829.html