-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRev_list_using_stack.c
66 lines (60 loc) · 1.02 KB
/
Rev_list_using_stack.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
//WAP to reverse a list using stack
#include<stdio.h>
#include<conio.h>
void push(int*,int);
int pop(int*);
display(int*,int);
#define MAX 20
int stack[20],list[20],revlist[20];
int TOP=-1;
void main()
{
int i,n;
printf("Enter the number of elements in the list:\t");
scanf("%d",&n);
printf("\nEnter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&list[i]);
push(stack,list[i]);
}
for(i=0;i<n;i++)
{
revlist[i]=pop(stack);
}
printf("\nEntered list:\t");
display(list,n);
printf("\n\nReversed list:\t");
display(revlist,n);
}
//------------------------------------------------------------------
void push(int stk[20],int p)
{
if(TOP==MAX-1)
printf("\nSTACK IS FULL!(OVERFLOW)");
else
{
TOP=TOP+1;
stk[TOP]=p;
}
}
int pop(int stk[20])
{
int temp;
if(TOP==-1)
printf("\nSTACK IS EMPTY!(UNDERFLOW)");
else
{
temp=stk[TOP];
TOP=TOP-1;
return temp;
}
}
display(int stk[20],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",stk[i]);
}
}