You are on page 1of 65

# Chapter 16

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

Limitations

Storing and processing data in an array has limitations Array size is fixed Only a fixed number of items can be stored in an array Searching for an item in an array is time consuming if data is not sorted If data is sorted, item insertion and deletion becomes time consuming

A linked list is a collection of components, called nodes Every node (except the last node) contains the address of the next node Every node in a linked list has two components The address of the first node of the list is stored in a separate location, called head or first

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

The down arrow in the last node indicates that this link field is NULL The arrow in each node indicates that the address of the node to which it is pointing is stored in that node

This linked list has four nodes The address of the first node is stored in the pointer head Each node has two components: a component, info, to store the info and another component, link, to store the address of the next node

Insertion

## First Part of Insertion

newNode = new nodeType; //create newNode newNode->info = 50; //store 50 in the new node

## Completing the List

Dangling List
If we reverse the sequence of the statements, that is, suppose we execute the statements in the following order p->link = newNode; newNode->link = p->link;

Simplifying Insertion

Reversed Order

## After the statement p->link = newNode;

Completing an Insert

## Properly Deleting a Node

The node with info 34 is removed from the list The memory is still occupied by this node, that is, this node is dangling

## Properly Deleting a Node

The following statements delete the node and deallocate the memory

## The basic operations on linked lists are:

1. 2. 3. 4. 5. 6. 7. 8.

Initialize the list Check if the list is empty Check if the list is full Search the list for a given item Insert an item in the list Delete an item from the list Destroy the list Print the list

We also include the following operation on linked lists, which is helpful if we want to check the info of the first node

## If the list is empty, output an error message

The node to be deleted is the first node

## Case 2: List is not empty

After deletion, the list becomes empty. Therefore, after deletion, both first and last are set to NULL.

Case 2: List is not empty The node to be deleted is the first node Consider the list of more than one node, as shown in Figure 17-28

Case 3: Node to be deleted is not the first node, but is somewhere in this list Case 3a: Node to be deleted is not the last node

Insertion

## Case 1: The list is empty

Insertion

Case 2: The list is not empty, and the item to be inserted is smaller than the smallest item in the list

Insertion

Case 3: The list is not empty, and the item to be inserted is larger than the first item in the list Case 3a: The item to be inserted is larger than the largest item in the list; that is, it goes at the end of the list

Insertion

Insertion

Case 3b: The item to be inserted goes somewhere in the middle of the list

## Print List in Reverse Order

Suppose that current is a pointer to a linked list if(current != NULL) { reversePrint(current->link); //print the tail cout<<current->info<<endl; //print the node } Here the base case is hidden. The list is printed only if the pointer to the list is not NULL Inside the if statement the recursive call is on the tail of the list There are statements after the recursive call, so when the transfer comes back to the calling function, we must execute the remaining statements

A doubly linked list is a linked list in which every node has a next and a back pointer

A doubly linked list can be traversed in either direction. We can traverse the list starting at the first node or if a pointer to the last node is given, we can traverse the list starting at the last node

1. 2. 3. 4. 5. 6. 7. 8. 9.

Initialize the list Destroy the list Check whether the list is empty Check whether the list is full Search the list for a given item Insert an item in the list Delete an item from the list Find the length of the list Print the list

insertNode

Four cases:

1. 2. 3. 4.

## Insertion Insertion Insertion Insertion

in an empty list at the beginning of a nonempty list at the end of a nonempty list somewhere in a nonempty list

Delete Node

## The delete operation has several cases:

1. The list is empty 2. The item to be deleted is in the first node of the list, which would require us to change the value of the pointer first 3. The item to be deleted is somewhere in the list 4. The item to be deleted is not in the list

## List after deleting 17 is shown in Figure 17-49:

Programming Example

For a family or an individual, a favorite place to go on weekends or holidays is to a video store to rent movies A new video store in your neighborhood is about to open However, it does not have a program to keep track of its videos and customers The store managers want someone to write a program for their system so that the video store can function

Programming Example

## The program should be able to perform the following operations:

1. Rent a video; that is, check out a video 2. Return, or check in, a video 3. Create a list of videos owned by the store 4. Show the details of a particular video 5. Print a list of all videos in the store 6. Check whether a particular video is in the store 7. Maintain a customer database 8. Print a list of all the videos rented by each customer

Programming Example

From the programming requirement, we see that there are two major components of the video store:

a video a customer 1. A list of all videos in the store 2. A list of all customers of the store 3. A list of rented videos

## The common things associated with a video are:

1. 2. 3. 4. 5. 6.

Name of the movie Names of the stars Name of the producer Name of the director Name of the production company Number of copies in store

Video List

This program requires us to maintain a list of all videos in the store, and we should be able to add a new video to our list We use a linked list to create a list of videos

Video List

The node of a video list has the form shown in Figure 17-54

## Part 2: Customer Component

The customer object stores information about a customer, such as first name, last name, account number, and a list of videos rented by the customer Thus, our video store program has two components:

1. A video object that contains the necessary information about a video as described previously 2. A customer object that contains the necessary information about a customer, which will be described soon

## We will create two lists that correspond to these two objects:

a. A list of all videos in the store (as described earlier) b. A list of all customers

1. 2. 3. 4.

## The The The The

customers first name customers last name customers account number list of rented videos

Basic Operations

## The basic operations on an object of the type personType are:

1. 2. 3. 4.

Print the name Set the name Show the first name Show the last name

Basic Operations

## The basic operations on an object of the type customerType are:

1. Print the name, account number, and number of rentals 2. Set the name, account number, and number of rentals 3. Rent a video, add video to the list of rented video list 4. Return a video, delete video from the list of rented video list 5. Show account number

Main Program

## The data in the input file is in the form:

video title (that is, name of the movie) movie star1 movie star2 movie producer movie director movie production co. number of copies . . .

## Algorithm of the Main Function

1. Open the input file, if input file does not exist, exit the program 2. Create the list of videoscreateVideoList 3. Show the menudisplayMenu 4. While not done, perform various operations

CreateVideoList

a. Read the data and store it in a video object b. Insert the video in the list c. Repeat steps a and b for each videos data in the file