You are on page 1of 21

EXPERIMENT NO.

: 6
Implement Single Linked List Using Stack ADT

NAME: HARSH KISHOR ALASHI

CLASS: SE COMPS

DIVISION: A

BATCH: A1

ROLL NO.: CEA304


AIM: Implement Single Linked List Using Stack ADT

THEORY:

Definition Of Singly Linked List :


 Singly linked list can be defined as the collection of ordered set of elements. The
number of elements may vary according to need of the program.
 A node in the singly linked list consists of two parts: data part and link part.
 Data part of the node stores actual information that is to be represented by the node
while the link part of the node stores the address of its immediate successor.
 One way chain or singly linked list can be traversed only in one direction. In other
words, we can say that each node contains only next pointer, therefore we cannot
traverse the list in the reverse direction.
 A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed
in only one direction from head to the last node (tail).
 Each element in a linked list is called a node. A single node contains data and a pointer
to the next node which helps in maintaining the structure of the list.
 The first node is called the head; it points to the first node of the list and helps us access
every other element in the list. The last node, also sometimes called the tail, points to
NULL which helps us in determining when the list ends.

Instead of using array, we can also use linked list to implement stack. Linked list allocates the
memory dynamically. However, time complexity in both the scenario is same for all the
operations i.e. push, pop and peek.
In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory. Each node contains a pointer to its immediate successor node in the stack. Stack is
said to be overflown if the space left in the memory heap is not enough to create a node.
The top most node in the stack always contains null in its address field.
Adding a node to the stack (Push operation)

Adding a node to the stack is referred to as push operation. Pushing an element to a stack in
linked list implementation is different from that of an array implementation. In order to push
an element onto the stack, the following steps are involved.
1. Create a node first and allocate memory to it.
2. 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.
3. 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.
Deleting a node from the stack (POP operation)

Deleting a node from the top of stack is referred to as pop operation. Deleting a node from the
linked list implementation of stack is different from that in the array implementation. In order
to pop an element from the stack, we need to follow the following steps :

1. Check for the underflow condition: The underflow condition occurs when we try to pop
from an already empty stack. The stack will be empty if the head pointer of the list
points to null.
2. Adjust the head pointer accordingly: In stack, the elements are popped only from one
end, therefore, the value stored in the head pointer must be deleted and the node must
be freed. The next node of the head node now becomes the head node.

Display the nodes (Traversing)

Displaying all the nodes of a stack needs traversing all the nodes of the linked list organized in
the form of stack. For this purpose, we need to follow the following steps.

1. Copy the head pointer into a temporary pointer.


2. Move the temporary pointer through all the nodes of the list and print the value field
attached to every node.

ALGORITHM:

push()

1. Create a newNode with the given data.


2. Check whether the stack is empty (TOP == NULL).
3. If it is empty, then set the pointer of the node to NULL.
4. If it is not empty, then make the node point to TOP.
5. Finally, make the newNode as TOP.
6. if (TOP == NULL)
newNode -> next = NULL
else
newNode -> next = TOP

pop()

1. Check whether stack is empty (top == NULL).


2. If it is empty, then display "EMPTY STACK"
3. If it is not empty, then create a temporary node and set it to TOP.
4. Print the data of TOP.
5. Make TOP to point to the next node.
6. Delete the temporary node.

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;


printf("Name:HARSH KISHOR ALASHI\nRoll No.304\n");

while(choice != 9)

printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete


from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an
element\n8.Show\n9.Exit\n");

printf("\nEnter your choice: ");

scanf("%d",&choice);

printf("\n");

switch(choice)

case 1:

beginsert();

break;

case 2:

lastinsert();

break;

case 3:

randominsert();

break;

case 4:

begin_delete();

break;

case 5:

last_delete();
break;

case 6:

random_delete();

break;

case 7:

search();

break;

case 8:

display();

break;

case 9:

exit(0);

break;

default:

printf("Please enter valid choice..");

void beginsert()

struct node *ptr;

int item;

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

if(ptr == NULL)
{

printf("\nOVERFLOW");

else

printf("\nEnter value\n");

scanf("%d",&item);

ptr->data = item;

ptr->next = head;

head = ptr;

printf("\nNode inserted\n");

void lastinsert()

struct node *ptr,*temp;

int item;

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

if(ptr == NULL)

printf("\nOVERFLOW");

else

{
printf("\nEnter value?\n");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

ptr -> next = NULL;

head = ptr;

printf("\nNode inserted\n");

else

temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = ptr;

ptr->next = NULL;

printf("\nNode inserted\n");

void randominsert()

{
int i,loc,item;

struct node *ptr, *temp;

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

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data = item;

printf("\nEnter the location after which you want to insert ");

scanf("\n%d",&loc);

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\ncan't insert\n");

return;

}
ptr ->next = temp ->next;

temp ->next = ptr;

printf("\nNode inserted\n");

void begin_delete()

struct node *ptr;

if(head == NULL)

printf("\nList is empty\n");

else

ptr = head;

head = ptr->next;

free(ptr);

printf("\nNode deleted from the begining ...\n");

void last_delete()

struct node *ptr,*ptr1;

if(head == NULL)
{

printf("\nlist is empty");

else if(head -> next == NULL)

head = NULL;

free(head);

printf("\nOnly node of the list deleted ...\n");

else

ptr = head;

while(ptr->next != NULL)

ptr1 = ptr;

ptr = ptr ->next;

ptr1->next = NULL;

free(ptr);

printf("\nDeleted Node from the last ...\n");

void random_delete()

{
struct node *ptr,*ptr1;

int loc,i;

printf("\n Enter the location of the node after which you want to perform deletion \n");

scanf("%d",&loc);

ptr=head;

for(i=0;i<loc;i++)

ptr1 = ptr;

ptr = ptr->next;

if(ptr == NULL)

printf("\nCan't delete");

return;

ptr1 ->next = ptr ->next;

free(ptr);

printf("\nDeleted node %d \n",loc+1);

void search()

struct node *ptr;

int item,i=0,flag;

ptr = head;
if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

while (ptr!=NULL)

if(ptr->data == item)

printf("item found at location %d \n",i+1);

flag=0;

else

flag=1;

i++;

ptr = ptr -> next;

if(flag==1)

{
printf("Item not found\n");

void display()

struct node *ptr;

ptr = head;

if(ptr == NULL)

printf("Nothing to print\n");

else

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

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

ptr = ptr -> next;

}
OUTPUT:
CONCLUSION:

Thus, we have successfully Implemented Single Linked List Using Stack ADT.

You might also like