You are on page 1of 65

Chapter 16

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

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

Linked Lists

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

Linked Lists

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

Linked Lists

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

Linked List Properties

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

Linked List Navigation

current = head;

Linked List Navigation

current = current->link;

Insertion

Consider the following linked list:

A new node with info 50 is to be created and inserted after p

First Part of Insertion


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

Tying New Node to List


newNode->link = p->link; p->link = newNode; newNode->link = p->link;

Completing the List


p->link = newNode;

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

The following two statements insert newNode between p and q:


newNode->link = q; p->link = newNode;

Execution order does not matter

Reversed Order

If the statements are executed in the following order


p->link = newNode; newNode->link = q;

After the statement p->link = newNode;

Completing an Insert
newNode->link = q;

Dangling Node Deletion

Suppose the node with info 34 is to be deleted from the list

Properly Deleting a Node

After the statement: p->link = p->link->link; The resulting figure is:

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
q = p->link; p->link = q->link; delete q;

After the statement q = p->link;

Properly Deleting a Node

After the statement p->link = q->link; the resulting figure is:

Properly Deleting a Node

After the statement delete q; the resulting figure is:

Deletion in the Link List ADT

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

Deletion in the Link List ADT

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

9. Retrieve the info contained in the first node

Deletion in the Link List ADT

Case 1: List is empty

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.

Deletion in the Link List ADT


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

Deletion in the Link List ADT

Deletion in the Link List ADT


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

Deletion in the Link List ADT

Deletion in the Link List ADT

Case 3b: Node to be deleted is 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

Doubly Linked Lists

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

Doubly Linked List Operations


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

Deletion Somewhere In List

Case 3. Consider the list shown in Figure 1747

Deletion Somewhere In 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

Also, we need to maintain three lists:


Part 1: The Video Component

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

Part 2: Customer Component

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

Part 2: Customer Component

The primary characteristics of a customer are:


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

displayMenu

a. Select one of the following b. 1: To check whether a particular video is in the store c. 2: To check out a video d. 3: To check in a video e. 4: To check whether a particular video is in the store f. 5: To print the titles of all videos g. 6: To print a list of all videos h. 9: To exit