Skip to content

Commit c4d05e7

Browse files
authored
Singly Linked List
1 parent 524ec78 commit c4d05e7

File tree

2 files changed

+363
-0
lines changed

2 files changed

+363
-0
lines changed

singlyLinkedList.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include "singlylinkedlist.h"
4+
5+
int main()
6+
{
7+
NODEPTR list,list2; // 'list' holds the initial node
8+
int choice, data, pos, search; // declaring other variables
9+
10+
11+
initList(&list); // function call to initialize list
12+
createList(&list); // function call to create list
13+
do
14+
{
15+
printf("\n---------MENU---------\n1.Display List\n2.Count Nodes\n3.Concat List\n4.Insert First\n5.Insert Last\n");
16+
printf("6.Insert WRT Position\n7.Insert WRT Information\n");
17+
printf("8.Delete First\n9.Delete Last\n10.Delete WRT Postition\n");
18+
printf("11.Delete WRT Information\n12.Reverse the list\n13.Exit\nEnter choice: ");
19+
scanf("%d",&choice);
20+
switch(choice)
21+
{
22+
case 1: displayList(list); // function call to display list
23+
break;
24+
case 2: printf("\nThere are %d number of node(s) in the list.\n",countNode(list)); // function call to count number of nodes in the list
25+
break;
26+
case 3: initList(&list2); // function call to initialize list
27+
createList(&list2); // function call to create list
28+
concatLists(&list,&list2); // function call to concat lists
29+
break;
30+
case 4: printf("\nEnter information to insert...");
31+
scanf("%d",&data);
32+
insertFirst(&list,data); // function call to insert at first
33+
break;
34+
case 5: printf("\nEnter information to insert...");
35+
scanf("%d",&data);
36+
insertLast(&list,data); // function call to insert at last
37+
break;
38+
case 6: printf("\nEnter position to insert...");
39+
scanf("%d",&pos);
40+
printf("\nEnter information to insert...");
41+
scanf("%d",&data);
42+
insertPos(&list,pos,data); // function call to insert at last
43+
break;
44+
case 7: printf("\nEnter information after which you want to insert...");
45+
scanf("%d",&search);
46+
printf("\nEnter information to insert...");
47+
scanf("%d",&data);
48+
insertInfo(&list,search,data); // function call to insert at last
49+
break;
50+
case 8: deleteFirst(&list);
51+
break;
52+
case 9: deleteLast(&list);
53+
break;
54+
case 10: printf("\nEnter position to delete...");
55+
scanf("%d",&pos);
56+
deletePos(&list,pos);
57+
break;
58+
case 11: printf("\nEnter information to delete...");
59+
scanf("%d",&data);
60+
deleteInfo(&list,data);
61+
break;
62+
case 12: reverseList(&list);
63+
break;
64+
case 13: printf("\nDONE!\n");
65+
break;
66+
default: printf("\nInvalid choice. Re-enter please...\n");
67+
break;
68+
}
69+
}
70+
while(choice!=13);
71+
return 0;
72+
}
73+
74+

singlylinkedlist.h

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
typedef struct node
2+
{
3+
int info;
4+
struct node *next;
5+
}*NODEPTR;
6+
7+
// function prototypes
8+
void initList(NODEPTR *); // initialize list
9+
void createList(NODEPTR *); // create list
10+
void displayList(NODEPTR ); // display list
11+
int countNode(NODEPTR); // count number of nodes in the list
12+
void concatLists(NODEPTR *,NODEPTR *); // concate two lists
13+
void insertFirst(NODEPTR *,int); // to insert a data at first node
14+
void insertAfter(NODEPTR *,int); // insert a node just after the node pointed to by this pointer
15+
void insertLast(NODEPTR *,int); // insert a data as the last node
16+
void insertPos(NODEPTR *,int,int); // insert data at a given position
17+
void insertInfo(NODEPTR *,int,int); // insert a data after a given data
18+
void deleteFirst(NODEPTR *); // delete the first node
19+
void deleteAfter(NODEPTR *); // delete the node just after the node pointed to by this pointer
20+
void deleteLast(NODEPTR *); // delete the last node
21+
void deletePos(NODEPTR *,int); // delete data from a given position
22+
void deleteInfo(NODEPTR *,int); // delete all occurences of a given data
23+
void reverseList(NODEPTR *); // to reverse the list
24+
25+
// function definitions
26+
// initialize list
27+
void initList(NODEPTR *pList)
28+
{
29+
*pList = NULL;
30+
}
31+
32+
// create list
33+
void createList(NODEPTR *pList)
34+
{
35+
NODEPTR newNode,prevNode;
36+
char choice;
37+
do
38+
{
39+
newNode = malloc(sizeof(struct node));
40+
if(newNode == NULL)
41+
{
42+
printf("\nSYSTEM MEMORY EXHAUSTED. \n");
43+
return;
44+
}
45+
46+
printf("\nEnter information: ");
47+
scanf("%d",&newNode->info);
48+
newNode->next = NULL;
49+
50+
if(*pList == NULL)
51+
*pList = newNode; // first node
52+
else
53+
prevNode->next = newNode; // remove NULL content
54+
55+
printf("\n Press Y / y to continue...\n");
56+
scanf(" %c",&choice);
57+
prevNode=newNode; // holds the last node created
58+
}
59+
while(choice=='Y' || choice=='y');
60+
}
61+
62+
// display list
63+
void displayList(NODEPTR pList)
64+
{
65+
if(pList == NULL)
66+
{
67+
printf("\nLIST EMPTY!!!\n");
68+
return;
69+
}
70+
printf("\nThe list: \n");
71+
for(;pList->next != NULL;pList = pList->next)
72+
printf("%d->",pList->info);
73+
printf("%d\n",pList->info);
74+
}
75+
76+
// count number of nodes in the list
77+
int countNode(NODEPTR pList)
78+
{
79+
int count=0;
80+
for(;pList!=NULL;pList = pList->next,++count)
81+
;
82+
return count;
83+
}
84+
85+
// concate two lists
86+
void concatLists(NODEPTR *list1,NODEPTR *list2)
87+
{
88+
NODEPTR temp;
89+
if(*list1 == NULL)
90+
{
91+
*list1 = *list2;
92+
return;
93+
}
94+
else if(*list2 == NULL)
95+
return;
96+
else
97+
{
98+
for(temp = *list1; temp->next!=NULL; temp = temp->next)
99+
;
100+
temp->next = *list2;
101+
}
102+
}
103+
104+
// to insert a data at first node
105+
void insertFirst(NODEPTR *pList,int data)
106+
{
107+
NODEPTR newNode;
108+
newNode = (struct node *)malloc(sizeof(struct node));
109+
if(newNode == NULL)
110+
{
111+
printf("\nMEMORY EXHAUSTED. Cannot continue...\n");
112+
exit(1);
113+
}
114+
newNode->info = data;
115+
newNode->next = *pList;
116+
*pList = newNode;
117+
}
118+
119+
// insert a node just after the node pointed to by this pointer
120+
void insertAfter(NODEPTR *pList,int data)
121+
{
122+
NODEPTR newNode;
123+
newNode = (struct node *)malloc(sizeof(struct node));
124+
if(newNode == NULL)
125+
{
126+
printf("\nMEMORY EXHAUSTED. Cannot continue...\n");
127+
exit(1);
128+
}
129+
newNode->info = data;
130+
newNode->next = (*pList)->next;
131+
(*pList)->next = newNode;
132+
}
133+
134+
// insert a data as the last node
135+
void insertLast(NODEPTR *pList,int data)
136+
{
137+
NODEPTR newNode, temp;
138+
if(*pList == NULL)
139+
insertFirst(pList,data);
140+
else
141+
{
142+
for(temp = *pList; temp->next!=NULL; temp = temp->next)
143+
;
144+
insertAfter(&temp,data);
145+
}
146+
}
147+
148+
// insert data at a given position
149+
void insertPos(NODEPTR *pList,int pos,int data)
150+
{
151+
NODEPTR temp;
152+
int i;
153+
//temp = *pList;
154+
if( (pos < 1) || (pos > countNode(*pList)+1) )
155+
{
156+
printf("\n\nINVALID POSITION!\n\n");
157+
exit(1);
158+
}
159+
if(pos==1)
160+
insertFirst(pList,data);
161+
else
162+
{
163+
for(temp = *pList, i = 1; i<pos-1; temp = temp->next )
164+
++i;
165+
insertAfter(&temp,data);
166+
}
167+
}
168+
169+
// insert a data after a given data
170+
void insertInfo(NODEPTR *pList,int search,int data)
171+
{
172+
NODEPTR temp;
173+
int status;
174+
status = 0; // to check if a match is found at all
175+
for(temp = *pList; temp!=NULL; temp = temp->next)
176+
{
177+
if( temp->info == search)
178+
{
179+
status = 1;
180+
insertAfter(&temp,data);
181+
if(temp->info == data) // to avoid infinite loop for same 'search' and 'data'
182+
temp = temp->next;
183+
}
184+
}
185+
if(!status)
186+
printf("\n\nNO MATCH FOUND!\n\n");
187+
}
188+
189+
// delete the first node
190+
void deleteFirst(NODEPTR *pList)
191+
{
192+
NODEPTR delNode;
193+
if(*pList == NULL)
194+
{
195+
printf("\nLIST EMPTY.\n");
196+
return;
197+
}
198+
delNode = *pList;
199+
*pList = delNode->next;
200+
free(delNode);
201+
}
202+
203+
// delete the node just after the node pointed to by this pointer
204+
void deleteAfter(NODEPTR *pList)
205+
{
206+
NODEPTR delNode;
207+
delNode = (*pList)->next;
208+
(*pList)->next = delNode->next;
209+
free(delNode);
210+
}
211+
212+
// delete the last node
213+
void deleteLast(NODEPTR *pList)
214+
{
215+
NODEPTR temp, delNode;
216+
if(*pList == NULL) // empty list
217+
{
218+
printf("\nLIST EMPTY.\n");
219+
return;
220+
}
221+
if((*pList)->next == NULL)
222+
deleteFirst(pList);
223+
else
224+
{
225+
temp = *pList;
226+
227+
for(; temp->next != NULL ; temp = temp->next)
228+
delNode = temp;
229+
deleteAfter(&delNode);
230+
}
231+
}
232+
233+
// delete data from a given position
234+
void deletePos(NODEPTR *pList,int pos)
235+
{
236+
NODEPTR delNode;
237+
int i;
238+
if((pos < 1) || (pos > countNode(*pList)) )
239+
{
240+
printf("\n\nINVALID POSITION!\n\n");
241+
exit(1);
242+
}
243+
if(pos==1)
244+
deleteFirst(pList);
245+
else
246+
{
247+
for(delNode = *pList, i = 1; i < pos - 1; delNode = delNode ->next)
248+
;
249+
deleteAfter(&delNode);
250+
}
251+
}
252+
253+
// delete all occurences of a given data
254+
void deleteInfo(NODEPTR *pList,int data)
255+
{
256+
NODEPTR temp,prevNode;
257+
if(*pList == NULL)
258+
{
259+
printf("\nINSUFFICIENT NUMBER OF NODES!\n");
260+
return;
261+
}
262+
for(temp = *pList, prevNode = NULL; temp!=NULL && temp->info!=data ;prevNode = temp, temp = temp->next)
263+
;
264+
if(temp == NULL)
265+
{
266+
printf("\n\nNO MATCH FOUND!\n\n");
267+
return;
268+
}
269+
if(prevNode == NULL)
270+
deleteFirst(pList);
271+
else
272+
deleteAfter(&prevNode);
273+
//*pList = temp;
274+
}
275+
276+
void reverseList(NODEPTR *pList) // to reverse the list
277+
{
278+
NODEPTR p, q, temp;
279+
p = q = temp = NULL;
280+
if(*pList == NULL)
281+
return;
282+
for(p = *pList; p != NULL; p=temp )
283+
{
284+
temp = p->next;
285+
p->next = q;
286+
q=p;
287+
}
288+
*pList = q;
289+
}

0 commit comments

Comments
 (0)