SlideShare une entreprise Scribd logo
1  sur  85
Chapter 17:
Linked Lists
Objectives
• In this chapter, you will:
– Learn about linked lists
– Become familiar with the basic properties of
linked lists
– Explore the insertion and deletion operations on
linked lists
– Discover how to build and manipulate a linked list
– Learn how to implement linked lists as ADTs
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
– Learn how to create linked list iterators
– Learn how to implement the basic operations on a
linked list
– Learn how to create unordered linked lists
– Learn how to create ordered linked lists
– Learn how to construct a
– Become familiar with circular linked lists doubly
linked list
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Introduction
• Data can be organized and processed
sequentially using an array, called a sequential
list
• Problems with an array
– Array size is fixed
– Unsorted array: searching for an item is slow
– Sorted array: insertion and deletion is slow
because it requires data movement
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked Lists
• Linked list: a collection of items (nodes)
containing two components:
– Data
– Address (link) of the next node in the list
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
• Example:
– Link field in the last node is nullptr
Linked Lists (cont’d.)
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked Lists (cont’d.)
• A node is declared as a class or struct
– Data type of a node depends on the specific
application
– Link component of each node is a pointer
• Variable declaration:
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked Lists: Some Properties
• Example: linked list with four nodes (Figure
17-4)
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked Lists: Some Properties
(cont’d.)
• current = head;
– Copies value of head into current
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked Lists: Some Properties
(cont’d.)
• current = current->link;
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Traversing a Linked List
• Basic operations of a linked list:
– Search for an item in the list
– Insert an item in the list
– Delete an item from the list
• Traversal: given a pointer to the first node of
the list, step through the nodes of the list
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Traversing a Linked List (cont’d.)
• To traverse a linked list:
• Example:
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
current = head;
while (current != nullptr)
{
//Process the current node
current = current->link;
}
current = head;
while (current != nullptr)
{
cout << current->info << " ";
current = current->link;
}
Item Insertion and Deletion
• Definition of a node:
• Variable declaration:
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insertion
• To insert a new node with info 50 after p in
this list:
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insertion (cont’d.)
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insertion (cont’d.)
• Can use two pointers to simplify the insertion
code somewhat:
• To insert newNode between p and q:
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insertion (cont’d.)
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Deletion
• Node with info 34 is to be deleted:
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Deletion (cont’d.)
• Node with info 34 is removed from the list,
but memory is still occupied
– Node is dangling
– Must keep a pointer to the node to be able to
deallocate its memory
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Deletion (cont’d.)
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List
• If data is unsorted, the list will be unsorted
• Can build a linked list forward or backward
– Forward: a new node is always inserted at the end
of the linked list
– Backward: a new node is always inserted at the
beginning of the list
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List Forward
• Need three pointers to build the list:
– One to point to the first node in the list, which
cannot be moved
– One to point to the last node in the list
– One to create the new node
• Example:
– Data: 2 15 8 24 34
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List Forward
(cont’d.)
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List Forward
(cont’d.)
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List Forward
(cont’d.)
• Now repeat this process three more times:
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Building a Linked List Backward
• Algorithm to build a linked list backward:
– Initialize first to nullptr
– For each item in the list
• Create the new node, newNode
• Store the data in newNode
• Insert newNode before first
• Update the value of the pointer first
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List as an ADT
• Basic operations on linked lists:
– Initialize the list
– Determine whether the list is empty
– Print the list
– Find the length of the list
– Destroy the list
– Retrieve info contained in the first or last node
– Search the list for a given item
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List as an ADT (cont’d.)
• Basic operations on linked lists (cont’d.):
– Insert an item in the list
– Delete an item from the list
– Make a copy of the linked list
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List as an ADT (cont’d.)
• Two general types of linked lists:
– Sorted and unsorted lists
• Algorithms to implement the operations
search, insert, and remove differ slightly for
sorted and unsorted lists
• abstract class linkedListType
will implement basic linked list operations
– Derived classes: unorderedLinkedList
and orderedLinkedList
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List as an ADT (cont’d.)
• For an unordered linked list, can insert a new
item at either the end or the beginning
– buildListForward inserts item at the end
– buildListBackward inserts item at beginning
• Will need two functions:
– insertFirst and insertLast
• Will use two pointers in the list:
– first and last
30C++ Programming: From Problem Analysis to Program Design, Seventh EditionC++ Programming: From Problem Analysis to Program Design, Seventh Edition
Structure of Linked List Nodes
• Each node has two member variables
• We implement the node of a linked list as a
struct
• Definition of the struct nodeType:
31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Member Variables of the class
linkedListType
• linkedListType has three member
variables:
– Two pointers: first and last
– count: the number of nodes in the list
32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List Iterators
• To process each node of the list
– List must be traversed, starting at first node
• Iterator: object that produces each element of
a container, one element at a time
– The two most common iterator operations:
++ (the increment operator)
* (the dereferencing operator)
33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List Iterators (cont’d.)
• An iterator is an object
– Need to define a class (linkedListIterator)
to create iterators to objects of the class
linkedListType
– Will have one member variable to refer to the
current node
34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List Iterators (cont’d.)
35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Linked List Iterators (cont’d.)
36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Default Constructor
• Default constructor:
– Initializes the list to an empty state
37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Destroy the List
• Function destroyList:
– Traverses the list to deallocate memory occupied
by each node
– Once list is destroyed, sets pointers first and
last to nullptr and count to 0
38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Initialize the List
• Function initializeList:
– Initializes list to an empty state
• Since constructor already did this,
initializeList is used to reinitialize an
existing list
39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Print the List
• Function print:
– Prints data contained in each node
– Traverses the list using another pointer
40C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Length of a List
• Function length:
– Returns the count of nodes in the list
– Uses the count variable
41C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Retrieve the Data
of the First or Last Node
• Function front:
– Returns the info contained in the first node
– If list is empty, program will be terminated
• Function back:
– Returns the info contained in the last node
– If list is empty, program will be terminated
42C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Begin and End
• Function begin:
– Returns an iterator to the first node in the list
• Function end:
– Returns an iterator to one past the last node in
the list
43C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Copy the List
• Function copyList:
– Makes an identical copy of a linked list
• Steps:
– Create a node called newNode
– Copy the info of the original node into
newNode
– Insert newNode at the end of the list being
created
44C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Destructor & Copy Constructor
• Destructor:
– Deallocates memory occupied by nodes when the
class object goes out of scope
– Calls destroyList to traverse the list and
delete each node
• Copy constructor:
– Makes an identical copy of the linked list
– Calls function copyList
45C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Assignment Operator
• Definition of the function to overload the
assignment operator
– Similar to the copy constructor
46C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Unordered Linked Lists
• class unorderedLinkedList
– Derived from the abstract class
linkedListType
– Implements the operations search,
insertFirst, insertLast, and
deleteNode
47C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Unordered Linked Lists (cont’d.)
48C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Search the List
• Function search:
– Searches the list for a given item
• Steps:
– Compare search item with current node in the list
• If info of current node is the same as search item,
stop the search
• Otherwise, make the next node the current node
– Repeat Step 1 until item is found or until no more
data is left in the list
49C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert the First Node
• Function insertFirst:
– Inserts a new item at the beginning of the list
• Steps:
– Create a new node
– Store the new item in the new node
– Insert the node before first
– Increment count by 1
50C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert the Last Node
• Function insertLast:
– Inserts a new node after last
– Similar to insertFirst function
51C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node
• Function deleteNode:
– Deletes a node with given info from the list
– Several possible cases to manage
• Case 1: List is empty
– If the list is empty, output an error message
• Case 2: Node to be deleted is the first node
– Adjust the pointer first and count
– If no other nodes, set first and last to
nullptr
52C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
53C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
• Case 3: Node to be deleted is not the first one
– Case 3a: Node to be deleted is not last one
– Update link field of the previous node
– Case 3b: Node to be deleted is the last node
– Update link field of the previous node to nullptr
– Update last pointer to point to previous node
54C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
55C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
56C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
• Case 4: Node to be deleted is not in the list
– List requires no adjustment
– Simply output an error message
57C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Ordered Linked Lists
• orderedLinkedList: derived from
class linkedListType
– Provides definitions of the abstract functions
insertFirst, insertLast, search, and
deleteNode
• Assume that elements of an ordered linked list
are arranged in ascending order
• Include the function insert to insert an
element in an ordered list at its proper place
58C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Ordered Linked Lists (cont’d.)
59C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Search the List
• Steps:
– Compare the search item with the current node in
the list
• If info of current node is >= to search item, stop
search
• Otherwise, make the next node the current node
– Repeat Step 1 until an item in the list >= to search
item is found, or no more data is left in the list
60C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node
• Case 1: The list is empty
– New node becomes first node
61C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node (cont’d.)
• Case 2: List is not empty, and the item to be
inserted is smaller than smallest item in list
– New node goes at beginning of list
62C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node (cont’d.)
• Case 3: New item to be inserted somewhere
in list
– Case 3a: New item is larger than largest item
• New item is inserted at end of list
• Case 3b: Item to be inserted goes somewhere in
the middle of the list
63C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node (cont’d.)
64C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node (cont’d.)
65C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert First and Insert Last
• Functions insertFirst and insertLast
– Must insert new item at the proper place to
ensure resulting list is still sorted
• These functions are not actually used
– Definitions must be provided because they are
declared as abstract in the parent class
• Function insertNode is used to insert at
the proper place
66C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node
• Case 1: List is initially empty  error
• Case 2: Item to be deleted is first node in list
– Adjust the head (first) pointer
• Case 3: Item is somewhere in the list
– current points to node with item to delete
– trailCurrent points to node previous to the
one pointed to by current
• Case 4: Item is not in the list error
67C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Print a Linked List in Reverse Order
(Recursion Revisited)
• Links are only in forward direction
– Cannot traverse list backward using links
• Can use recursion
– List is printed only if pointer to the list is not
nullptr
• Recursive call is on the tail of the list
– When tail is empty, recursion stops
68C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Doubly Linked Lists
• Doubly linked list: every node has next and
back pointers
– Can be traversed in either direction
69C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Doubly Linked Lists (cont’d.)
• Operations:
– Initialize or destroy the list
– Determine whether the list is empty
– Search the list for a given item
– Retrieve the first or last element of the list
– Insert or delete an item
– Find the length of the list
– Print the list
– Make a copy of the list
70C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Default Constructor
• Default constructor:
– Initializes doubly-linked list to empty state
– Sets first and last to nullptr, and count
to 0
• isEmptyList:
– Returns true if list is empty, false otherwise
71C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Destroy the List & Initialize the List
• Function destroy:
– Deletes all the nodes in the list, leaving list in an
empty state
– Sets count to 0
• Function initializeList:
– Reinitializes doubly linked list to an empty state
– Uses the destroy operation
72C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Length of the List & Print the List
• Function length
– Returns the count of nodes in the list
• Function print
– Traverses the list
– Outputs the info contained in each node
• Function reversePrint
– Traverses list in reverse order using back links
– Outputs the info in each node
73C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Search the List
• Function search:
– Returns true if search item is found, otherwise
false
– Algorithm is same as that for an ordered linked list
74C++ Programming: From Problem Analysis to Program Design, Seventh Edition
First and Last Elements
• Function front
– Returns first element of the list
• Function back
– Returns last element of the list
• If list is empty, both functions will terminate
the program
75C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node
• Four insertion cases:
– Case 1: Insertion in an empty list
– Case 2: Insertion at beginning of a nonempty list
– Case 3: Insertion at end of a nonempty list
– Case 4: Insertion somewhere in nonempty list
• Cases 1 & 2 require update to pointer first
• Cases 3 & 4 are similar:
– After inserting item, increment count by 1
76C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Insert a Node (cont’d.)
77C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node
• Case 1: The list is empty
• Case 2: The item to be deleted is first node in list
– Must update the pointer first
• Case 3: Item to be deleted is somewhere in the list
• Case 4: Item to be deleted is not in the list
• After deleting a node, count is decremented by 1
78C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
79C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Delete a Node (cont’d.)
80C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Circular Linked Lists
• Circular linked list: a linked list in which the
last node points to the first node
81C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Circular Linked Lists (cont’d.)
• Operations on a circular list:
– Initialize the list (to an empty state)
– Determine if the list is empty
– Destroy the list
– Print the list
– Find the length of the list
– Search the list for a given item
– Insert or delete an item
– Copy the list
82C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary
• A linked list is a list of items (nodes)
– Order of the nodes is determined by the address
(link) stored in each node
• Pointer to a linked list is called head or first
• A linked list is a dynamic data structure
• The list length is the number of nodes
83C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• Insertion and deletion does not require data
movement
– Only the pointers are adjusted
• A (single) linked list is traversed in only one
direction
• Search of a linked list is sequential
• The head pointer is fixed on first node
• Traverse: use a pointer other than head
84C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• Doubly linked list
– Every node has two links: next and previous
– Can be traversed in either direction
– Item insertion and deletion require the
adjustment of two pointers in a node
• A linked list in which the last node points to
the first node is called a circular linked list
85C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Contenu connexe

Tendances

9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)ViswanathanS21
 
Chapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABChapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABPranoti Doke
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05Terry Yoast
 
20100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture0220100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture02Computer Science Club
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Oriol López Massaguer
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 

Tendances (20)

9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Ch02
Ch02Ch02
Ch02
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Chapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABChapter 2 Basics of MATLAB
Chapter 2 Basics of MATLAB
 
Yacc
YaccYacc
Yacc
 
Matlab brochure
Matlab  brochureMatlab  brochure
Matlab brochure
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 
20100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture0220100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture02
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Chap02
Chap02Chap02
Chap02
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 

Similaire à Linked Lists Guide

Similaire à Linked Lists Guide (20)

Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
ch9.ppt
ch9.pptch9.ppt
ch9.ppt
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
Lec5
Lec5Lec5
Lec5
 
Week 2 - Data Structures and Algorithms
Week 2 - Data Structures and AlgorithmsWeek 2 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
linkedlist.ppt
linkedlist.pptlinkedlist.ppt
linkedlist.ppt
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
linked list
linked list linked list
linked list
 

Plus de Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Plus de Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Dernier

Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Dernier (20)

Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Linked Lists Guide

  • 2. Objectives • In this chapter, you will: – Learn about linked lists – Become familiar with the basic properties of linked lists – Explore the insertion and deletion operations on linked lists – Discover how to build and manipulate a linked list – Learn how to implement linked lists as ADTs 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) – Learn how to create linked list iterators – Learn how to implement the basic operations on a linked list – Learn how to create unordered linked lists – Learn how to create ordered linked lists – Learn how to construct a – Become familiar with circular linked lists doubly linked list 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Introduction • Data can be organized and processed sequentially using an array, called a sequential list • Problems with an array – Array size is fixed – Unsorted array: searching for an item is slow – Sorted array: insertion and deletion is slow because it requires data movement 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Linked Lists • Linked list: a collection of items (nodes) containing two components: – Data – Address (link) of the next node in the list 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 6. • Example: – Link field in the last node is nullptr Linked Lists (cont’d.) 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. Linked Lists (cont’d.) • A node is declared as a class or struct – Data type of a node depends on the specific application – Link component of each node is a pointer • Variable declaration: 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 8. Linked Lists: Some Properties • Example: linked list with four nodes (Figure 17-4) 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. Linked Lists: Some Properties (cont’d.) • current = head; – Copies value of head into current 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 10. Linked Lists: Some Properties (cont’d.) • current = current->link; 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. Traversing a Linked List • Basic operations of a linked list: – Search for an item in the list – Insert an item in the list – Delete an item from the list • Traversal: given a pointer to the first node of the list, step through the nodes of the list 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Traversing a Linked List (cont’d.) • To traverse a linked list: • Example: 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition current = head; while (current != nullptr) { //Process the current node current = current->link; } current = head; while (current != nullptr) { cout << current->info << " "; current = current->link; }
  • 13. Item Insertion and Deletion • Definition of a node: • Variable declaration: 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 14. Insertion • To insert a new node with info 50 after p in this list: 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Insertion (cont’d.) 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Insertion (cont’d.) • Can use two pointers to simplify the insertion code somewhat: • To insert newNode between p and q: 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 17. Insertion (cont’d.) 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. Deletion • Node with info 34 is to be deleted: 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 19. Deletion (cont’d.) • Node with info 34 is removed from the list, but memory is still occupied – Node is dangling – Must keep a pointer to the node to be able to deallocate its memory 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 20. Deletion (cont’d.) 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 21. Building a Linked List • If data is unsorted, the list will be unsorted • Can build a linked list forward or backward – Forward: a new node is always inserted at the end of the linked list – Backward: a new node is always inserted at the beginning of the list 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 22. Building a Linked List Forward • Need three pointers to build the list: – One to point to the first node in the list, which cannot be moved – One to point to the last node in the list – One to create the new node • Example: – Data: 2 15 8 24 34 22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 23. Building a Linked List Forward (cont’d.) 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 24. Building a Linked List Forward (cont’d.) 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 25. Building a Linked List Forward (cont’d.) • Now repeat this process three more times: 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 26. Building a Linked List Backward • Algorithm to build a linked list backward: – Initialize first to nullptr – For each item in the list • Create the new node, newNode • Store the data in newNode • Insert newNode before first • Update the value of the pointer first 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 27. Linked List as an ADT • Basic operations on linked lists: – Initialize the list – Determine whether the list is empty – Print the list – Find the length of the list – Destroy the list – Retrieve info contained in the first or last node – Search the list for a given item 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 28. Linked List as an ADT (cont’d.) • Basic operations on linked lists (cont’d.): – Insert an item in the list – Delete an item from the list – Make a copy of the linked list 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 29. Linked List as an ADT (cont’d.) • Two general types of linked lists: – Sorted and unsorted lists • Algorithms to implement the operations search, insert, and remove differ slightly for sorted and unsorted lists • abstract class linkedListType will implement basic linked list operations – Derived classes: unorderedLinkedList and orderedLinkedList 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 30. Linked List as an ADT (cont’d.) • For an unordered linked list, can insert a new item at either the end or the beginning – buildListForward inserts item at the end – buildListBackward inserts item at beginning • Will need two functions: – insertFirst and insertLast • Will use two pointers in the list: – first and last 30C++ Programming: From Problem Analysis to Program Design, Seventh EditionC++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 31. Structure of Linked List Nodes • Each node has two member variables • We implement the node of a linked list as a struct • Definition of the struct nodeType: 31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 32. Member Variables of the class linkedListType • linkedListType has three member variables: – Two pointers: first and last – count: the number of nodes in the list 32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 33. Linked List Iterators • To process each node of the list – List must be traversed, starting at first node • Iterator: object that produces each element of a container, one element at a time – The two most common iterator operations: ++ (the increment operator) * (the dereferencing operator) 33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 34. Linked List Iterators (cont’d.) • An iterator is an object – Need to define a class (linkedListIterator) to create iterators to objects of the class linkedListType – Will have one member variable to refer to the current node 34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 35. Linked List Iterators (cont’d.) 35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 36. Linked List Iterators (cont’d.) 36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 37. Default Constructor • Default constructor: – Initializes the list to an empty state 37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 38. Destroy the List • Function destroyList: – Traverses the list to deallocate memory occupied by each node – Once list is destroyed, sets pointers first and last to nullptr and count to 0 38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 39. Initialize the List • Function initializeList: – Initializes list to an empty state • Since constructor already did this, initializeList is used to reinitialize an existing list 39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 40. Print the List • Function print: – Prints data contained in each node – Traverses the list using another pointer 40C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 41. Length of a List • Function length: – Returns the count of nodes in the list – Uses the count variable 41C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 42. Retrieve the Data of the First or Last Node • Function front: – Returns the info contained in the first node – If list is empty, program will be terminated • Function back: – Returns the info contained in the last node – If list is empty, program will be terminated 42C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 43. Begin and End • Function begin: – Returns an iterator to the first node in the list • Function end: – Returns an iterator to one past the last node in the list 43C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 44. Copy the List • Function copyList: – Makes an identical copy of a linked list • Steps: – Create a node called newNode – Copy the info of the original node into newNode – Insert newNode at the end of the list being created 44C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 45. Destructor & Copy Constructor • Destructor: – Deallocates memory occupied by nodes when the class object goes out of scope – Calls destroyList to traverse the list and delete each node • Copy constructor: – Makes an identical copy of the linked list – Calls function copyList 45C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 46. Overloading the Assignment Operator • Definition of the function to overload the assignment operator – Similar to the copy constructor 46C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 47. Unordered Linked Lists • class unorderedLinkedList – Derived from the abstract class linkedListType – Implements the operations search, insertFirst, insertLast, and deleteNode 47C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 48. Unordered Linked Lists (cont’d.) 48C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 49. Search the List • Function search: – Searches the list for a given item • Steps: – Compare search item with current node in the list • If info of current node is the same as search item, stop the search • Otherwise, make the next node the current node – Repeat Step 1 until item is found or until no more data is left in the list 49C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 50. Insert the First Node • Function insertFirst: – Inserts a new item at the beginning of the list • Steps: – Create a new node – Store the new item in the new node – Insert the node before first – Increment count by 1 50C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 51. Insert the Last Node • Function insertLast: – Inserts a new node after last – Similar to insertFirst function 51C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 52. Delete a Node • Function deleteNode: – Deletes a node with given info from the list – Several possible cases to manage • Case 1: List is empty – If the list is empty, output an error message • Case 2: Node to be deleted is the first node – Adjust the pointer first and count – If no other nodes, set first and last to nullptr 52C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 53. Delete a Node (cont’d.) 53C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 54. Delete a Node (cont’d.) • Case 3: Node to be deleted is not the first one – Case 3a: Node to be deleted is not last one – Update link field of the previous node – Case 3b: Node to be deleted is the last node – Update link field of the previous node to nullptr – Update last pointer to point to previous node 54C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 55. Delete a Node (cont’d.) 55C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 56. Delete a Node (cont’d.) 56C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 57. Delete a Node (cont’d.) • Case 4: Node to be deleted is not in the list – List requires no adjustment – Simply output an error message 57C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 58. Ordered Linked Lists • orderedLinkedList: derived from class linkedListType – Provides definitions of the abstract functions insertFirst, insertLast, search, and deleteNode • Assume that elements of an ordered linked list are arranged in ascending order • Include the function insert to insert an element in an ordered list at its proper place 58C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 59. Ordered Linked Lists (cont’d.) 59C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 60. Search the List • Steps: – Compare the search item with the current node in the list • If info of current node is >= to search item, stop search • Otherwise, make the next node the current node – Repeat Step 1 until an item in the list >= to search item is found, or no more data is left in the list 60C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 61. Insert a Node • Case 1: The list is empty – New node becomes first node 61C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 62. Insert a Node (cont’d.) • Case 2: List is not empty, and the item to be inserted is smaller than smallest item in list – New node goes at beginning of list 62C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 63. Insert a Node (cont’d.) • Case 3: New item to be inserted somewhere in list – Case 3a: New item is larger than largest item • New item is inserted at end of list • Case 3b: Item to be inserted goes somewhere in the middle of the list 63C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 64. Insert a Node (cont’d.) 64C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 65. Insert a Node (cont’d.) 65C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 66. Insert First and Insert Last • Functions insertFirst and insertLast – Must insert new item at the proper place to ensure resulting list is still sorted • These functions are not actually used – Definitions must be provided because they are declared as abstract in the parent class • Function insertNode is used to insert at the proper place 66C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 67. Delete a Node • Case 1: List is initially empty  error • Case 2: Item to be deleted is first node in list – Adjust the head (first) pointer • Case 3: Item is somewhere in the list – current points to node with item to delete – trailCurrent points to node previous to the one pointed to by current • Case 4: Item is not in the list error 67C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 68. Print a Linked List in Reverse Order (Recursion Revisited) • Links are only in forward direction – Cannot traverse list backward using links • Can use recursion – List is printed only if pointer to the list is not nullptr • Recursive call is on the tail of the list – When tail is empty, recursion stops 68C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 69. Doubly Linked Lists • Doubly linked list: every node has next and back pointers – Can be traversed in either direction 69C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 70. Doubly Linked Lists (cont’d.) • Operations: – Initialize or destroy the list – Determine whether the list is empty – Search the list for a given item – Retrieve the first or last element of the list – Insert or delete an item – Find the length of the list – Print the list – Make a copy of the list 70C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 71. Default Constructor • Default constructor: – Initializes doubly-linked list to empty state – Sets first and last to nullptr, and count to 0 • isEmptyList: – Returns true if list is empty, false otherwise 71C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 72. Destroy the List & Initialize the List • Function destroy: – Deletes all the nodes in the list, leaving list in an empty state – Sets count to 0 • Function initializeList: – Reinitializes doubly linked list to an empty state – Uses the destroy operation 72C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 73. Length of the List & Print the List • Function length – Returns the count of nodes in the list • Function print – Traverses the list – Outputs the info contained in each node • Function reversePrint – Traverses list in reverse order using back links – Outputs the info in each node 73C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 74. Search the List • Function search: – Returns true if search item is found, otherwise false – Algorithm is same as that for an ordered linked list 74C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 75. First and Last Elements • Function front – Returns first element of the list • Function back – Returns last element of the list • If list is empty, both functions will terminate the program 75C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 76. Insert a Node • Four insertion cases: – Case 1: Insertion in an empty list – Case 2: Insertion at beginning of a nonempty list – Case 3: Insertion at end of a nonempty list – Case 4: Insertion somewhere in nonempty list • Cases 1 & 2 require update to pointer first • Cases 3 & 4 are similar: – After inserting item, increment count by 1 76C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 77. Insert a Node (cont’d.) 77C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 78. Delete a Node • Case 1: The list is empty • Case 2: The item to be deleted is first node in list – Must update the pointer first • Case 3: Item to be deleted is somewhere in the list • Case 4: Item to be deleted is not in the list • After deleting a node, count is decremented by 1 78C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 79. Delete a Node (cont’d.) 79C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 80. Delete a Node (cont’d.) 80C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 81. Circular Linked Lists • Circular linked list: a linked list in which the last node points to the first node 81C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 82. Circular Linked Lists (cont’d.) • Operations on a circular list: – Initialize the list (to an empty state) – Determine if the list is empty – Destroy the list – Print the list – Find the length of the list – Search the list for a given item – Insert or delete an item – Copy the list 82C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 83. Summary • A linked list is a list of items (nodes) – Order of the nodes is determined by the address (link) stored in each node • Pointer to a linked list is called head or first • A linked list is a dynamic data structure • The list length is the number of nodes 83C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 84. Summary (cont’d.) • Insertion and deletion does not require data movement – Only the pointers are adjusted • A (single) linked list is traversed in only one direction • Search of a linked list is sequential • The head pointer is fixed on first node • Traverse: use a pointer other than head 84C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 85. Summary (cont’d.) • Doubly linked list – Every node has two links: next and previous – Can be traversed in either direction – Item insertion and deletion require the adjustment of two pointers in a node • A linked list in which the last node points to the first node is called a circular linked list 85C++ Programming: From Problem Analysis to Program Design, Seventh Edition