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