Skip to content

Commit da0415c

Browse files
Merge pull request matthewsamuel95#603 from rishichawla/master
Added Linked List and Stack using Array
2 parents f1db52f + d5d7204 commit da0415c

File tree

3 files changed

+414
-0
lines changed

3 files changed

+414
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
struct Node *prev;
9+
};
10+
11+
struct Node *create_ll(int data)
12+
{
13+
struct Node *head = malloc(sizeof(struct Node));
14+
head->data = data;
15+
head->next = NULL;
16+
head->prev = NULL;
17+
}
18+
19+
void insertNode_beg(struct Node *head, int data)
20+
{
21+
struct Node *n = malloc(sizeof(struct Node));
22+
n->data = head->data;
23+
n->next = head->next;
24+
n->prev = head;
25+
head->data = data;
26+
head->next = n;
27+
}
28+
29+
void insertNode_bet(struct Node *head, int e, int data)
30+
{
31+
struct Node *ptr = head, *n = malloc(sizeof(struct Node));
32+
if (ptr != NULL && ptr->data == e)
33+
{
34+
insertNode_beg(head, data);
35+
return;
36+
}
37+
while (ptr != NULL && ptr->data != e)
38+
{
39+
ptr = ptr->next;
40+
}
41+
if (ptr == NULL)
42+
{
43+
printf("Element not found");
44+
return;
45+
}
46+
n->data = data;
47+
n->next = ptr;
48+
n->prev = ptr->prev;
49+
(ptr->prev)->next = n;
50+
ptr->prev = n;
51+
}
52+
53+
void insertNode_end(struct Node *head, int data)
54+
{
55+
struct Node *n = malloc(sizeof(struct Node));
56+
struct Node *ptr = head;
57+
while (ptr->next != NULL)
58+
{
59+
ptr = ptr->next;
60+
}
61+
ptr->next = n;
62+
n->prev = ptr;
63+
n->data = data;
64+
n->next = NULL;
65+
}
66+
67+
void deleteNode(struct Node *head, int e)
68+
{
69+
struct Node *ptr = head;
70+
if (ptr != NULL && ptr->data == e)
71+
{
72+
ptr = ptr->next;
73+
head->next = ptr->next;
74+
head->data = ptr->data;
75+
free(ptr);
76+
printf("Element deleted.\n");
77+
return;
78+
}
79+
while (ptr != NULL && ptr->data != e)
80+
{
81+
ptr = ptr->next;
82+
}
83+
if (ptr == NULL)
84+
{
85+
printf("Element not found");
86+
return;
87+
}
88+
(ptr->prev)->next = ptr->next;
89+
(ptr->next)->prev = ptr->prev;
90+
free(ptr);
91+
printf("Element deleted.\n");
92+
}
93+
94+
void printList(struct Node *head)
95+
{
96+
struct Node *ptr = head;
97+
printf("Elements entered are: \n");
98+
while (ptr != NULL)
99+
{
100+
printf("%d\t", ptr->data);
101+
ptr = ptr->next;
102+
}
103+
printf("\n");
104+
}
105+
106+
int main()
107+
{
108+
int n = 0, i = 0;
109+
printf("Enter element to create list: ");
110+
scanf("%d", &n);
111+
struct Node *head = create_ll(n);
112+
printf("Press 1 to enter element at the beginning.\nPress 2 to enter element before given element in the list.\nPress 3 to enter element at the end.\nPress 4 to delete element.\nPress 5 to print List\n");
113+
do
114+
{
115+
int j = 0;
116+
scanf("%d", &j);
117+
switch (j)
118+
{
119+
case 1:
120+
printf("Enter element to be inserted at the beginning: ");
121+
scanf("%d", &n);
122+
insertNode_beg(head, n);
123+
break;
124+
case 2:
125+
printf("Enter element in the list: ");
126+
int e = 0;
127+
scanf("%d", &e);
128+
printf("Enter element to be inserted: ");
129+
scanf("%d", &n);
130+
insertNode_bet(head, e, n);
131+
break;
132+
case 3:
133+
printf("Enter element to be inserted at the end: ");
134+
scanf("%d", &n);
135+
insertNode_end(head, n);
136+
break;
137+
case 4:
138+
printf("Enter element to be deleted: ");
139+
scanf("%d", &n);
140+
deleteNode(head, n);
141+
break;
142+
case 5:
143+
printList(head);
144+
break;
145+
default:
146+
printf("Invalid input\n");
147+
}
148+
printf("Press 1 to continue\n");
149+
scanf("%d", &i);
150+
} while (i == 1);
151+
return 0;
152+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
};
9+
10+
struct Node *create_ll(int data)
11+
{
12+
struct Node *head = malloc(sizeof(struct Node));
13+
head->data = data;
14+
head->next = NULL;
15+
}
16+
17+
void insertNode_beg(struct Node *head, int data)
18+
{
19+
struct Node *n = malloc(sizeof(struct Node));
20+
n->data = head->data;
21+
n->next = head->next;
22+
head->data = data;
23+
head->next = n;
24+
}
25+
26+
void insertNode_bet(struct Node *head, int e, int data)
27+
{
28+
struct Node *ptr = head, *n = malloc(sizeof(struct Node)), *p;
29+
if (ptr != NULL && ptr->data == e)
30+
{
31+
insertNode_beg(head, data);
32+
return;
33+
}
34+
while (ptr != NULL && ptr->data != e)
35+
{
36+
p = ptr;
37+
ptr = ptr->next;
38+
}
39+
if (ptr == NULL)
40+
{
41+
printf("Element not found");
42+
return;
43+
}
44+
n->data = data;
45+
n->next = ptr;
46+
p->next = n;
47+
}
48+
49+
void insertNode_end(struct Node *head, int data)
50+
{
51+
struct Node *n = malloc(sizeof(struct Node));
52+
struct Node *ptr = head;
53+
while (ptr->next != NULL)
54+
{
55+
ptr = ptr->next;
56+
}
57+
ptr->next = n;
58+
n->data = data;
59+
n->next = NULL;
60+
}
61+
62+
void deleteNode(struct Node *head, int e)
63+
{
64+
struct Node *ptr = head, *p;
65+
if (ptr != NULL && ptr->data == e)
66+
{
67+
ptr = ptr->next;
68+
head->next = ptr->next;
69+
head->data = ptr->data;
70+
free(ptr);
71+
printf("Element deleted.\n");
72+
return;
73+
}
74+
while (ptr != NULL && ptr->data != e)
75+
{
76+
p = ptr;
77+
ptr = ptr->next;
78+
}
79+
if (ptr == NULL)
80+
{
81+
printf("Element not found");
82+
return;
83+
}
84+
p->next = ptr->next;
85+
free(ptr);
86+
printf("Element deleted.\n");
87+
}
88+
89+
void printList(struct Node *head)
90+
{
91+
struct Node *ptr = head;
92+
printf("Elements entered are: \n");
93+
while (ptr != NULL)
94+
{
95+
printf("%d\t", ptr->data);
96+
ptr = ptr->next;
97+
}
98+
printf("\n");
99+
}
100+
101+
int main()
102+
{
103+
int n = 0, i = 0;
104+
printf("Enter element to create list: ");
105+
scanf("%d", &n);
106+
struct Node *head = create_ll(n);
107+
printf("Press 1 to enter element at the beginning.\nPress 2 to enter element before given element in the list.\nPress 3 to enter element at the end.\nPress 4 to delete element.\nPress 5 to print List\n");
108+
do
109+
{
110+
int j = 0;
111+
scanf("%d", &j);
112+
switch (j)
113+
{
114+
case 1:
115+
printf("Enter element to be inserted at the beginning: ");
116+
scanf("%d", &n);
117+
insertNode_beg(head, n);
118+
break;
119+
case 2:
120+
printf("Enter element in the list: ");
121+
int e = 0;
122+
scanf("%d", &e);
123+
printf("Enter element to be inserted: ");
124+
scanf("%d", &n);
125+
insertNode_bet(head, e, n);
126+
break;
127+
case 3:
128+
printf("Enter element to be inserted at the end: ");
129+
scanf("%d", &n);
130+
insertNode_end(head, n);
131+
break;
132+
case 4:
133+
printf("Enter element to be deleted: ");
134+
scanf("%d", &n);
135+
deleteNode(head, n);
136+
break;
137+
case 5:
138+
printList(head);
139+
break;
140+
default:
141+
printf("Invalid input\n");
142+
}
143+
printf("Press 1 to continue\n");
144+
scanf("%d", &i);
145+
} while (i == 1);
146+
return 0;
147+
}

0 commit comments

Comments
 (0)