25
Single Linked Lists Objectives In this lesson, you will learn to: Define single linked list Identify the following types of linked lists: Single linked list Circular linked list Double linked list Implement the following operations on single linked list: Inserting nodes

Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Embed Size (px)

DESCRIPTION

Single Linked Lists Linked List *Is a chain of objects in which each object consists of the data and a pointer that stores the address of the next logical object in the list *Types of linked lists: 3Single linked list 3Circular linked list 3Double linked list

Citation preview

Page 1: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

ObjectivesIn this lesson, you will learn to:

Define single linked list

Identify the following types of linked lists:

Single linked list

Circular linked list

Double linked list

Implement the following operations on single linked list:

Inserting nodes

Page 2: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Objectives (Contd.) Traversing a linked list

Querying information

Deleting nodes

Sorting lists

Page 3: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Linked List Is a chain of objects in which each object consists of

the data and a pointer that stores the address of the next logical object in the list

Types of linked lists:

Single linked list

Circular linked list

Double linked list

Page 4: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Single Linked List Is a chain of structures in which each structure

consists of data and a pointer

START

INFO NEXT NEXTINFO INFO NEXT NULL

Page 5: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Circular Linked List Has the START pointer pointing to the first node and

the last pointer also pointing to the first node

START

INFO NEXT NEXTINFO INFO NEXT INFO NEXT

Page 6: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Double Linked List Has 3 parts: the INFO, NEXT pointer, and PREV

pointer

Is also called a two-way list

START

NEXTINFO INFO NEXTPREV

LAST

PREVNULL

NEXTPREV INFO

NULL

Page 7: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Operations on ListsAre:

Insertion

Traversal

Deletion

Modification

Sorting

Page 8: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Applications of ListsAre:

Word processing applications

Index maintenance in a file-based database package

Maintenance of linked lists for data files that have read-write overhead

Page 9: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

INFO

INFOINFO INFO NEXTNEXTNEXT

NEXT

START

NULL

Inserting Nodes at the Beginning of a Single Linked List

Existing Links on the listLinks after inserting the new node

Page 10: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Inserting Nodes at the Beginning of a Single Linked List (Contd.)Requires to:

Check whether or not the list exists

Check whether the user supplied string is lesser than the INFO in the first node

Page 11: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Inserting a Node in the Middle or at the End of a Linked List

Tom NEXT

NEXT

prev

Existing Links on the list

Links after inserting the new node

NULL

Ann NEXT

currSTART

NEXT

Page 12: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Inserting a Node in the Middle or at the End of a Linked List (Contd.)Requires to:

Allocate the memory for the new node

Position the pointer prev on the node after which the new node is to be inserted and the pointer curr to be positioned on the node before which the new node is to be inserted.

Complete the link by making the NEXT of the previous node point to the new node

Page 13: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Traversing a Linked List Helps to display the contents of a list

Is done by following the listed steps :

1. Set a temporary pointer, temp, to START

2. Display the INFO part of the node pointed by temp

3. Advance the pointer, temp, so that it points to the next node

4. Repeat steps 2 and 3 until temp is not equal to NULL

Page 14: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Problem Statement 11.D.1Create an application that accepts the names of an unknown number of customers and displays them in alphabetical order.

Page 15: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Querying Information Is done by following the listed steps :

1. The pointer CURRENT, is positioned at the node that contains the name matching the supplied

string

2. The pointer PRECEDE, is positioned at the node that is just before the node pointed by CURRENT

3. If the pointer CURRENT contains NULL, it means that the entire list was traversed without finding the specified string

Page 16: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Deleting Nodes Can be done in two forms:

Logical deletion of the node from the linked list (de-linking the node from the list)

Physical deletion of the node to free the memory occupied by the node (the delete operator used to free the memory occupied by the node)

Page 17: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Deleting Nodes (Contd.)Is done by following the listed steps:

1. Check whether or not the supplied string is present in any of the nodes

2. Position the pointer CURRENT at the node to be deleted

3. Position the pointer PRECEDE at the node that is just before the node pointed by CURRENT

4. Reposition START if the node that was delinked was the first node in the list

5. Delete the memory for the CURRENT node

Page 18: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Problem Statement 11.D.2Modify the above application to allow deletion of unwanted customer records.

Page 19: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Problem Statement 11.P.1Modify the above application such that along with the name of the customer his address and customer code is also stored and on querying the record based on customer code, you can view customer details.

Page 20: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

SummaryIn this lesson, you learned that:

A linked list is a chain of objects in which each object consists of data and a pointer that stores the address (link) of the next logical object in the list

The main advantages of linked lists are:

Memory is allocated whenever required

Inserting and deleting can be handled efficiently, without having to restructure the list

Page 21: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Summary (Contd.)Linked lists can be of three types:

A single linked list is a chain of structures in which each structure consists of data as well as a pointer, called the node

A circular linked list has the START pointer pointing to the first node and the last node contains the address of the starting node

A double linked list is also called a two way list, which has a START pointer pointing to the first node and the pointer NEXT and PREV pointing to the next and previous node

Page 22: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Summary (Contd.)Each node has two parts:

The first part contains the information (the INFO)

The second part is a pointer, which contains the address of the subsequent node (the NEXT)

The different operations that can be performed on linked lists are:

Insertion -involves adding a new node to an existing linked list or creating a new linked list if one does not already exist

Page 23: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Summary (Contd.) Traversal - involves moving from one node to

another using pointers to display the contents of a node in an existing list

Deletion - involves deleting an existing node from the linked list

Modification - involves changing the existing information contained in the node to reflect the latest status

Sorting - involves rearranging the order of the nodes in the list

Page 24: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Summary (Contd.)The three classes which make a complete linked list

are as follows:

A class to represent the data or INFO part of the node

The Node structure, which will contain an object of the built-in data class called INFO and a pointer to a Node object called NEXT

The List class that will contain the starting pointer to a Node object

Page 25: Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify…

Single Linked Lists

Summary (Contd.)The insertion and deletion of a node can be

performed at three different levels:

At the beginning of the list

In the middle of the list

At the end of the list