[Chapter 4; Chapter 6, pp. 265-271] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp. 265-271]

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

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.
Linked Lists Linked Lists Representation Traversing a Linked List
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.
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Double Linked List Operations Dr. David Tsai 2010/4/12.
Linked List Variations
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Data Structures Using C++1 Chapter 5 Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
COMP x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Data Structure & Algorithms
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
C++ Programming:. Program Design Including
Linked List Introduction
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked List Variations
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
The Bag and Sequence Classes with Linked Lists
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Linked Lists: Implementation of Queue & Deque
Chapter 18: Linked Lists.
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
Lists CSE 373 Data Structures.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
LINKED LISTS.
Introduction to C++ Linear Linked Lists
Lists CSE 373 Data Structures.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Chapter 9 Linked Lists.
Presentation transcript:

[Chapter 4; Chapter 6, pp. 265-271] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp. 265-271]

Header Nodes Insert and Delete at the front of a linked list had to be handled as a special case (no “previous” node) One solution: add a header node Empty list represented by header node alone List representation in IntSet.h private: Node * hdr;//pointer to header node

Header Nodes Initialization in IntSet.cpp IntSet::Intset( ) { // create empty list hdr = new Node; hdr->next = NULL; } Remaining operations much as before

Circular Lists Sometimes there’s no reason to distinguish a particular node as the “first” node in the list Unordered data (sets) Data is list of items to be processed, but order doesn’t really matter (read sensors, update display) Idea: next field in “last” node points back to “first” node. Need to keep an external pointer to some node in the list.

Circular Lists Example: cp points to node in non-empty circular list of ints. Compute the sum of the node values. Node * cp; // ptr to one list Node Node * p; // ptr to next // unprocessed Node // process first node int sum = cp->val; p = cp->next; // process remaining nodes while (p != cp) { sum = sum + p->val; p = p->next; }

Doubly Linked Lists It’s easy to move forward in a single-linked list. Moving backward is expensive If access to a previous node is needed frequently, it can be done at the cost of an extra pointer in each node. // node for double-linked list of ints struct Dnode { int val; // node data Dnode * next;// ptr to next node Dnode * prev;// ptr to previous node } Dnode * dlist;// double-linked list

Delete (Double-Linked List) If p points to node in the middle of a double-linked list, that node can be deleted as follows // delete node *p p->prev->next = p->next; p->next->prev = p->prev; delete p; // optional--delete if this // node is no longer needed

Delete (Double-Linked List) If p points to node at the beginning or end of a list, the previous code fails (why?). Fixed version: // delete node *p (even if at front or // end) if (p->prev == NULL)//adjust previous node dlist = p->next; else p->prev->next = p->next; if (p->next != NULL) //adjust next node p->next->prev = p->prev // delete node (if needed) delete p; How would the code change if the list had a header node?

Insert (Doubly-Linked List) If p points to a node in the middle of a doubly-linked list, this will insert 42 in the list immediately after node p. Dnode * q = new Dnode; q->val = 42; q->next = p->next; q->prev = p; p->next->prev = q; p->next = q; As with delete, additional code is needed if p points to the last node in the list. Does order of assignment statements matter?

Combinations These techniques can be used in various combinations single vs double linked circular vs non-circular header vs no header node Choice depends on application, what operations are needed, which ones need to be fast New example: Circular, double-linked list with header node

Empty List (circular dll+hdr) An empty list is represented by a single header node that points to itself. Dnode * dl = new Dnode; dl->next = dl; dl->prev = dl;

Insert (circular dll+hdr) Insert has no special cases Processing is the same for empty/nonempty list, at front or back, etc. Example: insert new node containing 17 after node referred to by pointer p. Dnode * p Dnode * q = new Dnode; q->val = 17; q->next = p->next; q->prev = p; p->next->prev = q; p->next = q;