Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists.

Slides:



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

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
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.
Stacks, Queues & Deques CSC212.
Summary of lectures (1 to 11)
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
More on Linked List Algorithms. Linked List and Template Remember the implementation of a double linked list we learned before. Some of methods are shown.
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.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
(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.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 6 Last time: –Encapsulation –Javadoc documentation –Testing and Debugging.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
Review Array Array Elements Accessing array elements
Week 4 - Friday CS221.
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Lesson Objectives Aims
CSC 143 Java Linked Lists.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Linked Lists Chapter 5 (continued)
Presentation transcript:

Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists

Summer 2007CISC121 - Prof. McLeod2 You Will Need To: Look over solution to midterm. Look over exercise 3. Finish assignment 3. Hopefully assn 4 will be along soon!

Summer 2007CISC121 - Prof. McLeod3 Today Finish Linked Lists Stacks & Queues Next: Analysis of Complexity!

Summer 2007CISC121 - Prof. McLeod4 Where We Left Off… A singly linked list with head and tail pointers. The IntNode class defines the node, holding a data item and a link to the next node. We had to declare the two attributes of the node class public when the node was an external class. It is better to use an “inner class” for the node! 15 head 105 null 20 tail

Summer 2007CISC121 - Prof. McLeod5 Use of Inner Classes – Cont. public class IntSLList { private IntNode head; private IntNode tail; private class IntNode { private int info; private IntNode next; public IntNode (int i) { this(i, null); } public IntNode (int i, IntNode n) { info = i; next = n; } } // end IntNode // rest of IntSLList methods, constructors } // end IntSLList

Summer 2007CISC121 - Prof. McLeod6 Writing Linked List Methods Note that each method must work for: –An empty list, –A list with just one node, and –A list with two or more nodes. We have methods to: –Create a list (the constructor) –Add to the head of the list –Check for an empty list –Add to the tail of the list What other methods would be useful?

Summer 2007CISC121 - Prof. McLeod7 Singly Linked List - Other Methods Deleting a head node. Deleting all nodes! Deleting an inner node (not head or tail). Deleting a tail node. Others: –Searching for a certain node. –Counting nodes. –Adding a inner node (“insertion”, but why?)

Summer 2007CISC121 - Prof. McLeod8 Singly Linked List - Deleting Nodes Note that a deleting method may or may not return the data value or a link to the data Object that it has deleted. We will assume that the deleting method returns the value. The calling method can choose not to do anything with this value. Note that these deleting methods will return a value of -1 if the list is empty. What else could we do here?

Summer 2007CISC121 - Prof. McLeod9 Deleting the Head Node public int removeFromHead () { if (isEmpty()) return -1; int i = head.info; if (head.next == null) { head = null; tail = null; } else head = head.next; return i; } // end removeFromHead

Summer 2007CISC121 - Prof. McLeod10 Deleting the Head Node, Cont. 15 head tail 105 null 15 head tail 105 null After head = head.next; What happens to the node with 15?

Summer 2007CISC121 - Prof. McLeod11 Deleting All Nodes public void clearList() { if (!isEmpty()) { head = null; tail = null; } // end if } // end clearList Nodes that are no longer “pointed to” are garbage collected!

Summer 2007CISC121 - Prof. McLeod12 First, locate the node, then delete it. It is way too big a method to show on this slide!! See the next one: Deleting the Node that Contains the Value “delNum”

Summer 2007CISC121 - Prof. McLeod13 public void delete (int delNum) { // does not return i if (!isEmpty()) if (head==tail && delNum==head.info) { head = null; tail = null;} // only 1 node else if (delNum == head.info) // delete first node, more nodes in list head = head.next; else { IntNode pred = head; IntNode temp = head.next; while (temp != null && temp.info != delNum) { pred = pred.next; temp = temp.next; } // end while if (temp != null) { pred.next = temp.next; if (tail == temp) tail = pred; } // end if } // end else } // end delete method

Summer 2007CISC121 - Prof. McLeod14 Deleting an Inner Node - An Example list.delete(10); 15 head tail -5 null 15 head tail -5 null 15 head tail -5 null pred temp pred temp predtemp pred.next = temp.next;

Summer 2007CISC121 - Prof. McLeod15 Deleting an Inner Node – Iterators Note the use of the pred and temp objects in the delete method: –(“Java jargon”) These are called “iterators” because they are used to move through the list.

Summer 2007CISC121 - Prof. McLeod16 Deleting a Tail Node So how is this going to work? How can the tail pointer be moved up to the preceding node? 15 head 105 null 20 tail

Summer 2007CISC121 - Prof. McLeod17 Deleting a Tail Node - Cont. Since there is no link from the tail node to the previous node, the only way is to iterate through the entire list, starting from the head, until the tail is reached. (How can you tell when you have reached the tail?) Two iterators must be used (Why?), as in the delete method:

Summer 2007CISC121 - Prof. McLeod18 public int removeTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { head = null; tail = null;} else { IntNode pred = head; IntNode temp = head.next; while (temp.next != null) { pred = pred.next; temp = temp.next; } // end while tail = pred; tail.next = null; } // end else } // end if return i; } // end removeTail method

Summer 2007CISC121 - Prof. McLeod19 Deleting a Tail Node - Cont. That was a lot of work! Deleting the tail node this way is more time consuming than deleting an inner node. Would it not be nice if the tail node already had a link pointing to the previous node? No problem! Create a doubly linked list.

Summer 2007CISC121 - Prof. McLeod20 Doubly Linked Lists public class IntDLList { private IntDLNode head; private IntDLNode tail; private class IntDLNode { private int info; private IntDLNode next; private IntDLNode prev; // new link! public IntDLNode (int aNum) { this(aNum, null, null); } public IntDLNode (int aNum, IntDLNode n, IntDLNode p) { info = aNum; next = n; prev = p; } } // end IntDLNode // IntDLList constructors and methods } // end IntDLList

Summer 2007CISC121 - Prof. McLeod21 Doubly Linked List – Cont. Structure: head 20 tail null 105 null next prev

Summer 2007CISC121 - Prof. McLeod22 Doubly Linked List – Cont. To add a node to the tail of the list: // better add a constructor too! public IntDLList () { head = null; tail = null; } public boolean isEmpty () { return head == null; } public void addToTail (int aNum) { if (!isEmpty()) { tail = new IntDLNode(aNum, null, tail); tail.prev.next = tail; } else { head = new IntDLNode(aNum); tail = head; } } // end addToTail

Summer 2007CISC121 - Prof. McLeod23 Doubly Linked List – Cont. dLList.addToTail(-10); head 20 tail null 105 null head 20 tail null 105 null -10 null head 20 tail null null -10 null After “ new …”: After tail.prev.next = tail; After tail=…;

Summer 2007CISC121 - Prof. McLeod24 Doubly Linked List – Cont. To remove the tail node: public int removeFromTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { // one node in list head = null; tail = null; } else { tail = tail.prev; tail.next = null; } } // end if return i; } // end removeFromTail

Summer 2007CISC121 - Prof. McLeod25 Doubly Linked List – Cont. int temp = dLList.removeFromTail(); head 20 tail null null After tail = tail.prev; Before head 20 tail null null After tail.next = null; head 20 tail null 105 null -10 null temp is -10

Summer 2007CISC121 - Prof. McLeod26 Doubly Linked List – Cont. Now the removeFromTail method is much easier. So, adding or deleting head or tail nodes is “easy” - which operations will require iteration? Any operation that involves a node other than the head or tail!

Summer 2007CISC121 - Prof. McLeod27 Sample Code See IntDLList.java, for example.

Summer 2007CISC121 - Prof. McLeod28 A Few Variations on Linked Lists Circular Lists Skip Lists Self-Organizing Lists

Summer 2007CISC121 - Prof. McLeod29 Circular Lists In a circular list, the last node’s “ next ” value is no longer null – it is linked to the head node. A variable like “ current ” is needed to point to one of the nodes: current

Summer 2007CISC121 - Prof. McLeod30 Circular Lists - Cont. For a doubly linked list: current

Summer 2007CISC121 - Prof. McLeod31 Circular Lists - Cont. While this design seems “elegant”, it is really no improvement over the singly and doubly linked lists, with head and tail, described above. You might use this to model a data structure that does not have a beginning or an end. The structure just grows and shrinks in size. (?) But the next two List variations, on the other hand…

Summer 2007CISC121 - Prof. McLeod32 Skip Lists The biggest problem with linked lists is that they require iteration to locate elements that are not at the head or tail of the list. Even if nodes are ordered, a sequential search is still required. Skip lists were suggested in 1990 as a way to speed up searching. Structured in such a way that every second node contains a link that points two positions ahead, every fourth node has a link to a node four positions ahead, and so on. Each node will contain an array of links:

Summer 2007CISC121 - Prof. McLeod33 Skip Lists – Cont. Node class definition: public class IntSkipListNode { public int info; public IntSkipListNode[] next; public IntSkipListNode (int aNum, int n) { info = aNum; next = new IntSkipListNode[n]; for (int i = 0; i < n; i++) next[i] = null; } } // end IntSkipListNode

Summer 2007CISC121 - Prof. McLeod34 Singly linked skip list: In order to search the skip list, the nodes must be in order by some attribute value. Searching starts by skipping along the highest order links, and then by moving down into the lower order links when the upper order link moves past the target value. Searching is now like the binary search. Skip Lists – Cont

Summer 2007CISC121 - Prof. McLeod35 Skip Lists – Cont. Consider what needs to happen when a node is added or deleted! If the order of links is maintained, then all nodes on one side of the skip list have to be changed. This is very time consuming. If the node is just inserted at the lowest level of the list, and all other nodes are kept the same then the link level order is not maintained. Eventually this will reduce the searching speed to be the same as a sequential search, and the list behaves just as a singly linked list.

Summer 2007CISC121 - Prof. McLeod36 Self-Organizing Lists The idea here is to impose some organizational scheme on the list, in order to speed up searching. Many different organizations can be used, depending on the nature of the data to be stored in the list.

Summer 2007CISC121 - Prof. McLeod37 Self-Organizing Lists - Cont. Examples of organizations: –“Move to front” – when the desired element is located, move it to the front of the list. –“Transpose” – when the element is located, swap it with its predecessor, unless it is already at the head of the list. –“Count” – Order the list by the number of times elements are being accessed. –“Ordering” – Order by some criteria from the information being stored in the list. Choice of organization depends on the how often new elements are added to the list and how often they are accessed.

Summer 2007CISC121 - Prof. McLeod38 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.

Summer 2007CISC121 - Prof. McLeod39 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!

Summer 2007CISC121 - Prof. McLeod40 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

Summer 2007CISC121 - Prof. McLeod41 1

Summer 2007CISC121 - Prof. McLeod42 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?

Summer 2007CISC121 - Prof. McLeod43 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.

Summer 2007CISC121 - Prof. McLeod44 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.

Summer 2007CISC121 - Prof. McLeod45 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.

Summer 2007CISC121 - Prof. McLeod46 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.

Summer 2007CISC121 - Prof. McLeod47 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.

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

Summer 2007CISC121 - Prof. McLeod49 Linked Lists - Summary Linked lists really take advantage of Java’s use of Objects, by creating a structure based on pointers. A structure that can be easily tailored to suit the needs of a particular data structure. Only uses the space it needs, no empty data nodes, does not need contiguous memory. Remember to compare linked lists to arrays when choosing a data structure - advantages and disadvantages. (Hint: use diagrams to help write linked list code!)

Summer 2007CISC121 - Prof. McLeod50 Stacks What is a stack? For example a “PEZ” candy container:

Summer 2007CISC121 - Prof. McLeod51 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.

Summer 2007CISC121 - Prof. McLeod52 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?

Summer 2007CISC121 - Prof. McLeod53 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

Summer 2007CISC121 - Prof. McLeod54 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 ()

Summer 2007CISC121 - Prof. McLeod55 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.

Summer 2007CISC121 - Prof. McLeod56 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.

Summer 2007CISC121 - Prof. McLeod57 Array Stack Implementation See ArrayStack.java A bit clumsy? Of the three implementations, which is the best?

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

Summer 2007CISC121 - Prof. McLeod59 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.

Summer 2007CISC121 - Prof. McLeod60 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

Summer 2007CISC121 - Prof. McLeod61 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.

Summer 2007CISC121 - Prof. McLeod62 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 provides better information hiding than the Stack class.

Summer 2007CISC121 - Prof. McLeod63 Queues A queue is just a lineup, like at your favorite movie theatre. It would use the “FIFO” principle – “First In, First Out”. It would have the following operations: –clear() – clear the queue. –isEmpty() – check to see if the queue is empty. –isFull() – check to see if the queue is full. –enqueue(element) - put the element at the end of the queue. –dequeue() – take the first element from the queue. –firstEl() – return the first element in the queue without removing it.

Summer 2007CISC121 - Prof. McLeod64 Aside: “isFull()” Our implementations of stacks and queues do not have an “ isFull() ” method, because they do not need one. If you did need such a method to model a stack or queue that is limited in size, how would you do it? Suppose the maximum size is provided when the stack or queue is created (in the constructor).

Summer 2007CISC121 - Prof. McLeod65 Queues - Cont. Does a linked list implementation of a queue require both head and tail links? If a singly linked list is used would you enqueue to the head or the tail? Why? YUP! - enqueue to tail! - easier to remove head node than to remove tail node in singly linked list for dequeue operation

Summer 2007CISC121 - Prof. McLeod66 Queues – Cont. A queue can easily be implemented using a singly linked list with a head and tail. (See IntQueue.java, for example) A queue, in code, is often used as part of a model of a real-world process. In the “real-world” queues are everywhere – Airport runways, McDonalds, banks, assembly lines, etc.

Summer 2007CISC121 - Prof. McLeod67 Queues – Cont. How would you implement a queue with an ArrayList or an array? Not so easy, right? A “circular array” can be used, with variables that point to the first and last occupied positions in the array. If “last” moves to the end of the array, it can be wrapped back to the beginning. Linked lists really are the best way to implement a queue.

Summer 2007CISC121 - Prof. McLeod68 Queues – Priority Queue A Priority Queue is just a queue where the elements are also given a priority. In this case, an element with a higher priority can be removed before the element that is “first” in the the queue, when the first element is of a lower priority. (Like how an ambulance gets through traffic…)

Summer 2007CISC121 - Prof. McLeod69 Back to Stacks - Activation Frames Stacks are an integral part of computer architecture. For example, Java byte code is run by the “Java Virtual Machine”, or “JVM”. The JVM is stack – based. Each thread (or process…) in the JVM has its own private run-time stack in memory (our programs are “single threaded”). Each run-time stack contains “activation frames” – one for each method that has been activated. Only one activation frame (or method) can be active at a time for each thread. An activation frame is created, or “pushed”, every time a method is invoked. When the method is completed, the frame is popped off the stack.

Summer 2007CISC121 - Prof. McLeod70 Activation Frames An activation frame (or “stack frame” or “activation record”) contains (among other things): –All local variables. –A link to the frame of the calling method, so that control can be returned to line of code in the caller immediately after the method call. –Information for catching exceptions. –An “operand stack”, which is another stack that is used by the JVM as a source of arguments and a repository of results. An understanding of how this particular stack works will help to explain how recursive methods work…

Summer 2007CISC121 - Prof. McLeod71 Demonstration Run “RunTimeStackDemo.java” in debug mode, stepping though method calls and observe thread stack display.

Summer 2007CISC121 - Prof. McLeod72 Summary - Stacks & Queues We will use the thread stack to help explain how recursion works - later... Stacks & Queues are often used to model real- world processes. Providing an easy-to-use stack or queue object makes the modeling effort easier. Now we have to move on to something completely different!