Skip to content

Commit 56f5088

Browse files
Add files via upload
1 parent 8d8de2c commit 56f5088

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

linked.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
struct node {
5+
int data;
6+
struct node *next;
7+
};
8+
9+
struct node *start = NULL;
10+
void insert_at_begin(int);
11+
void insert_at_end(int);
12+
void traverse();
13+
void delete_from_begin();
14+
void delete_from_end();
15+
int count = 0;
16+
17+
int main () {
18+
int i, data;
19+
20+
for (;;) {
21+
printf("1. Insert an element at the beginning of linked list.\n");
22+
printf("2. Insert an element at the end of linked list.\n");
23+
printf("3. Traverse linked list.\n");
24+
printf("4. Delete an element from beginning.\n");
25+
printf("5. Delete an element from end.\n");
26+
// printf("6. Exit\n");
27+
28+
scanf("%d", &i);
29+
30+
if (i == 1) {
31+
printf("Enter value of element\n");
32+
scanf("%d", &data);
33+
insert_at_begin(data);
34+
}
35+
else if (i == 2) {
36+
printf("Enter value of element\n");
37+
scanf("%d", &data);
38+
insert_at_end(data);
39+
}
40+
else if (i == 3)
41+
traverse();
42+
else if (i == 4)
43+
delete_from_begin();
44+
else if (i == 5)
45+
delete_from_end();
46+
else if (i == 6)
47+
break;
48+
else
49+
printf("Please enter valid input.\n");
50+
}
51+
52+
return 0;
53+
}
54+
55+
void insert_at_begin(int x) {
56+
struct node *t;
57+
58+
t = (struct node*)malloc(sizeof(struct node));
59+
t->data = x;
60+
count++;
61+
62+
if (start == NULL) {
63+
start = t;
64+
start->next = NULL;
65+
return;
66+
}
67+
68+
t->next = start;
69+
start = t;
70+
}
71+
72+
void insert_at_end(int x) {
73+
struct node *t, *temp;
74+
75+
t = (struct node*)malloc(sizeof(struct node));
76+
t->data = x;
77+
count++;
78+
79+
if (start == NULL) {
80+
start = t;
81+
start->next = NULL;
82+
return;
83+
}
84+
85+
temp = start;
86+
87+
while (temp->next != NULL)
88+
temp = temp->next;
89+
90+
temp->next = t;
91+
t->next = NULL;
92+
}
93+
94+
void traverse() {
95+
struct node *t;
96+
97+
t = start;
98+
99+
if (t == NULL) {
100+
printf("Linked list is empty.\n");
101+
return;
102+
}
103+
104+
printf("There are %d elements in linked list.\n", count);
105+
106+
while (t->next != NULL) {
107+
printf("%d\n", t->data);
108+
t = t->next;
109+
}
110+
printf("%d\n", t->data); // Print last node
111+
}
112+
113+
void delete_from_begin() {
114+
struct node *t;
115+
int n;
116+
117+
if (start == NULL) {
118+
printf("Linked list is empty.\n");
119+
return;
120+
}
121+
122+
n = start->data;
123+
t = start->next;
124+
free(start);
125+
start = t;
126+
count--;
127+
128+
printf("%d deleted from the beginning successfully.\n", n);
129+
}
130+
131+
void delete_from_end() {
132+
struct node *t, *u;
133+
int n;
134+
135+
if (start == NULL) {
136+
printf("Linked list is empty.\n");
137+
return;
138+
}
139+
140+
count--;
141+
142+
if (start->next == NULL) {
143+
n = start->data;
144+
free(start);
145+
start = NULL;
146+
printf("%d deleted from end successfully.\n", n);
147+
return;
148+
}
149+
150+
t = start;
151+
152+
while (t->next != NULL) {
153+
u = t;
154+
t = t->next;
155+
}
156+
157+
n = t->data;
158+
u->next = NULL;
159+
free(t);
160+
161+
printf("%d deleted from end successfully.\n", n);
162+
}

0 commit comments

Comments
 (0)