Stack Implementation Java(Using Linked List)

Source code download here.


package data.structures.Stack;
import data.structures.LinkedList.LinkedList;
import data.structures.LinkedList.Node;
/**
*
* @author jasleen
* @param <T>
*/
public class Stack<T> {
final LinkedList<T> list;
public Stack() {
list=new LinkedList<>();
}
public void push(T t)
{
list.addAtHead(t);
}
public void pop()
{
if(!isEmpty())
list.removeHead();
else
System.out.println("There is no item to pop");
}
public Node extractTop()
{
Node t=list.head();
pop();
return t;
}
public T top()
{
if(isEmpty())
{
System.out.println("There is no item");
return null;
}
return list.head().key();
}
@Override
public String toString() {
StringBuilder builder=new StringBuilder();
builder.append("[");
builder.append("\t");
Node currentTraversing=list.head();
while(currentTraversing!=null)
{
builder.append(currentTraversing.key().toString());
builder.append("\t");
currentTraversing=currentTraversing.next();
}
builder.append("]");
return builder.toString();//To change body of generated methods, choose Tools | Templates.
}
public boolean isEmpty() {
return list.head()==null; //To change body of generated methods, choose Tools | Templates.
}
}

Linked List Implementation Java

Download Source Code LinkedList.java and Node.java

Check if given LinkedList is a pallindrome or not

Check if given LinkedList is a pallindrome or not

My version of the program. Open to corrections and improvements

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package interview.practice;
import data.structures.LinkedList.LinkedList;
import data.structures.LinkedList.Node;
import data.structures.Stack.Stack;
/**
*
* @author jasleen
*/
public class isPallindrome {
public static boolean isPallindrome(LinkedList list) {
Node single = list.head(), doubl = list.head();
Stack stack = new Stack();
if (single == null||single.next()==null) {
return false;
}
while (single.next() != null && doubl.next().next() != null) {
stack.push(single.key());
single = single.next();
doubl = doubl.next().next();
if ( doubl.next() == null) {
break;
}
}
System.out.println(doubl);
System.out.println(single);
System.out.println(stack);
if (doubl.next()!= null) {
stack.push(single.key());
single = single.next();
Node popped=stack.extractTop();
if (!single.key().toString().equals(popped.key().toString())) {
System.out.println("Returning at key" + popped.key().toString()+single.key());
return false;
}
}
single = single.next();
System.out.println(stack);
//System.out.println(single.key());
while (single != null) {
Node popped=stack.extractTop();
if (!single.key().toString().equals(popped.key().toString())) {
System.out.println("Returning at key" + popped.key().toString()+single.key());
return false;
}
single = single.next();
}
return true;
}
}
Download code for this program here.
Download code for LinkedList here.
Download code for Node here.
Download code for Stack here.