Data Structures Using C++1 Chapter 5 Linked Lists.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

Linked Lists Geletaw S..
JAVA & Linked List Implementation
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Data Structures: A Pseudocode Approach with C
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
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.
Linked Lists Dr. Youssef Harrath
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Data Structures Using Java1 Chapter 4 Linked Lists.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 3)
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
1 What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first”
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
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: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 16: Linked Lists.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
Linked lists.
LINKED LISTS CSCD 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.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Yan Shi CS/SE 2630 Lecture Notes
Linked lists.
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Applications of Arrays
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Data Structures Using C++1 Chapter 5 Linked Lists

Data Structures Using C++2 Chapter Objectives Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list

Data Structures Using C++3 Chapter Objectives Learn how to construct a doubly linked list Discover how to use the STL container list Learn about linked lists with header and trailer nodes Become aware of circular linked lists

Data Structures Using C++4 Linked Lists Definition: a list of items, called nodes, in which the order of the nodes is determined by the address, called the link, stored in each node. Every node in a linked list has two components: one to store the relevant information (the data); and one to store the address, called the link, of the next node in the list.

Data Structures Using C++5 Linked Lists The address of the first node in the list is stored in a separate location, called the head or first. The data type of each node depends on the specific application—that is, what kind of data is being processed; however, the link component of each node is a pointer. The data type of this pointer variable is the node type itself.

Data Structures Using C++6 Linked Lists Structure of a node Structure of a linked list

Data Structures Using C++7 Linked Lists: Some Properties The address of the first node in a linked list is stored in the pointer head Each node has two components: one to store the info; and one to store the address of the next node head should always point to the first node

Data Structures Using C++8 Linked Lists: Some Properties Linked list basic operations: –Search the list to determine whether a particular item is in the list –Insert an item in the list –Delete an item from the list

Data Structures Using C++9 Linked Lists: Some Properties These operations require traversal of the list. Given a pointer to the first node of the list, step through each of the nodes of the list Traverse a list using a pointer of the same type as head

Data Structures Using C++10 Insertion A linked list with pointers p and q newNode needs to be inserted

Data Structures Using C++11 Insertion Code Sequence I newNode->link = q p->link = newNode Code Sequence II p->link = newNode newNode->link = q

Data Structures Using C++12 Insertion Both code sequences produce the result shown below *** The sequence of events does NOT matter for proper insertion

Data Structures Using C++13 Deletion Node to be deleted is 34

Data Structures Using C++14 Deletion q = p->link; p->link = q->link; delete q; Does the sequence of operations matter this time?

Data Structures Using C++15 Building a Linked List There are two ways to build a linked list 1) forwards: first node inserted always stays at the head/start 2) backwards: first node inserted always stays at the tail/end

Data Structures Using C++16 Building a Linked List What is needed to build a linked list forward? -a pointer for the first node -a pointer for the last node -a pointer for the new node being added

Data Structures Using C++17 Building a Linked List Steps to build a linked list forward: –Create a new node called newNode –If first is NULL, the list is empty so you can make first and last point to newNode –If first is not NULL make last point to newNode and make last = newNode

Data Structures Using C++18 Building a Linked List What is needed to build a linked list backwards? –a pointer for the first node –a pointer to the new node being added

Data Structures Using C++19 Building a Linked List Steps to build a linked list backwards: –Create a new node newNode –Insert newNode before first –Update the value of the pointer first

Data Structures Using C++20 Linked List ADT Basic operations on a linked list are: –Initialize the list –Check whether the list is empty –Output the list –Find length of list –Destroy the list

Data Structures Using C++21 Linked List ADT Basic operations on a linked list are: –Get info from last node –Search for a given item –Insert an item –Delete an item –Make a copy of the linked list

Data Structures Using C++22 Ordered Link List In an ordered linked list the elements are sorted Because the list is ordered, we need to modify the algorithms (from how they were implemented for the regular linked list) for the search, insert, and delete operations –What changes are necessary?

Data Structures Using C++23 Doubly Linked List A doubly linked list is a linked list in which every node has a next pointer and a back pointer Every node (except the last node) contains the address of the next node, and every node (except the first node) contains the address of the previous node. A doubly linked list can be traversed in either direction

Data Structures Using C++24 Doubly Linked List

Data Structures Using C++25 STL Sequence Container: List List containers are implemented as doubly linked lists

Data Structures Using C++26 Linked Lists With Header and Trailer Nodes One way to simplify insertion and deletion is never to insert an item before the first or after the last item and never to delete the first node You can set a header node at the beginning of the list containing a value smaller than the smallest value in the data set You can set a trailer node at the end of the list containing a value larger than the largest value in the data set

Data Structures Using C++27 Linked Lists With Header and Trailer Nodes These two nodes, header and trailer, serve merely to simplify the insertion and deletion algorithms and are not part of the actual list. The actual list is between these two nodes.

Data Structures Using C++28 Circular Linked List A linked list in which the last node points to the first node is called a circular linked list In a circular linked list with more than one node, it is convenient to make the pointer first point to the last node of the list

Data Structures Using C++29 Circular Linked List