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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Singly linked lists Doubly linked lists
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
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 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
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.
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
Lecture 6 of Computer Science II
Doubly Linked List Review - We are writing this code
Programming Abstractions
Stack and Queue APURBO DATTA.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
Programming Abstractions
Chapter 4 Linked Lists.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Data Structures & Programming
Presentation transcript:

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 data structures: insert, remove, find_max, update, etc What are our choices so far to design such a data structure?

Data structures to store a collection of items What are the choices so far? Arrays Limitations fixed capacity, memory may not be fully utilized. Insert and remove can be expensive (a lot of copies) if we don’t want to leave holes in the middle of the array. Continuous memory for easy index Dynamic arrays Limitations: Capacity is dynamic, memory still may not be fully utilized, but better than static arrays. Insert and remove can be expensive, especially when the capacity changes. Continuous memory for easy index

Data structures to store a collection of items Linked list is another choice. A true dynamic data structure in that each item in the list is dynamically allocated using a new operator. Capacity is always the same as memory used (with tax) Insert and remove operations are cheap Memory are not continuous Limitations: no (or expensive) [] operator. Linked list is one of the “linked data structures”.

Linked list and array An array of string: S[0] = “abc”; S[1]=“white”; S[2] = “black”; A linked list of strings Each item has two fields A string field A pointer pointing to the next item. class listofstring { public: string item; listofstring *next; }; 0 “abc” 1 “white” 2 “black” 3 4 “abc” “white” “black” NULL head

Linked list No waste of memory (except for pointers). Each box is dynamically allocated by a new operation. Several variations Singly linked list Doubly linked lists “abc” “white” “black” NULL head NULL “abc” “white” “black” NULL prev item next head

A doubly linked list Let us assume that we store two data fields in each node: a string and a count. The node data structure is: class listnode { public: string s; int count; listnode *next; listnode *prev; listnode(): s(“”), count(0), next(NULL), prev(NULL) {}; listnode(const string & ss, const int &c): s(ss), count( c), next(NULL), prev(NULL) {}; }; s count prev next

The doubly linked list private data Protect data: head: pointer to the head of the list: head->prev == NULL tail: pointer to the tail of the list: tail->next == NULL size: number of nodes in the list class mylist { … Private: listnode * head; listnode *tail; int size; } head NULL “abc”, 0 “whi”, 0 “bla”, 0 NULL tail

mylist public interface mylist(); ~mylist(); void print(); mylist(const mylist & l); mylist& operator=(const mylist &l); void insertfront(const string &s, const int & c); void insertback(const string &s, const int & c); void insertbefore(listnode *ptr, const string &s, const int &c); void insertafter(listnode *ptr, const string &s, const int &c); void insertpos(const int & pos, const string &s, const int &c);

mylist public interface void removefront(); void removeback(); void remove(listnode * ptr); void removepos(const int & pos); listnode front() const; listnode back() const; int length() const; listnode *search(const string &s); listnode *findmaxcount(); void removemaxcount(); bool searchandinc (const string &s);

Mylist implementation Constructors and destructor Making an empty list (default constructor): head=tail=NULL, size = 0; (See mylist.cpp) Destructor: must use a loop to delete every single node in the list (all nodes are allocated with a new). See mylist.cpp Copy constructor and = operator: Similar logic to destructor: use a loop to walk through each node in the existing list, and insert (just insertback) the same node to the new list. The print function (see mylist.cpp) The main routines are different versions of insert, remove, and search.

Insert Insertback Two cases: Insert to the empty list Insert to list with items. Insert to empty list Create a new node (prev=NULL, next=NULL), both head and tail should point to the new node. listnode *t = new listnode(s, c); if (head == NULL) { // list is currently empty, both head and tail // should point to the new node head = t; tail = t; size++; }

Insertback Insertback to a list with items Step 1: create the new node Listnode *t = new listnode(s, c) head NULL “abc”, 0 “whi”, 0 “bla”, 0 NULL tail NULL “xxx”, 0 NULL

Insertback Insertback to a list with items Step 2: link new node to the tail of the list (next pointer) tail->next = t; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail NULL “xxx”, 0 NULL

Insertback Insertback to a list with items Step 3: link new node to the list (prev pointer) t->prev = tail; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL

Insertback Insertback to a list with items Step 4: tail point to the new node tail = t See complete code in mylist.cpp head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL

Insertbefore Insert before the head is equal to insertfront, which is similar to insertback Insertbefore into the middle of the list before ptr A new node is to be added between ptr->prev, and ptr. head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr

Insertbefore Insertbefore into the middle of the list before ptr A new node is to be added between ptr->prev, and ptr. Step 1: create the new node: listnode* t = new listnode(s,c); head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr NULL “yyy”, 0 NULL

Insertbefore Insertbefore into the middle of the list before ptr A new node is to be added between ptr->prev, and ptr. Step 1: try to chain the new node to the list t->next = ptr; t->prev = ptr->prev; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr “yyy”, 0

Insertbefore Insertbefore into the middle of the list before ptr A new node is to be added between ptr->prev, and ptr. Step 2: change ptr->prev’s next pointer ptr->prev->next = t; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr “yyy”, 0

Insertbefore Insertbefore into the middle of the list before ptr A new node is to be added between ptr->prev, and ptr. Step 3: change ptr’s prev pointer (see mylist.cpp) ptr->prev = t; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr “yyy”, 0

Insertbefore Can step 2 and step 3 change order? ptr->prev = t; ptr-prev->next = t; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr “yyy”, 0

Remove Removefront: Two cases: the list has only one element need to make empty list out of it. delete head; head = tail = NULL; size = 0; return; The list has more than on elements

Remove Removefront: The list has more than on elements Step 1: listnode *t = head; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL t

Remove Removefront: The list has more than on elements Step 2: Advance head: head = head->next; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL t

Remove Removefront: The list has more than on elements Step 3: delink the prev of head: head->prev = NULL; head NULL “abc”, 0 NULL “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL t

Remove Removefront: The list has more than on elements (see mylist.cpp) Step 4: delete t; head NULL “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL t

Removemiddle Remove an item pointed to by ptr Step 1: change ptr->prev’s next pointer ptr->prev->next = ptr->next; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr

Removemiddle Remove an item pointed to by ptr Step 2: change ptr->next’s prev pointer ptr->next->prev = ptr->prev; head NULL “abc”, 0 “whi”, 0 “bla”, 0 tail “xxx”, 0 NULL ptr

Removemiddle Remove an item pointed to by ptr Step 3: delete ptr; head NULL “abc”, 0 “whi”, 0 tail “xxx”, 0 NULL ptr

Search Use the while loop to walk through every nodes in the list (see mylist.cpp) listnode *t = head; while ((t!=NULL) && (t->s != s)) t = t->next; return t;