Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 6 Jun 13, 2005 Announcements

Similar presentations


Presentation on theme: "CS210- Lecture 6 Jun 13, 2005 Announcements"— Presentation transcript:

1 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) 4/28/2019 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 4/28/2019 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 4/28/2019 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 4/28/2019 CS210-Summer 2005, Lecture 6

5 Removing at the head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node 4/28/2019 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 4/28/2019 CS210-Summer 2005, Lecture 6

7 Removing at tail 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. 4/28/2019 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) 4/28/2019 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. 4/28/2019 CS210-Summer 2005, Lecture 6

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

11 Singly Linked List implementation of Stack
top null pop() “A” top pop() null 4/28/2019 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 4/28/2019 CS210-Summer 2005, Lecture 6

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

14 Singly Linked List implementation of Queue
tail dequeue () head “B” null head dequeue () null 4/28/2019 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. 4/28/2019 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. 4/28/2019 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. 4/28/2019 CS210-Summer 2005, Lecture 6

18 Doubly Linked List prev next elem DLNode A B header trailer 4/28/2019
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. 4/28/2019 CS210-Summer 2005, Lecture 6

20 Inserting at the head of DLL
header trailer A B C 4/28/2019 CS210-Summer 2005, Lecture 6

21 Deleting at the tail of DLL
header trailer A B 4/28/2019 CS210-Summer 2005, Lecture 6

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

23 Deleting at the head of DLL
header trailer A B Analysis Method Time size, isEmpty O(1) first, last insertFirst, insertLast removeFirst, removeLast 4/28/2019 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” 4/28/2019 CS210-Summer 2005, Lecture 6

25 Implementing Stack using a DLL
top pop () null null “A” top null pop () 4/28/2019 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. 4/28/2019 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. 4/28/2019 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. 4/28/2019 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. 4/28/2019 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. 4/28/2019 CS210-Summer 2005, Lecture 6

31 Realization of Deque by means of Vector
Deque Method Vector Method first() elemAtRank(0) last() ? insertFirst(o) insertLast(o) removeFirst() removeLast() 4/28/2019 CS210-Summer 2005, Lecture 6

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


Download ppt "CS210- Lecture 6 Jun 13, 2005 Announcements"

Similar presentations


Ads by Google