Stack Linked List
Stack Linked List
1. StackLinkedListImpl.java
1. StackLinkedListImpl.java
package com.nrs.ds.basic;
public class StackLinkedListImpl<T> {
NodeLL top;
public StackLinkedListImpl(){
top = null;
}
public void push(Object value){
NodeLL newNode= new NodeLL(value, null);
if(top == null){
top = newNode;
}else{
newNode.next = top;
top = newNode;
}
}
public T pop(){
if(top == null){
System.out.println("Stack is empty");
return null;
}
T value = (T) top.value;
top = top.next;
return value;
}
}
package com.nrs.ds.basic;
public class StackLLTest {
public static void main(String[] args) {
StackLinkedListImpl<String> sl = new StackLinkedListImpl<String>();
sl.pop();
sl.push("Ramu");
sl.push("Faheem");
sl.push("Naresh");
System.out.println(sl.pop());
}
}
Output:
Comments
Post a Comment