0% found this document useful (0 votes)
72 views11 pages

Experiment No.: 8: Implement A Stack Using A Single Linked List

The document describes how to implement a stack using a single linked list. It explains that a stack follows LIFO principles and those can be achieved using linked list nodes and operations. It provides pseudocode for push(), pop() and display() functions to add and remove nodes from the top of the stack and display the stack. An example C program implementation of these functions is also given along with sample output. The conclusion states that a stack has been successfully implemented using a single linked list.

Uploaded by

Harsh Alashi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
72 views11 pages

Experiment No.: 8: Implement A Stack Using A Single Linked List

The document describes how to implement a stack using a single linked list. It explains that a stack follows LIFO principles and those can be achieved using linked list nodes and operations. It provides pseudocode for push(), pop() and display() functions to add and remove nodes from the top of the stack and display the stack. An example C program implementation of these functions is also given along with sample output. The conclusion states that a stack has been successfully implemented using a single linked list.

Uploaded by

Harsh Alashi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

EXPERIMENT NO.

: 8
IMPLEMENT A STACK USING A SINGLE
LINKED LIST

NAME: HARSH KISHOR ALASHI

CLASS: SE COMPS

DIVISION: A

BATCH: A1

ROLL NO.: CEA304


AIM: Implement a stack using a single linked list

THEORY:

All the single linked list operations perform based on Stack operations LIFO(last in first out)
and with the help of that knowledge we are going to implement a stack using a single linked
list. Using single linked lists so how to implement a linked list here means that we are storing
the information in the form of nodes and we need to follow the stack rules and we need to
implement using single linked list nodes.

The implementation of a stack is a simple rule that is last in first out and all the operations we
should perform with the help of a top variable only with the help of top variable.

Create a node first and allocate memory to it.

If the list is empty then the item is to be pushed as the start node of the list. This includes
assigning value to the data part of the node and assign null to the address part of the node.

If there are some nodes in the list already, then we have to add the new element in the
beginning of the list (to not violate the property of the stack). For this purpose, assign the
address of the starting element to the address field of the new node and make the new node,
the starting node of the list.
Stack Operations using Linked List

To implement a stack using a linked list, we need to set the following things before
implementing actual operations.

Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.

Step 2 - Define a 'Node' structure with two members data and next.

Step 3 - Define a Node pointer 'top' and set it to NULL.

Step 4 - Implement the main method by displaying a Menu with a list of operations and make
suitable function calls in the main method.

push(value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack...

Step 1 - Create a newNode with a given value.

Step 2 - Check whether stack is Empty (top == NULL)

Step 3 - If it is Empty, then set newNode → next = NULL.

Step 4 - If it is Not Empty, then set newNode → next = top.

Step 5 - Finally, set top = newNode.


pop() - Deleting an Element from a Stack

We can use the following steps to delete a node from the stack...

Step 1 - Check whether the stack is Empty (top == NULL).

Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate
the function

Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.

Step 4 - Then set 'top = top → next'.

Step 5 - Finally, delete 'temp'. (free(temp)).

display() - Displaying stack of elements

We can use the following steps to display the elements (nodes) of a stack...

Step 1 - Check whether the stack is Empty (top == NULL).

Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.

Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.

Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until temp
reaches the first node in the stack. (temp → next != NULL).

Step 5 - Finally! Display 'temp → data ---> NULL'.

Program:

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node
{

int val;

struct node *next;

};

struct node *head;

void main ()

int choice=0;

printf("\n*********Stack operations using linked list*********\n");

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("\n\nChose one from the below options...\n");

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

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

}
case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

{
int val;

struct node *ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

printf("not able to push the element");

else

printf("Enter the value");

scanf("%d",&val);

if(head==NULL)

ptr->val = val;

ptr -> next = NULL;

head=ptr;

else

ptr->val = val;

ptr->next = head;

head=ptr;

printf("Item pushed");
}

void pop()

int item;

struct node *ptr;

if (head == NULL)

printf("Underflow");

else

item = head->val;

ptr = head;

head = head->next;

free(ptr);

printf("Item popped");

void display()

int i;
struct node *ptr;

ptr=head;

if(ptr == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements \n");

while(ptr!=NULL)

printf("%d\n",ptr->val);

ptr = ptr->next;

OUTPUT:
CONCLUSION:

Thus, we have successfully implemented Stack using Singly Linked List.

You might also like