[数据结构与算法分析] 单链表基本操作的实现

  这几天还在啃Weiss书的Ch.3,随手把书上单链表基本操作的代码打了一遍,顺便补充了一点自己写的东西(一堆注释以及几个函数),经过测试应该是没问题。

  这次尝试用所谓的"Google Style"写代码,习惯了缩进4空格的Windows风格后再改到缩进2空格,真的是有些不习惯。本来Google Style中,变量应该都是小写字母,但我实在不喜欢小写的L和P,变量命名仍就坚持自己的习惯——单字母变量大写,多字母小写。

  需要注意的一些地方:

  1,在几个函数中出现了类似while (P != NULL && P->Element != X) 这样子的代码,根据逻辑运算符的短路规则,如果P != NULL(链表末尾结点),就不会再检查后面的布尔表达式。

  2,原书使用  Position tmp = malloc(sizeof(struct Node)) 这样的代码,但根据Google Style 建议,改为" malloc(sizeof(tmp))"有利于保持同步(鉴于tmp的数据类型有可能改变)。

  Code is here~

  .h头文件:

 1 #ifndef LINKLIST_H_INCLUDED
 2 #define LINKLIST_H_INCLUDED
 3 
 4 struct Node;
 5 typedef struct Node *PtrToNode;
 6 //  List and Position are pointers
 7 typedef PtrToNode List;
 8 typedef PtrToNode Position;
 9 
10 #define ElementType int//set the type of element as Integer
11 
12 //  Functions:
13 List MakeEmpty(List L);
14 List InitialList();
15 int IsEmpty(List L);
16 int IsLast(Position P, List L);
17 Position Find(ElementType X, List L);
18 void Delete(ElementType X, List L);
19 Position FindPrevious(ElementType X, List L);
20 void Insert(ElementType X, List L, Position P);
21 void DeleteList(List L);
22 Position Header(List L);
23 Position First(List L);
24 Position Last(List L);
25 Position Advance(Position P);//not implemented yet
26 ElementType Retrive(Position P);//not implemented yet
27 void PrintElement(Position P);
28 void PrintList(List L);
29 int SizeOfList(List L);
30 int FindIndex(ElementType X, List L);
31 int InsertAsTail(ElementType X, List L);
32 int InsertAsHead(ElementType X, List L);
33 
34 #endif // LINKLIST_H_INCLUDED

  

  .c文件:

  1 #include "LinkList.h"
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 struct Node {
  6   ElementType Element;
  7   Position Next;
  8 };
  9 
 10 List MakeEmpty(List L) {
 11   //used for initialization
 12   Position P = Header(L);
 13   P->Next = NULL;
 14   P->Element = 0;
 15 }
 16 List InitialList() {//return a new List (header)
 17 
 18   List tmp;
 19   tmp = malloc(sizeof(tmp));//standard malloc way for "tmp"
 20   tmp->Next = NULL;
 21   //  tmp = MakeEmpty(tmp);
 22   return tmp;
 23 }
 24 int IsEmpty(List L) {//Return true if L is empty
 25   return L->Next == NULL;
 26 }
 27 int IsLast(Position P, List L) {//Return true if P is the last position of the list L
 28   return P->Next == NULL;
 29 }
 30 Position Header(List L) {
 31   Position P = L;
 32 }
 33 Position First(List L) {
 34   Position P = L->Next;
 35 }
 36 Position Last(List L) {
 37   Position P = Header(L);
 38   while (!IsLast(P,L)) {
 39     P = P->Next;
 40   }
 41   return P;
 42 }
 43 Position Find(ElementType X, List L) {
 44 //  Return Position of X in L;NULL if not found;
 45   Position P = L->Next;
 46   while (P != NULL && P->Element != X)
 47     //  You should first know if P is NULL, here we use shortcut "&&"
 48     P = P->Next;
 49   return P;
 50 }
 51 int FindIndex(ElementType X, List L) {
 52   //Return Index of X in L;-1 if not found;Index starts from 1(not 0).
 53   int index = 1;
 54   Position P = L->Next;
 55   while (P != NULL && P->Element != X) {
 56     //  You should first know if P is NULL, here we use shortcut "&&"
 57     P = P->Next;
 58     index++;
 59   }
 60   return index; //header node is not counted in index
 61 }
 62 void Delete(ElementType X, List L) {//Delete the first occurrence of X in List L
 63   Position P,tmp;
 64   P = FindPrevious(X,L);
 65   if (!IsLast(P,L)) {
 66     tmp = P->Next;  //Now "tmp" is just which node you want to "delete" here
 67     P->Next = tmp->Next;
 68     free(tmp);  //the data at address tmp is meaningless after free it
 69   }
 70 }
 71 Position FindPrevious(ElementType X, List L) {
 72   //If X is not found, then Next Field of Returned Position is NULL
 73   Position P = L;
 74   while(P->Next != NULL && P->Next->Element != X)
 75     P = P->Next;
 76   return P; //If X not found in this List, P is the last node, P->Next is NULL
 77 }
 78 void Insert(ElementType X, List L, Position P) {//Insert After Position P
 79   Position tmp = malloc(sizeof(tmp));
 80   if (tmp == NULL) {
 81     printf("Out of space!");  //  TODO(AllZY): Use a variable to indicate the state.
 82   } else {
 83     tmp->Element = X;
 84     tmp->Next = P->Next;
 85     P->Next = tmp;  //tmp should never be "NULL" when this line executed
 86   }
 87 }
 88 int InsertAsTail(ElementType X, List L) {
 89 //  return index if inserted successfully, otherwise return -1
 90 //  Insert it after the last ele.
 91   Position Tail = Last(L);
 92   Position tmp = malloc(sizeof(tmp));
 93   if (tmp == NULL) {
 94     return -1;
 95   } else {
 96     tmp->Element = X;
 97     tmp->Next = Tail->Next;
 98     Tail->Next = tmp;
 99   } //Is this what they called "google style"? I think it's a bit odd...
100 }
101 int InsertAsHead(ElementType X, List L) {
102 //  return index if inserted successfully, otherwise return -1
103 //  Insert it before the first ele.
104   Position Head = Header(L);
105   Position tmp = malloc(sizeof(tmp));
106   if (tmp == NULL) {
107     return -1;
108   } else {
109     tmp->Element = X;
110     tmp->Next = Head->Next;
111     Head->Next = tmp;
112   }
113 }
114 void DeleteList(List L) {
115   Position P,tmp;
116   P = L->Next;
117   L->Next = NULL;
118   while (P != NULL) {
119     tmp = P->Next;
120     free(P);
121     P = tmp;
122   }
123   /*
124   //an unreliable version, not recommended
125   while(P!=NULL)
126   {
127     free(P);
128     P = P->Next;
129   }
130   */
131 }
132 void PrintElement(Position P) {//Print one Element
133   ElementType e = P->Element;
134   printf("%d ",e);
135 }
136 void PrintList(List L) {//Print all elements in the List
137   Position P = First(L);
138   while (P != NULL) {
139     PrintElement(P);
140     P = P->Next;
141   }
142 }
143 int SizeOfList(List L) {
144   int count = 0;
145   Position P = First(L);
146   while (P != NULL) {
147     count++;
148     P = P->Next;
149   }
150   return count;
151 }
152 
153 int main()
154 {
155 //  List L = malloc(sizeof (List));
156 //  MakeEmpty(L);
157   List L = InitialList();
158   Position P = Header(L);
159   int i;
160   for (i = 1; i <= 10; i++) {
161     InsertAsTail(i,L);
162   }
163   for ( ; i <= 20; i++) {
164     InsertAsHead(i,L);
165   }
166 
167   PrintList(L);
168   printf("\nSize of list is %d",SizeOfList(L));
169   Position p10 = Find(10,L);
170   printf("\nThe element in the Position of value \"10\" is %d\n",p10->Element);
171   printf("The index of Element 11 is %d\n",FindIndex(11,L));
172 
173   DeleteList(L);
174   return 0;
175 }

 

  测试运行结果:

  

 

参考资料:

《数据结构与算法分析》

http://google.github.io/styleguide/cppguide.html

http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/

———————————————————————————————————————————

原创文章,转载请注明出处: http://www.cnblogs.com/allzy/

转载于:https://www.cnblogs.com/allzy/p/5174836.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值