Unit 3 - LL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Unit III

Linked Lists (16 Marks)


1. A linked list is a linear collection of data items which is created using
dynamic memory allocation.
2. It can be used to create linear and non-linear data structures. All
elements in a linked list have either zero, one or more successors.
3. The collection of elements in a linked list is called nodes. Each node
points to the next node. A node can be inserted and deleted at any
position.
4. The linked lists support dynamic memory allocation.
5. Node: Each node of the linked list has two parts. The first part is the
data field that contains the information, and the second part called
the link field or pointer field that contains the address of next node
in the list.
Node

Data field Pointer field

6. The pointer field of the last node is assigned a NULL value to indicate
end of the list. A pointer to the starting node of the list is used to
access the list. It is called the head pointer, that contains the address
of first node of the list. Once it is available the next successive nodes
can be accessed. Head pointer is also called as the external pointer.

10 20 30 40 50 NULL
head

7. When there is no node in the list it is called empty list. In this case
head pointer contains NULL value.
head NULL

Advantages of linked lists


1. Easy insertion and deletion of data. There is no need for shifting the
elements present in the linked list to accommodate a new element
or to delete an element.
2. Linked lists can grow and shrink during program execution. So no
problem of memory shortage.

Disadvantage of linked lists


1. Since there is no physical sequence of elements, we are restricted to
sequential searches only.
2. Linked lists may be more time consuming for the system to allocate
and free the storage to manipulate a variable sized list.
Dynamic representation
The linked list is implemented using the free pool of storage. It is a
collection of free memory spaces and a memory manager program that
manages it.
Dynamic memory management techniques are used to allocate
memory or to release unwanted space at runtime, thus optimizing the use
of storage space.
malloc – used to allocate a block of memory. It reserves the specified size
of memory and returns a pointer of type void.
ptr = (cast type *) malloc (bytesize);

free – this function is used to release the memory allocated by ptr


free(ptr);

Types of linked lists


1. Singly linked list
In a singly linked list, each node has two parts, a data field and a
pointer field that contains the address of the next node.

Data field Pointer field

Ex.:

10 20 30 NULL
head
2. Circular linked list
In a circular linked list, the last node is made to point to the first
node. So, there is no NULL pointer.

10 20 30
head

The structure defined for a node of a singly linked list or circular


linked list is as follows,

struct node
{
int data;
struct node *next;
};
The above structure has two members, a variable data, to hold the
element and the another element is the variable next, which stores the
address of the next node in the list.

3. Doubly linked list


In a doubly linked list each node has three parts, one is the data field,
the second is the pointer field that contains address of the previous node
and the third is the pointer field that contains the address of the next node
in the list.
This list can be traversed in both the directions, from beginning to
the end or in backward direction, from end of the list to the beginning,
Pointer to Pointer to
Data field
previous node next node

Ex.:
NULL 10 20 30 NULL

head

The node structure is given as follows,


struct node
{
int data;
struct node *left;
struct node *right;
};

Operations on Linked Lists


1. Creation
2. Insertion of a node
a. At beginning
b. At end
c. At specific location
3. Deletion of a node
4. Searching
5. Traversing
6. Reversing
7. Concatenation
8. Merging

Creating a linked list


1. To create an initial linked list of integer data elements, the structure of
the node can be defined as follows,

typedef struct node


{
int data;
struct node * next;
} Node;

Node *head, *add, *temp;


head = NULL;

The above statement declares three pointers, head, points to the starting
node of the list; add, points to every new node to be added to the list and
temp, stores the address of the last node added to the list.
Here, head = NULL, denotes the list is empty.
2. To create the linked list, the dynamic memory allocation function,
malloc is used. This function returns a pointer of type void. Hence it has
to be type cast as Node pointer.

Consider, first data element 10 to be added to the list.

add = (Node *) malloc (sizeof(Node));


add->data = 10;
add->next = NULL;
3. Since this is the first node in the list, the pointers head and temp, both
point to this new node.
head add
10 NULL
temp

if (head == NULL)
{
head = add;
temp = add;
}

4. Consider, the next data element 20 to be inserted.

First, a new node is created using the malloc statement.

add
20 NULL

Now, this node needs to be placed in the list, as follows,

if(head!=NULL)
{
temp->next = add;
temp = add;
}
add temp

10 20 NULL
head
This step is repeated for all the remaining nodes.
Insertion

1. For the insert operation a new node needs to be created using malloc
function.

2. Suppose the structure of node is as follows,

typedef struct node


{
int data;
struct node * next;
} Node;

Node *head, *add, *temp;


head = NULL;

The above statement declares three pointers, head, points to the


starting node of the list; add, points to every new node to be added to
the list and temp, stores the address of the last node added to the list.
3. Consider the following list,

10 20 30 NULL
head

The node for the new item, 40 can be created as follows,

add = (Node *) malloc (sizeof(Node));


add->data = 40;
add->next = NULL;

add
40 NULL
4. The new node add, can be inserted at three different positions,

a. At the beginning

b. At the end

c. At some specific location

5. Case 1: At the beginning

The new node, add can be added in the beginning of list as follows,
add->next = head;
head = add;

10 20 30 NULL

head add
40

6. Case 2: At the end

Here, we need to traverse till the end of the list using a temp pointer
as follows,

temp = head;
while(temp->next!=NULL) temp = temp->next;
temp->next = add;
temp

10 20 30
head

40 NULL
add

7. Case 3: At some specific location

Suppose the new node is to be inserted at location 3.

In this case, loc=3 and the so, temp pointer needs to placed at node 2.

temp=head;
for(i=1;i<loc-1;i++) temp=temp->next;
add->next = temp->next;
temp->next = add;
temp

10 20 30 NULL
head

add
40
Deletion
For deletion operation item to be deleted is compared with
elements in the list and when node is found its memory is released using
the free function.
Consider the following list,

10 20 30 40 NULL

head

There are two possible cases, either item to be deleted is present in head
node or it is present in any other node.

Case 1: Deleting head node, say item = 10


In this case head pointer is modified as follows,

temp = head;
head = head->next;
free(temp);

20 30 40 NULL

head

Case 2: Deleting node other than head node, say item = 30


Here, we need to traverse list to search node to be deleted. While
traversing a shadow pointer prev follows pointer temp as follows,
temp=head;
while(temp!=NULL) && temp->data != item)
{
prev = temp;
temp = temp->next;
}
By above loop, either the list ends or temp points to node to
be deleted. If node is found then the previous node’s address pointer is
modified as follows,
prev temp

10 20 30 40 NULL

head

if(temp==NULL)
printf(“Item not in list”);
else
prev->next = temp->next;
free(temp);

Searching
The process of searching a key element in a linked list requires
traversing through the list comparing each node’s data with key value. The
process ends when either the key value is found, or end of list occurs.
Consider the following list,

10 20 30 40 NULL

head
Suppose, item 30 is to be searched. The variable loc is initially set to 1 and
can be gradually incremented to give location of the key element in the
list. The list can be traversed as follows,
for(temp=head, loc=1 ; temp != NULL; temp=temp->next, loc++)
if(temp->data == item) break;

if(temp==NULL)
printf(“Item not in list”);
else
printf(“Item found at location %d”, loc);

1. Initially, temp = head and loc = 1


temp

10 20 30 40 NULL

head

Since temp->data ≠ 30 and temp!=NULL, loop is repeated

2. Here, loc =2
temp

10 20 30 40 NULL

head

Since temp->data ≠ 30 and temp!=NULL, loop is repeated

3. Here, loc = 3
temp

10 20 30 40 NULL

head

Since temp->data = 30 loop breaks


loc = 3 and item is found.

Algorithm to count number of nodes in a singly linked list.


Consider the following list,

10 20 30 40 NULL

head

Here, count of nodes is 4.


Let the linked list node be defined as structure data type Node.
Declare pointer head, to point to first node of list and temp, as pointer to
traverse the list.
The algorithm is as follows,
1. Initialize pointer temp=head and count =0
2. Repeat step 3 if temp !=NULL else goto 5
3. count++ and temp=temp->next
4. Display count
Example: Search the number 22 in a linked list with elements 15, 20, 22,
58, 60
Circular linked list
1. A linked list in which last node points to the header node is called the
circular linked list. This forms a circular loop.

10 20 30
head

A circular linked list can either be singly linked or doubly linked


• In singly linked circular list, the last node points to the head node.
• In doubly linked circular list the head node points to last node as
well
2. The structure of a singly circular linked list is same as a singly linked list
but there is no NULL pointer. The next pointer in the last node points
to the head node.
3. A disadvantage of a linked list is that with the node pointers in linked
list there is no way to reach the preceding nodes. This drawback can be
overcome by making the list circular.
4. Advantage:
a. In a linked list a member node is accessible from a particular node,
that is the head node only. But in a circular linked list every member
node is accessible from any node by simply chaining through the list.
Any node can be the starting point and we need to stop when that
node is visited again.
b. The NULL pointer is removed in circular linked list. It may create
problems if not managed properly.
c. Can be used to implement circular queue.
d. Circular doubly linked lists can be used to implement advanced data
structures like Fibonacci heap.
5. Disadvantage: While traversing the circular linked list due to lack of
NULL pointer there may be a possibility to get into an infinite loop.
Thus, it is necessary to identify the end of list using some pointer.

Array/ Static memory allocation Linked List/ Dynamic memory allocation


1. Array is a collection of 1. Linked list is a collection of similar
elements of similar data type nodes that can contain elements of
more than one data type
2. Array elements can be 2. Linked list elements can be
accessed using the index values accessed sequentially using
that starts from 0. Hence pointers. To access nth element
accessing is fast with time time complexity is O(n)
complexity O(1).
3. In array, the insertion and 3. In linked list a new element is
deletion operations take more stored in the first available free
time, as the memory locations memory location, the address of
are consecutive and fixed. which is stored in the previous
node’s address field. So, insertions
and deletions are comparatively
fast
4. Memory is allocated as soon as 4.Memory s allocated at run time as
the array is declared, at compile and when a new node is added. This
time. This is called static memory is called dynamic memory
allocation. allocation
5.In an array, each element is 5. In a linked list, each node points
independent and can be to the next node so only sequential
accessed randomly using its access is possible..
index
6. Arrays can be single 6. Linked list types are: singly,
dimensional, two dimensional or doubly and circular
multi-dimensional
7. Array representation 7. Linked list representation

arr[0] 10
arr[1] 20
arr[2] 30
10 20 30 NULL
arr[3]
arr[4] head
arr

Applications of linked lists


1. A linked list can be used to implement linear and non-linear data
structures stacks, queues, trees and graphs. The problem of static
memory allocation can be overcome using linked lists.
2. Polynomial manipulation – Linked lists are used for implementation
of polynomials. Operations such as addition, multiplication etc. can
be performed on these polynomials.
3. Linked dictionary – While compiling a program a symbol table is
used by the compiler. It is implemented as a linked dictionary. It
contains variable names and their corresponding values.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy