10000 Merge pull request #704 from rwithik/patch-1 · glitches-coder/Algorithms@90562b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 90562b4

Browse files
authored
Merge pull request VAR-solutions#704 from rwithik/patch-1
Create linkedList.c
2 parents 43fa40f + 9214ecf commit 90562b4

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

LinkedList/C/linkedList.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
// Define the node and declare head, temp variables.
5+
6+
struct node{
7+
int data;
8+
struct node* link;
9+
}*head, *temp;
10+
11+
//Function to insert at beginning
12+
void begins(int x){
13+
struct node* new = (struct node*)malloc(sizeof(struct node));
14+
new -> data = x;
15+
if (head == NULL){
16+
head = new;
17+
head -> link = NULL;
18+
return;
19+
}
20+
new -> link = head;
21+
head = new;
22+
}
23+
24+
//Function to insert at end
25+
void endins(int x){
26+
struct node *new, *ptr;
27+
new = (struct node*)malloc(sizeof(struct node));
28+
new -> data = x;
29+
if (head == NULL){
30+
head = new;
31+
head -> link = NULL;
32+
return;
33+
}
34+
ptr = head;
35+
while(ptr -> link != NULL){
36+
ptr = ptr -> link;
37+
}
38+
ptr -> link = new;
39+
new -> link = NULL;
40+
}
41+
42+
//Function to insert at a position
43+
void posins(int x, int pos){
44+
struct node *new, *ptr;
45+
new = (struct node*)malloc(sizeof(struct node));
46+
new -> data = x;
47+
if (head == NULL){
48+
head = new;
49+
head -> link = NULL;
50+
return;
51+
}
52+
ptr = head;
53+
int count = 0;
54+
while(count != pos && ptr -> link != NULL){
55+
ptr = ptr -> link;
56+
count++;
57+
}
58+
new -> link = ptr -> link;
59+
ptr -> link = new;
60+
}
61+
62+
//Function to insert before a specific data node
63+
void datains(int x, int data){
64+
struct node *new, *ptr, *ptr1;
65+
new = (struct node*)malloc(sizeof(struct node));
66+
new -> data = x;
67+
if (head == NULL){
68+
head = new;
69+
head -> link = NULL;
70+
return;
71+
}
72+
ptr = head;
73+
while(ptr -> data != data && ptr -> link != NULL){
74+
ptr1 = ptr;
75+
ptr = ptr -> link;
76+
}
77+
new -> link = ptr;
78+
ptr1 -> link = new;
79+
}
80+
81+
//Function to delete at beginning
82+
void begdel(){
83+
if (head == NULL){
84+
printf("LIST ALREADY EMPTY\n");
85+
return ;
86+
}
87+
temp = head;
88+
head = head -> link;
89+
free(temp);
90+
}
91+
92+
//Function to delete at end
93+
void enddel(){
94+
if (head == NULL){
95+
printf("LIST ALREADY EMPTY\n");
96+
return ;
97+
}
98+
struct node* ptr = head;
99+
while(ptr -> link -> link != NULL){
100+
ptr = ptr -> link;
101+
}
102+
temp = ptr -> link;
103+
ptr -> link = NULL;
104+
free(temp);
105+
}
106+
107+
//Function to delete at a position
108+
void posdel(int pos){
109+
if (head == NULL){
110+
printf("LIST ALREADY EMPTY\n");
111+
return ;
112+
}
113+
int count = 0;
114+
struct node* ptr = head, *ptr1;
115+
while(count != pos && ptr -> link != NULL){
116+
ptr1 = ptr;
117+
ptr = ptr -> link;
118+
count++;
119+
}
120+
temp = ptr;
121+
ptr1 -> link = ptr -> link;
122+
free(temp);
123+
}
124+
125+
//Function to delete before a specific data node
126+
void datadel(int data){
127+
if (head == NULL){
128+
printf("LIST ALREADY EMPTY\n");
129+
return ;
130+
}
131+
struct node* ptr = head, *ptr1;
132+
while(ptr -> link -> data != data && ptr -> link != NULL){
133+
ptr1 = ptr;
134+
ptr = ptr -> link;
135+
}
136+
temp = ptr;
137+
ptr1 -> link = ptr -> link;
138+
free(temp);
139+
}
140+
141+
//Function to neatly display the list
142+
void disp(){
143+
printf("\n======================================================\n");
144+
struct node* ptr = head;
145+
while (ptr != NULL){
146+
printf("%d ", ptr -> data);
147+
ptr = ptr -> link;
148+
}
149+
printf("\n======================================================\n");
150+
}
151+
152+
int main(){
153+
int c, x, pos;
154+
while(1){
155+
printf("1. Beg Ins\n2. End Ins\n3. Beg Del\n4. End Del\n5. Pos Ins\n6. Data Ins\n7. Pos Del\n8. Data Del\nEnter your choice: ");
156+
scanf("%d", &c);
157+
switch(c){
158+
case 1: printf("Enter the data to be inserted: ");
159+
scanf("%d", &x);
160+
begins(x);
161+
break;
162+
case 2: printf("Enter the data to be inserted: ");
163+
scanf("%d", &x);
164+
endins(x);
165+
break;
166+
case 3: begdel();
167+
break;
168+
case 4: enddel();
169+
break;
170+
case 5: printf("Enter the Position and Data: ");
171+
scanf("%d%d", &pos, &x);
172+
if(pos == 0){
173+
begins(x);
174+
break;
175+
}
176+
posins(x, pos-1);
177+
break;
178+
case 6: printf("Enter the insert-before-data data and data: ");
179+
scanf("%d%d", &pos, &x);
180+
datains(x, pos);
181+
break;
182+
case 8: printf("Enter the delete-before-data data: ");
183+
scanf("%d", &pos);
184+
datadel(pos);
185+
break;
186+
case 7: printf("Enter the Position: ");
187+
scanf("%d", &pos);
188+
if(pos == 0){
189+
begdel();
190+
break;
191+
}
192+
posdel(pos-1);
193+
break;
194+
default:printf("INVALID CHOICE\n");
195+
}
196+
disp();
197+
}
198+
return 0;
199+
}

0 commit comments

Comments
 (0)
0