320 likes | 347 Views
Learn about linked list and deque implementations for stacks and queues in programming. Explore insertion, deletion, and analysis of singly and doubly linked lists.
E N D
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
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
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
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
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
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
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
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
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
“A” Singly Linked List implementation of Stack top null push (“A”) top null push (“B”) top null “B” “A” CS210-Summer 2005, Lecture 6
Singly Linked List implementation of Stack top null pop() “A” top pop() null CS210-Summer 2005, Lecture 6
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
“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
“B” Singly Linked List implementation of Queue tail dequeue () head null head dequeue () null CS210-Summer 2005, Lecture 6
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
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
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
header trailer A B Doubly Linked List prev next elem DLNode CS210-Summer 2005, Lecture 6
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
Inserting at the head of DLL header trailer A B C CS210-Summer 2005, Lecture 6
Deleting at the tail of DLL header trailer A B CS210-Summer 2005, Lecture 6
Insert at the tail of DLL header trailer p A B C CS210-Summer 2005, Lecture 6
Deleting at the head of DLL header trailer A B Analysis CS210-Summer 2005, Lecture 6
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
Implementing Stack using a DLL top pop () null null “A” top null pop () CS210-Summer 2005, Lecture 6
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
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
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
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
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
Realization of Deque by means of Vector CS210-Summer 2005, Lecture 6
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