C Program for implementing two Stacks on Single Array

Saturday, June 9th, 2012

C Program for implementing two Stacks on Single Array.
Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT

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
#define SIZE 10            /* Size of Stack */
int s[SIZE],top[3]={0,-1,SIZE};
/* Global declarations */
push(int elem,int stno)
{
    int pos;	     /* Function for PUSH operation */
    if( Sfull()) printf("\n\n Overflow!!!!\n\n");
    else
    {
        if(stno==1) pos= ++top[stno];
        else pos=--top[stno];
        s[pos]=elem;
    }
}
 
int pop(int stno)
{                      /* Function for POP operation */
    int elem,pos;
    if(Sempty(stno)){ printf("\n\nUnderflow!!!!\n\n");
    return(-1); }
    else
    {
        pos=top[stno];
        elem=s[pos];
        if(stno == 1)top[stno]--;
        else
            top[stno]++;
        return(elem);
    }
}
 
int Sfull()
{                     /* Function to Check Stack Full */
    if(top[1] == top[2]-1) return 1;
    return 0;
}
 
int Sempty(stno)
{
    /* Function to Check Stack Empty */
    switch(stno)
    {
    case 1: if(top[1] == -1) return 1; else return 0;
    case 2: if(top[2] == SIZE) return 1;else return 0;
    }
}
 
display(int stno)
{                  /* Function to display status of Stack */
    int i;
    if(Sempty(stno)) printf(" \n Empty Stack\n");
    else
    {
        if(stno == 1)
        {
            for(i=0;i<=top[stno];i++)
                printf("%d\n",s[i]);
            printf("^Top");
        }
        else
        {
            for(i=SIZE-1;i>=top[stno];i--)
                printf("%d\n",s[i]);
            printf("^Top");
        }
    }
}
 
main()
{                         /* Main Program */
    int opn,elem,stno;
    do
    {
        clrscr();
        printf("\n ### Stack Operations ### \n\n");
        printf("\n Stack Number (1,2): ");
        scanf("%d",&stno);
        printf("\n Press 1-Push, 2-Pop,3-Display,4-Exit\n");
        printf("\n Your option ? ");
        scanf("%d",&opn);
 
        switch(opn)
        {
        case 1: printf("\n\nRead the element to be pushed ?");
            scanf("%d",&elem);
            push(elem,stno); break;
        case 2: elem=pop(stno);
            if( elem != -1)
                printf("\n\nPopped Element is %d \n",elem);
            break;
        case 3: printf("\n\nStatus of Stack %d \n\n",stno);
            display(stno); break;
        case 4: printf("\n\n Terminating \n\n"); break;
        default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
            break;
        }
        printf("\n\n\n\n  Press a Key to Continue . . . ");
        getch();
    }while(opn != 4);
}

Author Name :
Ansten Lobo

Total : 2 Comments


2 Responses to “C Program for implementing two Stacks on Single Array”

  1. Code Nirvana says:

    Awesome Program!
    Man more such programs at : http://www.codenirvana.in/

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: