单链表实现

  1 #include "stdio.h"
  2 #include "stdlib.h"
  3 
  4 #define OK 1
  5 #define ERROR 0
  6 
  7 typedef int elemType;
  8 typedef struct _LNode{
  9     elemType data;
 10     struct _LNode *Next;
 11 }LNode;
 12 
 13 //尾插法建表
 14 void CreatList2(LNode *L,elemType e){
 15     LNode *s = (LNode*)malloc(sizeof(LNode));
 16     s->data = e;
 17     s->Next = NULL;
 18     LNode *p = L;
 19     while(p->Next != NULL)
 20         p = p->Next;
 21     p->Next = s;
 22 }
 23 
 24 void TraverseList(LNode *L){
 25     LNode *p;
 26     p = L->Next;
 27     while(p){
 28         printf("%d ",p->data);
 29         p = p->Next;
 30     }
 31     printf("\n");
 32 }
 33 
 34 int ListInsert(LNode *L,int pos,elemType e){
 35     LNode *p;
 36     LNode *s=(LNode *)malloc(sizeof(LNode));
 37     int j = 0;
 38     p = L;
 39     while(p && j < pos-1){
 40         p = p->Next;
 41         ++j;
 42     }
 43     if(!p || j > pos-1)
 44         return ERROR;
 45     s->data = e;
 46     s->Next = p->Next;
 47     p->Next = s;
 48     return OK;
 49 }
 50 
 51 int ListDelete(LNode *L,int pos){
 52     LNode *p = L;
 53     LNode *q;
 54     int j = 0;
 55     while(p->Next && j < pos-1){
 56         p = p->Next;
 57         ++j;
 58     }
 59     if(j > pos-1 || !p->Next){
 60         return ERROR;
 61     }
 62     q = p->Next;
 63     p->Next = q->Next;
 64     free(q);
 65     return OK;
 66 }
 67 
 68 int ListSearch(LNode *L,elemType e){
 69     LNode *p = L->Next;
 70     int cursor = 1;
 71     while(p->Next != NULL){
 72         if(p->data == e){
 73             printf("%d在第%d个位置\n",e,cursor);
 74             return cursor;
 75         }
 76         p = p->Next;
 77         cursor++;
 78     }
 79     printf("没有找到!");
 80 }
 81 
 82 //链表倒置
 83 LNode *ListInverse(LNode *L){
 84     if(L == NULL)
 85         return NULL;
 86     if(L->Next == NULL)
 87         return L;
 88     LNode *pre = L->Next;
 89     LNode *cur = pre->Next;
 90     LNode *next = cur->Next;
 91     pre->Next = NULL;
 92     cur->Next = pre;
 93     pre = cur;
 94     cur = next;
 95     while(cur != NULL){
 96         next = cur->Next;
 97         cur->Next = pre;
 98         pre = cur;
 99         cur = next;
100     }
101     L->Next = pre;
102     return L;
103 }
104 //合并链表
105 LNode *MergeList(LNode *La,LNode *Lb,LNode *Lc){
106     LNode *pa = La->Next;
107     LNode *pb = Lb->Next;
108     LNode *pc;
109     Lc = pc = La;
110     while(pa && pb){
111         if(pa->data <= pb->data){
112             pc->Next = pa;
113             pc = pa;
114             pa = pa->Next;
115         }
116         else{
117             pc->Next = pb;
118             pc = pb;
119             pb = pb->Next;
120         }
121     }
122     pc->Next = pa?pa:pb;
123     free(Lb);
124     return La;
125 }
126 
127 int main(){  
128     LNode *list1=(LNode*)malloc(sizeof(LNode));  
129     list1->Next=NULL;  
130     int nums;elemType x;  
131     scanf("%d",&nums);  
132     int i;  
133     for(i=0;i<nums;i++)  {  
134         scanf("%d",&x);  
135         CreatList2(list1,x);  
136     }  
137     printf("创建好的线性表La=");  
138     TraverseList(list1);  
139     int pos;  
140     scanf("%d%d",&x,&pos);  
141     ListInsert(list1,pos,x);  
142     printf("插入一个元素后的线性表La=");  
143     TraverseList(list1);  
144     scanf("%d",&pos);  
145     ListDelete(list1,pos);  
146     printf("删除一个元素后的线性表La=");  
147     TraverseList(list1);  
148     scanf("%d",&x);  
149     ListSearch(list1,x);  
150     list1=ListInverse(list1);  
151     printf("逆置后的线性表La=");  
152     TraverseList(list1);  
153     LNode *list2=(LNode*)malloc(sizeof(LNode));  
154     list2->Next=NULL;  
155     scanf("%d",&nums);  
156     for(i=0;i<nums;i++)  {  
157         scanf("%d",&x);  
158         CreatList2(list2,x);  
159     }  
160     LNode *list3;  
161     MergeList(list1,list2,list3);  
162     printf("合并La和Lb后的线性表=");  
163     TraverseList(list1);  
164     return 0;  
165 }  

 

posted @ 2017-11-28 13:43  ixummer  阅读(211)  评论(0编辑  收藏  举报