Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify the following types of linked lists: Single linked.

Similar presentations


Presentation on theme: "Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify the following types of linked lists: Single linked."— Presentation transcript:

1 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: 3Inserting nodes

2 Single Linked Lists Objectives (Contd.) 3Traversing a linked list 3Querying information 3Deleting nodes 3Sorting lists

3 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

4 Single Linked Lists Single Linked List *Is a chain of structures in which each structure consists of data and a pointer START INFONEXT INFO NEXT NULL

5 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 INFONEXT INFO NEXTINFONEXT

6 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 NEXTPREV LAST PREV NULL NEXTPREVINFO NULL

7 Single Linked Lists Operations on Lists Are: *Insertion *Traversal *Deletion *Modification *Sorting

8 Single Linked Lists Applications of Lists Are: *Word processing applications *Index maintenance in a file-based database package *Maintenance of linked lists for data files that have read-write overhead

9 Single Linked Lists INFO NEXT START NULL Inserting Nodes at the Beginning of a Single Linked List Existing Links on the list Links after inserting the new node

10 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

11 Single Linked Lists Inserting a Node in the Middle or at the End of a Linked List Tom NEXT prev Existing Links on the list Links after inserting the new node NULL Ann NEXT currSTART NEXT

12 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

13 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

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

15 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

16 Single Linked Lists Deleting Nodes *Can be done in two forms: 3Logical 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)

17 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

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

19 Single Linked Lists Problem Statement 11.P.1 Modify 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.

20 Single Linked Lists Summary In 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: 3Memory is allocated whenever required 3Inserting and deleting can be handled efficiently, without having to restructure the list

21 Single Linked Lists Summary (Contd.) *Linked lists can be of three types: 3A 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

22 Single Linked Lists Summary (Contd.) *Each node has two parts: 3The first part contains the information (the INFO) 3The 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: 3Insertion -involves adding a new node to an existing linked list or creating a new linked list if one does not already exist

23 Single Linked Lists Summary (Contd.) 3Traversal - involves moving from one node to another using pointers to display the contents of a node in an existing list 3Deletion - involves deleting an existing node from the linked list 3Modification - involves changing the existing information contained in the node to reflect the latest status 3Sorting - involves rearranging the order of the nodes in the list

24 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

25 Single Linked Lists Summary (Contd.) *The insertion and deletion of a node can be performed at three different levels: 3At the beginning of the list 3In the middle of the list 3At the end of the list


Download ppt "Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify the following types of linked lists: Single linked."

Similar presentations


Ads by Google