Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists."— Presentation transcript:

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

2 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!

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

4 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

5 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

6 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?

7 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?)

8 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?

9 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

10 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?

11 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!

12 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”

13 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

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

15 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.

16 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

17 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:

18 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

19 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.

20 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

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

22 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

23 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 105-10 null -10 null After “ new …”: After tail.prev.next = tail; After tail=…;

24 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

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

26 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!

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

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

29 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: 1510520 current

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

31 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…

32 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:

33 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

34 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. 1 3 5 6 7 8 9 10

35 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.

36 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.

37 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.

38 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.

39 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!

40 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

41 Summer 2007CISC121 - Prof. McLeod41 1

42 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?

43 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.

44 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.

45 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.

46 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.

47 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.

48 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.

49 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!)

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

51 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.

52 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?

53 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

54 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 ()

55 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.

56 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.

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

58 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

59 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.

60 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

61 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.

62 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.

63 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.

64 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).

65 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

66 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.

67 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.

68 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…)

69 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.

70 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…

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

72 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!


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

Similar presentations


Ads by Google