Menu

[R22] Data Structures Lab Manual [ Lab Programs ]


Aim:

Write a program that implement Stack (its operations) using Linked List (Pointer)

Solution :

C Program to implement Stack using Linked List(Pointer)

Stack using Linked List
#include<stdio.h>
#include<stdlib.h>

struct node{
    int data;
    struct node*next;
}*head=NULL;

struct node *create(int value)
{
    struct node *temp;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data=value;
    temp->next=NULL;
    return temp;
}

void push(int value)
{
    struct node *newnode;
    newnode=create(value);
    if(head==NULL)
    {
        head=newnode;
    }
    else
    {
        newnode->next=head;
        head=newnode;
    }
}

void pop()
{
    struct node *temp;
    if(head==NULL)
    {
        printf("Stack is underflow");
    }
    else
    {
        temp=head;
        head=head->next;
        free(temp);
    }
}

void show()
{
    struct node *temp;
    if(head==NULL)
    {
        printf("Stack is empty");
    }
    else
    {
        temp=head;
        while(temp!=NULL)
        {
            printf("%d\n",temp->data);
            temp=temp->next;
        }
    }
}

void main()
{
    int ch,pos,value;
    do
    {
        printf("\n1. Push\n2. Pop\n3. Show\n4. Exit");
        printf("\nEnter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1: printf("\nEnter the value: ");
                scanf("%d",&value);
                push(value);
                break;
        case 2: pop();
                break;
        case 3: show();
                break;
        case 4:break;
        default: printf("\nyour choice is wrong!.. ");
        }
    }while(ch!=4);
}		

OUTPUT
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 10
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 20
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 30
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
30
20
10

1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
20
10

1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
Stack is underflow
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
Stack is empty
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 4

Related Content :

Data Structures Lab Programs

1) Write a program that uses functions to perform the following operations on singly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

2) Write a program that uses functions to perform the following operations on doubly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

3) Write a program that uses functions to perform the following operations on circular linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

4) Write a program that implement Stack (its operations) using Array View Solution

5) Write a program that implement Stack (its operations) using Linked List (Pointer) View Solution

6) Write a program that implement Queue(its operations) using Array View Solution

7) Write a program that implement Queue (its operations) using Linked List (Pointer) View Solution

8) Write a program that implements Quick sort sorting methods to sort a given list of integers in ascending order View Solution

9) Write a program that implements Merge sort sorting methods to sort a given list of integers in ascending order View Solution

10) Write a program that implements Heap sort sorting methods to sort a given list of integers in ascending order View Solution

11) Write a program to implement the tree traversal methods using Recursive View Solution

12) Write a program to implement the tree traversal methods using Non Recursive View Solution

13) Write a program to implement Binary Search Tree (its operations) View Solution

14) Write a program to implement AVL Tree (its operations) View Solution

15) Write a program to implement Red - Black Tree (its operations) View Solution

16) Write a program to implement B Trees (its operations) View Solution

17) Write a program to implement B+ Trees (its operations) View Solution

18) Write a program to implement the graph traversal methods (Breadth First Search) View Solution

19) Write a program to implement the graph traversal methods (Depth First Search) View Solution

20) Write a program to Implement a Pattern matching algorithms using Boyer- Moore View Solution

21) Write a program to Implement a Pattern matching algorithms using Knuth-Morris-Pratt View Solution