1 / 32

CS210- Lecture 6 Jun 13, 2005

CS210- Lecture 6 Jun 13, 2005. Announcements Assignment 2 has been posted. Part 1: Programming - Due on 6/19 (Sunday) Part 2: Problems – Due on 6/21 (Tuesday). Agenda. Linked Lists Linked List implementation of Stack Linked List implementation of Queue Double Ended Queue

wmele
Télécharger la présentation

CS210- Lecture 6 Jun 13, 2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS210- Lecture 6Jun 13, 2005 • Announcements • Assignment 2 has been posted. • Part 1: Programming - Due on 6/19 (Sunday) • Part 2: Problems – Due on 6/21 (Tuesday) CS210-Summer 2005, Lecture 6

  2. Agenda • Linked Lists • Linked List implementation of Stack • Linked List implementation of Queue • Double Ended Queue • Implementing a deque with a doubly linked list • Implementing a Stack with doubly linked list • Vectors CS210-Summer 2005, Lecture 6

  3. Singly Linked Lists • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores • element • link to the next node next node elem head  A B C D CS210-Summer 2005, Lecture 6

  4. Inserting at the head • Allocate a new node • Insert new element • Have new node point to old head • Update head to point to new node CS210-Summer 2005, Lecture 6

  5. Update head to point to next node in the list Allow garbage collector to reclaim the former first node Removing at the head CS210-Summer 2005, Lecture 6

  6. Inserting at the tail • Allocate a new node • Insert new element • Have new node point to null • Have old last node point to new node • Update tail to point to new node CS210-Summer 2005, Lecture 6

  7. Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node. Removing at tail CS210-Summer 2005, Lecture 6

  8. Analyzing the Singly Linked List Method Time Inserting at the head O(1) Inserting at the tail O(1) Deleting at the head O(1) Deleting at the tail O(n) CS210-Summer 2005, Lecture 6

  9. Singly Linked List implementation of Stack • We can implement a stack with a singly linked list by inserting and removing elements at head. • The top element is stored at the first node of the list. • The space used is O(n) and each operation of the Stack ADT takes O(1) time. CS210-Summer 2005, Lecture 6

  10. “A” Singly Linked List implementation of Stack top null push (“A”) top null push (“B”) top null “B” “A” CS210-Summer 2005, Lecture 6

  11. Singly Linked List implementation of Stack top null pop() “A” top pop() null CS210-Summer 2005, Lecture 6

  12. Singly Linked List implementation of Queue • We can implement a queue with a singly linked list • The front element is stored at the first node (head) • The rear element is stored at the last node (tail) • We insert at tail and remove at head • Why would it be bad to insert at the head and remove at the tail ? • The space used is O(n) and each operation of the Queue ADT takes O(1) time CS210-Summer 2005, Lecture 6

  13. “A” “A” “B” Singly Linked List implementation of Queue head null tail enqueue (“A”) head null tail enqueue (“B”) head null CS210-Summer 2005, Lecture 6

  14. “B” Singly Linked List implementation of Queue tail dequeue () head null head dequeue () null CS210-Summer 2005, Lecture 6

  15. Double Ended Queues (Deque) • Deque is a queue like data structure that supports insertion and deletion at both the front and rear of the queue. • Also known as “deck” to avoid confusion with the dequeue method of the regular queue ADT. CS210-Summer 2005, Lecture 6

  16. The Deque ADT • insertFirst(o): Insert a new object o at the beginning of the deque. • insertLast(o): Insert a new object o at the end of the deque • removeFirst(): Remove and return the first element of the deque. • removeLast(): Remove and return the last element of the deque. CS210-Summer 2005, Lecture 6

  17. Implementing a Deque with a Doubly Linked List • As deque requires insertion and deletion at both ends of a list, using a singly linked list would be inefficient. • There is another kind of linked list that allows us to insert and remove elements at both ends in O(1) time – doubly linked list • Node in a double linked list has two references – a next link and a prev link. CS210-Summer 2005, Lecture 6

  18. header trailer A B Doubly Linked List prev next elem DLNode CS210-Summer 2005, Lecture 6

  19. Doubly Linked List • Header and Trailer are dummy or sentinel nodes. These do not store any element. • The header has a valid next reference but a null prev reference. • The trailer has a valid prev reference but a null next reference. CS210-Summer 2005, Lecture 6

  20. Inserting at the head of DLL header trailer A B C CS210-Summer 2005, Lecture 6

  21. Deleting at the tail of DLL header trailer A B CS210-Summer 2005, Lecture 6

  22. Insert at the tail of DLL header trailer p A B C CS210-Summer 2005, Lecture 6

  23. Deleting at the head of DLL header trailer A B Analysis CS210-Summer 2005, Lecture 6

  24. Implementing Stack using a DLL top null top push (“A”) null null “A” top push (“B”) null null “B” “A” CS210-Summer 2005, Lecture 6

  25. Implementing Stack using a DLL top pop () null null “A” top null pop () CS210-Summer 2005, Lecture 6

  26. Vector, List and Sequence ADT • Suppose we have a collection S of n elements stored in a certain linear order, so that we can refer to the elements is S as first, second and third so on. • Such a collection is generically referred to as a sequence. • Vector, List and Sequence ADTs are sequences. CS210-Summer 2005, Lecture 6

  27. Vector, List and Sequence ADT • Each represents a collection of linearly arranged elements and provides methods for accessing, inserting and removing arbitrary elements. • These sequences differ in the ways in which the different operations are defined. • Stacks, Queues and Deques can be viewed as restricted types of sequences that access only first and/or last elements. CS210-Summer 2005, Lecture 6

  28. Vectors and Array Lists • A vector which is also known as array list, is an abstraction and extension of the concrete array data structure. • It provides accessor methods that can index into the middle of a sequence and it also provides update methods for adding and removing elements by their indices. • We typically use the term rank to refer to the index of an element in a vector. CS210-Summer 2005, Lecture 6

  29. Vectors and Array Lists • We define rank of an element e in S to be the number of elements that are before e in S. • First element in S has rank 0 and the last element has rank n – 1. CS210-Summer 2005, Lecture 6

  30. Vector ADT • elemAtRank(r): return the element of S with rank r. Error occurs if r < 0 and r > size() - 1 . • replaceAtRank(r, e): Replace with e and return the element at rank r. • insertAtRank(r, e): Insert a new element e into S to have rank r. When will error occur? • removeAtRank(r): Remove from S the element at rank r. CS210-Summer 2005, Lecture 6

  31. Realization of Deque by means of Vector CS210-Summer 2005, Lecture 6

  32. Simple Array based implementation of Vector Shifting up for an insertion at rank r ( O(n) ) S 0 1 2 r n -1 N -1 Shifting down for a removal at rank r ( O(n) ) S 0 1 2 r n -1 N -1 CS210-Summer 2005, Lecture 6

More Related