Navigation

Saturday, 28 February 2015

Push, Pop and Traverse in Stack in C programming Language

In computer science, a stack is a last in, first out (LIFO) abstract data type and linear data structure.

Here its described to perform Push, pop,traverse an item in stack#include<conio.h>#include<stdlib.h>void push();int pop();void traverse();int stack[10];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("\n deleted element is %d",pop());      break;     case 3: traverse();       break;     default: printf("you entered wrong choice");     }     printf("\n do you wish to continue(y/n)");     fflush(stdin);     scanf("%c",&ch);     }     while(ch=='y');   }     void push()     {      int item;      if(top==10-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("\n%d",stack[i]);  }     }  }#include<stdio.h>

No comments:

Post a Comment