-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlist.h
169 lines (157 loc) · 3.58 KB
/
mlist.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef MYLIBMYLIST
#define MYLIBMYLIST
#include<stdio.h>
#include<stdlib.h>
#define list(a) head* a = linkedlist_init()
#define insertAt(a,indx,k) linkedlist_insert(a, indx, k)
#define deleteAt(a,indx) linkedlist_delete(a, indx)
#define pushFront(a,k) push_front(a,k)
#define pushBack(a,k) push_back(a,k)
#define popFront(a) pop_front(a)
#define popBack(a) pop_back(a)
typedef struct node{
int data;
struct node* next;
struct node* prev;
} linkedlist;
typedef struct headnode{
//linkedlist* temp;
linkedlist* h;
linkedlist* t;
int length;
} head;
head* linkedlist_init(){
head* new = (head*) malloc(sizeof(head));
new->h = NULL;
new->t = NULL;
new->t = NULL;
new->length = 0;
return new;
}
void push_back(head* a, int k){
linkedlist* new = (linkedlist*) malloc(sizeof(linkedlist));
new->data = k;
new->next = NULL;
if((a->h) == NULL){
new->prev = NULL;
(a->h) = new;
(a->t) = new;
a->length++;
}else{
linkedlist* p = (a->t);
p->next = new;
new->prev = p;
a->t = new;
a->length++;
}
}
void pop_back(head* a){
linkedlist* temp = a->t;
a->t = temp->prev;
(a->t)->next = NULL;
a->length--;
free(temp);
}
void pop_front(head* a){
linkedlist* temp = a->h;
a->h = temp->next;
a->length--;
free(temp);
}
void pop_value(head* a, int indx){
if(a->length == 0 || indx > a->length)
return;
linkedlist* p = a->h;
if(indx == 0){
pop_front(a);
return;
}
if(indx == a->length - 1){
pop_back(a);
return;
}
for(int i=0; i<indx; i++)
p = p->next;
linkedlist* temp = p->prev;
temp->next = p->next;
(p->next)->prev = temp;
a->length--;
free(p);
}
void push_front(head *a, int k){
if((a->h) == NULL){
push_back(a,k);
return;
}
linkedlist* new = (linkedlist*) malloc(sizeof(linkedlist));
new->data = k;
new->next = a->h;
new->prev = NULL;
(a->h)->prev = new;
a->h = new;
a->length++;
}
void linkedlist_insert(head* a, int indx, int k){
if((a->h) == NULL || indx >= a->length){
push_back(a,k);
return;
}
if(indx == 0){
push_front(a,k);
return;
}
linkedlist* p = (a->h);
linkedlist* new = (linkedlist*) malloc(sizeof(linkedlist));
new->data = k;
for(int i=0 ; i< indx; i++)
p = p->next;
linkedlist* temp = p->prev;
p->prev = new;
new->next = p;
temp->next = new;
new->prev = temp;
a->length++;
}
void linkedlist_delete(head*a , int indx){
if((a->h) == NULL || indx > a->length) return;
if(indx == 0){
pop_front(a);
return;
}
if(indx == a->length - 1){
pop_back(a);
return;
}
linkedlist* p = a->h;
for(int i=0; i<indx; i++)
p= p->next;
linkedlist* temp = p->prev;
temp->next = p->next;
(p->next)->prev = temp;
free(p);
}
void display(head* a){
linkedlist* p = (a->h);
while(p != NULL){
printf("%d ", p->data);
p = p->next;
}
putchar('\n');
}
void rdisplay(head* a){
linkedlist* p = (a->t);
while(p != NULL){
printf("%d ", p->data);
p = p->prev;
}
putchar('\n');
}
void destructor(head* a){
while(a->h != NULL){
linkedlist* tmp = a->h;
(a->h) = (a->h)->next;
free(tmp);
}
free(a);
}
#endif