-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB_08_CODE.C
139 lines (129 loc) · 2.97 KB
/
LAB_08_CODE.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Stack & Queue implementation using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void push() {
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL) {
printf("Stack overflow\n");
}
else {
printf("Enter value to be inserted: ");
scanf("%d", &item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("Node inserted\n");
}
}
void pop() {
struct node *ptr;
if(head == NULL) {
printf("Stack empty\n");
}
else {
ptr = head;
head = ptr ->next;
free(ptr);
printf("Node deleted\n");
}
}
void stackDisplay() {
struct node *ptr;
ptr = head;
if(head == NULL) {
printf("Stack empty\n");
}
else {
printf("Stack: \n");
while(ptr != NULL) {
printf("%d \n", ptr->data);
ptr = ptr->next;
}
}
}
void enqueue() {
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL) {
printf("Queue Full\n");
}
else {
printf("Enter value to be inserted: ");
scanf("%d", &item);
ptr->data = item;
if(head == NULL) {
ptr->next = NULL;
head = ptr;
printf("Node inserted\n");
}
else {
temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("Node inserted\n");
}
}
}
void dequeue() {
struct node *ptr;
if(head == NULL) {
printf("Stack empty\n");
}
else {
ptr = head;
head = ptr ->next;
free(ptr);
printf("Node deleted\n");
}
}
void queueDisplay() {
struct node *ptr;
ptr = head;
if(head == NULL) {
printf("Queue empty");
}
else {
printf("Queue: \n");
while(ptr != NULL) {
printf("%d \n", ptr->data);
ptr = ptr->next;
}
}
}
void main() {
int choice;
while(1) {
printf("Stack Operations: 1.Push 2.Pop 3.Display 4.Exit\n");
printf("Queue Operations: 5.Enqueue 6.Dequeue 7.Display 4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1: push();
break;
case 2: pop();
break;
case 3: stackDisplay();
break;
case 5: enqueue();
break;
case 6: dequeue();
break;
case 7: queueDisplay();
break;
case 4: exit(1);
break;
default: printf("Invalid Choice");
}
}
}