0% found this document useful (0 votes)
34 views

Stack Program

The document describes a Java program that implements a stack using a singly linked list. It creates Node and Stack classes, with Node representing stack elements and Stack performing operations like push, pop, isEmpty and display. The main method creates a Stack object and uses a menu to let the user push, pop and display elements until exiting. The program was tested and the output verified that it successfully implements stack operations on a linked list.

Uploaded by

gracesachinrock
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)
34 views

Stack Program

The document describes a Java program that implements a stack using a singly linked list. It creates Node and Stack classes, with Node representing stack elements and Stack performing operations like push, pop, isEmpty and display. The main method creates a Stack object and uses a menu to let the user push, pop and display elements until exiting. The program was tested and the output verified that it successfully implements stack operations on a linked list.

Uploaded by

gracesachinrock
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/ 4

Ex: 01 Implementation of Stack using Singly Linked List

Aim:

To write a Java program to create a stack data structure and to implement various stack operations.

Algorithm:

1. Create a Node class to represent individual elements of the stack. Each Node contains a
data value and a reference to the next Node.
2. Create a Stack class that maintains a reference to the top of the stack (the latest added
element).
3. Implement methods in the Stack class to perform stack operations:
a. Push: Add a new element to the top of the stack.
b. isEmpty: Check if the stack is empty.
c. Pop: Remove the top element from the stack.
d. Display: Print the elements of the stack.
4. In the Stackopn class (main class), create a Stack object and provide a user menu to interact
with the stack:
a. Push: Prompt the user to enter an element and add it to the stack.
b. Pop: Remove the top element from the stack.
c. Display: Print the elements of the stack.
d. Exit: Terminate the program.
5. Use a loop to continuously prompt the user for their choice until they choose to exit the
program.

Source Code:

import java.io.*;
class Node
{
public int data;
public Node next;
}
class Stack
{
public Node top;
public Node temp;
public Stack()
{
top=null;
}
public void Push(int no)
{
temp=new Node();
temp.data=no;
temp.next=top;
top=temp;
System.out.println("The Element Pushed into the Stack is:"+top.data);
}
public boolean isEmpty()
{
if(top==null)
return true;

else
return false;
}
public void Pop()
{
int no;
if(isEmpty())
System.out.println("The Stack is Empty!!");
else
{
temp=top;
no=temp.data;
System.out.println("The Element Popped is: "+no);
temp=temp.next;
top=temp;
}

}
public void Display()
{
temp=top;
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}
}
}
class Stackopn
{
public static void main(String args[])throws IOException
{
Stack S=new Stack();
int choice;
int no;
do
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Your Choice: ");
choice=Integer.parseInt(br.readLine());
System.out.println("User Menu:");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Display");
System.out.println("4.Exit");
switch(choice)
{
case 1:
System.out.println("Enter the Element to Push:");
no=Integer.parseInt(br.readLine());
S.Push(no);
break;
case 2:
S.Pop();
break;
case 3:
System.out.println("Enter the Element in the Stack are:");
S.Display();
break;
case 4:
System.exit(0);
default:
System.out.println("Enter the valid choice from 1 to 4!!");
}
} while (choice!=4);
}
}

Sample Output:
Linked Implementation of Stack:
---------------------------------------:
Enter Your Choice: 1
User Menu:
1.Push
2.Pop
3.Display
4.Exit
Enter the Element to Push: 22
The Element Pushed into the Stack is: 22
Enter Your Choice: 1
User Menu:
1.Push
2.Pop
3.Display
4.Exit
Enter the Element to Push: 33
The Element Pushed into the Stack is: 33
Enter Your Choice: 1
User Menu:
1.Push
2.Pop
3.Display
4.Exit
Enter the Element to Push: 44
The Element Pushed into the Stack is: 44
Enter Your Choice: 3
User Menu:
1.Push
2.Pop
3.Display
4.Exit
Enter the Element in the Stack are:
44
33
22
Enter Your Choice: 2
User Menu:
1.Push
2.Pop
3.Display
4.Exit
The Element Popped is: 44
Enter Your Choice: 3
User Menu:
1.Push
2.Pop
3.Display
4.Exit
Enter the Element in the Stack are:
33
22

Result: Thus the above stack implementation using singly linked list program was executed
successfully and the output was verified.

You might also like