inital commit
This commit is contained in:
commit
d1948b0e58
67 changed files with 5280 additions and 0 deletions
3
Lab105-VenbergGE/manifest.mf
Normal file
3
Lab105-VenbergGE/manifest.mf
Normal file
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
114
Lab105-VenbergGE/src/ASCIITable.java
Normal file
114
Lab105-VenbergGE/src/ASCIITable.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Gabriel Venberg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
*utility library for nicely formatted ascii tables.
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class ASCIITable {
|
||||
|
||||
/**
|
||||
* render 2D array data into a table. the top level array is an array of rows.
|
||||
* @param data 2D array of data. Must have toString.
|
||||
* @param padding space padding on either side of data.
|
||||
* @return String containing table.
|
||||
*/
|
||||
public static String render(Object data[][], int padding){
|
||||
int colWidth = calcColumnWidth(data);
|
||||
int rows = calcNoRows(data);
|
||||
String horizontalSpacer = assembleHorizontalSpacers(colWidth, padding, rows);
|
||||
/*ok, so each cell will have the colwidth for the data, then padding for padding,
|
||||
* then a | at the end. (plus 1 at the begginning of the table.
|
||||
there will be 2 rows for each row of data (horizontal sep) plus a horizontal sep
|
||||
at the end.
|
||||
*/
|
||||
String string = "";
|
||||
//got everything set up, build the table row by row.
|
||||
for(int i=0; i<data.length; i++){
|
||||
string = string+horizontalSpacer+"\n";
|
||||
string = string+dataString(colWidth, padding, data[i])+'\n';
|
||||
}
|
||||
string = string+horizontalSpacer;
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates the maximum number of entries the rows in the data set have
|
||||
* @param data 2D array of data
|
||||
* @return needed number of rows in the final table.
|
||||
*/
|
||||
private static int calcNoRows(Object data[][]){
|
||||
int rows = 0;
|
||||
for(int i=0; i<data.length; i++){
|
||||
rows = Math.max(rows, data[i].length);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
/**
|
||||
* calculates the needed column width for a data array without padding
|
||||
* @param data the array of data
|
||||
* @return an integer representing the needed width of the column
|
||||
*/
|
||||
private static int calcColumnWidth(Object data[][]){
|
||||
int maxWidth = 0;
|
||||
for(int i=0; i<data.length; i++){
|
||||
for(int j=0; j<data[i].length; j++){
|
||||
maxWidth = Math.max(maxWidth, data[i][j].toString().length());
|
||||
}
|
||||
}
|
||||
return maxWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* gives the horizontal spacer needed for the table
|
||||
* @param colWidth width of each column;
|
||||
* @param padding padding on each side of data.
|
||||
* @param noOfCols number of columns;
|
||||
* @return a string suitable to use as the horizontal spacer for the table.
|
||||
*/
|
||||
private static String assembleHorizontalSpacers(int colWidth, int padding, int noOfCols){
|
||||
String string = "+";
|
||||
for(int i=0; i<noOfCols; i++){
|
||||
for(int j=0; j<colWidth+2*padding; j++){
|
||||
string = string+'-';
|
||||
}
|
||||
string = string+'+';
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* takes a single row of the data array and returns a row. Make sure your colWidth is accurate.
|
||||
* @param colWidth width of each column
|
||||
* @param data 1D array of data to print
|
||||
* @return a string containing the data
|
||||
*/
|
||||
private static String dataString(int colWidth, int padding, Object data[]){
|
||||
String string ="|";
|
||||
//for each entry in the row
|
||||
for(int i=0; i<data.length; i++){
|
||||
//only calc this once.
|
||||
int length=data[i].toString().length();
|
||||
// front padding. Also, I wish java had string multiplication.
|
||||
for(int p=0; p<padding+(colWidth-length); p++){string = string+" ";}
|
||||
string = string+data[i].toString();
|
||||
for(int p=0; p<padding; p++){string = string+" ";}
|
||||
string = string+"|";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
}
|
88
Lab105-VenbergGE/src/ArrayList.java
Normal file
88
Lab105-VenbergGE/src/ArrayList.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragments 7.2-7.5
|
||||
*
|
||||
* An implementation of the ArrayList class
|
||||
* */
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class ArrayList<E> implements List<E> {
|
||||
//instance variables
|
||||
public static final int CAPACITY = 16; //default array capacity
|
||||
private E[] data; //generic array used for storage
|
||||
private int size = 0; //current number of elements
|
||||
|
||||
//constructors
|
||||
/**constructs the list with default capacity*/
|
||||
public ArrayList(){this(CAPACITY);}
|
||||
/**constructs the list with given capacity*/
|
||||
public ArrayList(int capacity){
|
||||
data = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
//public methods
|
||||
/**Returns the number of elements int eh array list*/
|
||||
public int size(){return size;}
|
||||
/**returns whether the array list is empty*/
|
||||
public boolean isEmpty(){return size==0;}
|
||||
|
||||
/** returns but does not remove the element at index i*/
|
||||
public E get(int i) throws IndexOutOfBoundsException{
|
||||
checkIndex(i, size);
|
||||
return data[i];
|
||||
}
|
||||
|
||||
/**Replaces the element at index i with e, and returns the replaced element*/
|
||||
public E set(int i, E e) throws IndexOutOfBoundsException {
|
||||
checkIndex(i, size);
|
||||
E temp = data[i];
|
||||
data[i]=e;
|
||||
return temp;
|
||||
}
|
||||
|
||||
/** inserts element e to be at index i, shifting all subsequent elements later*/
|
||||
public void add(int i, E e) throws IndexOutOfBoundsException/*, IllegalStateException */{
|
||||
checkIndex(i, size+1);
|
||||
if(size==data.length){ //not enough capacity
|
||||
//throw new IllegalStateException("array is full");
|
||||
resize(2*data.length);//so double current capacity
|
||||
}
|
||||
for(int k=size-1; k>=i; k--){//start by shifting elements
|
||||
data[k+1]=data[k];
|
||||
}
|
||||
data[i]=e;//ready to place new element
|
||||
size++;
|
||||
}
|
||||
|
||||
/**removes and returns the element at index i, shifting subsequent elements earlier*/
|
||||
public E remove(int i) throws IndexOutOfBoundsException {
|
||||
checkIndex(i, size);
|
||||
E temp = data[i];
|
||||
for(int k=i; k<size-1;k++){//shift elements to fill hole
|
||||
data[k]=data[k+1];
|
||||
}
|
||||
data[size-1]=null; //help GC
|
||||
size--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
//utility methods
|
||||
/**checks whether the given index is in the range [0, n-1]*/
|
||||
protected void checkIndex(int i, int n) throws IndexOutOfBoundsException{
|
||||
if(i<0||i>=n){
|
||||
throw new IndexOutOfBoundsException("illegal index: "+i+", size is "+size);
|
||||
}
|
||||
}
|
||||
|
||||
protected void resize(int capacity){
|
||||
E[] temp = (E[]) new Object[capacity];//safe cast, compiler may give warning
|
||||
for(int k=0; k<size; k++){
|
||||
temp[k]=data[k];
|
||||
}
|
||||
data=temp; //start using the new array
|
||||
}
|
||||
}
|
58
Lab105-VenbergGE/src/ArrayQueue.java
Normal file
58
Lab105-VenbergGE/src/ArrayQueue.java
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.10
|
||||
*
|
||||
* An implementation of the ArrayQueue class
|
||||
* */
|
||||
|
||||
/**
|
||||
*implementation of the queue ADT using a fixed-length array.
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class ArrayQueue<E> implements Queue<E>{
|
||||
//instance variables
|
||||
private E[] data; //generic array used for storage
|
||||
private int f = 0; //index of the front element
|
||||
private int sz = 0; //current number of elements
|
||||
private static final int CAPACITY = 1000; //default capacity, book is missing this.
|
||||
|
||||
//constructors
|
||||
/** constructs queue with default capacity*/
|
||||
public ArrayQueue(){this(CAPACITY);}
|
||||
/**constructs queue with given capacity*/
|
||||
public ArrayQueue(int capacity){
|
||||
data = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
//methods
|
||||
/** returns the number of elements in the queue*/
|
||||
public int size(){return sz;}
|
||||
|
||||
/** tests whether the queue is empty*/
|
||||
public boolean isEmpty(){return sz == 0;}
|
||||
|
||||
/**inserts an element at the rear of the queue*/
|
||||
public void enqueue(E e) throws IllegalStateException{
|
||||
if(sz==data.length){throw new IllegalStateException("queue is full");}
|
||||
int avail = (f+sz)%data.length; //use modular arithmatic
|
||||
data[avail]=e;
|
||||
sz++;
|
||||
}
|
||||
|
||||
/** returns but does not remove the first element in the queue or null if emtpy*/
|
||||
public E first(){
|
||||
if(isEmpty()){return null;}
|
||||
return data[f];
|
||||
}
|
||||
|
||||
/** removes and returns the first element of the queue or null if emtpy*/
|
||||
public E dequeue(){
|
||||
if(isEmpty()){return null;}
|
||||
E answer = data[f];
|
||||
data[f]=null; //dereference to help GC
|
||||
f=(f+1)%data.length;
|
||||
sz--;
|
||||
return answer;
|
||||
}
|
||||
}
|
55
Lab105-VenbergGE/src/ArrayStack.java
Normal file
55
Lab105-VenbergGE/src/ArrayStack.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.2
|
||||
*
|
||||
* An implementation of an ArrayStack class
|
||||
* */
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class ArrayStack<E> implements Stack<E> {
|
||||
public static final int CAPACITY = 1000;//default capacity
|
||||
private E[] data; //generic array used for storage.
|
||||
private int t=-1; //index of the top element in the stack
|
||||
|
||||
|
||||
//im swiching the authors comments to javadoc style, for ease of use.
|
||||
|
||||
/**
|
||||
* constructs stack with default capacity
|
||||
*/
|
||||
public ArrayStack(){this(CAPACITY);}
|
||||
|
||||
/**
|
||||
* constructs stack with given capacity
|
||||
* @param capacity capacity to construct the stack with.
|
||||
*/
|
||||
public ArrayStack(int capacity){
|
||||
data = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public int size(){return (t+1);}
|
||||
|
||||
public boolean isEmpty(){return(t==-1);}
|
||||
|
||||
public void push(E e) throws IllegalStateException{
|
||||
if(size()==data.length){throw new IllegalStateException("Stack is full");}
|
||||
data[++t]=e; //increment t before storing a new item.
|
||||
}
|
||||
|
||||
public E top(){
|
||||
if(isEmpty()){return null;}
|
||||
return data[t];
|
||||
}
|
||||
|
||||
public E pop(){
|
||||
if(isEmpty()){return null;}
|
||||
E answer = data[t];
|
||||
data[t] = null; //dereference to help with garbage collection.
|
||||
t--;
|
||||
return answer;
|
||||
}
|
||||
}
|
121
Lab105-VenbergGE/src/Client.java
Normal file
121
Lab105-VenbergGE/src/Client.java
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Gabriel Venberg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
|
||||
public class Client {
|
||||
public static void main(String args[]){
|
||||
|
||||
final int maxMag = 9;
|
||||
//maxMag-1 orders of magnitude (because we start from 10).
|
||||
//and 5 methods to try + col for iterations.
|
||||
long[][] data = new long[maxMag][6];
|
||||
long startTime;
|
||||
long endTime;
|
||||
|
||||
for(int i=1; i<=maxMag; i++){
|
||||
//figure out the number of iterations
|
||||
int limit = (int) Math.pow(10, i);
|
||||
//put number of iterations in table.
|
||||
data[i-1][0]=limit;
|
||||
|
||||
//array stack
|
||||
System.out.println("mag "+i+" arrayStack");
|
||||
ArrayStack arrayStack = new ArrayStack(limit);
|
||||
startTime = System.nanoTime();
|
||||
for(int j=0; j<limit; j++){
|
||||
arrayStack.push(j);
|
||||
}
|
||||
for(int j=0; j<limit; j++){
|
||||
arrayStack.pop();
|
||||
}
|
||||
endTime = System.nanoTime();
|
||||
data[i-1][1]=endTime-startTime;
|
||||
arrayStack = null;
|
||||
|
||||
//linked stack
|
||||
System.out.println("mag "+i+" linkedStack");
|
||||
LinkedStack linkedStack = new LinkedStack();
|
||||
startTime = System.nanoTime();
|
||||
for(int j=0; j<limit; j++){
|
||||
linkedStack.push(j);
|
||||
}
|
||||
for(int j=0; j<limit; j++){
|
||||
linkedStack.pop();
|
||||
}
|
||||
endTime = System.nanoTime();
|
||||
data[i-1][2]=endTime-startTime;
|
||||
linkedStack = null;
|
||||
|
||||
//array queue
|
||||
System.out.println("mag "+i+" arrayQueue");
|
||||
ArrayQueue arrayQueue = new ArrayQueue(limit);
|
||||
startTime = System.nanoTime();
|
||||
for(int j=0; j<limit; j++){
|
||||
arrayQueue.enqueue(j);
|
||||
}
|
||||
for(int j=0; j<limit; j++){
|
||||
arrayQueue.dequeue();
|
||||
}
|
||||
endTime = System.nanoTime();
|
||||
data[i-1][3]=endTime-startTime;
|
||||
arrayQueue = null;
|
||||
|
||||
//linked queue
|
||||
System.out.println("mag "+i+" linkedQueue");
|
||||
LinkedQueue linkedQueue = new LinkedQueue();
|
||||
startTime = System.nanoTime();
|
||||
for(int j=0; j<limit; j++){
|
||||
linkedQueue.enqueue(j);
|
||||
}
|
||||
for(int j=0; j<limit; j++){
|
||||
linkedQueue.dequeue();
|
||||
}
|
||||
endTime = System.nanoTime();
|
||||
data[i-1][4]=endTime-startTime;
|
||||
linkedQueue = null;
|
||||
|
||||
//array list
|
||||
System.out.println("mag "+i+" arrayList");
|
||||
ArrayList arrayList = new ArrayList();//testing the auto grow
|
||||
startTime = System.nanoTime();
|
||||
for(int j=0; j<limit; j++){
|
||||
arrayList.add(j, j);
|
||||
}
|
||||
for(int j=limit-1; j>0; j--){ //this takes AGES if i remove at index 0 with an incrementing loop due to having to shift everything.
|
||||
arrayList.remove(j);
|
||||
}
|
||||
endTime = System.nanoTime();
|
||||
data[i-1][5]=endTime-startTime;
|
||||
arrayList = null;
|
||||
}
|
||||
|
||||
//now we make an array of strings and format the numbers nicely...
|
||||
String[][] strings = new String[maxMag][6];
|
||||
for(int i=0; i<data.length; i++){
|
||||
for(int j=0; j<data[i].length; j++){
|
||||
strings[i][j]=String.format("%,d", data[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
//and print out the table.
|
||||
System.out.println(ASCIITable.render(strings, 2));
|
||||
}
|
||||
}
|
21
Lab105-VenbergGE/src/LinkedQueue.java
Normal file
21
Lab105-VenbergGE/src/LinkedQueue.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.11
|
||||
*
|
||||
* An implementation of the LinkedQueue class
|
||||
* */
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class LinkedQueue<E> implements Queue<E>{
|
||||
private SinglyLinkedList<E> list = new SinglyLinkedList(); //an empty list
|
||||
public LinkedQueue(){} //new queue relies on initaly empty list
|
||||
public int size(){return list.size();}
|
||||
public boolean isEmpty(){return list.isEmpty();}
|
||||
public void enqueue(E element){list.addLast(element);}
|
||||
public E first(){return list.first();}
|
||||
public E dequeue(){return list.removeFirst();}
|
||||
}
|
21
Lab105-VenbergGE/src/LinkedStack.java
Normal file
21
Lab105-VenbergGE/src/LinkedStack.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.4
|
||||
*
|
||||
* An implementation of the LinkedStack class
|
||||
* */
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public class LinkedStack<E> implements Stack<E> {
|
||||
private SinglyLinkedList<E> list = new SinglyLinkedList<>(); //an empty list.
|
||||
public LinkedStack(){}; //new stack relies on initaly empty list.
|
||||
public int size(){return list.size();}
|
||||
public boolean isEmpty(){return list.isEmpty();}
|
||||
public void push(E element){list.addFirst(element);}
|
||||
public E top(){return list.first();}
|
||||
public E pop(){return list.removeFirst();}
|
||||
}
|
31
Lab105-VenbergGE/src/List.java
Normal file
31
Lab105-VenbergGE/src/List.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 7.1
|
||||
*
|
||||
* An implementation of the List interface
|
||||
* */
|
||||
|
||||
/**
|
||||
*a simplified version of the java.util.List interface.
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public interface List<E> {
|
||||
/** returns the number of elements in the list*/
|
||||
int size();
|
||||
|
||||
/**returns whether the list is empty*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**returns but does not remove the element at index i.*/
|
||||
E get(int i) throws IndexOutOfBoundsException;
|
||||
|
||||
/**replaces the element at index i with e, and returns the replacement element.*/
|
||||
E set(int i, E e) throws IndexOutOfBoundsException;
|
||||
|
||||
/**inserts element e to be at index i, shifting all subsequent elements later*/
|
||||
void add(int i, E e) throws IndexOutOfBoundsException;
|
||||
|
||||
/**removes and returns the element at index i, shifting subsequent elements earlier*/
|
||||
E remove(int i) throws IndexOutOfBoundsException;
|
||||
}
|
28
Lab105-VenbergGE/src/Queue.java
Normal file
28
Lab105-VenbergGE/src/Queue.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.9
|
||||
*
|
||||
* An implementation of the Queue interface
|
||||
* */
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public interface Queue<E> {
|
||||
/** returns the number of elements in the queue*/
|
||||
int size();
|
||||
|
||||
/** tests whether the queue is empty*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**inserts an element at the rear of the queue*/
|
||||
void enqueue(E e);
|
||||
|
||||
/**returns, but does not remove, the first element of the queue (null if empty). */
|
||||
E first();
|
||||
|
||||
/** removes and returns the first element of the queue (null if empty)*/
|
||||
E dequeue();
|
||||
}
|
78
Lab105-VenbergGE/src/SinglyLinkedList.java
Normal file
78
Lab105-VenbergGE/src/SinglyLinkedList.java
Normal file
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
*SinglyLinkedListClass
|
||||
* Code Fragments 3.14, 3.15
|
||||
* from
|
||||
* Data Structures & Algorithms, 6th edition
|
||||
* by Michael T. Goodrich, Roberto Tamassia & Michael H. Goldwasser
|
||||
* Wiley 2014
|
||||
* Transcribed by
|
||||
* @author Gabe Venberg
|
||||
*/
|
||||
public class SinglyLinkedList<E> {
|
||||
|
||||
private static class Node<E> {
|
||||
private E element; //refrence to element stored at this node
|
||||
private Node<E> next; //refrence to subsequent node of list
|
||||
|
||||
public Node(E e, Node<E> n){
|
||||
element = e;
|
||||
next = n;
|
||||
}
|
||||
|
||||
public E getElement() {return element;}
|
||||
|
||||
public Node<E> getNext() {return next;}
|
||||
|
||||
public void setNext(Node<E> n) {next = n;}
|
||||
}
|
||||
|
||||
//instance variables of SinglyLinkedList
|
||||
private Node<E> head = null;//head node of list
|
||||
private Node<E> tail = null;//last node of list
|
||||
private int size = 0;//number of nodes in list
|
||||
|
||||
public SinglyLinkedList(){}//constructs an initaly empty list
|
||||
|
||||
//access methods
|
||||
public int size() {return size;}
|
||||
|
||||
public boolean isEmpty() {return size == 0;}
|
||||
|
||||
public E first(){//returns but does not remove the first element
|
||||
if (size == 0) {return null;} //special case
|
||||
return head.getElement();
|
||||
}
|
||||
|
||||
public E last(){//returns but does not remove last elemnt
|
||||
if (size ==0) {return null;}//special case
|
||||
return tail.getElement();
|
||||
}
|
||||
|
||||
//update methods
|
||||
public void addFirst(E e){//adds element e to the front of the list
|
||||
head = new Node<>(e, head);//create and link a new node
|
||||
if (size == 0) {tail = head;}//special case, head becomes tail also
|
||||
size++;
|
||||
}
|
||||
|
||||
public void addLast(E e){//adds element to end of list
|
||||
Node<E> newest = new Node<>(e, null);//create and link a new node
|
||||
if(size == 0){//special case, previously empty list
|
||||
head = newest;
|
||||
}
|
||||
else{
|
||||
tail.setNext(newest);//new node after existing tail
|
||||
}
|
||||
tail = newest;//new node becomes tail
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst(){//removes and returns the first element
|
||||
if(size == 0){return null;}//nothing to remove
|
||||
E answer = head.getElement();
|
||||
head = head.getNext();//will become null if list had only one node.
|
||||
size--;
|
||||
if(size==0){tail = null;}// special case as list is now empty
|
||||
return answer;
|
||||
}
|
||||
}
|
46
Lab105-VenbergGE/src/Stack.java
Normal file
46
Lab105-VenbergGE/src/Stack.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* Data Structures & Algorithms 6th Edition
|
||||
* Goodrich, Tamassia, Goldwasser
|
||||
* Code Fragment 6.1
|
||||
*
|
||||
* An implementation of the stack interface
|
||||
* */
|
||||
|
||||
/**
|
||||
* A collection of objects that are inserted and removed according to a last-in
|
||||
* first-out principle. Although similar in purpose, this interface differs from
|
||||
* java.util.stack.
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
public interface Stack<E> {
|
||||
|
||||
/**
|
||||
* returns the number of elements in the stack
|
||||
* @return number of elements in the stack.
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* tests whether the stack is empty.
|
||||
* @return true if stack is empty, false otherwise.
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* inserts an element at the top of the stack.
|
||||
* @param e the element to be inserted.
|
||||
*/
|
||||
void push(E e);
|
||||
|
||||
/**
|
||||
* returns, but does not remove, the top element of the stack.
|
||||
* @return top element of the stack or null if empty.
|
||||
*/
|
||||
E top();
|
||||
|
||||
/**
|
||||
* removes and returns the top element from the stack.
|
||||
* @return element removed or null if empty.
|
||||
*/
|
||||
E pop();
|
||||
}
|
Reference in a new issue