Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7bba988

Browse files
Added Stacks
1 parent bc2981f commit 7bba988

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

‎Stack/Stack.java

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
public class Stack {
2+
3+
// NODE
4+
public class Node {
5+
6+
public String data;
7+
private Node next;
8+
private Node previous;
9+
10+
public Node(String data) {
11+
this.data = data;
12+
this.next = null;
13+
}
14+
15+
public void setNextNode(Node node) {
16+
this.next = node;
17+
}
18+
19+
public void setPreviousNode(Node node) {
20+
this.previous = node;
21+
}
22+
23+
public Node getNextNode() {
24+
return this.next;
25+
}
26+
27+
public Node getPreviousNode() {
28+
return this.previous;
29+
}
30+
31+
}
32+
33+
// LINKED LIST
34+
public class LinkedList {
35+
36+
public Node head;
37+
38+
public LinkedList() {
39+
this.head = null;
40+
}
41+
42+
public void addToHead(String data) {
43+
Node newHead = new Node(data);
44+
Node currentHead = this.head;
45+
this.head = newHead;
46+
if (currentHead != null) {
47+
this.head.setNextNode(currentHead);
48+
}
49+
}
50+
51+
public void addToTail(String data) {
52+
Node tail = this.head;
53+
if (tail == null) {
54+
this.head = new Node(data);
55+
} else {
56+
while (tail.getNextNode() != null) {
57+
tail = tail.getNextNode();
58+
}
59+
tail.setNextNode(new Node(data));
60+
}
61+
}
62+
63+
public String removeHead() {
64+
Node removedHead = this.head;
65+
if (removedHead == null) {
66+
return null;
67+
}
68+
this.head = removedHead.getNextNode();
69+
return removedHead.data;
70+
}
71+
72+
public String toString() {
73+
Node currentNode = this.head;
74+
String output = "<head> ";
75+
while (currentNode != null) {
76+
output += currentNode.data + " ";
77+
currentNode = currentNode.getNextNode();
78+
}
79+
output += "<tail>";
80+
return output;
81+
}
82+
}
83+
84+
// STACK IMPLEMENTATION
85+
public LinkedList stack;
86+
public int size;
87+
static final int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
88+
public int maxSize;
89+
90+
public Stack() {
91+
this(DEFAULT_MAX_SIZE);
92+
}
93+
94+
public Stack(int maxSize) {
95+
this.stack = new LinkedList();
96+
this.size = 0;
97+
this.maxSize = maxSize;
98+
}
99+
100+
public boolean hasSpace() {
101+
return this.size < this.maxSize;
102+
}
103+
104+
public boolean isEmpty() {
105+
return this.size == 0;
106+
}
107+
108+
public void push(String data) {
109+
if (this.hasSpace()) {
110+
this.stack.addToHead(data);
111+
this.size++;
112+
System.out.println("Added " + data + "! Stack size is now " + this.size);
113+
} else {
114+
throw new Error("Stack is full!");
115+
}
116+
}
117+
118+
public String pop() {
119+
if (!this.isEmpty()) {
120+
String data = this.stack.removeHead();
121+
this.size--;
122+
System.out.println("Removed " + data + "! Stack size is now " + this.size);
123+
return data;
124+
} else {
125+
throw new Error("Stack is empty!");
126+
}
127+
}
128+
129+
public String peek() {
130+
if (this.isEmpty()) {
131+
return null;
132+
} else {
133+
return this.stack.head.data;
134+
}
135+
}
136+
137+
public static void main(String[]args) {
138+
Stack things = new Stack();
139+
things.push("house");
140+
things.push("car");
141+
things.pop();
142+
}
143+
}

‎Stack/readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Stacks
2+
A stack is a data structure which contains an ordered set of data.
3+
4+
Stacks provide _three_ methods for interaction:
5+
6+
1. **Push** - Adds data to the "top" of the stack
7+
2. **Pop** - Returns and removes data from the "top" of the stack
8+
3. **Peek** - Returns data from the "top" of the stack without removing it
9+
10+
It works on **LIFO** basis which means, "**Last In First Out**".
11+
12+
## Implementation
13+
Stacks can be implemented using a linked list as the underlying data structure because it’s more efficient
14+
than a list or array.
15+
16+
Depending on the implementation, the top of the stack is equivalent to the head node of a linked list and the
17+
bottom of the stack is equivalent to the tail node.
18+
19+
A constraint that may be placed on a stack is its size.
20+
21+
This is done to limit and quantify the resources the data structure will take up when it is "full".
22+
23+
Attempting to _push data_ onto an _already full stack_ will result in a **stack overflow**.
24+
25+
If you attempt to _pop data_ from an _empty stack_, it will result in a **stack underflow**.
26+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /