数据结构学习第四天

19:41:25 2019-08-19

继续学习

 

链表的游标实现

 

Cursor.h

 1 #ifndef _CURSOR_H
 2 #define _CURSOR_H
 3 #define Size 50     
 4 #define Null 0   //当Next为0 类似于空指针
 5 typedef int PtrToNode;
 6 typedef PtrToNode List;
 7 typedef PtrToNode Position;
 8 struct Node
 9 {
10     int Element;
11     Position Next;
12 };
13 extern struct Node CursorSpace[Size];
14 void InitializeCursorSpace();  //初始化空间
15 
16 List MakeEmpty(List L);     //建一个空的表头
17 int IsEmpty(const List L);    
18 int IsLast(const Position P, const List L);
19 Position Find(int Element, const List L);
20 void Delete(int Element, List L);
21 Position FindPrevious(int Element, const List L);
22 void Insert(int Element, List L, Position P);  //插入到P元素之前
23 void DeleteList(List L);                    //删除表
24 Position Header(List L);
25 Position First(List L);
26 int Retrive(const Position P);            //取回元素
27 Position CursorAlloc();
28 void FreeCursor(Position P);
29 #endif // !_CURSOR_H
View Code

Cursor.c

  1 #include"Cursor.h"
  2 #include<stdio.h>
  3 
  4 struct Node CursorSpace[Size];
  5 Position CursorAlloc()  //从已有空间中 申请空间
  6 {
  7     Position P;
  8     P = CursorSpace[0].Next;   //每次都申请从第一个元素开始
  9     if (P == 0)
 10     {
 11         printf("申请失败");
 12         return 0;
 13     }
 14     CursorSpace[0].Next = CursorSpace[P].Next;
 15     CursorSpace[P].Next = Null;     //将申请的空间初始化
 16     return P;
 17 }
 18 
 19 void FreeCursor(Position P)
 20 {
 21     CursorSpace[P].Element = 0;
 22     CursorSpace[P].Next = CursorSpace[0].Next;
 23     CursorSpace[0].Next = P;
 24 }
 25 void InitializeCursorSpace()   
 26 {
 27     for (int i = 0; i < Size - 1; i++)
 28     {
 29         CursorSpace[i].Element = 0;   
 30         CursorSpace[i].Next = i + 1;
 31     }
 32     CursorSpace[Size - 1].Next = 0;    //尾部连接在了头部   访问位置为0的元素是非法的
 33     CursorSpace[Size - 1].Element = 0;
 34 }
 35 List MakeEmpty(List L)
 36 {
 37     L = CursorAlloc();
 38     return L;
 39 }
 40 int IsEmpty(const List L)
 41 {
 42     return    CursorSpace[L].Next == Null;
 43 }
 44 int IsLast(const Position P, const List L)
 45 {
 46     return CursorSpace[P].Next == Null;
 47 }
 48 Position Find(int Element, const List L)
 49 {
 50     Position P = CursorSpace[L].Next;
 51     /*while (P!=Null)
 52     {
 53         if (CursorSpace[P].Element ==Element)
 54             return P;
 55         P = CursorSpace[P].Next;
 56     }
 57     return Null;*/
 58     //下面这个版本更加简化
 59     while (P && CursorSpace[P].Element != Element)
 60     {
 61         P = CursorSpace[P].Next;
 62     }
 63     return P;    //返回值为0说明 没有找到
 64 }
 65 
 66 void Delete(int Element, List L)
 67 {
 68     Position P1, P2;
 69     P1 = P2 = FindPrevious(Element, L);   //找到前置元素
 70     if (P1 == 0)
 71     {
 72         printf("该元素不存在");
 73         return;
 74     }
 75     P2 = CursorSpace[P1].Next;
 76     //CursorSpace[P1].Next = CursorSpace[CursorSpace[P1].Next].Next;
 77     CursorSpace[P1].Next = CursorSpace[P2].Next;
 78     FreeCursor(P2);
 79 }
 80 
 81 Position FindPrevious(int Element, const List L)
 82 {
 83     Position P = L;
 84     /*while (CursorSpace[P].Next!=Null)
 85     {
 86         if (CursorSpace[CursorSpace[P].Next].Element == Element)
 87             return P;
 88         P = CursorSpace[P].Next;
 89     }
 90     return Null;*/
 91     while (CursorSpace[P].Next && CursorSpace[CursorSpace[P].Next].Element != Element)
 92     {
 93         P = CursorSpace[P].Next;
 94     }
 95     return P;
 96 }
 97 void Insert(int Element, List L, Position P)  //插入P前
 98 {
 99     Position P1 = CursorAlloc();
100     CursorSpace[P1].Element = Element;
101     CursorSpace[P1].Next = P;  
102     Position P2 = FindPrevious(CursorSpace[P].Element, L);
103     CursorSpace[P2].Next = P1;
104 }
105 void DeleteList(List L)
106 {
107     Position P1, P2;
108     P1 = P2 = CursorSpace[L].Next;
109     while (P2 != Null)
110     {
111         P2 = CursorSpace[P1].Next;
112         FreeCursor(P1);
113         P1 = P2;
114     }
115 }
116 int Retrive(const Position P)
117 {
118     return CursorSpace[P].Element;
119 }
120 Position Header(List L)
121 {
122     return L;
123 }
124 Position First(List L)
125 {
126     return CursorSpace[L].Next;
127 }
View Code

main.c

 1 #include<stdio.h>
 2 #include"Cursor.h"
 3 int main()
 4 {
 5     InitializeCursorSpace();   //初始化
 6     int L = 0;
 7     L=MakeEmpty(L);
 8     printf("%d\n",  IsEmpty(L));   
 9     printf("%d\n", CursorSpace[L].Element);
10     int P = CursorAlloc();
11     CursorSpace[P].Element = 20;
12     CursorSpace[L].Next = P;
13     printf("%d\n", CursorSpace[P].Element);
14     Insert(25, L, P);
15     printf("%d\n", CursorSpace[CursorSpace[L].Next].Element);
16     printf("%d\n", Retrive(P));
17     printf("%d\n", CursorSpace[P].Next);
18     printf("%d\n", Find(20, L));
19     Delete(20, L);
20     Delete(25, L);
21     printf("%d\n", CursorSpace[L].Next);
22     return 0;
23 }
View Code

 

之前几天写完都没有测试过 今天突然想测试下 出了一堆bug

之后每个写完都要去测试 前面的会抽时间补上的

其实只有一个bug 原因是我对编译过程不了解 一个变量声明在了头文件中 导致重复定义  

看来得尝试用linux编程了

posted @ 2019-08-20 00:28  57one  阅读(125)  评论(0编辑  收藏  举报