Skip to content

Commit be7d9ee

Browse files
Previous stack implementation bug fixed and queue implemented with singly linked list
1 parent 56f340d commit be7d9ee

File tree

3 files changed

+106
-9
lines changed

3 files changed

+106
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package stackwithsinglylinkedlist;
8+
9+
import java.util.EmptyStackException;
10+
11+
/**
12+
*
13+
* @author mac
14+
*/
15+
public class Queue <E> {
16+
// Here we need head to traverse the linked list from starting to lastNode
17+
private SinglyNode<E> head;
18+
//Last node will help us to add a new value at the last with out moving the
19+
private SinglyNode<E> lastNode;
20+
//Node count will track the length of the Queue
21+
private int nodeCount = 0;
22+
23+
24+
/**
25+
* Initializing head. First time both head and lastNode will be the same
26+
* @param value
27+
*/
28+
private void add(E value) {
29+
if(head == null) {
30+
SinglyNode<E> node = new SinglyNode<>(null, value);
31+
head = node;
32+
lastNode = node;
33+
} else {
34+
SinglyNode<E> node = new SinglyNode<>(null, value);
35+
lastNode.next = node;
36+
lastNode = lastNode.next;
37+
}
38+
nodeCount++;
39+
}
40+
41+
public void removeFirst() {
42+
head = head.next;
43+
nodeCount--;
44+
}
45+
46+
public void push(E value) {
47+
add(value);
48+
}
49+
50+
public void pop() {
51+
if(nodeCount != 0) {
52+
removeFirst();
53+
}
54+
}
55+
56+
public E peek() throws EmptyStackException {
57+
if(nodeCount > 0) {
58+
return head.getItem();
59+
} else {
60+
61+
throw new EmptyStackException();
62+
}
63+
}
64+
65+
public void display() {
66+
if(head == null) return;
67+
SinglyNode<E> tempNode = head;
68+
69+
while(!tempNode.isLastNode()) {
70+
System.out.println(tempNode.item);
71+
tempNode = tempNode.getNext();
72+
}
73+
// Printing the last item
74+
System.out.println(tempNode.item);
75+
}
76+
77+
}
78+

LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
* @author SouravPalit
1414
*/
1515
public class Stack<E> {
16-
// Here we need head to traverse the linked list from starting to lastNode
16+
1717
private SinglyNode<E> head;
18-
//Last node will help us to add a new value at the last with out moving the
19-
private SinglyNode<E> lastNode;
20-
//Node count will track the length of the linked list
18+
//Node count will track the length of the stack
2119
private int nodeCount = 0;
2220

2321

@@ -29,11 +27,11 @@ private void add(E value) {
2927
if(head == null) {
3028
SinglyNode<E> node = new SinglyNode<>(null, value);
3129
head = node;
32-
lastNode = node;
3330
} else {
34-
SinglyNode<E> node = new SinglyNode<>(null, value);
35-
lastNode.next = node;
36-
lastNode = lastNode.next;
31+
SinglyNode<E> newNode = new SinglyNode<>(null, value);
32+
SinglyNode<E> tempNode = head;
33+
newNode.next = head;
34+
head = newNode;
3735
}
3836
nodeCount++;
3937
}

LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ public class StackWithSinglyLinkedList {
1818
public static void main(String[] args) {
1919
// TODO code application logic here
2020

21+
System.out.println("Printing Queue Values......");
22+
23+
Queue<String> myQueue = new Queue<String>();
24+
25+
myQueue.push("1");
26+
myQueue.push("2");
27+
myQueue.push("3");
28+
myQueue.push("4");
29+
myQueue.push("5");
30+
31+
myQueue.pop();
32+
33+
String value = myQueue.peek();
34+
35+
System.out.println("Peek Value is = " + value);
36+
37+
myQueue.display();
38+
39+
40+
System.out.println("Printing Stack Values.....");
41+
2142
Stack<String> myStack = new Stack<String>();
2243

2344
myStack.push("1");
@@ -28,7 +49,7 @@ public static void main(String[] args) {
2849

2950
myStack.pop();
3051

31-
String value = myStack.peek();
52+
value = myStack.peek();
3253

3354
System.out.println("Peek Value is = " + value);
3455

0 commit comments

Comments
 (0)