1+ #include <stdio.h>
2+ #include "LL.h"
3+
4+ /* For visualization: 4 --> 10 --> 7 --> 5 --> 16 */
5+
6+ /* Globally declaring two static NODE pointers, head and tail */
7+ static NODE * head = NULL ;
8+ static NODE * tail = NULL ;
9+ // Global/static pointers would anyway get initialized to NULL if left uninitialized
10+
11+
12+ int main (){
13+
14+ int hdata ;
15+ printf ("Enter desired head-node data\n" );
16+ scanf ("%d" , & hdata );
17+
18+ create (hdata );
19+
20+ append (9 );
21+ create (99 );
22+
23+ insert (8 , 0 );
24+
25+ prepend (7 );
26+
27+ insert (11 , 3 );
28+
29+ takeout (0 );
30+ append (7 );
31+
32+ reverse ();
33+
34+ sortAscndg ();
35+
36+ return 0 ;
37+ }
38+
39+
40+ void create (int head_data ){
41+
42+ if (head != NULL ){
43+ printf ("List already exists. Consider using append(%d) or insert(%d) instead\n" , head_data , head_data );
44+ return ;
45+ }
46+ head = (NODE * ) malloc (sizeof (NODE ));
47+ head -> data = head_data ;
48+ head -> ptr = NULL ;
49+ tail = head ;
50+
51+ printf ("LL after create(%d): \n" , head_data );
52+ print (head );
53+ return ;
54+ }
55+
56+
57+ void print (NODE * head ){
58+
59+ NODE * iter = head ;
60+
61+ if (NULL == iter ){
62+ printf ("Empty list. Nothing to print.\n" );
63+ return ;
64+ }
65+ while (iter -> ptr != NULL ){ // exits once iter points at Tail node
66+
67+ printf ("%d --> " , iter -> data );
68+ iter = iter -> ptr ;
69+ }
70+ printf ("%d \n" , iter -> data ); // BEAUTIFICATION: to avoid printing an arrow after Tail node
71+ return ;
72+ }
73+
74+
75+ void append (int node_data ){
76+
77+ /* ITCO presence of Tail pointer */
78+ /*
79+ if (head == NULL){
80+ printf("Empty list. Call create() first\n");
81+ return;
82+ }
83+ NODE* temp = (NODE*) malloc (sizeof(NODE));
84+ temp->data = node_data;
85+ temp->ptr = NULL;
86+ tail->ptr = temp;
87+ tail = temp;
88+ temp = NULL; // to avoid misuse of temp pointer
89+ return;
90+ */
91+
92+ /* ITCO absence of Tail pointer */
93+
94+ if (head == NULL ){
95+ printf ("Empty list. Call create() first\n" );
96+ return ;
97+ }
98+ NODE * temp = (NODE * ) malloc (sizeof (NODE ));
99+ temp -> data = node_data ;
100+ temp -> ptr = NULL ;
101+
102+ // Define iter NODE ptr to get to the end of LL
103+ NODE * iter = head ;
104+ while (iter -> ptr != NULL ){ // exits when iter points to Tail node
105+
106+ iter = iter -> ptr ;
107+ }
108+ iter -> ptr = temp ;
109+
110+ printf ("LL after append(%d): \n" , node_data );
111+ print (head );
112+ return ;
113+ }
114+
115+
116+ void insert (int node_data , int pos ){
117+
118+
119+ if (0 == pos ){ // to cover the case of pos == 0
120+
121+ printf ("Consider using prepend(%d) instead, but sure\n" , node_data );
122+ prepend (node_data );
123+
124+ return ;
125+ }
126+
127+ // else if // MODIFYLATER to cover pos > Tail node pos
128+
129+ NODE * newnode = (NODE * ) malloc (sizeof (NODE ));
130+ newnode -> data = node_data ;
131+ newnode -> ptr = NULL ; // to avoid indeterminacy
132+
133+ NODE * temp = head ;
134+ int count = 0 ;
135+
136+ while ((temp -> ptr != NULL ) && (count != (pos - 1 ))){ // Traverse till (pos-1)
137+ temp = temp -> ptr ;
138+ count ++ ;
139+ }
140+
141+ if (count == (pos - 1 )){ // If reached the right index/pos
142+ newnode -> ptr = temp -> ptr ;
143+ temp -> ptr = newnode ;
144+
145+ printf ("LL after insert(%d, %d): \n" , node_data , pos );
146+ print (head );
147+
148+ return ;
149+ }
150+
151+ else if (temp -> ptr == NULL ){ // If reached the end of the LL
152+ printf ("No such index, thus appending \n" );
153+ append (node_data );
154+
155+ printf ("LL after insert(%d, %d): \n" , node_data , pos );
156+ print (head );
157+
158+ return ;
159+ }
160+
161+
162+ }
163+
164+
165+ void prepend (int hnode_data ){
166+
167+ NODE * newnode = (NODE * ) malloc (sizeof (NODE ));
168+ newnode -> data = hnode_data ;
169+ newnode -> ptr = head ;
170+ head = newnode ;
171+
172+ printf ("LL after prepend(%d): \n" , hnode_data );
173+ print (head );
174+ return ;
175+ }
176+
177+ void takeout (int pos ){
178+
179+ // if pos == 0 --> handle separately
180+ // iter till pos-1
181+ // iter->ptr = iter->ptr->ptr
182+ // print taken out NODE data
183+ // deallocate takenout NODE
184+ // MODIFYLATER to cover pos == Tail NODE, as well as > Tail NODE
185+
186+ if (0 == pos ){
187+
188+ NODE * temp = head ;
189+ head = head -> ptr ;
190+ printf ("Takenout NODE data = %d\n" , temp -> data );
191+ free (temp );
192+
193+ printf ("LL after takeout(%d): \n" , pos );
194+ print (head );
195+ return ;
196+ }
197+ NODE * iter = head ;
198+ int count = 0 ;
199+
200+ NODE * temp = NULL ;
201+ while (count != (pos - 1 )){ // exits when iter is at pos-1
202+
203+ iter = iter -> ptr ;
204+ count ++ ;
205+ }
206+ temp = iter -> ptr ;
207+ iter -> ptr = temp -> ptr ;
208+ printf ("Takenout NODE data = %d\n" , temp -> data );
209+ free (temp );
210+
211+ printf ("LL after takeout(%d): \n" , pos );
212+ print (head );
213+ return ;
214+ }
215+
216+
217+ void reverse (void ){
218+
219+ NODE * iter = head ;
220+
221+ if (NULL == iter ){
222+
223+ printf ("Empty list. Nothng to reverse\n" );
224+ return ;
225+ }
226+
227+ NODE * iterp = NULL ;
228+ NODE * iterf = iter -> ptr ;
229+
230+ while (iter -> ptr != NULL ){
231+
232+ iter -> ptr = iterp ;
233+ iterp = iter ;
234+ iter = iterf ;
235+ iterf = iterf -> ptr ;
236+ }
237+
238+ iter -> ptr = iterp ;
239+ head = iter ;
240+
241+ printf ("LL after reverse(): \n" );
242+ print (head );
243+
244+ return ;
245+ }
246+
247+ void sortAscndg (void ){
248+
249+ // Using Selection Sort
250+
251+ NODE * iter = head ;
252+ if (NULL == iter ){
253+
254+ printf ("Empty list. Nothng to sort\n" );
255+ return ;
256+ }
257+ NODE * iter2 = NULL ;
258+
259+ while (iter -> ptr != NULL ){
260+
261+ iter2 = iter -> ptr ;
262+
263+ while (iter2 != NULL ){
264+
265+ if (iter2 -> data < iter -> data )
266+ {
267+ int tmp = iter -> data ;
268+ iter -> data = iter2 -> data ;
269+ iter2 -> data = tmp ;
270+ }
271+ iter2 = iter2 -> ptr ;
272+ }
273+ iter = iter -> ptr ;
274+ }
275+
276+ printf ("LL after sortAscndg(): \n" );
277+ print (head );
278+
279+ return ;
280+ }
0 commit comments