Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9dbe767

Browse files
author
Ram swaroop
committed
stack and queue implementation done
1 parent 6b8ff13 commit 9dbe767

File tree

11 files changed

+420
-130
lines changed

11 files changed

+420
-130
lines changed

src/me/ramswaroop/Main.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
11
package me.ramswaroop;
22

3+
import me.ramswaroop.common.LinkedQueue;
4+
import me.ramswaroop.common.LinkedStack;
5+
36
public class Main {
47

58
public static void main(String[] args) {
69
// write your code here
10+
11+
System.out.println("======== Stack ========");
12+
13+
LinkedStack<Integer> stack = new LinkedStack();
14+
stack.push(5);
15+
stack.push(7);
16+
stack.push(2);
17+
stack.push(6);
18+
stack.print();
19+
stack.pop();
20+
stack.pop();
21+
stack.pop();
22+
stack.pop();
23+
stack.print();
24+
stack.push(1);
25+
stack.push(2);
26+
stack.print();
27+
System.out.print(stack.peek());
28+
stack.print();
29+
30+
System.out.println("\n======== Queue ========");
31+
32+
LinkedQueue<Integer> queue = new LinkedQueue();
33+
queue.add(5);
34+
queue.add(7);
35+
queue.add(2);
36+
queue.add(6);
37+
queue.add(8);
38+
queue.add(10);
39+
queue.add(11);
40+
queue.add(4);
41+
queue.print();
42+
queue.remove();
43+
queue.remove();
44+
queue.remove();
45+
queue.print();
46+
queue.remove();
47+
queue.print();
48+
queue.remove();
49+
queue.remove();
50+
queue.remove();
51+
queue.remove();
52+
queue.print();
53+
//queue.remove();
54+
//queue.remove();
55+
queue.add(1);
56+
queue.add(2);
57+
queue.add(3);
58+
queue.add(5);
59+
queue.print();
60+
queue.remove();
61+
queue.remove();
62+
queue.remove();
63+
queue.print();
64+
queue.remove();
65+
queue.print();
766
}
867
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.ramswaroop.common;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
* User: ramswaroop
6+
* Date: 4/11/15
7+
* Time: 7:11 PM
8+
* To change this template go to Preferences | IDE Settings | File and Code Templates
9+
*/
10+
public class BinaryNode<E> {
11+
E data;
12+
BinaryNode left;
13+
BinaryNode right;
14+
15+
public BinaryNode(E data, BinaryNode left, BinaryNode right) {
16+
this.data = data;
17+
this.left = left;
18+
this.right = right;
19+
}
20+
21+
public E getData() {
22+
return data;
23+
}
24+
25+
public void setData(E data) {
26+
this.data = data;
27+
}
28+
29+
public BinaryNode getLeft() {
30+
return left;
31+
}
32+
33+
public void setLeft(BinaryNode left) {
34+
this.left = left;
35+
}
36+
37+
public BinaryNode getRight() {
38+
return right;
39+
}
40+
41+
public void setRight(BinaryNode right) {
42+
this.right = right;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.ramswaroop.common.interfaces;
1+
package me.ramswaroop.common;
22

33
/**
44
* Created by IntelliJ IDEA.
@@ -7,6 +7,6 @@
77
* Time: 10:57 PM
88
* To change this template go to Preferences | IDE Settings | File and Code Templates
99
*/
10-
public interface BinaryTree<T> extends Tree<T> {
10+
public interface BinarySearchTree<E> extends Tree<E> {
1111

1212
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package me.ramswaroop.common;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 4/12/15
9+
* Time: 11:07 AM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class LinkedQueue<E> implements Queue<E> {
13+
14+
Node<E> front;
15+
Node<E> rear;
16+
17+
public LinkedQueue() {
18+
front = null;
19+
rear = null;
20+
}
21+
22+
@Override
23+
public E add(E item) {
24+
if (front == null || rear == null) {
25+
front = rear = new Node<>(item, null);
26+
} else {
27+
rear.next = new Node<>(item, null);
28+
rear = rear.next;
29+
}
30+
return item;
31+
}
32+
33+
@Override
34+
public E remove() {
35+
if (rear.next == front) {
36+
throw new NoSuchElementException();
37+
}
38+
E item = element();
39+
front = front.next;
40+
return item;
41+
}
42+
43+
@Override
44+
public E element() {
45+
if (front == null) {
46+
throw new NoSuchElementException();
47+
}
48+
return front.data;
49+
}
50+
51+
@Override
52+
public int size() {
53+
int count = 0;
54+
if (rear.next == front) return count;
55+
for (Node node = front; node != rear; node = node.next) {
56+
count++;
57+
}
58+
return count;
59+
}
60+
61+
@Override
62+
public boolean isEmpty() {
63+
return rear.next == front;
64+
}
65+
66+
@Override
67+
public void print() {
68+
Node<E> node;
69+
System.out.print("[");
70+
if (rear.next == front) {
71+
System.out.print("]");
72+
return;
73+
}
74+
for (node = front; node != rear; node = node.next) {
75+
System.out.print(node.data + ",");
76+
}
77+
System.out.print(node.data + "]");
78+
}
79+
80+
private class Node<E> {
81+
E data;
82+
Node<E> next;
83+
84+
public Node(E data, Node<E> next) {
85+
this.data = data;
86+
this.next = next;
87+
}
88+
}
89+
}

src/me/ramswaroop/common/LinkedStack.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package me.ramswaroop.common;
22

33

4-
import me.ramswaroop.common.interfaces.Stack;
5-
6-
import java.util.NoSuchElementException;
4+
import java.util.EmptyStackException;
75

86
/**
97
* Created by IntelliJ IDEA.
@@ -21,16 +19,21 @@
2119
*/
2220
public class LinkedStack<E> implements Stack<E> {
2321

24-
private Node top = null;
22+
private Node<E> top;
23+
24+
public LinkedStack() {
25+
top = null;
26+
}
2527

2628
/**
2729
* Pushes an item onto the top of this stack.
2830
*
2931
* @param item
3032
*/
3133
@Override
32-
public void push(E item) {
33-
top = new Node(item, top);
34+
public E push(E item) {
35+
top = new Node<>(item, top);
36+
return item;
3437
}
3538

3639
/**
@@ -53,7 +56,7 @@ public E pop() {
5356
@Override
5457
public E peek() {
5558
if (top == null) {
56-
throw new NoSuchElementException();
59+
throw new EmptyStackException();
5760
}
5861
return top.data;
5962
}
@@ -66,12 +69,29 @@ public E peek() {
6669
@Override
6770
public int size() {
6871
int count = 0;
69-
for (Node node = top; node != null; node = top.next) {
72+
for (Node node = top; node != null; node = node.next) {
7073
count++;
7174
}
7275
return count;
7376
}
7477

78+
/**
79+
* Prints the content of the stack.
80+
*/
81+
@Override
82+
public void print() {
83+
Node<E> node;
84+
System.out.print("[");
85+
if (top == null) {
86+
System.out.print("]");
87+
return;
88+
}
89+
for (node = top; node.next != null; node = node.next) {
90+
System.out.print(node.data + ",");
91+
}
92+
System.out.print(node.data + "]");
93+
}
94+
7595
/**
7696
* Tests if this stack is empty.
7797
*
@@ -82,9 +102,9 @@ public boolean isEmpty() {
82102
return top == null;
83103
}
84104

85-
private class Node {
105+
private class Node<E> {
86106
E data;
87-
Node next;
107+
Node<E> next;
88108

89109
Node(E data, Node next) {
90110
this.data = data;

src/me/ramswaroop/common/Node.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/me/ramswaroop/common/Queue.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package me.ramswaroop.common;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
* User: ramswaroop
6+
* Date: 4/12/15
7+
* Time: 10:39 AM
8+
* To change this template go to Preferences | IDE Settings | File and Code Templates
9+
*/
10+
public interface Queue<E> {
11+
12+
13+
/**
14+
* Inserts the specified element into this queue.
15+
*
16+
* @param item
17+
* @return
18+
*/
19+
public E add(E item);
20+
21+
22+
/**
23+
* Retrieves and removes the head of this queue. This method throws an
24+
* exception if this queue is empty.
25+
*
26+
* @return
27+
*/
28+
public E remove();
29+
30+
31+
/**
32+
* Retrieves, but does not remove, the head of this queue. This method throws an
33+
* exception if this queue is empty.
34+
*
35+
* @return
36+
*/
37+
public E element();
38+
39+
40+
/**
41+
* Returns the size of this queue.
42+
*
43+
* @return
44+
*/
45+
public int size();
46+
47+
48+
/**
49+
* Tests whether the queue is empty or not.
50+
*
51+
* @return
52+
*/
53+
public boolean isEmpty();
54+
55+
56+
/**
57+
* Prints the content of the queue.
58+
*
59+
*/
60+
public void print();
61+
62+
63+
}

0 commit comments

Comments
 (0)