0% found this document useful (0 votes)
94 views

Linked List Slides

The document discusses linked lists, their properties, and how to implement them in C++. Key points covered include: - Linked lists allow for dynamic sizes and efficient insertion/deletion by linking nodes together via pointers - Each node contains a data field and pointer to the next node - Traversal involves iterating from node to node via their links - Insertion adds a new node between two existing nodes by adjusting their pointers - Deletion removes a node by adjusting pointers of the preceding and following nodes - Linked lists can be built forward or backward by sequentially adding nodes to the start or end.

Uploaded by

parasdeep singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Linked List Slides

The document discusses linked lists, their properties, and how to implement them in C++. Key points covered include: - Linked lists allow for dynamic sizes and efficient insertion/deletion by linking nodes together via pointers - Each node contains a data field and pointer to the next node - Traversal involves iterating from node to node via their links - Insertion adds a new node between two existing nodes by adjusting their pointers - Deletion removes a node by adjusting pointers of the preceding and following nodes - Linked lists can be built forward or backward by sequentially adding nodes to the start or end.

Uploaded by

parasdeep singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C++ Programming:

Program Design Including


Data Structures, Fifth Edition

Linked Lists
Objectives
In this chapter, you will:
• 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
• Learn how to construct a doubly linked list

C++ Programming: Program Design Including Data Structures, Fifth Edition 2


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

C++ Programming: Program Design Including Data Structures, Fifth Edition 3


Linked Lists
• 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

Link field in last node


is NULL
C++ Programming: Program Design Including Data Structures, Fifth Edition 4
Linked Lists (cont'd.)
• Because each node of a linked list has two
components, we need to declare each
node as a class or struct
– Data type of a node depends on the specific
application
– The link component of each node is a pointer

C++ Programming: Program Design Including Data Structures, Fifth Edition 5


Linked Lists: Some Properties

C++ Programming: Program Design Including Data Structures, Fifth Edition 6


Linked Lists: Some Properties
(cont'd.)
• current = head;
– Copies value of head into current

C++ Programming: Program Design Including Data Structures, Fifth Edition 7


Linked Lists: Some Properties
(cont'd.)
• current = current->link;

C++ Programming: Program Design Including Data Structures, Fifth Edition 8


Traversing a Linked List
• The basic operations of a linked list are:
– Search to determine if an item is 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

C++ Programming: Program Design Including Data Structures, Fifth Edition 9


Traversing a Linked List
(cont'd.)
• To traverse a linked list:

• Example:

C++ Programming: Program Design Including Data Structures, Fifth Edition 10


Item Insertion and Deletion
• Consider the following definition of a node:

• We will use the following variable


declaration:

C++ Programming: Program Design Including Data Structures, Fifth Edition 11


Insertion
• Consider the following linked list:

• A new node with info 50 is to be created


and inserted after p

C++ Programming: Program Design Including Data Structures, Fifth Edition 12


Insertion (cont'd.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 13


Insertion (cont'd.)
• Using two pointers, we can simplify the
insertion code somewhat

• To insert newNode between p and q:


The order in which these statements
execute does not matter

C++ Programming: Program Design Including Data Structures, Fifth Edition 14


Insertion (cont'd.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 15


Deletion

Node with info 34 is removed from the list, but


memory is still occupied; node is dangling
C++ Programming: Program Design Including Data Structures, Fifth Edition 16
Deletion (cont’d.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 17


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

C++ Programming: Program Design Including Data Structures, Fifth Edition 18


Building a Linked List Forward
• You 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

C++ Programming: Program Design Including Data Structures, Fifth Edition 19


Building a Linked List Forward
(cont’d.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 20


Building a Linked List Forward
(cont’d.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 21


Building a Linked List Forward
(cont'd.)

We now repeat statements 1 through 6b three more times:

C++ Programming: Program Design Including Data Structures, Fifth Edition 22


Building a Linked List Forward
(cont’d.)

C++ Programming: Program Design Including Data Structures, Fifth Edition 23


Building a Linked List Backward
• The algorithm is:
– Initialize first to NULL
– For each item in the list
• Create the new node, newNode
• Store the item in newNode
• Insert newNode before first
• Update the value of the pointer first

C++ Programming: Program Design Including Data Structures, Fifth Edition 24

You might also like