Stack Implementation using SinglyLinkedList

Program


package com.wordpress.sreeharshasite;
public class NodeG<T> {
int data;
NodeG<T> nextNode;
public NodeG(int data)
{
this.data=data;
this.nextNode=null;
}
}


package com.wordpress.sreeharshasite;
public class StackSingleLinkedList<T> {
int size=0;
NodeG<T> headNode=null;
public void push(int data)
{
NodeG<T> newHeadNode=new NodeG<T>(data);
NodeG<T> currNode=headNode;
/*scenario 1: if headNode is null */
if(currNode==null)
{
headNode=newHeadNode;
size++;
}
else
{
if(currNode!=null)
{
/*scenario 2: if headNode nextnode is null */
if(currNode.nextNode==null)
{
newHeadNode.nextNode=currNode;
currNode.nextNode=null;
currNode=currNode.nextNode;
headNode=newHeadNode;
size++;
}
/*scenario 3: if headNode nextnode is not null
* add the element at the head position */
else
{
newHeadNode.nextNode=currNode;
currNode=null;
headNode=newHeadNode;
size++;
}
}
}
}
public void pop()
{
NodeG<T> currNode=headNode;
/*scenario 1: if no nodes to delete return */
if(currNode==null)
{
return ;
}
else
{
/*scenario 3: delete the headNode */
if(currNode.nextNode!=null)
{
headNode=currNode.nextNode;
size--;
}
/*scenario 3: if only head node to delete */
else
{
currNode=null;
headNode=currNode;
size--;
}
}
}
/*display the headNode*/
public NodeG<T> peek()
{
if(headNode==null)
{
return null;
}
else
{
return headNode;
}
}
public void display()
{
NodeG<T> currNode=headNode;
while(currNode!=null)
{
System.out.print(currNode.data+" ");
currNode=currNode.nextNode;
}
System.out.println();
}
}


package com.wordpress.sreeharshasite;
public class StackSingleLinkedListDemo {
public static void main(String[] args) {
StackSingleLinkedList<Integer> sll=new StackSingleLinkedList<Integer>();
sll.push(10);
sll.push(20);
sll.push(30);
sll.push(40);
sll.push(50);
sll.push(10);
System.out.println("After Insertion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
NodeG<Integer> node=sll.peek();
System.out.println("Head Node value is:"+node.data);
}
}

Output

After Insertion
10 50 40 30 20 10 
After Deletion
50 40 30 20 10 
After Deletion
40 30 20 10 
After Deletion
30 20 10 
Head Node value is:30

 

 

Advertisements

I am Sreeharsha from Bengaluru. I am working in java for the past 6+ years. In this blog i will be sharing posts regarding java , advanced java , monthly challenges, collection of quotes , my themes and many more. If you like my posts please follow my blog.

Tagged with: , ,
Posted in Core Java, Data Structures

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow My Diary on WordPress.com
Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

%d bloggers like this: