Linked List DSA

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Linked List:

Software Tools:-

1. Code Blocks with GCC compiler.

Theory:-

You have already seen how data is organized and processed sequentially using an array, called a
sequential list. You have performed several operations on sequential lists, such as sorting,
inserting, deleting, and searching. You also found that if data is not sorted, searching for an item in
the list can be very time consuming, especially with large lists. Once the data is sorted, you can
use a binary search and improve the search algorithm. However, in this case, insertion and deletion
become time consuming, especially with large lists because these operations require data
movement. Also, because the array size must be fixed during execution, new items can be added
only if there is room. Thus, there are limitations when you organize data in an array.
This session helps you to overcome some of these problems. Last session showed how memory
(variables) can be dynamically allocated and deallocated using pointers. This session uses
pointers to organize and process data in lists, called linked lists. Recall that when data is stored in an
array, memory for the components of the array is contiguous—that is, the blocks are allocated one
after the other. However, as we will see, the components (called nodes) of a linked list need not be
contiguous.

LINKED LIST:-
A linked list is a collection of components, called nodes. Every node (except the last node) contains
the address of the next node. Thus, every node in a linked list has two components: one to store
the relevant information (that is, data) and one to store the address, called the link, of the next
node in the list. The address of the first node in the list is stored in a separate location, called the
head or first. Figure 1 is a pictorial representation of a node.

Figure 1

Linked list: 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 list in Figure 2 is an example of a linked list.

Figure 2

The arrow in each node indicates that the address of the node to which it is pointing is stored in
that node. The down arrow in the last node indicates that this link field is NULL.
For a better understanding of this notation, suppose that the first node is at memory location

1200, and the second node is at memory location 1575, see Figure 3.

Figure 3

The value of the head is 1200, the data part of the first node is 45, and the link component of the
first node contains 1575, the address of the second node. We will use the arrow notation
whenever we draw the figure of a linked list.

For simplicity and for the ease of understanding and clarity, Figures 3 through 5 use decimal
integers as the values of memory addresses. However, in computer memory the memory
addresses are in binary.

Because each node of a linked list has two components, we need to declare each node as a class or
struct. The data type of each node depends on the specific application—that is, what kind of data is
being processed. However, the link component of each node is a pointer. The data type of this
pointer variable is the node type itself. For the previous linked list, the definition of the node is as
follows. (Suppose that the data type is int.)

struct nodeType
{
int info;
nodeType* link;
};

The variable declaration is as follows:

nodeType* head;
Linked Lists: Some Properties
To better understand the concept of a linked list and a node, some important properties of linked
lists are described next.

Consider the linked list in Figure 4.

Figure 4

This linked list has four nodes. The address of the first node is stored in the pointer head. Each
node has two components: info, to store the info, and link, to store the address of the next node.
For simplicity, we assume that info is of type int. Suppose that the first node is at location 2000,
the second node is at location 2800, the third node is at location 1500, and the fourth node is at
location 3600. Table 1 shows the values of head and some other nodes in the list shown in Figure 4.

Table 1: Values of head and some of the nodes of the linked list in Figure 4

Suppose that current is a pointer of the same type as the pointer head. Then the statement
current = head;

Copies the value of head into current. Now consider the following statement:

current = current->link;

This statement copies the value of current->link, which is 2800, into current. Therefore, after this
statement executes, current points to the second node in the list. (When working with linked
lists, we typically use these types of statements to advance a pointer to the next node in the list.)
See Figure 5.

Figure 5:List after the statement current = current->link; executes

Table 2 shows the values of current, head, and some other nodes in Figure 5.

Table 2: Values of current, head, and some of the nodes of the linked list in Figure5
From now on, when working with linked lists, we will use only the arrow notation.

TRAVERSING A LINKED LIST:-


The basic operations of a linked list are as follows: Search the list to determine whether a particular
item is in the list, insert an item in the list, and delete an item from the list. These operations
require the list to be traversed. That is, given a pointer to the first node of the list, we must step
through the nodes of the list.

Algorithm:-
(Traversing a Linked List) Let LIST be a linked list in memory. This algorithm traverses LIST applying
an operation PROCESS to each element of LIST. The variable PTR points to the node currently being
processed.

1. Set PTR=START [Initialize pointer PTR]

2. Repeat Step 3 and 4 while PTR ≠ NULL.

3. Apply PROCESS TO INFO[PTR].

4. Set PTR=LINK[PTR] [PTR now points to the next node] [End of Step 2 Loop]

5. Exit.
Suppose that the pointer head points to the first node in the list, and the link of the last node is
NULL. We cannot use the pointer head to traverse the list because if we use the head to traverse
the list, we would lose the nodes of the list. This problem occurs because the links are in only
one direction. The pointer head contains the address of the first node, the first node contains the
address of the second node, the second node contains the address of the third node, and so on. If
we move head to the second node, the first node is lost (unless we save a pointer to this node). If
we keep advancing head to the next node, we will lose all the nodes of the list (unless we save a
pointer to each node before advancing head, which is impractical because it would require
additional computer time and memory space to maintain the list).

Therefore, we always want head to point to the first node. It now follows that we must traverse
the list using another pointer of the same type. Suppose that current is a pointer of the same type
as head. The following code traverses the list:

current =head;
while(current!=NULL)
{
cout<<current->info<<"";
current=current->link;
}
………………………………………………………………………………………………………………………………………………………..

This section discusses how to insert an item into, and delete an item from, a linked list. Consider
the following definition of a node. (For simplicity, we assume that the info type is int.

struct nodeType
{
int info nodeType* link;
};

We will use the following variable nodeType

*head, *p, *q, *newNode; INSERTION:-


Algorithms which insert nodes into the linked list come up in various situations. We discuss
three of them here. The first one inserts a node at the beginning of the list, the second one inserts a
node after a node with a given location, and the third one inserts a node into the sorted list.

Inserting at the Beginning of the List:-

Suppose our linked list is not necessarily sorted and there is no reason to insert a new node in
any special place in the list. Then the easiest place to insert the node is at the beginning of the
list. An algorithm that does so follows.

Algorithm:-

INSFIRST(INFO,LINK,START,AVAIL,ITEM)

This algorithm inserts ITEM as the first node in the list.

1. [OVERFLOW?] If AVAIL=NULL then write OVERFLOW and Exit.

2. [Remove first node from AVAIL list.] Set NEW=AVAIL and


AVAIL=LINK[AVAIL]

3. Set INFO[NEW]=ITEM [Copies new data into new node]

4. Set LINK[NEW]=START [New node now points to the original first node]

5. Set START=NEW [Change START so it points to the new node ]

6. Exit.

Inserting after a Given Node:- Algorithm:-

INSLOC(INFO,;INK,START,AVAIL,LOC,ITEM)

This algorithm inserts ITEM so that item follows the

node with location LOC or inserts ITEM as a first node

when LOC=NULL.
1. [OVERFLOW?] If AVAIL=NULL then write OVERFLOW and Exit.

2. [Remove First Node from the AVAIL list].

Set NEW=AVAIL and AVAIL=LINK[AVAIL].

3. Set INFO[NEW] =ITEM. [Copies new data into new node.]

4. If LOC=NULL then [Inserts as first node] Set LINK[NEW]=START


and START=NEW.

else [Insert after node with location LOC]

Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW. [End of If Structure]


5. Exit.

Inserting into a Sorted Linked List:-

Suppose ITEM is to be inserted into a sorted linked list. Then ITEM must be inserted between
nodes A and B so that

INFO(A) < ITEM < INFO(B)

The following is the procedure which finds the location LOC of node A that is which finds the
location LOC of the last node in the list whose value is less than ITEM. Traverse the list using
pointer variables PTR and comparing ITEM with INFO[PTR] at every node. While traversing keep
track of the location of the preceding node by using a pointer variable SAVE. Thus SAVE and PTR
are updated by assignments.
Algorithm:-

FINDA(INFO, LINK START, ITEM,LOC)

This procedure finds the location LOC of the last node in a sorted list such that INFO[LOC] <
ITEM or set LOC = NULL.

1. [List Empty?] If START = NULL then set LOC= NULL and

Return.

2. [Special Case?] If ITEM<INFO[START] then Set LOC =NULL and

Return.

3. Set SAVE= START and PTR = LINK[START] [Initialize

Pointers]

4. Repeat Step 5 and 6 while PTR ≠ NULL.

5. If ITEM<INFO[PTR] then

Set LOC =SAVE and Return. [End of If Structure]

6. Set SAVE =PTR and PTR= LINK[PTR] [Update Pointers] [End of Step 4
Loop]

7. Set LOC=SAVE.

8. Exit.

Now we have all the components to present an algorithm which inserts ITEM into a linked list. The
simplicity of the algorithm comes from using the previous two procedures.

Algorithm:-

INSERT(INFO, LINK , START, AVAIL, ITEM)

This algorithm inserts ITEM into a sorted linked list.

1. Call FINDA(INFO, LINK, START, AVAIL , ITEM)

2. Call INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM).

3. Exit.
Consider the linked list shown in Figure 6.

Suppose that p points to the node with info 65, and a new node with info 50 is to be created and
inserted after p. Consider the following statements:

newNode=new nodeType; //create new Node newNode-

>info=50; //store 50 in the newnode newNode->link=p->link;

p->link=newNode;

Table 3 shows the effect of these statements. Table 3: Inserting a node in a linked list
Note that the sequence of statements to insert the node, that is,

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

is very important because to insert newNode in the list we use only one pointer, p, to adjust
the links of the nodes of the linked list. Suppose that we reverse the sequence of the
statements and execute the statements in the following order:
p->link = newNode;
newNode->link = p->link;

Figure: List after the execution of the statement p->link = newNode; followed by the execution of
the statement newNode->link = p->link;

From Figure, it is clear that newNode points back to itself and the remainder of the list is lost.
……………………………………………………………………………………………………………………………………………..

Lab Task:-

Write a C++ code using functions for the following operations.

1. Creating a linked List.

2. Traversing a Linked List.

Create a complete menu for the above options and also create option for reusing it.

Lab Task:-
Write a C++ code using functions for the following operations.

1. Creating a linked List.

2. Traversing a Linked List.

3. Inserting the node at the start of the list.

4. Inserting a node after a given node.

5. Inserting a node in a sorted list.

Create a complete menu for the above options and also create option for reusing it.

You might also like