-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursorLinkedList.c
More file actions
138 lines (118 loc) · 3.03 KB
/
CursorLinkedList.c
File metadata and controls
138 lines (118 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
#define spaceSize 11
typedef int List;
typedef int Position;
void InitializeCursorSpace();
List CursorList();
int CursorAlloc();
List MakeEmpty( List L );
int IsEmpty( List L );
int IsLast();
void Insert( int X, List L );
Position Find( int X, List L );
Position FindPrevious( int X, List L );
void Delete( int X, List L );
void CursorFree( Position P );
void DeleteList( List L );
void PrintList( List L );
struct node {
int element;
int next;
};
struct node cursorSpace[spaceSize];
int main(){
InitializeCursorSpace();
for(int i = 0; i < spaceSize ; i++ ){
printf("%d\t",cursorSpace[i].next);
}
return 7;
}
List CursorList(){
List L = CursorAlloc();
cursorSpace[ L ].next = 0;
return L;
}
void InitializeCursorSpace(){
for(int i = 0; i<spaceSize-1; i++)
cursorSpace[i].next=i+1;
cursorSpace[spaceSize - 1].next = 0;
}
int CursorAlloc(){
Position P;
P = cursorSpace[ 0 ].next;
cursorSpace[ 0 ].next = cursorSpace[ P ].next;
if( P == 0 ){
printf("Out of space!\n");
return -1;
}
return P;
}
List MakeEmpty( List L ){
if( L )
DeleteList( L );
L = CursorAlloc();
if( L == 0 )
printf("Out of memory!");
cursorSpace[ L ].next = 0;
return L;
}
int IsEmpty( List L ){
return cursorSpace[ L ].next == 0; //header
}
int IsLast( Position P, List L ){
return cursorSpace[ P ].next == 0;
}
void Insert( int X, List L ){
Position TmpCell, P = L;
TmpCell = CursorAlloc( );
while( P && cursorSpace[P].next != 0)
P = cursorSpace[P].next;
cursorSpace[ TmpCell ].element = X;
cursorSpace[ TmpCell ].next = cursorSpace[ P ].next;
cursorSpace[ P ].next = TmpCell;
}
Position Find( int X, List L ) {
Position P;
P = cursorSpace[ L ].next;
while( P && cursorSpace[ P ].element != X )
P = cursorSpace[ P ].next;
return P;
}
Position FindPrevious( int X, List L ){
Position P;
P = L;
while( cursorSpace[ P ].next && cursorSpace[ cursorSpace[ P ].next ].element != X )
P = cursorSpace[ P ].next;
return P;
}
void Delete( int X, List L ){
Position P, TmpCell;
P = FindPrevious( X, L );
if( !IsLast( P, L ) ){
TmpCell = cursorSpace[ P ].next;
cursorSpace[ P ].next = cursorSpace[ TmpCell ].next;
CursorFree( TmpCell );
}
}
void CursorFree( Position P ){
cursorSpace[ P ].next = cursorSpace[ 0 ].next;
cursorSpace[ 0 ].next = P;
}
void DeleteList( List L ){
Position P, Tmp;
P = cursorSpace[ L ].next; /* Header assumed */
cursorSpace[ L ].next = 0;
while( P != 0 ){
Tmp = cursorSpace[ P ].next;
CursorFree( P );
P = Tmp;
}
}
void PrintList( List L ){
Position P = L;
while( P != 0 ){
printf("%3d \t %3d \t %3d\n", P,cursorSpace[P].element, cursorSpace[P].next);
P = cursorSpace[P].next;
}
}