Midterm1
• NextFriday2/17inclass,50minutes• Whatisonit?• Anythingyou’vepracticedincludingHW3(incl.quizzes,peerinstructions,HWs,discussionwork)
• Isthereanypractice/review?• We’llputupatleast1priorexam/solutions• Discussionon2/13-2/15willbeusedasareview(comereadytoworkonpracticeexamquestions)
• Notesallowed?• 1double-sidedsheetofnotes
CS2230CSII:Datastructures
Meeting9:ListADT,BrandonMyers
UniversityofIowa
Today’sbigideas
• InterfacescanbeusedtoexpressAbstractDataTypes,likeList
• Generics allowustobuilddatastructuresthatcanholdelementsofanytype
• LearnabouttwomoreAbstractDataTypes,StackandQueue
public interface List {// add the element to the end of the listpublic void append(Object element);
// check if the element exists in the listpublic boolean contains(Object element);
// remove first element from the list and return itpublic Object removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
class ArrayList implements List {private Object[] elements;private int numElements;@Overridepublic void append(Object element) {}
@Overridepublic boolean contains(Object element) {
}
@Overridepublic Object removeFirst() {
}
@Overridepublic boolean isEmpty() {
}}
class LinkedList implements List {private ListNode header;
@Overridepublic void append(Object element) {}
@Overridepublic boolean contains(Object element) {
}
@Overridepublic Object removeFirst() {
}
@Overridepublic boolean isEmpty() {
}}
InArrayList’s implementationofList.append,whatshouldwedointhiscase?
a) allocateabiggerarrayandcopyalltheelementstoitb) returnanerrorc) increasethelengthofthearraytomakeroomd) replacethelastelementwith900e) don’tchangeanything
myList.append(900)
400 500 600 700 800
5numElements
elements
myList
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
class ArrayList implements List {private Object[] elements;private int numElements;
@Overridepublic void append(Object ele) {
// finish this method}
}
public interface List {// add the element to the end of the listpublic void append(Object ele);
}
foryourinformation,hereistherelevantpartofList
makeagroupof3students:• manager(ensureeveryonecontributes)• scribe(sitsinthemiddle,doesn’tdecidewhattowrite)• skeptic(questioneverything)
400 500 600 700 800
5numElements
elements
myList
Generics allowustobuilddatastructuresthatcanholdelementsofanytype
/*Interface for a generic List ADT, an ordered collection of elements
GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {
// add the element to the end of the listpublic void append(T element);
// check if the element exists in the listpublic boolean contains(T element);
// remove first element from the list and return itpublic T removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
public interface Glist<T> {// add the element to the end of the listpublic void append(T element);
// check if the element exists in the listpublic boolean contains(T element);
// remove first element from the list and return itpublic T removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
class LinkedList<T> implements Glist<T> {private ListNode header;
@Overridepublic void append(T element) {}
@Overridepublic boolean contains(T element) {
}
@Overridepublic T removeFirst() {
}
@Overridepublic boolean isEmpty() {
}}
unlimitednumberofLinkedList classesforthepriceof1!!
\
head
tail
data next data next data next
200 300 \100
head
tail
data next data next data next
"Siamese" 5.8
"Tabby" 6"GrumpyC
at" 1
LinkedList<Cat>
LinkedList<Integer>
"B" "C" \"A"
head
tail
data next data next data next
LinkedList<String>
public interface Glist<T> {// add the element to the end of the listpublic void append(T element);
// check if the element exists in the listpublic boolean contains(T element);
// remove first element from the list and return itpublic T removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
class ArrayList<T> implements GList<T> {private T[] elements;private int numElements;@Overridepublic void append(T element) {}
@Overridepublic boolean contains(T element) {
}
@Overridepublic T removeFirst() {
}
@Overridepublic boolean isEmpty() {
}}
class LinkedList<T> implements Glist<T> {private ListNode header;
@Overridepublic void append(T element) {}
@Overridepublic boolean contains(T element) {
}
@Overridepublic T removeFirst() {
}
@Overridepublic boolean isEmpty() {
}}
LearnabouttwomoreAbstractDataTypes,StackandQueue
QueueADT
https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/2000px-Data_Queue.svg.png
public interface Queue<T> {/* Insert element at back of queue */public void enqueue(T ele);
/* Remove element from front of queueand return it*/public T dequeue();
/* Return the element at the front of queue */public T peek();
}
/*Interface for a generic List ADT, an ordered collection of elements
GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {
// add the element to the end of the listpublic void append(T element);
// check if the element exists in the listpublic boolean contains(T element);
// remove first element from the list and return itpublic T removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
public interface Queue<T> {/* Insert element at back of queue */public void enqueue(T ele);
/* Remove element from front of queueand return it*/public T dequeue();
/* Return the element at the front of queue */public T peek();
}
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
True/False
Queue<T>couldbeimplementedusingaGList<T>
StackADT
Real-lifeexamplewhere...
• aprocess/situation/etc behaveslikeastack• or,aspecificapplicationwhereastackdatastructurewouldbeuseful
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
public interface Stack<T> {/* put the element on the top of the stack */public void push(T ele);
/* remove the element on top of the stackand return it*/public T pop();
/* return the element on top of the stack */public T peek();
}
/*Interface for a generic List ADT, an ordered collection of elements
GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {
// add the element to the end of the listpublic void append(T element);
// check if the element exists in the listpublic boolean contains(T element);
// remove first element from the list and return itpublic T removeFirst();
// return true if the list is emptypublic boolean isEmpty();
}
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
True/False
Stack<T>couldbeimplementedusingaGList<T>
public interface Stack<T> {/* put the element on the top of the stack */public void push(T ele);
/* remove the element on top of the stackand return it*/public T pop();
/* return the element on top of the stack */public T peek();
}
Today’sbigideas
• InterfacescanbeusedtoexpressAbstractDataTypes,likeList
• Generics allowustobuilddatastructuresthatcanholdelementsofanytype
• LearnabouttwomoreAbstractDataTypes,StackandQueue