A directory of Objective Type Questions covering all the Computer Science subjects. Here you can access and discuss Multiple choice questions and answers for various compitative exams and interviews.

C program to implement Stack using Linked List


Problem Description


This C Program implement a stack using linked list. Stack is a Last in, First out (LIFO) abstract data type and linear data structure. Linked list is a data structure consisting of a group of nodes which together represent a sequence.


C program to implement Stack using Linked List - Source code
     
                
  /*
 * C Program to Implement a Stack using Linked List
 */
#include <stdio.h>
#include <stdlib.h>

struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;

int top_element();
void push(int data);
void pop();
void display_stack();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
    int no, c, e;

    printf("\n 1 - Push operation");
    printf("\n 2 - Pop operation");
    printf("\n 3 - Top of Stack");
    printf("\n 4 - Exit");
    printf("\n 5 - Display");
    printf("\n 6 - Stack Count");
    printf("\n 7 - Destroy stack");

    create();

    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &c);

        switch (c)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            push(no);
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)
                printf("No elements in stack");
            else
            {
                e = top_element();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            exit(0);
        case 5:
            display_stack();
            break;
        case 6:
            stack_count();
            break;
        case 7:
            destroy();
            break;
        default :
            printf(" Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}

/* Create empty stack */
void create()
{
    top = NULL;
}

/* Count stack elements */
void stack_count()
{
    printf("\n No. of elements in stack : %d", count);
}

/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }
    count++;
}

/* Display stack elements */
void display_stack()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }

    while (top1 != NULL)
    {
        printf("%d ", top1->info);
        top1 = top1->ptr;
    }
 }

/* Pop Operation on stack */
void pop()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("\n Stack is empty.");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}

/* Return top element */
int top_element()
{
    return(top->info);
}


/* Destroy entire stack */
void destroy()
{
    top1 = top;

    while (top1 != NULL)
    {
        top1 = top->ptr;
        free(top);
        top = top1;
        top1 = top1->ptr;
    }
    free(top1);
    top = NULL;

    printf("\n All stack elements destroyed");
    count = 0;
}

     
      

Program Output


 1 - Push operation
 2 - Pop operation
 3 - Top of Stack
 4 - Exit
 5 - Display
 6 - Stack Count
 7 - Destroy stack
 Enter choice : 1
Enter data : 10

 Enter choice : 1
Enter data : 20

 Enter choice : 1
Enter data : 30

 Enter choice : 1
Enter data : 40

 Enter choice : 1
Enter data : 50

 Enter choice : 2

 Popped value : 50
 Enter choice : 3

 Top element : 40
 Enter choice : 5
40 30 20 10
 Enter choice : 6

 No. of elements in stack : 4
 Enter choice : 7

 All stack elements destroyed
 Enter choice :

Program Explanation


1. This C program consist of seven functions beside the main function. These functions are for push and pop operations, to get top element of stack, to display the stack, to count the number of elements in stack, to destroy the stack and to exit from the program.

2. The selection to perform specific function is done through the use of switch statement.

3. Function create simply creates a empty node with top = null.

4. The push function first allocate the memory for a node and assign the number to the info field of list. The pointers are updated using a temp node.