Write a stack for string data with pointers and linked lists
Write a stack for string data with pointers and linked lists.
Hint: Add(push) the new element between the head and the first link.
Remove(pop) the first element..
Stop when head = null.
Test with 10 words... able, bread, corn, dog, elephant, frog, ground, House, Italy, Jam
Test all Functionality.
Solution
Hi, please find my implementation.
Please let me know in case of any issue.
########## LinkedList.java ###############
public class LinkedList {
static class Node{
String data;
Node next;
Node(String d){
data = d;
next =null;
}
}
private Node head;
public LinkedList() {
head = null;
}
public boolean isEmpty(){
return head == null;
}
public void append(String data){
if(head == null)
head = new Node(data);
else{
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new Node(data);
}
}
public void addFront(String data){
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void removeFromLast(){
if(head == null || head.next == null)
head = null;
else{
Node temp = head;
while(temp.next.next != null)
temp = temp.next;
temp.next = null; // removing last element
}
}
public String removeFirst(){
String item = null;
if(head != null){
item = head.data;
head = head.next;
}
return item;
}
public boolean search(String data){
Node temp = head;
while(temp != null){
if(temp.data.equals(data))
return true;
temp = temp.next;
}
return false;
}
public void display(){
Node temp = head;
while(temp != null){
System.out.print(temp.data+\" \");
temp =temp.next;
}
System.out.println();
}
}
############ Stack.java ############
public class Stack {
private LinkedList stack;
public Stack() {
stack = new LinkedList();
}
public void Add(String data){
stack.addFront(data);
}
public String Remove() throws Exception{
if(!stack.isEmpty())
return stack.removeFirst();
throw new Exception(\"Stack is Empty\");
}
public boolean isEmpty(){
return stack.isEmpty();
}
public void display(){
stack.display();
}
}
########### StackTest.java #############
public class StackTest {
public static void main(String[] args) throws Exception {
Stack stack = new Stack();
stack.Add(\"able\");
stack.Add(\"bread\");
stack.display();
stack.Add(\"bread\");
stack.display();
stack.Add(\"dog\");
stack.Add(\"elephant\");
stack.Add(\"frog\");
stack.Add(\"ground\");
stack.display();
stack.Add(\"House\");
stack.Add(\"Italy\");
stack.Add(\"Jam\");
stack.display();
System.out.println(\"POP: \"+stack.Remove());
stack.display();
System.out.println(\"POP: \"+stack.Remove());
stack.display();
System.out.println(\"POP: \"+stack.Remove());
stack.display();
System.out.println(\"POP: \"+stack.Remove());
stack.display();
}
}
/*
Sample run:
bread able
bread bread able
ground frog elephant dog bread bread able
Jam Italy House ground frog elephant dog bread bread able
POP: Jam
Italy House ground frog elephant dog bread bread able
POP: Italy
House ground frog elephant dog bread bread able
POP: House
ground frog elephant dog bread bread able
POP: ground
frog elephant dog bread bread able
*/





