Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

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.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Data Structures: A Pseudocode Approach with C
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
Pointers OVERVIEW.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
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.
Duke CPS Recursion l Often phrased as “a function calls itself” ä function actually calls a clone, not itself ä each clone has its own variables/parameters/state.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
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.
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.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
STACKS & QUEUES for CLASS XII ( C++).
Unit – I Lists.
COMP 53 – Week Eight Linked Lists.
More Linking Up with Linked Lists
Lists CS 3358.
Plan for the Week Review Big-Oh
Exercise 1 From Lec80b-Ponter.ppt.
From data to information
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
What’s the Difference Here?
Owen Astrachan Jeff Forbes October 19, 2017
Programming Abstractions
Hashing Exercises.
Chapter 4 Linked Lists
Linked List Sudeshna Sarkar.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Final Exam on August 17th Wednesday at 16:00.
Introduction to Linked Lists
Lists.
Chapter 16-2 Linked Structures
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Programming Abstractions
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Pointers & Dynamic Data Structures
Lists.
CSC 143 Java Linked Lists.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis of common hash-table implementations (later) Similar to how trees are implemented, but simpler Linked lists as ADT Constant-time or O(1) insertion/deletion from anywhere in list, but first must get to the list location Linear or O(n) time to find an element, sequential search Like a film or video tape: splicing possible, access slow Good for sparse structures: when data are scarce, allocate exactly as many list elements as needed, no wasted space/copying (e.g., what happens when vector grows?)

Linked list applications Remove element from middle of a collection, maintain order, no shifting. Add an element in the middle, no shifting What’s the problem with a vector (array)? Emacs visits several files, internally keeps a linked-list of buffers Naively keep characters in a linked list, but in practice too much storage, need more esoteric data structures What’s (3x5 + 2x3 + x + 5) + (2x4 + 5x3 + x2 +4x) ? As a vector (3, 0, 2, 0, 1, 5) and (0, 2, 5, 1, 4, 0) As a list ((3,5), (2,3), (1,1), (5,0)) and ________? Most polynomial operations sequentially visit terms, don’t need random access, do need “splicing” What about (3x100 + 5) ?

Linked list applications continued If programming in C, there are no “growable-arrays”, so typically linked lists used when # elements in a collection varies, isn’t known, can’t be fixed at compile time Could grow array, potentially expensive/wasteful especially if # elements is small. Also need # elements in array, requires extra parameter With linked list, one pointer used to access all the elements in a collection Simulation/modelling of DNA gene-splicing Given list of millions of CGTA… for DNA strand, find locations where new DNA/gene can be spliced in Remove target sequence, insert new sequence

Linked lists, CDT and ADT As an ADT A list is empty, or contains an element and a list ( ) or (x, (y, ( ) ) ) As a picture As a CDT (concrete data type) struct Node { Node * p = new Node(); string info; p->info = “hello”; Node * next; p->next = 0; // NULL }; p

Building linked lists Add words to the front of a list (draw a picture) Create new node with next pointing to list, reset start of list struct Node { string info; Node * next; Node(const string& s, Node * link) : info(s), next(link) { } }; // … declarations here Node * list = 0; while (input >> word) { list = new Node(word, list); } What about adding to the end of the list?

Dissection of add-to-front List initially empty First node has first word Each new word causes new node to be created New node added to front Rhs of operator = completely evaluated before assignment list list A list = new Node(word,list); B Node(const string& s, Node * link) : info(s), next(link) { }

Building linked lists continued What about adding a node to the end of the list? Can we search and find the end? If we do this every time, what’s complexity of building an N-node list? Why? Alternatively, keep pointers to first and last nodes of list If we add node to end, which pointer changes? What about initially empty list: values of pointers? Will lead to consideration of header node to avoid special cases in writing code What about keeping list in order, adding nodes by splicing into list? Issues in writing code? When do we stop searching?

Standard list processing (iterative) Visit all nodes once, e.g., count them int size(Node * list) { int count = 0; while (list != 0) { count++; list = list->next; } return count; What changes in code above if we change what “process” means? Print nodes? Append “s” to all strings in list?

Standard list processing (recursive) Visit all nodes once, e.g., count them int recsize(Node * list) { if (list == 0) return 0; return 1 + recsize(list->next); } Base case is almost always empty list – NULL/0 node Must return correct value, perform correct action Recursive calls use this value/state to anchor recursion Sometimes one node list also used, two “base” cases Recursive calls make progress towards base case Almost always using list->next as argument

Recursion with pictures recsize(Node * list) return 1+ recsize(list->next) Counting recursively int recsize(Node * list) { if (list == 0) return 0; return 1 + recsize(list->next); } recsize(Node * list) return 1+ recsize(list->next) recsize(Node * list) return 1+ recsize(list->next) ptr recsize(Node * list) return 1+ recsize(list->next) cout << recsize(ptr) << endl;

Recursion and linked lists Print nodes in reverse order Print all but first node and… Print first node before or after other printing? void Print(Node * list) { if (list != 0) } Print(list->next); cout << list->info << endl; cout << list->info << endl; Print(list->next);

Changing a linked list recursively Pass list to function, return altered list, assign to passed param list = Change(list, “apple”); Node * Change(Node * list, const string& key) { if (list != 0) { list->next = Change(list->next, key); if (list->info == key) return list->next; else return list; } return 0; What does this code do? How can we reason about it? Empty list, one-node list, two-node list, n-node list Similar to proof by induction

Header (aka dummy) nodes Special cases in code lead to problems Permeate the code, hard to reason about correctness Avoid special cases when trade-offs permit Space, time trade-offs In linked lists it is useful to have a header node, the empty list is not NULL/0, but a single “blank” node Every node has a node before it, avoid special code for empty lists Header node is skipped by some functions, e.g., count the values in a list What about a special “trailing” node? What value is stored in the header node?

Header Nodes example/motivation Node * addInOrder(Node * list, const string& s) // pre: list in order (or empty) // post: node with s added to list, list in order { if (list == 0) { return new Node(s,0); } if (s <= list->info) { return new Node(s, list); // what does loop look like here?

Circularly linked list If the last node points to NULL/0, the pointer is “wasted” Can make list circular, so it is easy to add to front or back Want only one pointer to list, should it point at first or last node? How to create first node? Potential problems? Failures? // circularly linked, list points at last node Node * first = list->next; Node * current = first; do { Process(current); current = current->next; } while (current != first); list

Eric Raymond Open source evangelist The Cathedral and the Bazaar http://ot.op.org/cathedral-bazaar.html How to construct software “Good programmers know what to write. Great ones know what to rewrite (and reuse).” How to convince someone that guns are a good idea? Put this sign up: THIS HOME IS A GUN-FREE ZONE