-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-stack-insertion.c
112 lines (104 loc) · 1.95 KB
/
sorted-stack-insertion.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
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(int);
int pop(void);
void display(void);
void sortinsert(int);
int main()
{
int x;
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.SORTED PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
sortinsert(x);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push(int x)
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
top++;
stack[top]=x;
}
}
int pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
top--;
return stack[top+1];
}
}
void sortinsert(int ele)
{
int x;
if(top<=-1 || stack[top]<ele)
{
push(ele);
return;
}
else
{
x=pop();
sortinsert(ele);
push(x);
}
}
void display()
{
int x;
if(top==-1)
return;
else
{
x=pop();
display();
printf("%d",x);
push(x);
}
}