单链表的实现

来源:互联网 发布:淘宝兼职招聘网 编辑:程序博客网 时间:2024/06/18 11:41
<pre name="code" class="cpp">//单链表的实现+链表的bubble 排序#include "stdafx.h"#include<malloc.h>typedef int status;typedef int elemtype;typedef struct LNode{elemtype          num;struct LNode    *next;}LNode,*LinkList;int List_Length(LinkList L)  /*获取单链表的长度*/{  int len = 0;  LinkList p;p = L->next;  while( p)  {  len++;  p = p->next;  }  return len;  }  void BubbleSort_L(LinkList L)  {  LinkList pre;  LinkList cur;  LinkList next;  int i, j;  i = List_Length(L);  printf("\r\nList length is = %d\n", i);  while(i != 1)  {  pre = L;  cur = L->next;  next = cur->next;  j = i;  i--;  while(j != 1)  {  j--;  if(cur->num  > next->num)  /*前面结点的值大于后面结点的值,交换结点在链表中的位置*/{  cur->next   = next->next;  pre->next   = next;  next->next = cur;  pre = next;  next = cur->next;  }  else  {  pre = cur;  cur = next;  next = next->next;  }  }  }     }  void CreatList_L(LinkList *L,int n)/*逆位序输入n个元素的值,创建带头结点的链表L*/{int i;LinkList p;*L=(LinkList)malloc(sizeof(LNode)); /*创建单链表的head结点*/if (NULL == *L){printf("\r\n Failed to creat the head node.\r\n");return;}(*L)->next=NULL;for(i = n;  i > 0; i--){p=(LinkList)malloc(sizeof(LNode));if(NULL == p){printf("\r\n failed to allocate  list node.]\r\n");return;}scanf("%d",&p->num);p->next=(*L)->next; (*L)->next=p;    /*将新分配的结点插入到head 结点之后*/}}LinkList Creat_List(int n)/*创建一个链表*/{int i;LinkList p,L,head;head=L=(LinkList)malloc(sizeof(LNode));/*创建一个head结点*/if(NULL == head){printf("\r\n Failed to creat the head node.\r\n");return NULL;}for(i = 0; i < n; i++){p=(LinkList)malloc(sizeof(LNode));if(NULL == p){printf("\r\n failed to allocate  list node.\r\n");return NULL;}scanf("%d",&p->num);L->next=p;L=p;}p->next=NULL;return(head);/*返回链表头结点*/}void PrintList(LinkList L)   /*输出链表的函数*/{LinkList p;printf("输出该链表:\n");p=L->next;if(NULL == L->next){printf("\r\n The list is empty.\r\n");}else{while (p){printf("%d ",p->num);p=p->next;}}printf("\n");}status ListInsert_L(LinkList L,int i,elemtype e)/*在带头结点的单链表L中的第i个元素前插入一个值e*/{int j=0;LinkList p,q;p=L;while(p && (j < i-1)){p=p->next;   /*寻址第i-1个结点 */j++;}q = (LinkList)malloc(sizeof(LNode));//申请新结点if(NULL == q){printf("\r\n Failed to allocat the list node.\r\n");return 0;}q->num=e;q->next=p->next; //插入到链表L中 p->next=q;return 1;}status ListDelet_L(LinkList L,int i,elemtype *e)/*在带头结点的单链表L中删除第i个元素并用e返回其值*/{LinkList p,q;int j=0;p=L;while(p->next  && j < i-1){p=p->next;j++;}if ((NULL == p->next) ||(j > i-1)) /*删除位置不合理*/{return 0;}q = p->next;   /*删除并释放结点*/p->next = q->next;*e = q->num;free(q);return 1;}void MergeList_L(LinkList La,LinkList Lb)/*合并两个递增的链表*/{LinkList pa,pb,h,head;head=h=(LinkList)malloc(sizeof(LNode));h->next=NULL;pa=La->next;pb=Lb->next;while(pa&&pb){if(pa->num<=pb->num){h->next=pa;h=h->next;pa=pa->next;}else{h->next=pb;h=h->next;pb=pb->next;}}h->next = pa ? pa:pb;//插入剩余段free(La); //释放La头结点free(Lb);//释放Lb头结点PrintList(head);}int _tmain(int argc, _TCHAR* argv[]){LinkList La,Lb,Lc,Ld;int n,e,k;printf("请输入链表La的长度:\n");scanf("%d",&n);printf("请输入链表La的数据:\n");CreatList_L(&La,n);PrintList(La);printf("对链表La进行冒泡排序:\n");BubbleSort_L(La);printf("冒泡排序后的结果是:\n");PrintList(La);printf("请输入链表Lb的长度:\n");scanf("%d",&n);printf("请输入链表Lb的数据:\n");Lb=Creat_List(n);printf("请输入要Lb插入的数据:\n");scanf("%d",&e);printf("请输入Lb要插入元素的位置:\n");scanf("%d",&n);ListInsert_L(Lb,n,e);PrintList(Lb);printf("请输入Lb要删除的数据的位置:\n");scanf("%d",&n);printf("Lb被删除的元素是:\n");ListDelet_L(La,n,&k);printf("%d\n",k);PrintList(Lb);printf("请输入链表Lc的长度:\n");scanf("%d",&n);printf("请输入链表Lc的数据:\n");Lc=Creat_List(n);printf("请输入链表Ld的长度:\n");scanf("%d",&n);printf("请输入链表Ld的数据:\n");Ld=Creat_List(n);printf("Lc 和 Ld合并后的链表是:\n");MergeList_L(Lc,Ld);return 0;}

1 0