Sei sulla pagina 1di 47

Linked List

DATASTRUCTURE

DATRUCTURE IS THE WAY OF


ORGANIZING ALL DATA ITEMS IN
ORDER THAT NOT ONLY ELEMENTS TO
BE STORED BUT ALSO THE RELATION
BETWEEN THE ELEMENTS
DATASTRUCTURE

LINEAR NONLINEAR

ARRAY
TREES&
LINKLIST STACK
QUEUE
GRAPH
Linked List
What are the problems with Arrays
- Size is fixed
-Array Items are stored contiguously
-Insertions and deletion at particular position is
complex
Why Linked list ?
-Size is not fixed
-Data can be stored at any place
-Insertions and deletions are simple and faster
Advantages of Linked List
Dynamic Memory Allocation : Linked list are dynamic
data structure i.e. they can grow or shrink during the
execution of a program
Efficient memory utilization: In linked list (or
dynamic) representation, memory is not pre-allocated
i.e. Memory is allocated whenever it is required and it is
deallocated (or removed) when it is not needed
Insertion and deletion are easier and efficient:
Linked list provides flexibility in inserting a data item at
a specified position and deletion of a data item from the
given position
Linked List
Definition: The linked list is a linear data
structure consists of nodes which is created
dynamically.
Each node of the linked list is having two parts,
one is info part and another is link part.
info: It contains information or data element.
link: It is a pointer variable which holds the
address of the next node.
In the linked list there exists a special pointer
called as start, which holds the address of the
What does a Linked List look like ?
A linked list is a collection of nodes with various fields

It contains data field and Address field or Link field


Pointer to the
first node

Link Field/
Info field
Address Field
Fig : Node

Fig : Linked List with three nodes

Initial value of start = NULL

Fig : Linked List representation in Memory


Linked List Example
Suppose we want to store a list of integer numbers using
linked list. Then it can be schematically represented as

Linked list has following disadvantages


1. More memory: to store an integer number, a node
with integer data and address field is allocated That
is more memory space is needed
2. Access to an arbitrary data item is little bit
cumbersome and also time consuming
Memory Requirement : Array Vs Linked List
What is the amount of memory required to store
five floating point numbers in an (i)array
(ii)linked list ????????????

If we want to store 5 floating point numbers


in a linked list, then the total number of bytes
required is 5 6 + 2 = 32 bytes Whereas we
store the same in an array, it will require 5 x 4
= 20 bytes which is less than 32 bytes
Memory representation of Linked list

For representing the above linked list LIST in memory, two


linear arrays are required such that
INFO INFO[K] contains the information part of a node
of LIST
LINK - LINK[K] contains the next pointer field of a node
of LIST
Node declaration in a Link List with one information part
struct node
{
int info; 2 Bytes
struct node *link; 2 Bytes
};
typedef struct node *NODE;
Meaning of this: A node is of the type struct and
contains info field and link field
Node declaration in a Link List with more than one
information part
A node may have more than one information part, for example:

struct node
{ int age;
char name[20];
float sal;
struct node *link; };
Linked List Types
Singly Linked list
Circular singly linked list
Doubly linked lists
Circular doubly linked lists
Graphical Representation : Singly Linked List

First

10 1000 15 20 NULL
2000

4000 1000 2000


Graphical Representation : Circular Singly
Linked List
First

10 1000 15 2000 20 4000

4000 1000 2000

Last node contains the address of the


first node
Graphical Representation : Doubly Linked List
First

NULL 10 2000 1000 15 3000 2000 20 NULL

1000 2000 3000

Contains the address of previous node


and next node
Graphical Representation : Circular Doubly
Linked List
First

3000 10 2000 1000 15 3000 2000 20 1000

1000 2000 3000

Contains the address of first node and


last node
Operations on Singly Linked List
The basic operations on linked list are as follows:
Creation: Creation of a linked list
Insertion:
At the beginning of a linked list
At the end of a linked list
At the specified position
Deletion:
Beginning of a linked list
End of a linked list
Specified position in the list
Operations on Singly Linked List
Traversing: Visiting each node exactly
once, it may be for displaying, counting,
searching
Concatenation: It is the processing of
appending (joining) the second list to the
end of the first list consisting of m nodes
When we concatenate 2-lists, the 2nd list
having n nodes, then the concatenated list
will have (m + n) nodes
Operations on Singly Linked List
Reverse: Accessing all elements in a
linked list in reversed order i.e. Last
element will be accessed 1st and 1st element
will be accessed at the end
Searching: Finding weather a given item is
found in the given list
Sorting: Arranging all the elements in the
linked list in a required sequence i.e. in a
ascending or descending order
Single Linked List : Creation

start: It is a pointer pointing to the 1st node in the


list
curr: It is a pointer which keeps the address of
newly created node
temp: It is a pointer always pointing to last node
Dynamic Allocation of Memory
malloc ( ):
malloc() allocates single block of memory
The name malloc stands for "memory allocation The
function malloc( ) reserves a block of memory of specified size
and return a pointer of specified form
By default garbage value
Syntax:
ptr = (cast-type *) malloc (byte-size);
Example:
ptr = (int *) malloc (100*sizeof (int) );
curr = (struct node *) malloc(sizeof(struct node));
Dynamic Allocation of Memory
calloc ( ):
The name calloc stands for "contiguous allocation"
calloc() allocates multiple blocks of memory each of same size
By default the allocated memory will be initialized to zero
Syntax:
ptr = (cast-type*) calloc (n, element-size);
Example:
ptr = (float*) calloc (25, sizeof (float) );
curr = (struct node *) calloc (10, sizeof (struct node) );
Single Linked List : Creation

The above picture denotes a situation where the first node is


created for an empty link list
That means before creation of this node the linked list was
empty i.e. start = curr = temp = NULL
Single Linked List : Creation
struct node
{ int info; 2 Bytes
struct node *link; 2 Bytes };
typedef struct node *NODE;

Allocation of memory to newnode


newnode =( NODE ) malloc (sizeof(struct node));
It returns address of the location with piece
of memory of the size struct i.e. for information
field and address field
Creating a linked list and adding nodes

Here the new node created is appended to the linked list i.e.
the new node is added to the end of the linked list
Inserting a new node to the end
Creating a link list
Algorithm: create_link_list (start, curr, info, link, item, temp)
info: information part of a node
link: link part of the node
item: item to be stored in the info part of
node
start: It is a pointer pointing to the 1st node
temp: pointer pointing to the last node
curr: It is a pointer which keeps the address
of newly created node
Algorithm: create_link_list (start, curr, info, link, item, temp)
Step 1: create a new node and store its address in curr
Step 2: [check for overflow]
if ( curr = = NULL)
print Overflow Step 6: if you wish to continue
goto step 1
exit
Step 3: a) curr info = item Step 7: exit
b ) curr link = NULL
Step 4: if (start = = NULL)
start = curr
temp = curr
else
temp link = curr
temp = curr
Inserting a node as a 1st node
Inserting a node as a 1st node in the linked list
follows the steps:
Creating a node.
Checking for overflow.
Putting the information in the information
part of the newly created node.
curr info = item;
or scanf (%d, &curr info);
curr link = NULL.
curr link = start
Algorithm : insert_node as a first node (list, link, info, curr, item)
list: Given single linked list link: Link part of the node.
info: Information part of the node curr: Newly created node.
item: Item to be inserted at 1st position of a linked list.
Step 1: [checking for overflow]
if (curr = = NULL)
print Overflow
goto step 4
[end if ]
Step 2:
curr info = item curr link = NULL
Step 3: curr link = start
start = curr
Step 4: exit
Inserting a node as a last node
insert_lastnode (link, list, info, curr, temp, item)
link: Link part of the node
list: Given single link list
info: Information part of the node
curr: Newly created node
temp: Temporary pointer variable, which will be
used for traversing and at last will hold address of
the last node
item: Item to be inserted as a last node of the
linked list
Step 1: if (curr = = NULL) print overflow
goto step 6
Step 2: curr info = item
curr link = NULL
Step 3: if(start = = NULL)
start = curr
go to step 6
Step 4: temp = start
while (temp link ! = NULL)
temp = temp link
Step 5: temp link = curr
Step 6: exit.
Traverse_linklist (list, start, temp, info, link)
list: The given linked list start: Has address of the 1st node
temp: Temporary pointer variable link: Link Part of the node
Step 1: [Check for empty list]
if (start = = NULL)
print List is empty
go to step 4.
Step 2: temp = start
Step 3: while (temp ! = NULL)
print temp info
temp= temp link
[end while]
Step 4: exit.
Reversing a Singly Linked Linear List
Physically we cannot traverse a single linked
list in reverse order, but we can display the
elements in reverse order by storing the
values of each node in an array at the time of
forward traversing

After storing the elements in the array,


display the array in reverse order
Algorithm:reverse_traverse ( list , start, temp , info, link, a)
list: The given linked list
info: Information part of the node
link: Link part of the node.
start: Has address of the 1st node
temp: Pointer variable used for traversing
a: A linear array
Step 1: If (start = = NULL)
print List is empty
goto step 6
Step 2: temp = start and set i = 0
Step 3: for (temp = start; temp ! = NULL; temp = temp link)
a [ i ] = temp info
i++
Step 4: i--
Step 5: while ( i > = 0)
print a [ i ]
i=i1
Step 6: exit.
Sorting in single linked list
Sorting is a process in which the data elements of a
list are arranged either in ascending or
descending order sequentially in the node of that
list.
Algorithm: Sort (list, start, link, info, num, temp1, temp
Step 1: set temp1 = start and temp2 = temp1->link
Step 2: while temp1 NULL repeat step (3) to step (5)
Step 3: A. while temp2 NULL repeat step (a) and (b)
Step 4: if temp1 info > temp2 info then :
set num = temp1 info
set temp1 info = temp2 info
temp2 info = num.
B. set temp2 = temp2 link
[End of step 4 while loop]
Step 5: set temp1 = temp 1 link
temp2=temp1 link [End of step2 while loop]
Step 6: Exit.
Concatenation of two Single Linked Lists

start1

100 15 212 25 305 9 \0

100 212 305

start2

200 32 315 41 112 12 415 2 \0

315 112 415


200
Algorithm : Concatenate (list1, list2, start1, start2, temp, link, info)

list1: Existing link list1 list2: Existing link list2


start1 and start2: Pointer variables, holding the starting
address of the 1st node of list1 and list2 respectively
temp:Temporary pointer variable used for traversing the list1
link: Pointer variable, Which holds the address of next node.
info: Information part of the node.
Step1: a. set temp = start1
b. while (temp link ! = Null)
set temp = temp link
[End While]
Step2: Set temp link = start 2.
Step4: Exit