Skip to content

Commit 2275a48

Browse files
authored
Create Single Linked List.c
C Program to Implement the Singly Linked List using Dynamic Memory Allocation.
1 parent 87a2775 commit 2275a48

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

linked-list/Single Linked List.c

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
6+
struct node
7+
{
8+
int data;
9+
int key;
10+
struct node *next;
11+
};
12+
13+
struct node *head = NULL;
14+
15+
//display the list
16+
void printList()
17+
{
18+
struct node *ptr = head;
19+
printf("\n[ ");
20+
21+
//start from the beginning
22+
while(ptr != NULL)
23+
{
24+
printf("(%d,%d) ",ptr->key,ptr->data);
25+
ptr = ptr->next;
26+
}
27+
28+
printf(" ]");
29+
}
30+
31+
//insert node at the first location
32+
void insertFirst(int key, int data)
33+
{
34+
//create a link
35+
struct node *link = (struct node*) malloc(sizeof(struct node));
36+
37+
link->key = key;
38+
link->data = data;
39+
40+
//point it to old first node
41+
link->next = head;
42+
43+
//point first to new first node
44+
head = link;
45+
}
46+
47+
//delete first item
48+
struct node* deleteFirst()
49+
{
50+
//save reference to first link
51+
struct node *tempLink = head;
52+
53+
//mark next to first link as first
54+
head = head->next;
55+
56+
//return the deleted link
57+
return tempLink;
58+
}
59+
60+
//is list empty
61+
bool isEmpty()
62+
{
63+
return head == NULL;
64+
}
65+
66+
//returns the length of the chain
67+
int length()
68+
{
69+
int length = 0;
70+
struct node *current;
71+
72+
for(current = head; current != NULL; current = current->next)
73+
{
74+
length++;
75+
}
76+
77+
return length;
78+
}
79+
80+
//find a link with given key
81+
struct node* find(int key)
82+
{
83+
84+
//start from the first link
85+
struct node* current = head;
86+
87+
//if list is empty
88+
if(head == NULL)
89+
{
90+
return NULL;
91+
}
92+
93+
//navigate through list
94+
while(current->key != key)
95+
{
96+
//if it is last node
97+
if(current->next == NULL)
98+
{
99+
return NULL;
100+
}
101+
else
102+
{
103+
//go to next link
104+
current = current->next;
105+
}
106+
}
107+
108+
//if data found, return the current Link
109+
return current;
110+
}
111+
112+
//delete a link with given key
113+
struct node* delete(int key)
114+
{
115+
116+
//start from the first link
117+
struct node* current = head;
118+
struct node* previous = NULL;
119+
120+
//if list is empty
121+
if(head == NULL)
122+
{
123+
return NULL;
124+
}
125+
126+
//navigate through list
127+
while(current->key != key)
128+
{
129+
130+
//if it is last node
131+
if(current->next == NULL)
132+
{
133+
return NULL;
134+
}
135+
else
136+
{
137+
//store reference to current link
138+
previous = current;
139+
//move to next link
140+
current = current->next;
141+
}
142+
}
143+
144+
//found a match, update the link
145+
if(current == head)
146+
{
147+
//change first to point to next link
148+
head = head->next;
149+
}
150+
else
151+
{
152+
//bypass the current link
153+
previous->next = current->next;
154+
}
155+
156+
return current;
157+
}
158+
159+
//sort the linked list
160+
void sort()
161+
{
162+
int i, j, k, tempKey, tempData ;
163+
struct node *current;
164+
struct node *next;
165+
166+
int size = length();
167+
k = size ;
168+
169+
for ( i = 0 ; i < size - 1 ; i++, k-- )
170+
{
171+
current = head ;
172+
next = head->next ;
173+
174+
for ( j = 1 ; j < k ; j++ )
175+
{
176+
177+
if ( current->data > next->data )
178+
{
179+
tempData = current->data ;
180+
current->data = next->data;
181+
next->data = tempData ;
182+
183+
tempKey = current->key;
184+
current->key = next->key;
185+
next->key = tempKey;
186+
}
187+
188+
current = current->next;
189+
next = next->next;
190+
}
191+
}
192+
}
193+
194+
//reverse the linked list
195+
void reverse(struct node** head_ref)
196+
{
197+
struct node* prev = NULL;
198+
struct node* current = *head_ref;
199+
struct node* next;
200+
201+
while (current != NULL)
202+
{
203+
next = current->next;
204+
current->next = prev;
205+
prev = current;
206+
current = next;
207+
}
208+
209+
*head_ref = prev;
210+
}
211+
212+
int main()
213+
{
214+
insertFirst(1,10);
215+
insertFirst(2,20);
216+
insertFirst(3,30);
217+
insertFirst(4,1);
218+
insertFirst(5,40);
219+
insertFirst(6,56);
220+
221+
printf("Original List: ");
222+
223+
//print list
224+
printList();
225+
226+
while(!isEmpty())
227+
{
228+
struct node *temp = deleteFirst();
229+
printf("\nDeleted value:");
230+
printf("(%d,%d) ",temp->key,temp->data);
231+
}
232+
233+
printf("\nList after deleting all items: ");
234+
printList();
235+
insertFirst(1,10);
236+
insertFirst(2,20);
237+
insertFirst(3,30);
238+
insertFirst(4,1);
239+
insertFirst(5,40);
240+
insertFirst(6,56);
241+
printf("\nRestored List: ");
242+
printList();
243+
printf("\n");
244+
245+
struct node *foundLink = find(4);
246+
247+
if(foundLink != NULL)
248+
{
249+
printf("Element found: ");
250+
printf("(%d,%d) ",foundLink->key,foundLink->data);
251+
printf("\n");
252+
}
253+
else
254+
{
255+
printf("Element not found.");
256+
}
257+
258+
delete(4);
259+
printf("List after deleting an item: ");
260+
printList();
261+
printf("\n");
262+
foundLink = find(4);
263+
264+
if(foundLink != NULL)
265+
{
266+
printf("Element found: ");
267+
printf("(%d,%d) ",foundLink->key,foundLink->data);
268+
printf("\n");
269+
}
270+
else
271+
{
272+
printf("Element not found.");
273+
}
274+
275+
printf("\n");
276+
sort();
277+
278+
printf("List after sorting the data: ");
279+
printList();
280+
281+
reverse(&head);
282+
printf("\nList after reversing the data: ");
283+
printList();
284+
285+
return 0;
286+
}

0 commit comments

Comments
 (0)