CS210- Lecture 6 Jun 13, 2005 Announcements

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
Queues and Linked Lists
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Elementary Data Structures
Cpt S 122 – Data Structures Abstract Data Types
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
Double-Ended Queues Chapter 5.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Queues Queues Queues.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Sequences and Iterators
Stacks, Queues, and Deques
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
Vectors, Lists, and Sequences
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Stacks and Linked Lists
Problem Understanding
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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