[go: up one dir, main page]

0% found this document useful (0 votes)
43 views10 pages

Pavi Ds 1 B

This document outlines a C program to implement a stack using a linked list data structure. The program aims to teach programmers how to perform push, pop, and display operations on a stack. It provides an algorithm that uses a struct node with data and a pointer to implement these stack operations through functions like push(), pop(), and display(). The full C program code is then provided to demonstrate an interactive menu-driven implementation of the stack using linked lists.

Uploaded by

Kavi Sanjai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views10 pages

Pavi Ds 1 B

This document outlines a C program to implement a stack using a linked list data structure. The program aims to teach programmers how to perform push, pop, and display operations on a stack. It provides an algorithm that uses a struct node with data and a pointer to implement these stack operations through functions like push(), pop(), and display(). The full C program code is then provided to demonstrate an interactive menu-driven implementation of the stack using linked lists.

Uploaded by

Kavi Sanjai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EXP NO:

DATE:

AIM:
To write a c program to perform implementation of stack operations using linked list.

OBJECTIVE:
From this program, a programmer will learn how to PUSH, POP & DISPLAY operations are
implemented in a stack using linked list.

Develop an linked-based stack implementation to reinforce programming skills, emphasizing data


structure design, linked management, error handling, and testing proficiency.

ALGORITHM:
1. Create a struct node with integer data and a pointer to the next node.

2. Declare a global variable `top` to keep track of the linked list.

3. Implement the following functions:

a. push()

 Create a newNode with given value.


 Check whether stack is Empty (top == NULL)
 If it is Empty, then set newNode → next = NULL.
 If it is Not Empty, then set newNode → next = top.
 Finally, set top = newNode.

b.pop()

 Check whether stack is Empty (top == NULL).


 If it is Empty, then display "Underflow” and terminate the function
 If it is Not Empty, then define a Node pointer 'top 1’ and set it to 'top'.
 Then set 'top 1 = top 1 → next'.
 Finally, delete 'top'. (free(top)).

c.display()

 Check whether stack is Empty (top 1 == NULL).


 If it is Empty, then display 'Stack underflow!!!' and terminate the function.
 If it is Not Empty, then define a Node pointer 'top 1' and initialize with top.
 Display 'ptr → data --->' and move it to the next node. Repeat the same until top reaches to the
first node in the stack. (top 1 → next != NULL).
 Finally! Display 'top 1 =top 1-> next'.

4. Stop the program.

PROGRAM:
#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

}*top,*top1,*temp;

int count = 0;

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++;
printf("Node is Inserted\n\n");

int pop()

top1 = top;

if (top1 == NULL)

printf("\nStack Underflow\n");

return -1;

else

top1 = top1->ptr;

int popped = top->info;

free(top);

top = top1;

count--;

return popped;

void display()

top1 = top;

if (top1 == NULL)

printf("\nStack Underflow\n");

return;

printf("The stack is \n");

while (top1 != NULL)

printf("%d--->", top1->info);
top1 = top1->ptr;

printf("NULL\n\n");

int main()

int choice, value;

printf("\nImplementation of Stack using Linked List\n");

while (1)

printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("\nEnter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", &value);

push(value);

break;

case 2:

printf("Popped element is :%d\n", pop());

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:
printf("\nWrong Choice\n");

RESULT:
Thus, the c program for implementation of stack using linked list was executed and the output was
verified.
FLOW CHART:

You might also like