Friday, October 13, 2017

Program to Implement Stack Using Array in C

/*==========================================================================================
**
**  File Name     : stack_array.c
**  Creation Date : Fri 13 Oct 2017 10:20:56 PM IST
**  Last Modified : Fri 13 Oct 2017 10:50:21 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**=========================================================================================*/

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int stack_array[MAX];
int top = -1;
void push(int item);
int pop();
int peek();
int isEmpty();
int isFull();
void display();

int main()
{
    printf("\n= = = = = = = = = = Stack Using Array in C = = = = = = = = = =\n");
    int choice,item;
    while(1)
    {
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Display all stack elements\n");
        printf("4. Display the top element\n");
        printf("5. Quit\n");
        printf("Enter your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1 :
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                printf("Enter the item to be pushed : ");
                scanf("%d",&item);
                push(item);
                printf("The item %d pushed into the stack\n",item);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 2:
                item = pop();
                printf("Item %d has been Popped out\n",item);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 3:
                display();
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 4:
                printf("Item at the top is : %d\n", peek() );
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 5:
                printf("Program Terminated\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                return;
            default:
                printf("Oops.. Wrong choice! Select Again\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        }
    }
}

void push(int item)
{
    if( isFull() )
    {
        printf("Stack Overflow\n");
        return;
    }
    top = top+1;
    stack_array[top] = item;
}

int pop()
{
    int item;
    if(isEmpty())
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    item = stack_array[top];
    top = top-1;
    return item;
}

void display()
{
    int i;
    if(isEmpty())
    {
        printf("Stack is empty\n");
        return;
    }
        printf("Stack elements are:\n\n");
    for(i=top; i>=0; i--)
        printf("%d\t",stack_array[i]);
    printf("\n\n");
}

int peek()
{
    if( isEmpty() )
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    return stack_array[top];
}

int isEmpty()
{
    if(top == -1)
        return 1;
    else
        return 0;
}

int isFull()
{
    if(top == MAX-1)
        return 1;
    else
        return 0;
}


OutPut:




manoj@ubuntu:~/ds/stack$ gcc stack_array.c -o stack_array 
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
manoj@ubuntu:~/ds/stack$ ./stack_array

= = = = = = = = = = Stack Using Array in C = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 5
The item 5 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 5 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 3
Stack elements are:

3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 4
Item at the top is : 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all stack elements
4. Display the top element
5. Quit
Enter your choice: 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$

No comments:

Post a Comment