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.

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.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
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.
CHP-5 LinkedList.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
CS Data Structures Chapter 4 Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
More Linking Up with Linked Lists
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked lists.
Abstract Data Types Sparse Matrices CSCI 240
Programmazione I a.a. 2017/2018.
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
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.
Review & Lab assignments
Linked lists.
Presentation transcript:

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 be organized in ascending or descending order based on some specific piece of information called a key List header contains : a link to the first node A link to the last node Other optional info (size) List is made up of items called a node which contains: The key Other pertinent information A field called a link that indicated the location of the next node

Unordered List HeadTailSize 4 AddAtFront() AddAtTail() TakeFromFront() TakeFromTail()

Unordered Lists Stacks and Queues are special cases of unordered lists An unordered list where only AddAtFront and TakeFromFront are used is a stack An unordered list where only AddAtFront() andTakeFromRear() are used is a queue

Ordered List HeadTailSize Key Info Link Insert() Take() Delete() Find()

Representations Static usually used on systems without dynamic allocation Array Array based list of static nodes Struct Dynamic Array Collection of dynamically allocated nodes use when you have no idea of how big or small the list will ultimately be

Static Array Based int front, rear, cursor; item list[MAXSIZE] ; Problem: a lot of data movement especialy on inserts or add at front Static Struct Based typedef struct { int front, rear, cursor; item data[MAXSIZE]; } list Problem: same as above

Array of Nodes typedef struct { item v; int next; } node; typedef struct { int front, freelist, cursor; node data[MAXSIZE]; } list;

lets say that an item is just an int so a node is just a struct of two ints and the list is an array where each element is two ints (it kinda looks like a 2 dim array), but we will treat it as if it were two lists; a list of unused nodes and a list of nodes in use next front free cursor to initialize the list(s) as empty, link all of the nodes to gether set free to point at the first free node and set front to -1 to indicate that the list is empty. There are two lists; a list of free nodes that has all of the nodes and a empty list that has no nodes

to insert a node next front free cursor insert(7)take the free pointer and put it in front take the next pointer of the front node an put it in free replace the next pointer of the front node with -1 to indicate it is the end of the list put the data (7) in the front node Now the list has one node in it and the free list is one node shorter 7

allocate Lets call this process of moving a node from the free list to the list allocate; it should always move the node at the front of the freelist to wherever a new piece of data is to be added (front, back or middle i.e. between two nodes). Every insert should start off by allocating a node, then inserting it into the list and finally putting the data into it This is exactly like using malloc to allocate dynamic node from the system heap, malloc and free allow the OS to manage the memory. In the absence of an OS (embedded systems) you have to do this yourself

deallocate This is the process of moving a node from the list back to the free list and is functionally the same as the free operation used to move unneeded memory back to the system heap. In the case of deleting a node from the list the process consists of two steps; find the node to be deleted then deallocate it Note that deleting a node will effect the next pointers of the node preceding it and that the list will now be one node shorter. The deallocated node should always be added at the front of the free list making it one node longer

Linked List Improvements Header Node if instead of using a variables for the front pointer we use a node that contains no information (dumy node) that just points to the first node then insertion into an empty list and insertion at the end of the list look the same and eliminate one of the 3 special cases (front, back and middle) that need to be checked for

front dummy cursor = id->front; while (id->Nodes[cursor].next != NULL) cursor = id->Nodes[cursor]. next; //insert after cursor t = allocate(); id->Nodes[t].next = NULL; id->Nodes[cursor].next = t; front dummy cursor See how the empty case and the end case look the same…

Another Improvement Make the list circular with a dummy header node If the dummy header node initially points at itself then the insertion of the first node looks like its between the front and back List is empty when front = dummy.next

front dummy cursor front dummy cursor this again simplifies insertion and deletion as the action is always taking place between two nodes cursor = id->front; while (id->Nodes[cursor].next != NULL) cursor = id->Nodes[cursor]. next; //insert after cursor t = allocate(); id->Nodes[t].next = NULL; id->Nodes[cursor].next = t;

Doubly Linked Lists each node has two pointers one to the predecessor one to the successor this simplifies insert and delete as you always have a pointer to the predecessor you dont have to drag an extra pointer behind the cursor to keep track where the predecessor is The cost of this is one additional pointer per node two additional assignment instructions

prev next t cursor // inserting t = allocate(id); pv = id->Nodes[cursor].prev ; id->Nodes[pv].next = t; id->Nodes[cursor].prev = t ; id->Nodes[t].next = cursor ; id->Nodes[t].prev = pv // deleting pv = id->Nodes[cursor].prev ; nx = id->Nodes[cursor].next ; id->Nodes[pv].next = nx ; id->Nodes[nx].prev = pv ; deallocate(cursor) cursor

DLL Improvements Make it circular eliminates special cases for empty list and adding at the end of list

Multilists if a list needs to be sorted by different key values for various reasons add an additional next pointer for each sorting insert then inserts a node into several list at the same time can get very complicated very quickly but can be very useful if you need data sorted multiple ways

Sparse Arrays Another use for linked lists is to represent sparse arrays needs two forward links per node one for row pointer one for column pointer

typedef enum {head,entry} tagfield; typedef struct matrixNode *matrixPointer; typedef struct entryNode { int row; int col; int value; }; typedef struct matrixNode { matrixPointer down; martixPointer right; tagfield tag; union { matrixPointer next; entryNode entry; } u; matrixPointer hdnode[MAXSIZE];