forked from CodingPsychos/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dll.c
164 lines (152 loc) · 2.95 KB
/
dll.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
struct dll_node {
int data;
dll_node *next;
dll_node *prev;
};
dll_node* dll_new_node(int data) {
dll_node* temp = malloc(sizeof(dll_node));
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
return temp;
}
void dll_print(dll_node* head) {
dll_node* p = head;
while(p!=NULL) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
void dll_push_at_end(dll_node** head, dll_node** tail, int data) {
dll_node* new_node = dll_new_node(data);
if((*head) == NULL) {
(*head) = new_node;
(*tail) = (*head);
}
else if((*head)->next == NULL) {
new_node->prev = *head;
(*head)->next = new_node;
(*tail) = new_node;
}
else {
new_node->prev = *tail;
(*tail)->next = new_node;
(*tail) = (*tail)->next;
}
}
void dll_push_at_start(dll_node** head, dll_node** tail, int data) {
if((*head) == NULL) {
(*head) = dll_new_node(data);
(*tail) = (*head);
}
else {
dll_node* p = dll_new_node(data);
p->next = (*head);
(*head) = p;
}
}
int dll_pop_at_end(dll_node** head, dll_node** tail) {
int res = 0;
if(*head == NULL) {
}
else if(*head == *tail) {
res = (*head)->data;
(*head) = NULL;
(*tail) = NULL;
}
else if((*head)->next == (*tail)) {
res = (*tail)->data;
(*head)->next = NULL;
(*tail) = (*tail)->prev;
(*tail)->next = NULL;
}
else {
res = (*tail)->data;
(*tail) = (*tail)->prev;
(*tail)->next = NULL;
}
return res;
}
int dll_pop_at_start(dll_node** head, dll_node** tail) {
int res = 0;
if((*head) == NULL) {
}
else if(*head == *tail) {
res = (*head)->data;
(*head) = NULL;
(*tail) = NULL;
}
else {
res = (*head)->data;
(*head) = (*head)->next;
(*head)->prev = NULL;
}
return res;
}
void dll_push_at_position(dll_node** head,dll_node** tail,int pos,int data){
dll_node* newnode=dll_new_node(data);
int count=0;
dll_node* temp=*head;
while(temp!=NULL){
temp=temp->next;
count++;
}
temp=*head;
if(pos<=count){
if(pos==1){
newnode->next=*head;
(*head)->prev=newnode;
*head=newnode;
}else if(pos==count){
newnode->prev=*tail;
(*tail)->next=newnode;
*tail=newnode;
}
else{
int c=2;
while(c!=pos){
temp=temp->next;
c++;
//printf("\ntemp value=%d \n",temp->data);
}
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
}
}else{
printf("Position exceeds list\n");
}
}
int dll_pop_at_position(dll_node** head,dll_node** tail,int pos){
int count=0;
int res=0;
dll_node* temp=*head;
while(temp!=NULL){
temp=temp->next;
count++;
}
temp=*head;
if(pos<=count){
if(pos==1){
res=dll_pop_at_start(head,tail);
}
else if(pos==count){
res=dll_pop_at_end(head,tail);
}
else{
int c=1;
while(c!=pos){
temp=temp->next;
c++;
}
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
res=temp->data;
return res;
}
}else{
printf("Position exceeds list\n");
}
}