WELCOME TO MY BLOG BY SHABEEB.K

C,C++ PROGRAMMING: STACK

Search This Blog

Saturday, 12 October 2013

STACK


 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
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

int i,ch,top=0,stack[10];        //varibles needed for stack operation.

void menu();         //function declaration.
void push();
void pop();
void show();

void main()
{

menu();

}




void menu()
{

cout<<"\n\n\n1:PUSH\n2:POP\n3:SHOW\n";  //user choices to select for their preference PUSH, POP OR SHOW  element IN stack.
cin>>ch;

switch(ch)
{
case 1 : push(); main();   //if user select choice 1 functions push(); executed and come back to main();
break;

case 2 : pop(); main();     //if user select choice 2 functions pop(); executed and come back to main();   
break;

case 3 : show(); main();    //if user select choice 1 functions push(); executed and come back to main();

 break;
}
}

void push()         //push  is a function to insert element to stack.
{
clrscr();
if(top<=9)
{

cout<<"ENTER ELEMENT TO ADD\n"; 
cin>>stack[top];
top++;                             //increment top by  1
}
else
{
cout<<"STACK IS FULL\n";
}
}

void pop()         //function to delete a element from stack.
{
if(top<=0)
{
cout<<"STACK IS EMPTY\n";
}
else
{
--top;
cout<<"\n"<<stack[top]<<" IS DELETED FROM STACK\n";
}
}

void show()              //function to show elements of stack.
{
cout<<"\n\n\n";
if(top>0)
{

for(i=0;i<top;i++)
{
cout<<stack[i]<<",";
}
}
else                                            //if no elements in stack it will display 'STACK IS EMPTY'.
{
cout<<"STACK IS EMPTY\n";
}
}

No comments:

Post a Comment