Expt-8 - Implementation of Stack Using Circular Linked List
Expt-8 - Implementation of Stack Using Circular Linked List
Ex.No.8
Implementation of Stack using Circular Linked List
10-10-2022
AIM: To write and execute JAVA program to perform Stack using Circular Singly Linked
List.
Pseudocode:
push()
1. Initialize variable data
2. create *newnode of type struct node
3.allocate memory to newnode
4.if newnode=NULL
5. print(“Unable to allocate memory”)
6.else print(“Enter value”)
7. if top=NULL
8. newnode->data=data;
9. newnode->next=newnode;
10. top=newnode;
11. else
12. temp=top;
13. newnode->data=data
14. newnode->next=top;
15. while(temp->next != top)
16. temp=temp->next;
17. temp->next=newnode;
18. top=newnode;
pop()
1.create *temp,*t of type struct node
2.if top=NULL
3. print (“Underflow”)
4.else t=temp=top;
5. If temp->next==top
6. free(temp)
7. top=NULL
8. else
9. while(temp->next!=top)
10. temp=temp->next;
11. top=top->next;
12. temp->next=top;
13. print(“data to be deleted”,t->data) free(t);
display()
1.struct node *temp;
2. temp<-top;
3.if top=NULL
4. print(“stack is empty”)
5.else
6. while(temp->next!=top)
7. print(“stack elements”)
8. temp=temp->next
9. print(temp->data)
Explanation:
push()
We are going to push/insert elements into stack
We have created a newnode pointer and allocated memory to it.
In line 4 we are checking if newnode=NULL if yes then it will print unable to
allocate memory ,if false else part will execute.
From line 6 we are taking the element value from the user and checking if
top=NULL , that means if there are no element inside the stack , if yes if will assign
newnode->data=data
Newnode->next=newnode , because in circular singly linked list last node is
connected to first node
Top=newnode ,now our first element is top
In line 11 we are checking if there is already element in stack do newnode-
>data=data
Newnode->next=top
Do temp=temp->next until temp->next != top to traverse all elements
now temp is at the last node and we are assigning temp->next=newnode
top=newnode
pop()
In pop function we delete the element that is at the top
We are creating a temp,t pointer that is pointing at top
If top=NULL that means there are no elements in the stack so it will print underflow
Else t=temp=top , all pointer are pointing at the top element
Do temp=temp->next until temp->next != top
Top=top->next , top will now point to the second element
Temp->next=top ,temp is at the last node now it will point to the second node
Then we deallocate the first node free(t)
display()
We are taking a new pointer temp
Assigning temp=top
If top =NULL , it will print stack is empty
Otherwise, it will print stack elements by checking while(temp->next!=top)
Print temp->data
Temp=temp->next , this loop is going to traverse all the nodes and print their data.
Print (temp->data) to print data of last node as well.
Example:
Program Code:
import java.util.Scanner;
public class stack_csll {
static node top=null;
static node temp=null;
static Scanner sc= new Scanner(System.in);
public static void main(String[] args) {
int ch = 0;
while (ch != 4) {
System.out.println("\n1.Enqueue\n2.Delete\n3.Display\n4.Exit");
System.out.println("\nEnter your choice:");
ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println(" Enter the data to be inserted: ");
int i = sc.nextInt();
add(i);
break;
case 2:
delete();
break;
case 3:
print();
break;
case 4:
System.out.println("\nExiting...");
break;
default:
System.out.println("\nEnter valid choice");
break;
}
}
}
static void add(int data)
{
if(top==null)
{
System.out.print("Nothing to delete");
return;
}
if(temp==top)
{
top=null;
temp=null;
return;
}
System.out.println("-----------------------------");
System.out.println("Top element has been deleted");
System.out.println("------------------------------");
top=top.next;
temp.next=top;
}
}
class node
{
int data;
node next;
node(int data)
{
this.data=data;
this.next=null;
}
}
Output Screenshots:
RESULT: Thus, the programs for the given problem statements has been executed and
the results are verified successfully.