Stacks: can be implemented using arrays, LinkedLists, ArrayLists
public class Stack{
private int[] data;
private int top;
public Stack(int maxSize){
data = new int[maxSize];
top = -1;
}
public void push(int n){
//can you add? is there space?
if(top < data.length-1){
top++;
data[top] = n;
}
}
public int getTop(){
//returns the item at the top
return data[top];
}
public int pop(){
top--;
return data[top + 1];
}
public boolean isEmpty(){
//checks if the array is empty
if(top == -1){
return true;
}
return false;
}
public int getSize(){
//how many items are in the array
return top + 1;
}
public void makeEmpty(){
top = -1;
}
}
//main
Stack plates = new Stack(20); //making an object called Stack, 20 is the size of the array, not the item number
plates.push(12);
plates.push(231);
plates.push(184);
System.out.println("top: " + plates.getTop());
Elements leave from the front, and are inserted from the rear
public class Queue{
public Queue (int maxSize){
data = new int[maxSize];
front = -1;
rear = -1;
}
public int getFront(){
return data[front];
}
public int dequeue(int n){
//taking them out of the lineup
//move the front + 1
//display the front (dequeued queue)
//use mod to rotate through
}
public int enqueue(int n){
rear+=1;
int location = rear%data.length;
// adding an item into the queue
//actually adding something to the queue
//when we inqueue something, the rear increases by 1
//increase the rear by 1
//use mod to rotate through
//
}
public int size(){
}
public void makeEmpty(){
}
}
//main
location = rear % data.length
the size is how many people are in the queue