Skip to content

Commit e5596d9

Browse files
committed
DoublyLinkedList
1 parent 643e8c7 commit e5596d9

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

doubleLinkedList/doubleLinkedList.c

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,27 @@ struct node *head = NULL, *last = NULL;
1212

1313
struct node *createNode(int key)
1414
{
15-
head = (struct node *)malloc(sizeof(struct node));
15+
head = (struct node *)malloc(sizeof(struct node)); //allocate the required space to *node variable
1616
head->data = key;
1717
head->next = NULL;
1818
head->prev = NULL;
1919
last = head;
2020
return head;
2121
}
2222

23-
struct node *additionOfNodeAtBeginning( int key)
23+
struct node *additionOfNodeAtBeginning(int key) //adding at the beginning of the linked list
2424
{
2525
struct node *temp;
2626
temp = (struct node *)malloc(sizeof(struct node));
2727
temp->data = key;
28-
temp->next = NULL;
29-
temp->next = head;
30-
temp->prev = NULL;
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
3130
head->prev = temp;
3231
head = temp;
3332
return head;
3433
}
3534

36-
struct node *additionOfNodeAtEnding( int key)
35+
struct node *additionOfNodeAtEnding(int key) //adding the element at the last of the linked list
3736
{
3837
struct node *temp;
3938
temp = (struct node *)malloc(sizeof(struct node));
@@ -46,15 +45,20 @@ struct node *additionOfNodeAtEnding( int key)
4645
return last;
4746
}
4847

49-
struct node *additionOfNodeAtRandomeIndex( int key, int index)
48+
struct node *additionOfNodeAtRandomeIndex(int key, int index) //adding the element at the given index
5049
{
5150
struct node *temp;
5251
temp = (struct node *)malloc(sizeof(struct node));
5352
temp->data = key;
5453
temp->next = NULL;
55-
struct node *temp1, *temp2, previous;
54+
if (index == 0)
55+
{
56+
return additionOfNodeAtBeginning(key);
57+
}
58+
struct node *temp1, *temp2;
5659
temp1 = head;
5760
int i = 0;
61+
//iterating through the list till the iterator variable is index or list is empty
5862
while (i < index - 1 && temp1->next != NULL)
5963
{
6064
temp1 = temp1->next;
@@ -72,52 +76,62 @@ struct node *additionOfNodeAtRandomeIndex( int key, int index)
7276
return head;
7377
}
7478

75-
struct node *deletingFromBeginning()
79+
struct node *deletingFromBeginning() //deleting the element at head
7680
{
7781
struct node *temp;
7882
temp = head;
79-
head = head->next;
83+
head = head->next; //making the head pointer to point the element next to it
8084
head->prev = NULL;
81-
free(temp);
85+
free(temp); //free the space occupied by former head pointer
8286
return head;
8387
}
8488

85-
struct node *deletingFromEnding()
89+
struct node *deletingFromEnding() //deleting the element from the ending
8690
{
8791
struct node *temp, *temp2;
88-
temp=last->prev;
89-
temp2=last;
90-
temp->next=NULL;
91-
last=temp;
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
9296
free(temp2);
9397
return head;
9498
}
9599

96-
struct node *deletingFromRandomIndex(int index)
100+
struct node *deletingFromRandomIndex(int index) //deleting the element from given index
97101
{
98102
struct node *temp, *temp2;
99103
temp = head;
100104
int i = 0;
101-
while (i < index - 1)
105+
while (temp != NULL && i < index)
102106
{
103107
i++;
104108
temp = temp->next;
105109
}
106-
if (temp->next == NULL)
110+
if (index == 0)
111+
{
112+
return deletingFromBeginning();
113+
}
114+
else if (temp == last)
107115
{
108116
return deletingFromEnding();
109117
}
110-
temp2 = temp->next;
111-
temp2->next->prev = temp;
112-
temp->next = temp->next->next;
113-
free(temp2);
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+
}
114128
return head;
115129
}
116130

117-
void displayInReverse()
131+
void displayInReverse() //display the linked list in reverse order
118132
{
119133
printf("the Linked List in Reverse Order is ");
120-
struct node *temp = last;
134+
struct node *temp = last; //creating iterator pointing to the last element
121135
while (temp != NULL)
122136
{
123137
printf("%d ", temp->data);
@@ -126,10 +140,10 @@ void displayInReverse()
126140
printf("\n\n");
127141
}
128142

129-
void display()
143+
void display() //display the linked list
130144
{
131145
printf("the Linked List is ");
132-
struct node *temp = head;
146+
struct node *temp = head; //creating iterator pointing to the head
133147
while (temp != NULL)
134148
{
135149
printf("%d ", temp->data);
@@ -141,31 +155,31 @@ void display()
141155
int main()
142156
{
143157
struct node *root = NULL;
158+
root = createNode(4); // creating the list
144159

145-
root = createNode( 4);
146-
147-
additionOfNodeAtBeginning(6);
148-
additionOfNodeAtEnding(90);
160+
additionOfNodeAtBeginning(6); //adding the value 6 at beginning
161+
additionOfNodeAtEnding(90); //adding value 90 at last
149162
printf("After adding 90 at last ,");
150-
display();
151-
additionOfNodeAtRandomeIndex(12,1);
163+
display(); //display the list
164+
//index are starting from 0
165+
additionOfNodeAtRandomeIndex(12, 1); //adding 12 at index 1
152166
printf("After adding 12 at index 1 ,");
153167
display();
154168
additionOfNodeAtBeginning(1);
155169
additionOfNodeAtEnding(2);
156170
additionOfNodeAtBeginning(5);
157171
additionOfNodeAtEnding(98);
158-
additionOfNodeAtRandomeIndex(122,2);
172+
additionOfNodeAtRandomeIndex(122, 2); //index are starting from 0
159173
display();
160-
deletingFromBeginning();
174+
deletingFromBeginning(); //deleting the head element
161175
printf("After Deletion from beginning, ");
162176
display();
163-
deletingFromEnding();
177+
deletingFromEnding(); //deleting the last element from the list
164178
printf("After deleting from end, ");
165179
display();
166-
deletingFromRandomIndex(3);
180+
deletingFromRandomIndex(3); //deleting the element at index 3(0 is starting index)
167181
printf("After deleting from index 3 ,");
168182
display();
169183

170-
displayInReverse();
184+
displayInReverse(); //display the linked list in reverse order
171185
}

0 commit comments

Comments
 (0)