This in progress blog
Stack linked list implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.stack.stack; | |
import com.tvidushi.stack.exception.StackOverFlowException; | |
import com.tvidushi.stack.exception.StackUnderFlowException; | |
public class StackLlistImpl<T> implements IStack{ | |
Node head; | |
public StackLlistImpl(Node node) { | |
this.head = node; | |
} | |
public void push(Object num) throws StackOverFlowException { | |
if(head == null){ | |
head = new Node(num,null); | |
return; | |
} | |
Node previous = head; | |
head = new Node(num,previous); | |
} | |
public T peek() throws StackUnderFlowException { | |
if(head == null) throw new StackUnderFlowException("Stack is empty "); | |
return (T) head.data; | |
} | |
public T pop() throws StackUnderFlowException { | |
if(isEmpty()) throw new StackUnderFlowException("") ; | |
T value = (T) head.data; | |
head = head.next; | |
return value; | |
} | |
public boolean isEmpty() { | |
return false; | |
} | |
public boolean isFull() { | |
return false; | |
} | |
public static class Node<T> { | |
Node next; | |
T data; | |
public Node(T data, Node node){ | |
this.next = node; | |
this.data = data; | |
} | |
} | |
} |
Stack array implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.stack.stack; | |
import com.tvidushi.stack.exception.StackUnderFlowException; | |
import com.tvidushi.stack.exception.StackOverFlowException; | |
public class StackArrayImpl implements IStackArray { | |
int top; | |
Integer[] num ; | |
int size; | |
//STEP 1: initialize the array | |
public StackArrayImpl(int top,int size,Integer[] num) { | |
this.size =size; | |
this.top = top; | |
this.num = num; | |
} | |
public void push(Integer number) throws StackOverFlowException { | |
if(isFull()){ | |
throw new StackOverFlowException("Stack over flow"); | |
} | |
num[++top] = number; | |
System.out.println(" Element pushed "+num[top]); | |
} | |
public Integer pop() throws StackUnderFlowException { | |
if(isEmpty()) throw new StackUnderFlowException(""); | |
int currentElement = top; | |
top--; | |
System.out.println(" Element pop "+num[currentElement]); | |
return num[currentElement]; | |
} | |
public Integer peek() throws StackUnderFlowException { | |
if(isEmpty()) throw new StackUnderFlowException(""); | |
return num[top]; | |
} | |
public boolean isEmpty(){ | |
return (top==-1); | |
} | |
public boolean isFull(){ | |
return (top==size-1); | |
} | |
} |
package com.tvidushi.stack;
import org.junit.Test;
import com.tvidushi.stack.exception.StackOverFlowException;
import com.tvidushi.stack.exception.StackUnderFlowException;
import com.tvidushi.stack.stack.StackLlistImpl;
import com.tvidushi.stack.stack.StackLlistImpl.Node;
import junit.framework.TestCase;
/**
* @author takshila.vidushi
*
*/
public class TestStackLlistImpl extends TestCase {
StackLlistImpl stack = new StackLlistImpl(new Node(90,null));
public void setup() {
}
@Test
public void testPush() throws StackOverFlowException {
stack.push(45);
stack.push(89);;
}
@Test
public void testPeek() throws StackUnderFlowException {
stack.peek();
}
@Test
public void testPop() throws StackUnderFlowException {
stack.pop();
}
@Test(expected =StackUnderFlowException.class)
public void testPop1() throws StackUnderFlowException {
stack.pop();
}
Download here
No comments:
Post a Comment