单链表的逆置,单链表中插入一个元素,删除单链表等 #include <stdio.h> #include <stdlib.h> typedef struct Node { Node* next; int value; }Node; Node* ReverseLink(Node* head) { Node* cur = NULL; //如果指针没有初始化,结果正确,但程序会崩溃 Node* last = NULL; while(head->next) { cur = head; head = head->next; cur->next = last; last = cur; } head->next = last; return head; } Node* InsertNode(Node* head,int value) { if(head == NULL) { head = (Node*)malloc(sizeof(Node)); if(head == NULL) printf("malloc failed."); else { head->next = NULL; head->value = value; } } else { Node* cur = (Node*)malloc(sizeof(Node)); if(cur == NULL) printf("malloc failed."); else { cur->next = head; head = cur; cur->value = value; } } return head; } Node* FreeLink(Node* head) { while(head->next) { Node* cur = head->next; free(head); head = cur; } return NULL; } void PrintLink(Node* head) { Node* p=head; while(p->next) { printf("%d ",p->value); p = p->next; } printf("%d/n",p->value); } int main() { Node * link =NULL; link = InsertNode(link,1); link = InsertNode(link,2); link = InsertNode(link,3); link = InsertNode(link,4); PrintLink(link); link = ReverseLink(link); PrintLink(link); FreeLink(link); return 0; }