hi friends,
i have a problem in implementing a stack data structure.
this is a simple array + struc implementation
i would be greatful if anyone could give me guidance
actually if i am push some elements into the stack
then displaying it ,
my delete function does not work, because the subscript is decremented in display
fun
to 0. so the stack throws up a message stack is empty.
if i am not going to my display function after pushing in elements to the stack
then the prog work okay (becuse top subscript is not decremented to zero)
i would be greatful if anyone could point out my mistakes
both
for logical
and efficiency
thanks a lot
for the help
Code:include<stdio.h> #define stacksize 15 void delete(); void push(); /* function prototype */ void display(); struct stack /* creating a stack */ { int top; /* subscript for pointing to top of stack */ int item[stacksize]; /* array to hold the data value */ int n; /* no of item to be pushed into stack */ }; struct stack s; int x; void push() { printf(" Enter no of elements to be pushed\n "); scanf("%d",&s.n); do { printf("enter the data value to be inserted \n"); if (s.top >= stacksize) { printf(" the stack is full "); } else { s.top=s.top+1; s.item[s.top] = x ; } s.n--; }while (s.n > 0); } void delete() { if (s.top == 0) /* check for empty condition */ { printf(" the stack is empty \n"); } else { x = s.item[s.top]; s.top--; } } void display() /* function to display the elements of the stack */ { printf(" the elements of the stack are \n"); while(s.top > 0) /* until not equal to zero */ { printf("%d\n ",s.item[s.top]); s.top--; } } int main() { char c; int ch; do { printf("ENTER U'R CHOIC OF OPERATION \n"); printf("1.PUSH 2.DISPLAY 3.DELETE \n "); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2: display(); break; case 3: delete(); break; } printf("Type 'y' to continue \n"); scanf("%s",&c); } while (c=='y'); return 0; }



LinkBack URL
About LinkBacks


