zoukankan      html  css  js  c++  java
  • 13.1.22:带头结点的链表结构

      1 /*
      2 1.带头结点L的单链表结构
      3 2.参照严慧敏单链表的链表结构
      4 
      5 Levi
      6      date:13.1.22
      7 */
      8 
      9 #include <stdio.h>
     10 #include "sys/malloc.h"
     11 #include <stdlib.h>
     12 #define ElemType int
     13 
     14 
     15 struct LNode{
     16     ElemType data;
     17     struct LNode * next;
     18 };
     19 
     20 typedef struct LNode * LinkList;
     21 
     22 void InitList(LinkList *L){
     23     *L=(LinkList)malloc(sizeof(struct LNode));
     24     if(!(*L))
     25               printf("Failed to allocate memory for L !!!");
     26     (*L)->next=NULL;
     27 }
     28 
     29 void DestroyList(LinkList *L){
     30     LinkList p;
     31     while(*L){
     32         p=(*L)->next;
     33         free(*L);
     34         *L=p;
     35     }
     36 }
     37 
     38 void ClearList(LinkList L){
     39     LinkList p,q;
     40     p=L->next;
     41     while(p){
     42         q=p->next;
     43         free(p);
     44         p=q;
     45     }
     46     L->next=NULL;
     47 }
     48 
     49 void ListEmpty(LinkList L){
     50     if(L->next)
     51               printf("The LinkList is not NULL\n");
     52     else
     53               printf("The LinkList is NULL\n");
     54               
     55 }
     56 
     57 int ListLength(LinkList L){
     58     int i=1;
     59     LinkList q;
     60     q=L->next;
     61     while(q->next){
     62         q=q->next;
     63         ++i;
     64     }
     65     return i;
     66 }
     67 
     68 int GetElem(LinkList L,int i,ElemType *e){
     69     LinkList p;
     70     p=L->next;
     71     int count=1;
     72     while(p&&count<i){
     73         p=p->next;
     74         ++count;
     75     }
     76     if(!p||count>i){
     77               printf("The GetElem is failure !!!\n");
     78               return -1;
     79     }
     80     *e=p->data;
     81     return 0;
     82 }
     83 
     84 int LocateElem(LinkList L,ElemType x){
     85     LinkList p;
     86     p=L->next;
     87     int count=1;
     88     while(p->data!=x&&p){
     89         ++count;
     90         p=p->next;
     91     }
     92     if(!p){
     93               printf("The linkList have not the Element .\n");
     94               return -1;
     95     }
     96     return count;
     97 }
     98 
     99 int ListInsert(LinkList L,int i,ElemType e){
    100     LinkList p;
    101     p=L->next;
    102     LinkList s=(LinkList)malloc(sizeof(struct LNode));
    103     if(!s){
    104               printf("S error!\n");
    105               return -1;
    106     }
    107     if(i==1){
    108         s->data=e;
    109         s->next=p;
    110         L->next=s;
    111         return 0;
    112     }
    113     int count=1;
    114     while(p&&count<i-1){
    115         p=p->next;
    116         ++count;
    117     }
    118     if(!p||count>i-1){
    119         printf("Insert failure .\n");
    120         return -1;
    121     }
    122     s->data=e;
    123     s->next=p->next;
    124     p->next=s;
    125     return 0;
    126 }
    127 
    128 int ListDelete(LinkList L,int i,ElemType *e){
    129     LinkList p,q;
    130     p=L->next;
    131     int count=1;
    132 //    if (i<1||i>ListLength(L)){
    133 //        printf("i is error !\n");
    134 //        return 0;
    135 //    }
    136      if (i==1){
    137         L->next=p->next;
    138         *e=p->data;
    139         free(p);
    140         return 0;
    141     }
    142     while(p&&count<i-1){
    143         p=p->next;
    144         ++count;
    145     }
    146     if(!p||count>i-1){
    147         printf("Delete failure.\n");
    148         return 0;
    149     }
    150     q=p->next;
    151     *e=q->data;
    152     p->next=q->next;
    153     free(q);
    154     q=NULL;
    155     return 0;
    156 }
    157 
    158 void print(ElemType c){
    159     printf("%d ",c);
    160 }
    161 
    162 void print(ElemType c);
    163 void ListTraverse(LinkList L,void(* vi)(ElemType)){
    164     LinkList p=L->next;
    165     while(p){
    166         vi(p->data);
    167         p=p->next;
    168     }
    169     printf("\n");
    170 }
    171 
    172 
    173 
    174 void CreateList(LinkList *L,int n){
    175     int i;
    176     LinkList p;
    177     *L=(LinkList)malloc(sizeof(struct LNode));
    178     (*L)->next=NULL;
    179     printf("Please input  Link_len = %d : \n",n);
    180     for(i=n;i>0;--i){
    181         p=(LinkList)malloc(sizeof(struct LNode));
    182         scanf("%d",&p->data);
    183         p->next=(*L)->next;
    184         (*L)->next=p;
    185     }
    186 }
    187 
    188 void CreateList1(LinkList *L,int n){
    189     int i;
    190     LinkList p,q;
    191     (*L)=(LinkList)malloc(sizeof(struct LNode));
    192     (*L)->next=NULL;
    193     q=(*L);
    194     printf("Please input  Link_len = %d : \n",n);
    195     for(i=n;i>0;--i){
    196         p=(LinkList)malloc(sizeof(struct LNode));
    197         scanf("%d",&p->data);
    198         q->next=p;
    199         q=q->next;
    200     }
    201     p->next=NULL;
    202 }
    203 
    204 void MergeList(LinkList La,LinkList Lb,LinkList *Lc){
    205     LinkList pa=La->next,pb=Lb->next,pc;
    206     *(Lc)=pc=La;
    207     while(pa&&pb){
    208         if(pa->data<=pb->data){
    209             pc->next=pa;
    210             pc=pa;
    211             pa=pa->next;
    212         }
    213         else{
    214             pc->next=pb;
    215             pc=pb;
    216             pb=pb->next;
    217         }
    218         pc->next=pa?pa:pb;
    219         free(Lb);
    220         Lb=NULL;
    221     }
    222 }
    223 
    224 int main(){
    225     LinkList La,Lb,Lc;
    226     
    227     InitList(&L);
    228     int j;
    229     for(j=1;j<=5;j++){
    230         ListInsert(L,1,j);
    231     }
    232     ListTraverse(L,print);
    233     ListEmpty(L);
    234     int e;
    235     ListDelete(L,41,&e);    
    236     ListTraverse(L,print);
    237     printf("%d\n",e);
    238     printf("********\n");
    239     printf("%d\n",ListLength(L));
    240     ListEmpty(L);
    241 
    242     printf("********\n");
    243     GetElem(L,4,&e);
    244     printf("%d\n",e);
    245 
    246     printf("********\n");
    247     int dis=LocateElem(L,2);
    248     printf("%d",dis);
    249     
    250     int n=5;
    251     CreateList1(&La,n);
    252     ListTraverse(La,print);
    253     CreateList1(&Lb,n);
    254     ListTraverse(Lb,print);
    255     MergeList(La,Lb,&Lc);
    256     ListTraverse(Lc,print);
    257     
    258     scanf("%*s");
    259 
    260     return 0;
    261 }
  • 相关阅读:
    内存表id,name解决方案,举例(workspaces表)
    建立mysql远程访问账号
    mysql主从设定笔记
    mysql安装
    SAMBA 让Unix与Windows轻松共享 (2)
    /rc.d/rc.mysqld举例
    HTML编码规范1.0
    创建mysql存储过程
    《Linux企业应用案例精解》样章
    欢迎参加51CTO的技术门诊《OSSIM,企业信息安全管理利器》讨论
  • 原文地址:https://www.cnblogs.com/suiyun/p/2871633.html
Copyright © 2011-2022 走看看