Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
COMP 103 Linked Stack and Linked Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Stacks And Queues Chapter 18.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
List Interface and Linked List Mrs. Furman March 25, 2010.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Linked Data Structures
Sections 3.4 Formal Specification
Stacks II David Lillis School of Computer Science and Informatics
Marcus Biel, Software Craftsman
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Pointers and Linked Lists
THURSDAY, OCTOBER 17 IN LAB
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks CS-240 Dick Steflik.
CSC 143 Java Linked Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Presentation transcript:

Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last Sunday). You will need to download a new copy, if you have not done so this week already. Solution to midterm posted shortly.

Winter 2006CISC121 - Prof. McLeod2 Dept. Math. & Stats. Colloquium Today!! Friday, March 10, 2:30 p.m., Jeffery Hall 234 (bad vibes?) Speaker: Olgica Milenkovic University of Colorado Title: Two problems in analysis of algorithms (see next slide for abstract…)

Winter 2006CISC121 - Prof. McLeod3 Abstract: We address the problem of analyzing the complexity of two algorithms from the field of computer algebra and bioinformatics. The first algorithm, known as Gosper's method for indefinite summation of hypergeometric terms, is investigated in the asymptotic, average-case complexity regime. We show that for several classes of input functions represented in terms of urn models, the complexity of Gosper's algorithm is related to the extremal behavior of a class of generalized random-walks. The second algorithm we discuss is concerned with the problem of sorting signed permutations by reversals. Algorithms for sorting by reversals represent one of the most important tools in analyzing rearrangements in the mouse vs. human genome. We point out some shortcomings of the current approach to modeling reversals and propose two new extensions for the notion of reversal distance. We investigate the complexity of a Viterbi and M-type algorithm for computing cost-constrained reversal distances. The first part of the talk is a joint work with Kevin J. Compton from the University of Michigan.

Winter 2006CISC121 - Prof. McLeod4 Last Time (Tutorial!) Before that: –Doubly linked lists. –Circular lists. –Skip lists. –Self – organizing lists.

Winter 2006CISC121 - Prof. McLeod5 Today Replacing sparse tables with linked lists. LinkList in java.util Stacks (if we have time) Next time: Queues

Winter 2006CISC121 - Prof. McLeod6 Sparse Tables A sparse table is defined as a table where the available cells are only partly populated. For example, consider a table where the columns are student ID’s and the rows are courses taken, for the entire University: –A cell can contain a grade for a course taken by the student. –Of course, not all students take all courses, so only a small portion of each column is taken up with data. –Such a “sparse table” is probably not an efficient use of memory.

Winter 2006CISC121 - Prof. McLeod7 Sparse Tables – Cont. Replace the table by a system of linked lists. Here is one possible design: –Have an ArrayList of course Objects. Each course Object contains all the necessary info about each course and a link to the first student enrolled in the course. –Have an ArrayList of student Objects. Each student Object contains the necessary student information and has a link to the first course taken by that student. –However, make the node design in such a way that the nodes are shared by both lists!

Winter 2006CISC121 - Prof. McLeod8 Sparse Tables – Cont. –Each node contains: student number class number grade code (0 is “A”, 9 is “F”) link to next student link to next course –One node for each course the student has taken. No empty nodes. See the structure on the next slide: Data Links

Winter 2006CISC121 - Prof. McLeod9 1

Winter 2006CISC121 - Prof. McLeod10 Sparse Tables – Cont. (I don’t know why SN and course# have to be in each node in the diagram…) How to navigate through this structure? –Start with a student or start with a course. –But from any given node you can flip the direction of your navigation. At the moment, you have to follow links from oldest to newest in one direction – this would be a painful way of finding recent students in a course that has been around for a while! How would you fix this?

Winter 2006CISC121 - Prof. McLeod11 Sparse Tables – Cont. More efficient use of memory because there are no empty table elements. Uses 17% of the memory used by the sparse table for a typical University setting. Can easily grow as required.

Winter 2006CISC121 - Prof. McLeod12 Linked Lists in java.util java.util contains a class called “ LinkedList ”. ( E is the element type to be stored in the list.) As does the ArrayList class, LinkedList contains many useful pre-defined methods. LinkedList implements a linked list as a “generic doubly-linked list with references to the head and tail”. Many of the LinkedList methods throw Exceptions for illegal parameters. LinkedList only stores “ Objects ” of type E.

Winter 2006CISC121 - Prof. McLeod13 Linked Lists in java.util – Cont. Methods include (“ ob ” is an object of type E ): void add(ob) // adds ob to end of list. void add(pos, ob) // adds ob at pos. void addFirst(ob) // adds ob at beginning of list. void addLast(ob) // same as add(ob). void clear() // removes all objects from the list. boolean contains(ob) // returns true if the list contains ob.

Winter 2006CISC121 - Prof. McLeod14 Linked Lists in java.util – Cont. Object get(pos) // returns the object at pos. Object getFirst() // returns first object in list. Object getLast() // returns last object in list. int indexOf(ob) // returns position of first occurrence of ob, or –1 if ob is not found. boolean isEmpty() // returns true if list is empty, false otherwise.

Winter 2006CISC121 - Prof. McLeod15 Linked Lists in java.util – Cont. Iterator iterator() // generates and returns an iterator for the list. LinkedList() // creates an empty linked list. boolean remove(ob) // removes first occurrence of ob and returns true. Object removeFirst() // removes and returns first element in list.

Winter 2006CISC121 - Prof. McLeod16 Linked Lists in java.util – Cont. Object removeLast() // removes and returns last element in list. int size() // returns number of elements in list.

Winter 2006CISC121 - Prof. McLeod17 Stacks What is a stack? For example a “PEZ” candy container:

Winter 2006CISC121 - Prof. McLeod18 Stacks – Cont. Another example are those plate dispensers in cafeterias. A stack follows the “Last In, First Out” or “LIFO” principle. A stack would have the following operations: –clear() – clear the stack. –isEmpty() – check to see if the stack is empty. –isFull() – check to see if the stack is full. –push(element) - put the element on top of the stack. –pop() – take the topmost element from the stack. –peek() – return the topmost element in the stack without removing it.

Winter 2006CISC121 - Prof. McLeod19 Stacks – Cont. Any other methods would not be “legal” – you would no longer be modeling a stack. This is a restrictive data structure! Why bother?

Winter 2006CISC121 - Prof. McLeod20 Stacks – Cont. A stack is modeled with another data structure “under the hood”. If you used a linked list: –What kind of a linked list would you use? –What linked list methods would be used to carry out the following stack methods:? push pop clear peek

Winter 2006CISC121 - Prof. McLeod21 Stacks – Cont. See IntStack.java for a linked list implementation. Some features of IntStack: –An inner, inner class for the node. –An inner class for the linked list. –Only public methods are: clear () boolean isEmpty () push (int) int pop () int peek ()

Winter 2006CISC121 - Prof. McLeod22 Stacks – Cont. A stack can also be implemented with an ArrayList or even an array (But not as well, IMHO). Note that none of the stack operations require iteration through the linked list.

Winter 2006CISC121 - Prof. McLeod23 ArrayList Stack Implementation See ALStack.java (Note that we are assuming the user will check to see if the stack is empty before calling a “pop()” operation. What else could we do?) “Features”: –We need to keep track of position in the ArrayList. –We could use (but did not) the automatic “un-boxing” and boxing feature in Java 5.0 (huh?).

Winter 2006CISC121 - Prof. McLeod24 Array Stack Implementation See ArrayStack.java A bit clumsy? Of the three implementations, which is the best?

Winter 2006CISC121 - Prof. McLeod25 A Stack in Use How to add numbers that are too large to be stored in a long type variable? See LongAddition.java

Winter 2006CISC121 - Prof. McLeod26 The Stack Class in java.util java.util has a “ Stack ” class that implements the above methods using a Vector as the storage object. (A Vector is like an ArrayList…) Stack is a sub-class of Vector, and as such, inherits all the Vector methods.

Winter 2006CISC121 - Prof. McLeod27 The Stack Class – Cont. Unique Stack methods: boolean empty() // same as isEmpty Object peek() Object pop() Object push(element) int search(target) // returns position of target in stack, if not found –1 is returned. Stack() // constructor

Winter 2006CISC121 - Prof. McLeod28 The Stack Class – Cont. In Stack, “ push ” also returns a reference to the Object added to the stack, so both peek and push can change that topmost object on the stack. Since Stack “is a” Vector, methods like “ setElementAt ” and “ removeElementAt ” can be used on stack elements – but these methods would be illegal by our definition of what a stack is! Also, when Vector ’s are used to implement the stack, re-sizing of the Vector can greatly slow down the push method.

Winter 2006CISC121 - Prof. McLeod29 The Stack Class – Cont. For these reasons it is better to implement a stack as we have done above, using a private linked list (best) or a private array(next best) as a data object within the definition of the class. Implementing a stack using a linked list defined using an inner class for the list and an “inner, inner” class for the node provides better information hiding than the Stack class.