数据结构---链接表 代码实现


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

***********************************************************************************************************************************************

自己写的

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #define OK      1
  4 #define ERROR   0
  5 
  6 typedef int ElemType;
  7 
  8 typedef struct LNode{
  9     ElemType       data;
 10     struct LNode   *next;
 11 } LNode, *LinkList;
 12 
 13 //*******************************************************************************************************************
 14 // 单链表建立方法一(函数用返回值得到表头指针)
 15 // 函数名:    CreatOne(int n)
 16 // 参数:      传入int n ,线性表结点数量
 17 // 作用:      建立空线性表
 18 // 返回值:    LNode *型返回接头体指针,得到表头指针
 19 
 20 LNode* CreatOne(int n)
 21 {
 22     LNode *head, *p;
 23     int i;
 24 
 25     head = (LNode *)malloc(sizeof(LNode));
 26     head->next = NULL;
 27 
 28     printf("Please input the data for LinkList Nodes\n");
 29     for(i = n; i > 0; i--)
 30     {
 31         p = (LNode *)malloc(sizeof(LNode));
 32         scanf("%d", &p->data);
 33         p->next = head->next;
 34         head->next = p;
 35     }
 36 
 37     return head;
 38 }
 39 
 40 // 方法二:无返回值,直接传入一个链表指针和结点数量
 41 // 函数名:    CreatTwo(LNode *head, int n);
 42 // 参数:      链表指针 和 结点数量
 43 // 作用:      建立空线性表
 44 
 45 void CreatTwo(LinkList head, int n)
 46 {
 47     LNode *p;
 48     int i;
 49 
 50 
 51     head->next = NULL;
 52 
 53     printf("Please input the data for LinkList Nodes\n");
 54     for(i = n; i > 0; i--)
 55     {
 56         p = (LNode *)malloc(sizeof(LNode));
 57         scanf("%d", &p->data);
 58         p->next = head->next;
 59         head->next = p;
 60     }
 61 
 62 }
 63 
 64 //*******************************************************************************************************************
 65 //逐个打印链表内容
 66 void DisplayList(LinkList L)
 67 {
 68     LNode *p;
 69     p = L->next;
 70 
 71     printf("------------------------\n");
 72     while(p != NULL)
 73     {
 74         printf("%d  ", p->data);
 75         p = p->next;
 76     }
 77     printf("\n------------------------\n");
 78 }
 79 
 80 //*******************************************************************************************************************
 81 //插入元素e进入链表L第i元素前面
 82 int InsertElem(LNode *L, int i, ElemType e)
 83 {
 84     int j = 0;
 85     LNode *p, *s;
 86     p = L;
 87 
 88     while(j < i - 1 && p)           //寻找第i - 1个结点
 89     {
 90         p = p -> next;
 91         j++;
 92     }
 93     if(!p || j > i - 1)            //当i小于1或者大于链表长度
 94     {
 95         printf("Error, the locate wrong\n");
 96         return ERROR;
 97     }
 98     s = (LNode *)malloc(sizeof(LNode));
 99     s -> data = e;
100     s -> next = p -> next;
101     p ->next = s;
102 
103     printf("\nInsert Element %d before locate %d Success!\n", e, i);
104     return OK; 
105 }
106 
107 //*******************************************************************************************************************
108 //删除单链表的第i个元素,并以e返回。
109 int DeleteElem(LNode *L, int i, ElemType *e)
110 {
111     int j = 0;
112     LNode *p, *q;
113 
114     p = L;
115     while(p && j < i - 1)
116     {
117         p = p -> next;
118         j++;
119     }
120     if(!p || j > i - 1)
121     {
122         printf("Error, the locate wrong\n");
123         return ERROR;        
124     }
125     q = p -> next;
126     p -> next = q -> next;
127     *e = q -> data;
128     free(q);
129 
130     printf("\nOK,Delete Element %d in Locate %d success\n", *e, i);
131 
132     return OK;
133 }
134 
135 //*******************************************************************************************************************
136 //在链表中查找是否有元素e如果有则返回它的位置i
137 int FindElem(LNode *L, ElemType e, int *i)
138 {
139     int j = 1;
140     LNode *p;
141 
142     p = L;
143     p = p -> next;
144     while(p)
145     {
146         if(p -> data == e)
147         {
148             *i = j;
149             printf("Find Element %d In Locate %d \n", e, j);
150             return OK;
151         }
152         j++;
153         p = p -> next;
154     }
155 
156     printf("No Find Element %d \n", e);
157     return ERROR;
158 }
159 
160 //*******************************************************************************************************************错误
161 //按顺序合并两个链表L1、L2为一个链表L3,合并完同时释放另一个链表
162 //函数名:      MergeList(LinkList *L1, LinkList *L2, LinkList *L3)
163 //参数:        LinkList *L1 为指向链表L1的头指针的指针,传递指针的指针目的是为了改变指针内存放的值,让两个
164                 //链表链接成一个链表,L3只是一个指针他让L1和L2中的next相互指。类似temp。:)
165 void MergeList(LinkList *L1, LinkList *L2, LinkList *L3)
166 {
167     LNode *pl1, *pl2, *pl3;
168 
169     pl1 = (*L1) -> next;
170     pl2 = (*L2) -> next;
171     *L3 = pl3 = *L1;
172 
173     while(pl1 && pl2)
174     {
175         if(pl1 -> data >= pl2 -> data)
176         {
177             pl3 -> next = pl1;
178             pl3 = pl1;
179             pl1 = pl1 -> next;
180         }
181         else
182         {
183             pl3 -> next = pl2;
184             pl3 = pl2;
185             pl2 = pl2 -> next;            
186         }
187     }
188 
189     pl3 -> next = pl1 ? pl1 : pl2;
190     free(*L2);
191     printf("MergeList Finished!\n");
192 }
193
 1 LNode* MergeList(LNode *L1, LNode *L2, LNode *L3)
//不需要两层指针,只要将两个链接表内各项通过 pL3 链接在一起 2 { 3 LNode *pl1, *pl2, *pl3; 4 5 pl1 = L1 -> next; 6 pl2 = L2 -> next; 7 L3 = pl3 = L1; 8 9 while(pl1 && pl2) 10 { 11 if(pl1 -> data >= pl2 -> data) 12 { 13 pl3 -> next = pl1; 14 pl3 = pl1; 15 pl1 = pl1 -> next; 16 } 17 else 18 { 19 pl3 -> next = pl2; 20 pl3 = pl2; 21 pl2 = pl2 -> next; 22 } 23 } 24 25 pl3 -> next = pl1 ? pl1 : pl2; 26 free(L2); 27 printf("MergeList Finished!\n"); 28 29 return L3; 30 }

 

194 
195 int main(void)
196 {
197     LNode * L1, *L2, *L3;
198 
199     int NodeNum, NodeNum2, NodeNum3;
200 
201     printf("Please input the Init LinkNode1 Number\n");
202     scanf("%d", &NodeNum);
203     L1 = CreatOne(NodeNum);
204     DisplayList(L1);
205 
206     printf("Please input the Init LinkNode2 Number\n");
207     scanf("%d", &NodeNum2);
208     L2 = (LinkList)malloc(sizeof(LNode));
209     CreatTwo(L2, NodeNum2);
210     DisplayList(L2);
211 
212     // printf("Please input the Init LinkNode3 Number\n");
213     // scanf("%d", &NodeNum3);
214     // CreatTwo(L3, NodeNum3);
215 
216     printf("Start MergeList:\n");
217     MergeList(&L1, &L2, &L3);
218     DisplayList(L3);
219 
220     while(getchar() != 'q')
221         ;
222 
223     return 0;
224 }

 

posted @ 2015-01-31 18:32  xnuwu  阅读(98)  评论(0编辑  收藏  举报