top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Write a C program to implement stack using single linked list?

+3 votes
205 views
Write a C program to implement stack using single linked list?
posted Apr 2, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

0 votes

This C Program implement a stack using linked list. Stack is a type of queue that in practice is implemented as an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are “pushed onto” the stack. When the function returns,these locals and parameters are “popped.” Because of this, the size of a program’s stack fluctuates constantly as the program is running, but it has some maximum size. stack is as 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. Here we need to apply the application of linkedlist to perform basic operations of stack.
Here is source code of the C Program to implement a stack using linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * 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 topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

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

    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Dipslay");
    printf("\n 7 - Stack Count");
    printf("\n 8 - Destroy stack");

    create();

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

        switch (ch)
        {
        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 = topelement();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            stack_count();
            break;
        case 8:
            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()
{
    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 Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}

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

/* Check if stack is empty or not */
void empty()
{
    if (top == NULL)
        printf("\n Stack is empty");
    else
        printf("\n Stack is not empty with %d elements", count);
}

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

Output:

$ cc pgm2.c
$ a.out

1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56

Enter choice : 1
Enter data : 80

Enter choice : 2

Popped value : 80
Enter choice : 3

Top element : 56
Enter choice : 1
Enter data : 78

Enter choice : 1
Enter data : 90

Enter choice : 6
90 78 56
Enter choice : 7

No. of elements in stack : 3
Enter choice : 8

All stack elements destroyed
Enter choice : 4

Stack is empty
Enter choice : 5
answer Apr 2, 2016 by Shivaranjini
0 votes
#include<stdio.h>
#include<stdlib.h>
struct node{
    int item;
    struct node *nextnode;
};
struct node *START=NULL;
struct node *creatnode_for_stack()
{
    struct node *ptr;
    ptr=(struct node*)malloc(sizeof(struct node));
    return(ptr);
}
void InsertNode()
{
    struct node*temp,*t;
    temp=creatnode_for_stack();
    printf("put an item in stack(NUMBER):\t");
    scanf("%d",&temp->item);
    temp->nextnode=NULL;
    if(START==NULL)
    {
        START=temp;
    }
    else
    {
        t=START;
        while(t->nextnode!=NULL)
        {
            t=t->nextnode;
        }
        t->nextnode=temp;
    }
}
struct node *deletenode()
{
  struct node *temp =START;
  struct node *t;
  if(START->nextnode==NULL)
  {
    free(START);
    START=NULL;
  }
  else
  {
     while(temp->nextnode != NULL)
     {
        t=temp;
        temp=temp->nextnode;
     }
     free(t->nextnode);
     t->nextnode=NULL; 
  }    
  return START;
}
void viewlist()
{
    struct node *t;
    if(START==NULL)
    {
        printf("stack is empty\n");
    }
    else
    {
        t=START;
        while(t!=NULL)
        {
            printf("%d\t",t->item);
            t=t->nextnode;
        }
    }
}
int menu()
{
    int ch;
    printf("\n1: ADD value in stack");
    printf("\n2: DELETE value frpm top of stack");
    printf("\n3: VIEW list");
    printf("\n4: EXIT");
    printf("\nENTER your choice:\t");
    scanf("%d",&ch);
    return ch;
}
main()
{
    while(1)
    {
        switch(menu())
        {
            case 1:
                InsertNode();
                break;
            case 2:
                deletenode();
                break;
            case 3:
                viewlist();
                break;
            case 4:
                exit (0);
            default :
                printf("INVALID choice\n");     
        }
        getch();
    }
}
answer Apr 21, 2016 by Ajay Kumar
OUTPUT:

1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      1
put an item in stack(NUMBER):   11

1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      1
put an item in stack(NUMBER):   22

1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      1
put an item in stack(NUMBER):   33

1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      3
11      22      33
1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      2

1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:      3
11      22
1: ADD value in stack
2: DELETE value frpm top of stack
3: VIEW list
4: EXIT
ENTER your choice:4
0 votes
/*
 * 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 topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

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

    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Dipslay");
    printf("\n 7 - Stack Count");
    printf("\n 8 - Destroy stack");

    create();

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

        switch (ch)
        {
        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 = topelement();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            stack_count();
            break;
        case 8:
            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()
{
    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 Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}

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

/* Check if stack is empty or not */
void empty()
{
    if (top == NULL)
        printf("\n Stack is empty");
    else
        printf("\n Stack is not empty with %d elements", count);
}

/* 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;
}
answer Apr 22, 2016 by Rajan Paswan
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...