#include<stdio.h> #include<malloc.h> typedef struct List_Node{ int info; struct List_Node *next; }node;//结点结构体 /******************************/ /* 尾插法建立带头结点的单链表 */ /******************************/ node* Creat_Node() { node *head,*pre,*p; int x; head=(node*)malloc(sizeof(node));; head->next=NULL; pre=head; printf("输入各结点的值,以0结束:"); while(EOF!=(scanf("%d",&x))&&x!=0) { p=(node*)malloc(sizeof(node)); p->info=x; p->next=pre->next; pre->next=p; pre=pre->next; } return head; } /******************************/ /* 头插法建立带头结点的单链表 */ /******************************/ node* Build_Node() { node *head,*p; int x; head=(node*)malloc(sizeof(node)); head->info = 0; head->next=NULL; printf("输入各结点的值,以0结束:"); while(EOF!=(scanf("%d",&x))&&x!=0) { p=(node*)malloc(sizeof(node)); p->info=x; p->next=head->next; head->next=p; } return head; } /******************************/ /* 打印单链表 */ /******************************/ void Print_Node(node *head) { node *p=head->next; printf("输出该链表:"); printf("head->info = %d/n",head->info); while(p) { printf("%-5d--->",p->info); p=p->next; } if(p==NULL) { printf("^/n/n/n"); } } int Count_Node(node *head) { node *p=head->next; int num=0; while(p!=NULL) { num++; p=p->next; } return num; } /**********************************/ /* 删除重复 */ /**********************************/ void Delete_Repeat_Node(node *head) { node *p,*pre,*s; pre=head->next; p=pre->next; while(p) { s=p->next; while(s&&s->info!=p->info) { s=s->next; } if(s) { pre->next=p->next; free(p); p=pre->next; } else { pre=p; p=p->next; } } } /************************************/ /* 在Y前插入X */ /************************************/ void Before_y_Insert_x(node* head,int y,int x) { node *pre,*p,*s; pre=head; p=pre->next; while(p&&p->info!=y) { pre=p; p=p ->next; } if(p==NULL) { printf("error!%d不在该链表中/n",y); } else { s=(node*)malloc(sizeof(node)); s->info=x; s->next=p; pre->next=s; } } /************************************/ /* 判断链表是否有序 */ /************************************/ int Is_Sort(node *head) { node *p,*pre; int flag; pre=head->next; p=pre->next; flag=pre->info>p->info?1:0; while(p) { pre=p; p=p->next; if(p) { if(flag!=pre->info>p->info?1:0) { return 0; } } } return 1; } /************************************/ /* 链表反序 */ /************************************/ void convert_Node(node *head) { node *pre,*p=head->next; head->next=NULL; while(p) { pre=p; p=p->next; pre->next=NULL; pre->next=head->next; head->next=pre; } } /****************************************************/ /* 直接插入排序 */ /****************************************************/ void Insert_Sort(node *head) { node *p,*pre,*s,*r; p=head->next; head->next=NULL; while(p) { pre=p->next; r=head; s=head->next; while(s&&s->info<p->info) { r=s; s=s->next; } p->next=r->next; r->next=p; p=pre; } } /****************************************************/ /* 删除头结点,带头结点的情况 */ /****************************************************/ void Remove_Head(node* head) { node*p; p=head->next; printf("%d/n",p->info); head->next=p->next; free(p); } /****************************************************/ /* 删除头结点,不带头结点的情况 */ /****************************************************/ node* Remove_Head_1(node* head) { node*p; p=head->next; free(head); return p; } int main() { int x,y,flag; node *head; /****************************************************/ printf("下面测试《创建链表》:/n"); head=Creat_Node(); Print_Node(head); /****************************************************/ goto rh; /****************************************************/ printf("下面测试《计算节点个数》:/n"); printf("结点个数为:%d/n/n",Count_Node(head)); /****************************************************/ /****************************************************/ printf("下面测试《删除重复》:/n"); Delete_Repeat_Node(head); Print_Node(head); /****************************************************/ /****************************************************/ printf("下面测试《在y前插入x》:/n"); printf("在y前插入x,输入y,x:"); scanf("%d%d",&y,&x); Before_y_Insert_x(head,y,x); Print_Node(head); /****************************************************/ /****************************************************/ /* 判断链表是否有序 */ printf("下面测试《判断链表是否有序》:/n"); flag=Is_Sort(head); if(flag==1) { printf("该链表有序!/n"); } else { printf("该链表无序!/n"); } printf("/n"); /****************************************************/ /****************************************************/ /* 链表反序 */ printf("下面测试《链表反序》:/n"); convert_Node(head); Print_Node(head); /****************************************************/ /****************************************************/ /* 直接插入排序 */ printf("下面测试《直接插入排序》:/n"); Insert_Sort(head); Print_Node(head); /****************************************************/ rh: /****************************************************/ /* 移除头结点 */ printf("下面测试《删除头结点》:/n"); head=Remove_Head_1(head); Print_Node(head); /****************************************************/ return 0; }