Lesson 8: Linked List Deque

Similar documents
CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic

Big- (O): c.) binary search O(logn)

CS 261: Data Structures. Dynamic Array Queue

Worksheet 28: Skip Lists

Dynamic Data Structures

CS 261: Data Structures. Dynamic Array Queue

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

COMP 524 Spring 2018 Midterm Thursday, March 1

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

Linked Structures in C

Elementary Data Structures: Lists

CSCE 110 PROGRAMMING FUNDAMENTALS

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Writing Functions in C

Linked List Practice Questions

Linked List using a Sentinel

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Solution for Data Structure

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Ashish Gupta, Data JUET, Guna

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

Array Elements as Function Parameters

Computer Science 62. Midterm Examination

CS : Data Structures

C Programming, Autumn 2013, Exercises for the Second Week

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays


EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

Homework Assignment #1

Chapter 20: Binary Trees

Algorithms, Data Structures, and Problem Solving

Robust Programming. Style of programming that prevents abnormal termination and unexpected actions

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Arrays & Linked Lists

Linked Lists and other Dynamic Data Structures

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CS61C : Machine Structures

EECE.2160: ECE Application Programming

Phase One: Process and Semaphore Queue Modules

Ashish Gupta, Data JUET, Guna

Midterm Review. CS 211 Fall 2018

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

CS 2113 Software Engineering

lecture09: Linked Lists

SOFTWARE Ph.D. Qualifying Exam Fall 2017

CS 314 Final Fall 2012

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CMSC 341 Lecture 7 Lists

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Advanced Programming. Lists. A list is a data structure based on usage of pointers and dynamic allocation of memory.

INSERT AS A FIRST NODE

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Lecture 10 Notes Linked Lists

CS32 Discussion Week 3

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

Chapter 17: Linked Lists

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

CS61, Fall 2012 Section 2 Notes

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CPSC 261 Midterm 2 Thursday March 17 th, 2016

Lecture 16 More on Hashing Collision Resolution

CS-211 Fall 2017 Test 2 Version A November 29, Name:

Introduction to Programming in C Department of Computer Science and Engineering

Linked Lists. C Linked List

Programming II (CS300)

CS 261 Data Structures

Simple C Dynamic Data Structure

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Chapter 17: Linked Lists

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Example Final Questions Instructions

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

LINKED IMPLEMENTATION OF LIST

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

PROGRAMMAZIONE I A.A. 2017/2018

DS L9: Queues

This resource describes how to program the myrio in C to perform timer interrupts.

Lecture 10 Notes Linked Lists

Data Structures (INE2011)

Data Structures and Algorithms. Roberto Sebastiani

Transcription:

Lesson 8: Linked List Deque A linked list deque will combine features that we have examined in previous lessons. The deque will maintain a pointer to both the front and the back, use double links, and once again make use of a sentinel. But this time will have a sentinel at both the head and tail of the list: tail = head = (Sentinel) 3 7 4 1 (Sentinel) A deque, you recall, allows insertions at both the beginning and the end of the container. The interface is shown at right. Sentinels ensure the list is never completely empty. They also mean that all instructions can be described as struct ListDeque { struct DLink *head; /* Sentinel at front of list. */ struct DLink *tail; /* Sentinel at back of list. */ int cnt; /* Number of data elements in list. */ ; void initlistdeque(struct ListDeque *d); int isemptylistdeque(struct ListDeque *d); void addfrontlistdeque(struct ListDeque *d, TYPE v); void addbacklistdeque(struct ListDeque *d, TYPE v); void removefrontlistdeque(struct ListDeque *d); void removebacklistdeque(struct ListDeque *d); TYPE frontlistdeque(struct ListDeque *d); TYPE backlistdeque(struct ListDeque *d); special cases of more general routines. Because we are using a sentinel on both ends (at the front, or head, and at the end, or tail, of the list), both additions to the front and additions to the back can be described as insert a new link immediately before an existing link, where the existing link is either the link after the current front of the list (i.e., head->next) or the end of the list (or tail). The use of the previous pointer allows us to find the element prior to a link. Similarly, removal at both ends can be implemented by a single method, _removelink., that removes the link after the front (head->next) for removefront and before the end (tail->prev) for removeback. Both _addlink and _removelink use the underscore convention to indicate they are internal methods i.e., they are not declared in the header file and are not meant to be called directly from the outside world. When a value is removed from a list make sure you free the associated link fields. Use an assertion to check that allocations of new links are successful, and that you do not attempt to remove a value from an empty list. Finish the implementation of the ListDeque based on these ideas: An Active Learning Approach to Data Structures using C 1

struct DLink { TYPE val; struct DLink *next; struct DLink *prev; ; struct ListDeque { struct DLink *head; /* Sentinel at front of list. */ struct DLink *tail; /* Sentinel at back of list. */ int cnt; /* Number of data elements in list. */ ; void initlistdeque(struct ListDeque *q) { q->head = (struct DLink *)malloc(sizeof(struct DLink)); assert(q->head!= 0); q->tail = (struct DLink *)malloc(sizeof(struct DLink)); assert(q->tail!= 0); q->head->prev = 0; q->head->next = q->tail; q->tail->next = 0; q->tail->prev = q->head; q->cnt = 0; /* Add before the link, l*/ void _addlink(struct DLink *l, TYPE v) { struct Dlink *newlink = (struct Link *) malloc(sizesof(struct DLink)); assert(newlink!= 0); newlink->val = v; newlink->next = l; newlink->prev = l->prev; l->prev->next = newlink; l->prev = newlink; void addfrontlistdeque(struct ListDeque *q, TYPE v) { _addlink(q->head->next, v); q->cnt++; void addbacklistdeque(struct ListDeque *q, TYPE v) { _addlink(q->tail, v); q->cnt++; An Active Learning Approach to Data Structures using C 2

void _removelink(struct DLink *l) { l->next->prev = l->prev; l->prev->next = l->next; free(l); void removefrontlistdeque(struct ListDeque *q) { assert(!isemptylistdeque(q)); _removelink(q->head->next); q->cnt--; void ListDequeRemoveBack(struct ListDeque *q) { assert(! ListDequeIsEmpty(q)); _removelink(q->tail->prev); q->cnt--; TYPE frontlistdeque(struct ListDeque *q) { assert(!isemptylistdeque(q)); return q->head->next->value; TYPE backlistdeque(struct ListDeque *q) { Assert(!isEmptyListDeque(q)); Return q->tail->prev->value; int isemptylistdeque(struct ListDeque *q) { return(q->cnt == 0)); An Active Learning Approach to Data Structures using C 3

On Your Own 1. Explain how the use of a tail sentinel allows us to view insertions at both the front and the back of the deque as special cases of the same operation. What is this operation? Since we have a tail, we can simply insertbefore it since we ll always have it. The tail also guarantees that we always have a next for the head, even when the list is empty, so we can always insert in front of head->next(); 2. What are the algorithmic time complexities of the deque operations in this implementation? They are all O(1) 3. Suppose you wanted to test your implementation of the linked list deque. What would be good boundary test cases? Write a test harness to execute the linked list deque using your test cases. As usual, you would want to test addfront, removefront, addback and removeback for empty lists as well as lists with at least one element in them. You would also want to test front() and back() with empty lists as well as lists with at least one element in them. You would also want to write test where you put several elements in the list, remove them all and then try to perform each of the above operations. An alternative to the use of both a head and tail sentinel is the use of a single sentinel value for both positions. This is shown in the following illustration. Now there are no null pointers whatsoever, as every link points to another link. Insertions and removals from the front of the deque are performed to one side of the sentinel, while insertions and removals from the back are performed on the opposite side. This structure is termed a circular deque. An Active Learning Approach to Data Structures using C 4

(Sentinel) An Active Learning Approach to Data Structures using C 5