2.代表头的单向链表的基本操作
#include <stdio.h> #include <malloc.h> #define NULL 0 typedef struct node { int data; struct node *next; }ElemSN; ElemSN * creat_link(int ms); //创建一个带表头的链表 void print_link(ElemSN *head); //输出单向链表 void delete_node(ElemSN *head, int x); //删除结点 void insert_node(ElemSN *head, int x); //插入结点 void clear_link(ElemSN *head); //删除链表 int main() { ElemSN *head; int ms, x; printf("Please input node number:"); scanf("%d", &ms); head = creat_link(ms); //创建链表 print_link(head); printf("Please input delete node:"); scanf("%d", &x); delete_node(head, x); //删除结点 print_link(head); printf("Please input insert node:"); scanf("%d", &x); insert_node(head, x); print_link(head); clear_link(head); print_link(head); } ElemSN * creat_link(int ms) //带表头的单向链表除了头结点其余结点 //创建方法一致,所以不用逆向创建 { ElemSN *h = NULL, *p; int i, x; h = p = (ElemSN *)malloc(sizeof(ElemSN)); for(i = 0; i < ms; i++) { p->next = (ElemSN *)malloc(sizeof(ElemSN)); //创建新结点 printf("Please input node data:"); scanf("%d", &x); p->next->data = x; //新结点初始化 p->next->next = NULL; //新结点初始化 p = p->next; } return h; } void print_link(ElemSN *head) { if(NULL == head->next) { printf("Link is null."); } for(head=head->next; head; head = head->next) { printf("%d ", head->data); } printf("\n"); } void delete_node(ElemSN *head, int x) { ElemSN *p, *q; for(p=head, q=head->next; q && q->data != x; p=q, q=q->next){} if(q != NULL) { p->next = q->next; free(q); } } void insert_node(ElemSN *head, int x) { ElemSN *p, *q; if(NULL == head) return; if(NULL == head->next) //链表为空时 { head->next = (ElemSN *)malloc(sizeof(ElemSN)); head->next->next = NULL; head->next->data = x; return; } p = head; q = head->next; if(NULL == q->next) //链表中只有一个元素时 { p->next = (ElemSN *)malloc(sizeof(ElemSN)); p->next->data = x; p->next->next = q; return; } if(q->data >= q->next->data) //递减链表 { for(; q && q->data >= x; p = q, q = q->next){} } else //递增链表 { for(; q && q->data <= x; p = q, q = q->next){} } if(NULL == q) //插入在链表尾部 { q = (ElemSN *)malloc(sizeof(ElemSN)); q->data = x; q->next = NULL; p->next = q; } else if(q == head->next) //插入在表首 { q = (ElemSN *)malloc(sizeof(ElemSN)); q->data = x; q->next = head->next; head->next = q; } else //插入在中间位置 { p->next = (ElemSN *)malloc(sizeof(ElemSN)); p->next->next = q; p->next->data = x; } } void clear_link(ElemSN *head) { ElemSN *p; while(head->next) { p = head->next; head->next = p->next; free(p); } }
原文地址:http://blog.csdn.net/laoniu_c/article/details/37989845