Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Expt-8 - Implementation of Stack Using Circular Linked List

This document contains a Java program to implement a stack using a circular singly linked list. It includes pseudocode and explanations for push(), pop(), and display() methods. The program code defines a Node class and uses these methods to add elements to the stack, delete the top element, and display the stack. The output screenshots verify that the program works as intended.

Uploaded by

Naveen Bara
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Expt-8 - Implementation of Stack Using Circular Linked List

This document contains a Java program to implement a stack using a circular singly linked list. It includes pseudocode and explanations for push(), pop(), and display() methods. The program code defines a Node class and uses these methods to add elements to the stack, delete the top element, and display the stack. The output screenshots verify that the program works as intended.

Uploaded by

Naveen Bara
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

School of Computing Science and Engineering

VIT Bhopal University

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

Name: Naveen Bara [1] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

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

Name: Naveen Bara [2] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

 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:

Name: Naveen Bara [3] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

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)
    {

Name: Naveen Bara [4] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

        node n=new node(data);


        System.out.println(data+" has been added to stack");
        if(top==null)
        {
            top=n;
            temp=top;
            return;
        }
       
        n.next=top;
        top=n;
        temp.next=top;
        return;
    }
    static void print()
    {
        node t=top;
       
        if(top==null)
        {
            System.out.println("Stack is empty");
            return;
        }
       
            System.out.println("Stack:-");
            System.out.println(" ---");
            do
            {
                System.out.println("| "+t.data+" |");
                t=t.next;
            }
            while(t!=top&&top.next!=null);
            System.out.println(" ---");
       
        return;
    }
    static void delete()
    {

        if(top==null)
        {
            System.out.print("Nothing to delete");
            return;
        }
        if(temp==top)
        {
            top=null;
            temp=null;

Name: Naveen Bara [5] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

            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:

Name: Naveen Bara [6] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

Name: Naveen Bara [7] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

RESULT: Thus, the programs for the given problem statements has been executed and
the results are verified successfully.

Name: Naveen Bara [8] Reg. Number: 22MCA10116

You might also like