-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlab3.c
106 lines (103 loc) · 1.87 KB
/
lab3.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define maxstk 10
int s[maxstk], top = 0, item, n;
void push();
void pop();
void display();
void pali();
int main()
{
int choice = 1;
while (choice)
{
printf("\n---STACK OPERATIONS---\n1.Push\n 2.Pop\n3.Palindrome\n4.Display\n5.Exit\n");
printf("\nEnter your choice:\t");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}
return 0;
}
//Inserting element into the stack
void push()
{
if (top == maxstk)
{
printf("\nStack Overflow:");
return;
}
else
{
printf("Enter the single digit element to be inserted into stack:\t");
scanf("%d", &item);
s[++top] = item;
}
}
//deleting an element from the stack
void pop()
{
if (top == 0)
{
printf("Stack Underflow:");
return;
}
else
{
item = s[top--];
printf("\nPoped element is : %d\n", item);
}
}
void pali()
{
int low = 1;
int high = top;
while (high > low)
{
if (s[low++] != s[high--])
{
printf("\n Stack is Not Palindrome");
return;
}
}
printf("\n Stack is palindrome");
}
//display the content of the stack
void display()
{
int i;
if (top == 0)
{
printf("\nStack is Empty:");
return;
}
else
{
printf("\nThe stack elements are:\n");
for (i = top; i >= 1; i--)
{
printf("%d\n", s[i]);
}
}
}