Skip to content

Commit 1b2e2ca

Browse files
committed
Stack Updates
Stack Updates
1 parent 708badf commit 1b2e2ca

File tree

8 files changed

+180
-1
lines changed

8 files changed

+180
-1
lines changed
240 Bytes
Binary file not shown.

bin/chapter4stacks/LinkedStack.class

-250 Bytes
Binary file not shown.
1.36 KB
Binary file not shown.

bin/chapter4stacks/StackSets.class

2.78 KB
Binary file not shown.

src/chapter3linkedlists/LinkedList.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ public synchronized ListNode removeFromBegin() {
9393
}
9494
return node;
9595
}
96+
97+
// Remove and return the node at the end of the list
98+
public synchronized ListNode getLast() {
99+
if (head == null)
100+
return null;
101+
if (head.getNext() == null) {
102+
return head;
103+
}
104+
ListNode p = head.getNext();
105+
while(p.getNext() != null) {
106+
p = p.getNext();
107+
}
108+
return p;
109+
}
96110

97111
// Remove and return the node at the end of the list
98112
public synchronized ListNode removeFromEnd() {
@@ -201,6 +215,11 @@ public int getPosition(int data) {
201215
return Integer.MIN_VALUE;
202216
}
203217

218+
// Size of the list.
219+
public boolean isEmpty(){
220+
return length==0;
221+
}
222+
204223
// Remove everything from the list.
205224
public void clearList(){
206225
head = null;

src/chapter4stacks/LinkedStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import chapter3linkedlists.*;
1717
import java.util.EmptyStackException;
1818

19-
public class LinkedStack<T>{
19+
public class LinkedStack{
2020
private int length; // indicates the size of the linked list
2121
private ListNode top;
2222

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : StackForStackSets.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter4stacks;
16+
17+
class StackForStackSets {
18+
19+
private int top = -1;
20+
private int[] arr;
21+
// Maximum size of stack
22+
private int capacity;
23+
24+
StackForStackSets(int capacity){
25+
this.capacity = capacity;
26+
arr = new int[capacity];
27+
}
28+
29+
public void push(int v){
30+
arr[++top] = v;
31+
}
32+
33+
public int pop(){
34+
35+
return arr[top--];
36+
}
37+
38+
// if the stack is at capacity
39+
public Boolean isAtCapacity(){
40+
return capacity == top + 1;
41+
}
42+
43+
//return the size of the stack
44+
public int size(){
45+
return top+1;
46+
}
47+
48+
public String toString(){
49+
String s = "";
50+
int index = top;
51+
while(index >= 0){
52+
s += "[" + arr[index--] + "]"+ " --> ";
53+
}
54+
return s;
55+
56+
}
57+
}

src/chapter4stacks/StackSets.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : StackSets.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter4stacks;
16+
17+
import java.util.ArrayList;
18+
19+
public class StackSets{
20+
// Number of elements for each stack
21+
private int threshold;
22+
private ArrayList<StackForStackSets> listOfStacks = new ArrayList<StackForStackSets>();
23+
24+
StackSets(int threshold) {
25+
this.threshold = threshold;
26+
}
27+
//get the last stack
28+
public StackForStackSets getLastStack(){
29+
int size = listOfStacks.size();
30+
if(size <= 0)
31+
return null;
32+
else return listOfStacks.get(size - 1);
33+
}
34+
35+
//get the nth stack
36+
public StackForStackSets getNthStack(int n){
37+
System.out.println(n);
38+
int size = listOfStacks.size();
39+
if(size <= 0)
40+
return null;
41+
else return listOfStacks.get(n - 1);
42+
}
43+
44+
//push value
45+
public void push(int value){
46+
StackForStackSets lastStack = this.getLastStack();
47+
48+
if(lastStack == null){
49+
lastStack = new StackForStackSets(threshold);
50+
lastStack.push(value);
51+
listOfStacks.add(lastStack);
52+
}else {
53+
if( !lastStack.isAtCapacity())
54+
lastStack.push(value);
55+
else {
56+
StackForStackSets newLastStack = new StackForStackSets(threshold);
57+
newLastStack.push(value);
58+
listOfStacks.add(newLastStack);
59+
}
60+
}
61+
}
62+
// the pop
63+
public int pop(){
64+
StackForStackSets lastStack = this.getLastStack();
65+
int v = lastStack.pop();
66+
if(lastStack.size() == 0) listOfStacks.remove(listOfStacks.size() - 1);
67+
return v;
68+
}
69+
70+
71+
//pop from the nth stack
72+
public int pop(int nth){
73+
StackForStackSets nthStack = this.getNthStack(nth);
74+
int v = nthStack.pop();
75+
if(nthStack.size() == 0) listOfStacks.remove(listOfStacks.size() - 1);
76+
return v;
77+
78+
}
79+
80+
public String toString(){
81+
String s = "";
82+
for(int i = 0; i < listOfStacks.size(); i++){
83+
StackForStackSets stack = listOfStacks.get(i);
84+
s = "Stack "+ i + ": " + stack.toString() + s;
85+
}
86+
87+
return s;
88+
}
89+
90+
91+
public static void main(String[] args){
92+
StackSets stacks = new StackSets(3);
93+
stacks.push(10); stacks.push(9); stacks.push(8);
94+
stacks.push(7); stacks.push(6); stacks.push(5);
95+
stacks.push(4); stacks.push(3); stacks.push(2);
96+
97+
System.out.println("Popping " + stacks.pop(2));
98+
System.out.println("Popping from stack 1" + stacks.pop(1));
99+
System.out.println("Popping " + stacks.pop(3));
100+
System.out.println("Popping " + stacks.pop(2));
101+
System.out.println("the stack is: " + stacks);
102+
}
103+
}

0 commit comments

Comments
 (0)