-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedList.c
148 lines (128 loc) · 2.36 KB
/
linkedList.c
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
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct link{
char element;
struct link *next;
} Link;
typedef struct{
Link *head;
Link *tail;
Link *curr;
int cnt;
} List;
Link *create_link(char it, Link *nextval){
Link *n;
n = (Link*) malloc(sizeof(Link));
n->element = it;
n->next = nextval;
return n;
}
Link *create_Link(Link *nextval){
Link *n;
n = (Link*) malloc(sizeof(Link));
(*n).next = nextval;
return n;
}
List *create_list(){
List *l;
l = (List*) malloc(sizeof(List));
l->head = create_Link(NULL);
l->tail = l->head;
l->curr = l->tail;
l->cnt = 0;
return l;
}
void clear(List *l){
Link *temp;
temp = l->head->next;
while(l->head != NULL){
free(l->head);
l->head = temp;
temp = l->head->next;
}
l->cnt = 0;
free(l);
}
void insert(List *l, char it){
l->curr->next = create_link(it, l->curr->next);
if(l->tail == l->curr){
l->tail = l->curr->next;
}
l->cnt++;
}
void append(List *l,char it){
l->tail->next = create_link(it, NULL);
l->tail = l->tail->next;
l->cnt++;
}
char rmv(List *l){
Link *temp;
if(l->curr->next == NULL){
return '1';
}
if(l->tail == l->curr->next){
l->tail = l->curr;
}
temp = l->curr->next;
l->curr->next = l->curr->next->next;
free(temp);
l->cnt--;
return '0';
}
void moveToStart(List *l){
l->curr = l->head;
}
void moveToEnd(List *l){
l->curr = l->tail;
}
void prev(List *l){
if(l->curr == l->head){
return;
}
Link *temp;
temp = l->head;
while(temp->next != l->curr){
temp = temp->next;
}
l->curr = temp;
}
void next(List *l){
if(l->curr != l->tail){
l->curr = l->curr->next;
}
}
int lenght(List *l){
return l->cnt;
}
int currPos(List *l){
Link *temp;
temp = l->head;
int i = 0;
while(l->curr != temp){
temp = temp->next;
i++;
}
return i;
}
void moveToPos(List *l, int pos){
if(pos < 0 || pos > l->cnt){
return;
}
l->curr = l->head;
int i = 0;
while(i < pos){
l->curr = l->curr->next;
i++;
}
}
char getValue(List *l){
if(l->curr->next == NULL){
return -1;
}
return l->curr->next->element;
}
#define max 100000
int main(){
return 0;
}