Thread: stack implementation using arrays

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    1

    Unhappy stack implementation using arrays

    Code:
    #include <stdio.h>#include <unistd.h>
    #include <stdlib.h>
    #define STACKSIZE 100
    
    
    struct stackk
    {
        int top;
        int items[STACKSIZE];
    };
    typedef struct stackk *s;
    
    
    void push(int n)
    {
        if(s.top==STACKSIZE-1)
        {
            printf("Stack overflow");
            exit(0);
        }
        s.items[s.top++]=n;
        return;
    }
    int pop()
    {
        int n;
        if(s.top==-1)
        {
            printf("Stack underflow");
            exit(0);
        }
        n=s.items[s.top--];
        return;
    
    
    }
    void display()
    {
        int i;
        for(int i=0;i<s.top+1;i++)
        printf("%d\n",s.items[i]);
    }
    int main()
    {
        int ch,n;
        while(1)
        {
            printf("1.Enter item\n2.Remove item\m3.Exit\n\nEnter your choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
            case 1:
                printf("Enter the dats\n");
                scanf("%d",&n);
                push(n);
                display();
                break;
            case 2:
                pop();
                display();
                break;
            case 3:
                exit(0);
            }
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you have a specific question, or are you looking for a general critique of your code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Aritmethic operations on Stack (Array implementation)
    By ilerleartik in forum C Programming
    Replies: 12
    Last Post: 03-11-2012, 06:24 AM
  2. Dual stack implementation using a single socket
    By aswathy in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-24-2011, 02:05 PM
  3. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  4. code review request: stack implementation
    By cs32 in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 02:26 PM
  5. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM

Tags for this Thread