-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble linked list
137 lines (126 loc) · 3 KB
/
double linked list
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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node *head, *tail = NULL;
void create(int data) {
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
newnode->prev = NULL;
if (head == NULL) {
head = newnode;
tail = newnode;
} else {
newnode->prev = tail;
tail->next = newnode;
tail = tail->next;
}
}
void linkedlisttraversal(struct node *ptr) {
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
struct node *insertatfirst(struct node *head, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = data;
ptr->next = head;
ptr->prev = NULL;
head->prev = ptr;
head = ptr;
return head;
}
struct node *insertatpos(struct node *head, int pos, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp = head;
int i = 0;
while (i != pos - 1) {
temp = temp->next;
i++;
}
ptr->data = data;
ptr->next = temp->next;
ptr->prev = temp;
if (temp->next != NULL) {
temp->next->prev = ptr;
}
temp->next = ptr;
return head;
}
struct node *insertatlast(struct node *head, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
ptr->data = data;
ptr->prev = temp;
temp->next = ptr;
ptr->next = NULL;
return head;
}
struct node *deleteatfirst(struct node *head) {
if (head == NULL) {
return NULL;
}
struct node *ptr = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(ptr);
return head;
}
struct node *deleteatpos(struct node *head, int pos) {
struct node *ptr = head;
struct node *temp = head->next;
for (int i = 0; i < pos - 1; i++) {
ptr = ptr->next;
temp = temp->next;
}
ptr->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = ptr;
}
free(temp);
return head;
}
struct node *deleteatlast(struct node *head) {
if (head == NULL) {
return NULL;
}
struct node *ptr = head;
struct node *temp = head->next;
while (temp->next != NULL) {
ptr = ptr->next;
temp = temp->next;
}
ptr->next = NULL;
free(temp);
return head;
}
int main() {
create(10);
create(20);
create(30);
linkedlisttraversal(head);
head = insertatfirst(head, 60);
linkedlisttraversal(head);
head = insertatpos(head, 2, 50);
linkedlisttraversal(head);
head = insertatlast(head, 70);
linkedlisttraversal(head);
head = deleteatfirst(head);
linkedlisttraversal(head);
head = deleteatpos(head, 2);
linkedlisttraversal(head);
head = deleteatlast(head);
linkedlisttraversal(head);
return 0;
}