CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Queues and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List Variations
CSE Lecture 12 – Linked Lists …
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
CS Winter 2011 Further Introduction to the C programming language.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
CS Winter 2011 Introduction to the C programming language.
CSSE221: Software Dev. Honors Day 29 Announcements Announcements Any questions on strings? Any questions on strings? Section 2: pass in quiz after question.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
Exam 1 Review CS Total Points – 60 Points Writing Programs – 20 Points Tracing Algorithms, determining results, and drawing pictures – 40 Points.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List by Chapter 5 Linked List by
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Linked List Implementation of the Deque
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS261 Data Structures Linked Lists - Introduction.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List Introduction
Linked List Stacks, Linked List Queues, Dequeues
Sequences 6/18/2018 8:51 PM C201: Linked List.
Doubly Linked List Review - We are writing this code
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Top Ten Words that Almost Rhyme with “Peas”
Exam 1 Review CS 3358.
Linked Lists: Implementation of Queue & Deque
Topic 16 Queues "FISH queue: n.
Exam 1 Review CS 3358.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Dynamic Array: Implementation of Queue & Deque
Chapter 9 Linked Lists.
Presentation transcript:

CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag

Three New Variations Today we are going to continue to explore Linked Lists with three new variations Double links - links both forwards and backwards Sentinel - a Special marker link at end Keep size as part of the structure Close to the way standard libs do it

Double Links Double links point both forwards and backwards. Allow access to both next and previous link struct link { EleType value; struct link * next; struct link * previous; };

Picture of List with double link

2nd Variation: Sentinels A Sentinel is a special marker node at front or back Has no value, is never removed Can remove special case code, since pointer is never null An Empty list still has a sentinel node

Picture of List with Sentinel

If one Sentinel is good Just to make the LinkedList slightly more interesting, we will add a sentinel to BOTH front and back Eliminates even more special cases most Real LinkedList use two sentinels, double links, and more or less combines all the interfaces. (Java, C++)

Keep size as a field in the header Maintain the size value explicitly in header. Can you think why we want to do it this way? struct linkedList { struct link * frontSentinel; struct link * backSentinel; int size; };

Deque Why would you use a sentinel? Consider a deque, with pointer to front and pointer to sentinel How do you access the front of the queue? How do you access the back? Draw pictures, name the values you are looking for

Generalize add What about adding values Remember, for a queue, can add either to the front OR to the back Add to front and add to back are now special cases of more general add After operation.

Deque code void listDequeAddBack (struct list *q, EleType newValue) { _addLinkAfter(q, q->frontSentinel, newValue); } void listDequeAddFront (struct list *q, EleType newValue) { _addLinkAfter(q, q->backSentinel->prev, newValue); }

Are there any special cases? Draw pictures Does this work even if the list is empty? Always try to imagine special cases, make sure your code works for them

Both a stack and a queue To use a deque as a stack, use addFront and removeFront To use as a queue, use addBack and removeFront (or vice versa) Works for both equally well

What does add do? Needs to add a new link to a chain, right before the link given as the argument. Draw a picture. Label the names of everything you know. Think through the steps. Why are we passing pointer to header? You will get to do this in the worksheet.

Removes also generalized void listRemoveFirst (struct list *q) { assert(! listIsEmpty(q)); _removeLink (q, q->firstLink); } void listRemoveLast (struct list *q) { assert(! listIsEmpty(q)); _removeLink (q, q->sentinel->prev); }

Again, draw pictures Draw a picture to see what you are doing. Label the names for things. What are the steps? Do they need to be done in a particular order? Why are we passing the header node to the removeLink routine?

You will get your chance You will get your chance to write addLink and removeLink in a moment. The same removeLink idea can be used to implement the remove operation in the LinkedListBag Remember from yesterday, remove was the only complicated operation

Remember the Bag operations Add (already done) Contains (same as before, walk down the links, looking at every element) Remove (walk down the links, when you find the value you want, call removeLink) Size (this one is easy)

Now your chance Questions? Doubly linked list is used in programming assignment 3. Mainly you need to write addLink and removeLink

Oh, that exam on Tuesday? Questions on big-Oh (what is the running time of …) Implementation questions like we have been doing (how to implement … )