User is asked to PUSH, POP , or TRAVERSE.. PUSH is to add items to the Stack, POP is to remove item from the Stack, and TRAVERSE is to traverse the whole Stack
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- #define MAXSIZE 10
- void push();
- int pop();
- void traverse();
- int stack[MAXSIZE];
- int Top=-1;
- void main()
- {
- int choice;
- char ch;
- do
- {
- clrscr();
- printf("\n1. PUSH ");
- printf("\n2. POP ");
- printf("\n3. TRAVERSE ");
- printf("\nEnter your choice ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: push();
- break;
- case 2: printf("\nThe deleted element is %d ",pop());
- break;
- case 3: traverse();
- break;
- default: printf("\nYou Entered Wrong Choice");
- }
- printf("\nDo You Wish To Continue (Y/N)");
- fflush(stdin);
- scanf("%c",&ch);
- }
- while(ch=='Y' || ch=='y');
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void push()
- {
- int item;
- if(Top == MAXSIZE - 1)
- {
- printf("\nThe Stack Is Full");
- getch();
- exit(0);
- }
- else
- {
- printf("Enter the element to be inserted ");
- scanf("%d",&item);
- Top= Top+1;
- stack[Top] = item;
- }
- }
- int pop()
- {
- int item;
- if(Top == -1)
- {
- printf("The stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- item = stack[Top];
- Top = Top-1;
- }
- return(item);
- }
- void traverse()
- {
- int i;
- if(Top == -1)
- {
- printf("The Stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- for(i=Top;i>=0;i--)
- {
- printf("Traverse the element ");
- printf("%d\n",stack[i]);
- }
- }
- }
7 comments:
Write a program to implement a stack with 2d array
gd
how to copy it in c++
how to copy it in c++
gurls r alwaz gurls
this wil work on c++ compiler bt is ths realy c++ oop
frm my point of view ths iz jst c pgm
there is no class use in this program . how some one can say this a c++ program ?
hey can u please tell me what the scanf does and why do we use d% and can't we simple use cin or gets ??
Post a Comment