You are on page 1of 63

IT202 – DS - Unit 2

Lists
(Singly Linked List, Doubly Linked List and Circular List)

1
Array Declaration
int a[7];
Limitations of arrays
we can use arrays whenever we have to store and manipulate
collections of elements. But it has limitations
◦ The dimension of an array is determined the moment the array is created, and cannot be
changed later on
◦ The array occupies an amount of memory that is proportional to its size, independently of
the number of elements that are actually of interest;
◦ If we want to insert a new value in a specific position, or remove it, for each such
operation we may need to move many elements (on the average, half of the elements of
the array); this is very inefficient.
Insertion in Linear Array-an element at the beginning of the array

Insert operation is to insert one or more data elements into an array.


Based on the requirement, new element can be added at the beginning, end or any given index of array
• For each one of these three cases the count of elements increases by 1
• If the array is full, insertion not possible
Insertion in Linear Array- at the end of the array
Insertion in Linear Array- at the given position J
Deletion in Array – at Beginning
• Deletion in array means removing an element and replacing it with the next element or element present
at next index.
• It involves three cases - for any one of these three cases the count of elements decreases by 1
Deletion in Array – an element at the end of the array
Deletion in Array – an element at the given position J
Linked list
◦ LinkedList is a linear data structure where each element is an object. Unlike Array, Each
element is linked to the next through a pointer.
◦ Each element in the LinkedList is called Node.
◦ Each node contains a data and an additional pointer for pointing the next element in the list.
◦ Initial pointer in a LinkedList is called Head. To store a linked list, we just need to remember
where the first element is. We can find the rest by following the pointers.
Linked List - Characteristics
Dynamic nature:
◦ LinkedList is a dynamic structure, means the list can grow or shrink depending upon the
data - making it more powerful and flexible than Arrays.
◦ Unlike Arrays, LinkedList is not stored in a contiguous memory location. Each element in
the list is spread across the memory and are linked by the pointers in the Node.
◦ Thus whenever a new element needs to be added a separate memory is allocated enough
to store both key and the pointer to the next element.
Linked lists
Advantages of Linked List
◦ Linked list is dynamic in nature which allocates the memory when required.
◦ It reduces the access time.
◦ Insert and delete operation can be easily implemented in linked list.

Disadvantages of Linked List


◦ Reverse traversing is difficult in linked list.
◦ Linked list has to access each node sequentially; no element can be accessed randomly.
◦ In linked list, the memory is wasted as pointer requires extra memory for storage.
Linked List Implementation
Two methods
◦ Using two arrays (one for data and another for link)
◦ Using dynamic memory allocation / deallocation (with pointers) – (eg. malloc(), free())
Linked List Representation – using arrays

DATA start
LINK 105 110 117 123 0
1 110 3
2
Storage Pool
3 117 9
Node
start

4 Values
5 105 1 Storage Getnode() – provides array index x if it is a free node (eg. 2, 4, 6..)
6 functions Ret(x) – returns node x to the storage pool
Declaration int DATA[9], LINK[9], start;
7
And coding Start = 5;
8 DATA[5] = 105; LINK[5] =1;
9 123 0 Operations Insert, delete
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 3 105 110 117 123 0
2
Y 3 117 9
Y
start 4
5 105 1
Insert(X, 119) – STEPS
6 (assume X = 1)
7 Step 1 : Getnode(Y)
(assume Y = 4)
8
9 123 0
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 3 105 110 117 123 0
2
Y 3 117 9 119
3 Y
start 4 119
5 105 1
Insert(X, 119) – STEPS
6
(assume X = 1)
7 Step 1 : Getnode(Y)
8 (assume Y = 4)
Step 2: Copy data in the node Y
9 123 0 DATA[Y] = 119
Step 3 : Copy link of X to link of Y
LINK(Y) = LINK(X)
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 4 105 110 117 123 0
2
Y 3 117 9 119
3 Y
start 4 119
5 105 1 Insert(X, 119) – STEPS
6 (assume X = 1)
Step 1 : Getnode(Y)
7 (assume Y = 4)
8 Step 2: Copy data in the node Y
DATA[Y] = 119
9 123 0
Step 3 : Copy link of X to link of Y
LINK(Y) = LINK(X)
Step 4 : Copy Y to link of X
LINK(X) = Y
Linked List using arrays – insertion result
DATA LINK
start
1 110 4 105 110 117 123 0
2
3 117 9 119
3
start 4 119
5 105 1
6
7
8
9 123 0
Linked List using arrays – Deletion step
DATA LINK X Y
X
start
1 110 3 105 110 117 123 0
2
3 117 9
Y
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node Y positioned after X)
(assume X = 1 and so Y is 3)
7
8
9 123 0
Linked List using arrays – Deletion step
DATA LINK X Y
X
start
1 110 9 105 110 117 123 0
2
3 117 9
Y
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node positioned after X)
(assume X = 1 and so Y is 3)
7 Step 1 : detach / delink the node Y
8 LINK(X) = LINK(Y)
9 123 0
Linked List using arrays – Deletion step
DATA LINK X
X
start
1 110 9 105 110 123 0
2
3
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node positioned after X)
(assume X = 1 and so Y is 3)
7 Step 1 : detach / delink the node
8 LINK(X) = LINK(Y)
Step 2: delete / remove node Y
9 123 0 RET(Y)
Linked List using arrays – Deletion result
DATA LINK
start
1 110 9 105 110 123 0
2
3
4
5
start 105 1
6
7
8
9 123 0
Linked List using arrays – Additional operations
1. display(start) – algorithm to display the data in the linked list – start
2. InsertFront(int list, int d) – algorithm to insert the data d in the first position of the linked list
3. DeleteFront(int list)– algorithm to delete the first node of the linked list
4. InsertEnd(start, data) – algorithm to insert the data d at last position in the linked list
5. DeleteEnd(start, data) – algorithm to delete last node in the linked list

start
105 110 117 123 0
Linked List using arrays – Operations - Display
start
105 110 117 123 0

display(start) – algorithm to display the data in the linked list - start


display(int start)

Step1 : p = start;
Step 2 : while (p != 0)
Step 3 : print(DATA[p])
Step 4 : p = LINK[p]
Step 5 : Endwhile
Step 6 : End display

start
105 110 117 123 0
p
Linked List using arrays – Operations - InsertFront
list
105 110 117 123 0

list
105 110 117 123 0

d
p

d 105 110 117 123 0


list
InsertFront(int list, int d) – algorithm to insert the data d in the first
position of the linked list
InsertFront(int list, int d)
Step1 : Getnode(p);
Step 2 : DATA[p] = d;
Step 3 : LINK[p] = list;
Step 4 : list = p;
Step 5 : End InsertFront
Linked List using arrays – Operations - DeleteFront
list
105 110 117 123 0

list
105 110 117 123 0
p
list
p
105 110 117 123 0

list
110 117 123 0

DeleteFront(int list)– algorithm to delete the first node of the linked list
DeleteFront(int list)
Step1 : p = list;
Step 2 : list = LINK[list];
Step 3 : RET(p);
Step 4 : End DeleteFront
Linked List using arrays – Operations - InsertEnd
start
105 110 117 123 0

start
105 110 117 123 0
p
Y
start
105 110 117 123 d 0
p

InsertEnd(start, data) – algorithm to insert the data d at last position in the linked list
InsertEnd(int start, int d)
Step1 : p = start;
Step 2 : while (LINK[p] != 0)
Step 3 : p = LINK[p]
Step 4 : Endwhile
Step 5 : Getnode(Y);
Step 6 : DATA[Y] = d
Step 7 : LINK[p] = Y
Step 8 : LINK[Y] = 0
Step 9 : End InsertEnd
Linked List using arrays – Operations - DeleteEnd
start
105 110 117 123 0

start
105 110 117 123 0
p q
start
105 110 117 0 123 0
p q

DeleteEnd(start, data) – algorithm to delete last node in the linked list


DeleteEnd(int start)
Step1 : p = start; q = LINK[p];
Step 2 : while (LINK[q] != 0)
Step 3 : p = q; q = LINK[q];
Step 4 : Endwhile
Step 5 : RET(q);
Step 6 : LINK[p] = 0
Step 7 : End DeleteEnd
Linked list – Additional operations
◦ Insert a data in an ordered linked list
◦ Merge two linked lists
◦ Break a linked list into two at position i
◦ Search for a data in the linked list and report the position, if it matches or 0
◦ Reverse a linked list
◦ Traverse a linked list and check whether the forward and reverse order of the data are
same
◦ Replace / update each occurrence of the data ‘i’ with ‘j’ in the linked list
Singly Linked list using pointers / dynamic memory
#include<stdlib.h>
#include <stdio.h>

void create();
void display();
void insert_begin();
void insert_end();
void delete_begin();
void delete_end();

struct node Self Referential


structure using info next
{
int info; pointer
struct node *next;
};
start
struct node *start=NULL;
Singly Linked list using pointers / dynamic memory
int main()
{
int choice;
while(1){

printf(“\n MENU \n");


printf(“\n 1.Create \n");
printf(“\n 2.Display \n");
printf(“\n 3.Insert at the beginning \n");
printf(“\n 4.Insert at the end \n");
printf(“\n 5.Delete from beginning \n");
printf(“\n 6.Delete from the end \n");
printf(“\n 7.Exit \n");
printf(“\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
Singly Linked list using pointers / dynamic memory
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Delete from beginning
6.Delete from the end
7.Exit
--------------------------------------
Enter your choice: □
Singly Linked list using pointers / dynamic memory
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_begin();
break;
case 4: insert_end();
break;
case 5: delete_begin();
break;
case 6: delete_end();
break;
case 7: exit(0);
break;
default: printf(“\n Wrong Choice:\n");
break;
}
}
return 0;
}
Singly
void create()
Linked list using pointers – Create new list
{
Input start
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
Step 1 temp
printf(“\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL; Step 2 Enter the data value for the node : 34
if(start==NULL)
{ Step 3 temp 34 0
start=temp;
} Step 4 start 34 0
else output temp
{ Step 5
ptr=start; 34 50 17 25 9 0
Output temp
while(ptr->next!=NULL) start
ptr
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
Singly Linked list using pointers – display
void display()
{ 34 50 17 25 9 0
struct node *ptr; Input start
if(start==NULL)
{
Step 1 start
printf(“\nList is empty:\n"); 34 50 17 25 9 0
return; ptr
}
else
{ Step 2 start
34 50 17 25 9 0
ptr=start;
ptr
printf(“\nThe List elements are:\n");
while(ptr!=NULL) Output The List elements are:
{ 34 50 17 25 9
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
Singly Linked list using pointers – insert node at beginning
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL) Input 34 50 17 25 9 0
start
{
start=temp;
} temp start
else Step 1
34 50 17 25 9 0
{
temp->next=start;
start=temp; Step 2 Enter the data value for the node: 66
}
}
Step 3 temp 50 17 25 9 0
66 34
Output start
Singly Linked list using pointers – insert node at end
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL) start 66 34 50 17 25 9 0
Input
{
start=temp; start 66 9 0
34 50 17 25
} Step 1
else temp
{
Step 2 Enter the data value for the node: 40
ptr=start;
while(ptr->next !=NULL)
{ Step 3 start 66 34 50 17 25 9 0 40 0
ptr=ptr->next ; ptr
} temp
Step 4
ptr->next =temp; start 66 34 50 17 25 9 40 0
Output
}
} ptr
Singly Linked list using pointers – delete begin
Input start 66 34 50 17 25 9 40 0
void delete_begin()
{ Step 1 start 66 34 50 17 25 9 40 0
struct node *ptr; ptr
if(ptr==NULL)
{ Step 2 ptr 66 34 50 17 25 9 40 0
printf(“\nList is Empty:\n");
return; start
} Step 3 The deleted element is : 66
else
{ Step 4 start
34 50 17 25 9 40 0
ptr=start; Output
start=start->next ;
printf("nThe deleted element is :%dt",ptr->info);
free(ptr);
}
}
Singly Linked list using pointers – delete end
void delete_end()
{ struct node *temp,*ptr; Input start
if(start==NULL) Step 1 ptr 34 50 17 25 9 40 0
{ printf(“\nList is Empty:");
start
exit(0); } Step 2 34 50 17 25 9 40 0
else if(start->next ==NULL)
{ ptr=start; temp ptr
start
start=NULL; 34 50 17 25 9 0 40 0
Step 3
printf(“\nThe deleted element is:%d\t",ptr->info); ptr
free(ptr); } temp
else Step 3 The deleted element is : 40
{ ptr=start;
while(ptr->next!=NULL)
Step 4 start
{ temp=ptr; 34 50 17 25 9 0
Output
ptr=ptr->next; }
temp->next=NULL; temp
printf(“\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
Singly Linked List – Advantages
◦ Insertions and Deletions can be done easily.
◦ It does not need movement of elements for insertion and deletion.
◦ It space is not wasted as we can get space according to our requirements.
◦ Its size is not fixed.
◦ It can be extended or reduced according to requirements.
◦ Elements may or may not be stored in consecutive memory available, even then we can
store the data in computer.
◦ It is less expensive.
Singly Linked List – Disadvantages
◦ It requires more space as pointers are also stored with information.
◦ Different amount of time is required to access each element.
◦ If we have to go to a particular element then we have to go through all those elements that
come before that element.
◦ Off by one errors causes crashes. The deference of location zero typically causes a crash.
Circular Linked list
Circular Linked list – Insertion / Deletion
Insertion of elements into Circular Linked List
◦ Insertion in circular linked list is similar to that of insertion in singly linked list, except
next of the last element is pointed to head.

Deletion of elements from Circular Linked List


◦ There is no change in implementation of deletion of a node, it is similar to that of singly
linked list.
Array Representation of Two circular linked lists
START1

S01 S03 S06 S08 S10 S11

S09 S07 S05 S04 S02

START2
Inserting a Node at the Beginning of a Circular Linked List
Inserting a Node at the End of a Circular Linked List
Deleting a Node from a Circular Linked List
Deleting the Last Node from a Circular Linked List
Doubly Linked Lists
START

0 H E L L O 0
DLL Using Arrays – Node structure with 3 arrays

DLL Using Pointers – Node structure definition


struct node
{
struct node *prev;
int data;
struct node *next;
};
Doubly Linked List Operations - Insertion
Inserting a Node at the Beginning of a Doubly Linked List

Input

Step 1
NEW_NODE

Step 2
Output
Doubly Linked List Operations - Insertion
Inserting a Node at the End of a Doubly Linked List

Input

Step 1

Step 2

Step 3
Result
Doubly Linked List Operations - Insertion
Inserting a Node After a Given Node in a Doubly Linked List

Input

Step 1

Step 2

Step 3

Step 4

Output
Doubly Linked List Operations - Deletion
Deleting the First Node from a Doubly Linked List

Input

Step 1
Output
Doubly Linked List Operations - Deletion
Deleting the Last Node from a Doubly Linked List

Input

Step 1

Step 2

Step 3
Output
Doubly Linked List Operations - Deletion
Deleting the Node After a Given Node in a Doubly Linked List

Input

Step 1

Step 2

Step 3

Step 4
Output
APPLICATIONS OF LINKED LISTS
◦ Linked lists can be used to represent polynomials and the different operations that can
be performed on them.
◦ Consider a polynomial 6x3 + 9x2 + 7x + 1.
◦ Every individual term in a polynomial consists of two parts, a coefficient and a power.
◦ Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their
powers respectively.
◦ The linked list representation of the polynomial
Polynomial Addition
Polynomial Addition using Single Linked list

List 1 = 5X3+2X2+4

List 2 = 8X2+10X+3

R-List = 5X3+10X2+10X+7

struct node
{
int co-ef, exp;
struct node *next;
};

You might also like