Skip to content

Commit cfbb046

Browse files
authored
Merge pull request #746 from Monik09/DoubleLinkedListC
Added Double linked list[C]
2 parents 9ece5b2 + 0c94cc8 commit cfbb046

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node
5+
{
6+
int data;
7+
struct node *next;
8+
struct node *prev;
9+
};
10+
11+
struct node *head = NULL, *last = NULL;
12+
13+
struct node *createNode(int key)
14+
{
15+
head = (struct node *)malloc(sizeof(struct node)); //allocate the required space to *node variable
16+
head->data = key;
17+
head->next = NULL;
18+
head->prev = NULL;
19+
last = head;
20+
return head;
21+
}
22+
23+
struct node *additionOfNodeAtBeginning(int key) //adding at the beginning of the linked list
24+
{
25+
struct node *temp;
26+
temp = (struct node *)malloc(sizeof(struct node));
27+
temp->data = key;
28+
temp->next = head; //making temp variable point to head of the linked list
29+
temp->prev = NULL; //pointing the prev of temp to NULL pointer
30+
head->prev = temp;
31+
head = temp;
32+
return head;
33+
}
34+
35+
struct node *additionOfNodeAtEnding(int key) //adding the element at the last of the linked list
36+
{
37+
struct node *temp;
38+
temp = (struct node *)malloc(sizeof(struct node));
39+
temp->data = key;
40+
temp->next = NULL;
41+
temp->prev = last;
42+
last->next = temp;
43+
temp->next = NULL;
44+
last = temp;
45+
return last;
46+
}
47+
48+
struct node *additionOfNodeAtRandomeIndex(int key, int index) //adding the element at the given index
49+
{
50+
struct node *temp;
51+
temp = (struct node *)malloc(sizeof(struct node));
52+
temp->data = key;
53+
temp->next = NULL;
54+
if (index == 0)
55+
{
56+
return additionOfNodeAtBeginning(key);
57+
}
58+
struct node *temp1, *temp2;
59+
temp1 = head;
60+
int i = 0;
61+
//iterating through the list till the iterator variable is index or list is empty
62+
while (i < index - 1 && temp1->next != NULL)
63+
{
64+
temp1 = temp1->next;
65+
i++;
66+
}
67+
if (temp1->next == NULL)
68+
{
69+
additionOfNodeAtEnding(key);
70+
}
71+
temp2 = temp1->next;
72+
temp->prev = temp1;
73+
temp1->next = temp;
74+
temp->next = temp2;
75+
temp2->prev = temp;
76+
return head;
77+
}
78+
79+
struct node *deletingFromBeginning() //deleting the element at head
80+
{
81+
struct node *temp;
82+
temp = head;
83+
head = head->next; //making the head pointer to point the element next to it
84+
head->prev = NULL;
85+
free(temp); //free the space occupied by former head pointer
86+
return head;
87+
}
88+
89+
struct node *deletingFromEnding() //deleting the element from the ending
90+
{
91+
struct node *temp, *temp2;
92+
temp = last->prev; //temp pointer pointing to the second last element of linked list
93+
temp2 = last; //temp2 pointing to last element
94+
temp->next = NULL;
95+
last = temp; //make second last pointer as last element
96+
free(temp2);
97+
return head;
98+
}
99+
100+
struct node *deletingFromRandomIndex(int index) //deleting the element from given index
101+
{
102+
struct node *temp, *temp2;
103+
temp = head;
104+
int i = 0;
105+
while (temp != NULL && i < index)
106+
{
107+
i++;
108+
temp = temp->next;
109+
}
110+
if (index == 0)
111+
{
112+
return deletingFromBeginning();
113+
}
114+
else if (temp == last)
115+
{
116+
return deletingFromEnding();
117+
}
118+
else if (temp != NULL) //if temp is NULL the index is out of Bound
119+
{
120+
temp->prev->next = temp->next;
121+
temp->next->prev = temp->prev;
122+
free(temp);
123+
}
124+
else
125+
{
126+
printf("Index Out Of Bound\n"); //index is out of bound
127+
}
128+
return head;
129+
}
130+
131+
void displayInReverse() //display the linked list in reverse order
132+
{
133+
printf("the Linked List in Reverse Order is ");
134+
struct node *temp = last; //creating iterator pointing to the last element
135+
while (temp != NULL)
136+
{
137+
printf("%d ", temp->data);
138+
temp = temp->prev;
139+
}
140+
printf("\n\n");
141+
}
142+
143+
void display() //display the linked list
144+
{
145+
printf("the Linked List is ");
146+
struct node *temp = head; //creating iterator pointing to the head
147+
while (temp != NULL)
148+
{
149+
printf("%d ", temp->data);
150+
temp = temp->next;
151+
}
152+
printf("\n\n");
153+
}
154+
155+
int main()
156+
{
157+
struct node *root = NULL;
158+
root = createNode(4); // creating the list
159+
160+
additionOfNodeAtBeginning(6); //adding the value 6 at beginning
161+
additionOfNodeAtEnding(90); //adding value 90 at last
162+
printf("After adding 90 at last ,");
163+
display(); //display the list
164+
//index are starting from 0
165+
additionOfNodeAtRandomeIndex(12, 1); //adding 12 at index 1
166+
printf("After adding 12 at index 1 ,");
167+
display();
168+
additionOfNodeAtBeginning(1);
169+
additionOfNodeAtEnding(2);
170+
additionOfNodeAtBeginning(5);
171+
additionOfNodeAtEnding(98);
172+
additionOfNodeAtRandomeIndex(122, 2); //index are starting from 0
173+
display();
174+
deletingFromBeginning(); //deleting the head element
175+
printf("After Deletion from beginning, ");
176+
display();
177+
deletingFromEnding(); //deleting the last element from the list
178+
printf("After deleting from end, ");
179+
display();
180+
deletingFromRandomIndex(3); //deleting the element at index 3(0 is starting index)
181+
printf("After deleting from index 3 ,");
182+
display();
183+
184+
displayInReverse(); //display the linked list in reverse order
185+
}

0 commit comments

Comments
 (0)