this is a code implementing stack(PusH, PoP & Display function)..
Software Used : Turbo C++
Code:
#include
#include
int push(int *, int, int); //PusH fxn-adds item in stack
int pop(int *,int); //PoP fxn - deletes item from stack
void display(int *, int); //Display fxn -displays stack
int menu(); //Menu fxn-ask wat user wanna do
int again(); //Again fxn-ask to continue..
int top,maxstack; //global variable
void main()
{
int stack[6],item,i,j,choice,askmore;
maxstack=5; //stack size defined
top=-1;
clrscr();
printf("hello\n");
while(choice!=3)
{ clrscr();
printf(" top =%d elements = %d\n",top,top+1);
display(stack,top);
printf("\n\n\n ");
choice=menu(); //menu fxn is called for asking user preference
switch(choice) //switch-to according to user's choice
{
case 1: top=push(stack,top,maxstack); //calls PusH & value of ToP is returned
printf(" top =%d",top);
askmore=again("enter");
if(askmore==1) //asks for more PusH operation cont. if yes else it breaks
{ top=push(stack,top,maxstack);
printf(" top =%d",top);
}
break;
case 2: top=pop(stack,top); //calls PoP fxn & returns ToP value
printf(" top =%d",top);
askmore=again("delete"); //ask for more PoPing..so continues if user says YES
if(askmore==1)
{ top=pop(stack,top);
printf(" top =%d",top);
}
break;
case 3: display(stack,top); //called for Display only
break;
case 4: exit(0); //user can exit on hitting 4
}
getch(); // to stop at scareen after each cycle of while looP
}
getch();
}
int push(int *stack,int top,int maxstack) //body of Push fxn
{
int item; //item to be added in stack is initialised
printf("\n Enter item in stack\n ");
scanf("%d",&item); //Item entered by user
if(top==maxstack) //Overflow condition is checked
{printf("overflow");
}
else //on nonoccurence of overflow..push operation is performed
{top=top+1; //by incrementing ..TOP is set to Zer0
stack[top]=item; //Item inserted in stack
}
printf(" %d is at %d",stack[top],top); //position of item in stack is displayed
return(top); //returns 'TOP' value
}
int pop(int *stack,int top) //PoP fxn Body
{ int item; //Item to be deleted is defined
printf("\n Deleting item"); //just to show control
if(top==-1) //checks occurence of UNDERFLOW condition
{printf("Underflow");
}
else //On nonoccurence of UNDERFLOW ..item at TOP of stack is deleted
{top=top-1; //By decrementing ,TOP is made to point the Item present next to the
ITEM to be deleted.
item=stack[top]; //Item is placed at the top of stack
}
printf("\n Item deleted = %d",stack[top+1]); //shows Deleted ITEM and its position in stack
assuring that deleted IteM was the TOPmost one
printf("\n Item at the new top = %d",item); //displays new item at the ToP
return(top); //returns TOP
}
void display(int *stack,int top)
{ int i;
printf("\n stack is as :");
for(i=0;i<=top;i++) //Displays items present in stack upto TOP of stack from bottom
{
printf("%d ",stack[i]);
}
}
int menu() //menu fxn body is created
{ int choice;
printf("\n 1.PUSH 2.POP 3.Display 4.Exit \n\n"); //Menu created...and displayrd
scanf("%d",&choice);
return(choice);
}
int again(string) // asking for continue ..if wanna do-click 'YES'...else click 'NO'
{ int askmore;
printf("\n\n\nWant to %s more...press accordingly...",string);
printf("\n 1.yes 2. no \n\n ");
scanf("%d",&askmore);
return(askmore); //return..the reply from user in form of integer
-- 1. for 'YeS' & 2. for 'NO'
}
...StacK ...
hmm.. now this seems to be taking the blog thing a bit too far.
ReplyDeleteI mean it's your blog and you can do whatever you want to, but it shouldn't mean you start using it as a programming forum, english class, social networking site, email client and a kitchen sink!
Why..are you saying so...???
ReplyDeleteIs this not possible...or Is it wrong...???
well actually I didn't even see the program, since I am more used getting these programs as an attachment to a mail!
ReplyDeletewell I'fin a way to attach things here..!!!
ReplyDeleteso that can simply download that..!!! from here..
and check them..!!!
But still I don't find discussing programs too useful here. I mean u are just trying out problems. There are a lot of other great places online where you can post this and get help.
ReplyDeletePlaces like..???
ReplyDeletetell me I'l try that too..:-)
Umm.. places like programming forums and groups. Sure, some of them would be available on FB (they were on orkut). Or you can try something like stackoverflow.com , coderanch.com (even though I haven't tried them, I have heard they are good places to ask for help, but only at the appropriate place).
ReplyDeleteokiiii I'll try..!!!
ReplyDeletethanks buddy :-)