Thread: stack implementation problem-help needed

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    stack implementation problem-help needed

    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;
    }

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    If you don't learn to debug your own code, you never will. That's not being mean, it's just true. If you can't, or don't want to do it yourself, then I would say development is not for you-- certainly not as a profession.

    Go back, flowchart your program (it's very easy to do), and I can almost guarantee that you will see the logic error and a way to correct it within minutes of completing your flowchart.

    Just walk through it. The greatest benefit a flowchart offers is allowing you to get the program out of your head and onto paper. It frees the mind and allows it to concentrate on things other than just holding on to information.

    good luck.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help for stack problem again
    By bahareh in forum C++ Programming
    Replies: 20
    Last Post: 11-24-2006, 10:01 AM
  2. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  3. Stack problem
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2003, 04:30 PM
  4. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM