Stack Reverse - Recursion

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

import java.util.

Scanner;
class LinkedList
{
Node head;
class Node
{
int value;
Node next;
Node(int d)
{
value=d;
next=null;
}
}
public int createStack(int new_value)
{
Node new_node=new Node(new_value);
new_node.next=head;
head=new_node;
return new_value;
}
public void displayStack()
{
Node temp=head;
while(temp!=null)
{
System.out.print(temp.value+" ");
temp=temp.next;
}
}
public void stackReverse()
{
Node previousNode=null;
Node nextNode;
Node currentNode=head;
while(currentNode!=null)
{
nextNode=currentNode.next;
currentNode.next=previousNode;
previousNode=currentNode;
currentNode=nextNode;
}
head=previousNode;
}
}
public class StackDriver
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
LinkedList b=new LinkedList();
System.out.println("Enter length of List:");
int val=sc.nextInt();
if(val<=0)
{
System.out.println("Invalid Length");
}
else
{
int a[]=new int[val];
for(int i=0;i<val;i++)
{
a[i]=i+1;
b.createStack(a[i]);
}
System.out.println("Elements in Stack");
b.stackReverse();
b.displayStack();
System.out.println("\nElements in the stack after reversal");
b.stackReverse();
b.displayStack();
}
}
}

You might also like