***下面是我的代码,在Linux下无问题,在window下运行出错:
#include <stdio.h> #include <stdlib.h> struct Test { int date; struct Test *next; }; void printLink(struct Test *head) { struct Test *point = head; while(point != NULL){ printf("%d ",point->date); point = point->next; } putchar('\n'); } int numberOfLinkNodes(struct Test *head) { struct Test *point = head; int cnt = 0; while(point != NULL){ point = point->next; cnt++; } return cnt; } int findLink(struct Test *head,int date) { struct Test *point = head; printf("The date to be searched is %d :\n",date); while(point != NULL){ if(point->date == date){ return 1; } point = point->next; } return 0; } int insertAfterNode(struct Test *head,int date,struct Test *new) { struct Test *point = head; while(point != NULL){ if(point->date == date){ new->next = point->next; point->next = new; puts("insert OK!"); return 1; } point = point->next; } puts("date not found.."); return 0; } struct Test* insertInFrontOfNode(struct Test *head,int date,struct Test *new) { struct Test *point = head; if(point->date == date){ new->next = head; puts("insert OK!"); return new; } while(point->next != NULL){ if(point->next->date == date){ new->next = point->next; point->next = new; puts("insert OK!"); return head; } point = point->next; } puts("date not found.."); return head; } struct Test* deleteNode(struct Test *head,int date) { struct Test *point = head; printf("The date to be deleted is %d :\n",date); if(point->date == date){ head = head->next; free(point); puts("delete OK!"); return head; } while(point->next != NULL){ if(point->next->date == date){ point->next = point->next->next; puts("delete OK!"); return head; } point = point->next; } puts("date not found.."); return head; } int changeLink(struct Test *head,int date,int newdate) { struct Test *point = head; while(point != NULL){ if(point->date == date) { point->date =newdate; puts("change OK!"); return 1; } point = point->next; } puts("date not found.."); return 0; } struct Test* headInsertion(struct Test *head,struct Test *new) { if(head == NULL){ head = new; }else{ new->next = head; head = new; } return head; } struct Test* headCreateLink(struct Test *head) { struct Test *new; while(1){ new = (struct Test*)malloc(sizeof(struct Test)); puts("please input new date insert in front (quit:0):"); scanf("%d",&(new->date)); if(new->date == 0){ puts("you input '0',so quit!"); free(new); return head; } head = headInsertion(head,new); } } struct Test* tailInsertion(struct Test *head,struct Test *new) { struct Test *point = head; if(head == NULL){ head = new; return head; } while(point->next != NULL){ point = point->next; } point->next = new; return head; } struct Test* tailCreateLink(struct Test *head) { struct Test *new; while(1){ new = (struct Test*)malloc(sizeof(struct Test)); puts("please input new date insert after (quit:0):"); scanf("%d",&(new->date)); if(new->date == 0){ puts("you input '0',so quit!"); free(new); return head; } head = tailInsertion(head,new); } } int main() { int array[] = {1,2,3,4,5}; int i; printf("array traverses:\n"); for(i=0;i<sizeof(array)/sizeof(array[0]);i++){ printf("%d ",array[i]); } putchar('\n'); struct Test *head = NULL; puts("Head insertion:"); head = headCreateLink(head); puts("print date from Head insertion:"); printLink(head); head = NULL; puts("Tail insertion:"); head = tailCreateLink(head); puts("print date from Tail insertion:"); printLink(head); // struct Test t1 = {1,NULL}; struct Test *p = (struct Test*)malloc(sizeof(struct Test)); struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; p->date = 1; // t1.next = &t2; p->next = &t2; t2.next = &t3; t3.next = &t4; t4.next = &t5; // head = &t1; head = p; printf("link traverses:\n"); printLink(head); puts("Number of link nodes:"); int ret = numberOfLinkNodes(head); printf("%d",ret); putchar('\n'); ret = findLink(head,3); if(ret == 1){ puts("find the date!"); }else{ puts("date not found.."); } struct Test new = {111,NULL}; puts("insert after node specified in link:"); insertAfterNode(head,3,&new); printLink(head); struct Test new2 = {222,NULL}; puts("insert in front of node specified in link:"); head = insertInFrontOfNode(head,3,&new2); printLink(head); puts("delete node:"); head = deleteNode(head,5); printLink(head); puts("change node:"); changeLink(head,4,444); printLink(head); return 0; }蹩脚英文,见谅!!