Data Structures - Module 4
Data Structures - Module 4
20ADS33
Module 4
Linked List
Other list structures
Hashing
Syllabus
Module 1 Data Structure operations
Sorting algorithms
Dynamic memory allocation
Module 2 Stacks
Representation of Stacks in C
Applications
Module 3 Queues
Module 4 Linked List
Other list structures
Hashing
Other list structures - Circular lists and it’s primitive operations, Doubly linked
lists and it’s primitive operations, Applications of linked lists: Addition of long
positive integers, addition of Polynomials.
Special name is given to the address of first node as Head and last
node in the linked list can be identified because its next portion
points to Null
Linked list Data Structure
• A node contains two fields i.e. data stored at that particular
address and the pointer which contains the address of the next
node in the memory.
• The last node of the list contains pointer to the null.
Uses of Linked List
The list is not required to be contiguously present in the memory.
The node can reside any where in the memory and linked
together to make a list. This achieves optimized utilization of
space.
list size is limited to the memory size and doesn't need to be
declared in advance.
Empty node can not be present in the linked list.
We can store values of primitive types or objects in the singly
linked list.
Why use linked list over array?
Array contains following limitations:
1.The size of array must be known in advance before using it in the program.
2.Increasing size of the array is a time taking process. It is almost impossible
to expand the size of the array at run time.
3.All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the
limitations of an array. Using linked list is useful because,
4.It allocates the memory dynamically. All the nodes of linked list are non-
contiguously stored in the memory and linked together with the help of
pointers.
5.Sizing is no longer a problem since we do not need to define its size at the
time of declaration. List grows as per the program's demand and limited to
the available memory space.
Singly Linked List
Arrange the name of the students
according to the 1st letter of their names
Michael
Rock
John
Smith
Tony
Alpha
Mark
Contains the
Contains address of the
the Data Link next node of the
actual list
data Node
Representation of the single linked
list
Suppose we want to store a list of the numbers: 23, 54, 78,
90
23 54 78 90
1000 2000 3000 4000
1000
Head
Self Referential Structure: It is a structure which
contains a pointer to a structure of the same type.
struct abc {
int a;
char b;
struct abc *self;
};
We use self referential structure for creating a node of the
single linked list
Node representation in C
struct node {
Node
int data;
struct node *link;
};
Creating a node in C
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link; Node
};
void main() Null
{ head
struct node *head = Null;
1000
head=(struct node *)malloc(sizeof(struct node )); head
head->data = 45;
45 Null
head->link=Null; 1000
printf(“%d”, head->data); 1000
} head
Traversal if(head==0)
#include <stdio.h> {
head=temp=newnode;
#include <stdlib.h>
}
struct node { else
int data; {
struct node* next; temp->next=newnode;
}; temp=newnode;
struct node *head, *newnode, *temp;}
head=0; printf(“Do you want to continue (0, 1)\
int choice; n”);
scanf(“%d”, &TraversingCode
choice);
while(choice)
} temp=head;
{ while(temp!=0)
newnode = (struct node*)malloc(sizeof(struct node));
{
printf(“Enter the data\n”); printf("%d", temp->data
scanf(“%d”, &newnode->data); temp=temp->next;
newnode->next=0; }
Operations on Singly Linked List
• Insertion
The insertion into a singly linked list can be performed at different positions.
Based on the position of the new node being inserted, the insertion is
categorized into the following categories.
Sl. No Operation Description
1. Insertion at It involves inserting any element at the front of the list. We just
beginning need to a few link adjustments to make the new node as the
head of the list.
2. Insertion at end It involves insertion at the last of the linked list. The new node
of the list can be inserted as the only node in the list or it can be inserted
as the last one.
3. Insertion after It involves insertion after the specified node of the linked list.
specified node We need to skip the desired number of nodes in order to reach
the node after which the new node will be inserted
head
100 500
Insertion at
beginning 7 200 1 300 9 0
printf("\nEnter value\n");
scanf("%d",&newnode->data);
newnode->next = head;
head = newnode;
printf("\nNode inserted");
}
head
100 500
Insertion at end
7 200 1 300 9 400
struct node { 0
int data; 300 100 200 300
200
struct node *next;
100 5 0
};
temp 400
400
struct node *head, *newnode *temp;
newnode = (struct node *) malloc(sizeof(struct node)); newnode
printf("\nEnter value\n");
scanf("%d",&newnode->data);
newnode->next = 0
temp=head;
while(temp->next!=0)
{
temp=temp->next;
}
temp->next=newnode;
newnode
Insertion at specified location 100 400 5 400
400
int pos, i=1;
struct node { 7 200 1 400 9 0
int data;
struct node *next; 100 200 300
100
};
struct node *head, temp
3 pos
*newnode, *temp;
newnode =
(struct node *) malloc(size 21 i
of(struct node));
printf("\nEnter the
position");
scanf("%d",&pos);
if(pos>count)
{
printf(“Invalid position”);
} printf("Enter the data");
else scanf("%d", &newdata->data);
{ newnode->next=temp->next;
temp=head; temp->next=newnode;
while(i<pos)
{
Deletion
The Deletion of a node from a singly linked list can be performed
at different positions. Based on the position of the node being
deleted, the operation is categorized into the following categories.
}; 3 300 9 400 1 0
struct node *head, *temp; 200 300 400
DeleteFromEnd() 200
{ temp 200 300 temp
struct node *prevnode;
Previous node Previous node
temp=head;
while(temp->next!=0)
{
prevnode=temp;
temp=temp->next;
}
if(temp==head){
head=0;
}
else{
prevnode->next=0;
}
struct node {
int data; head
void display()
8 200
{
void peek()
struct node *temp; 300
{
temp=top;
if(top==0) 5 100
if(top==0)
{
{ 200
printf("stack is empty\n");
printf("stack is empty\n");
} 2 0
}
else 100
else
{
{
printf("Top element us %d", top->data);
while(temp!=0)
}
{
}
printf("%d", temp->data);
temp=temp->link;
}}}
Implementation of stack using linked list
head
top
200
300
7 400 8 100 1 0 2 0
8 200 5 100 2 0 200 400 100 200
300 200 100
void pop()
{ 8 200
struct node *temp;
temp=top; 300
if(top==0) 5 100
{
200
printf("No elements");
} 2 0
else 100
{
printf("%d", top->data);
top=top->link;
free(temp)
}
}
Implementation of Queue using Linked List
Front rear
100 100 200 300
struct node
5 0 200 0 300 -3 0
{
int data; 100 200 300
void main()
struct node *next; if(front==0&&rear==0) {
}; { front=rear=newnode;
} enqueue(5);
struct node *front=0;
else enqueue(0);
{
struct node *rear=0; rear->next=newnode;
enqueue(-3);
void enqueue(int x) rear=newnode; } } display();
{ dequeue();
struct node *newnode; peek();
newnode=(struct node *) malloc(sizeof(struct node));
newnode->data=x;
}
newnode->next=0;
}
Implementation of Queue using Linked List
Front rear
100 100 200 300
struct node {
5 0 200 0 300 -3 0
int data;
struct node *next; 100 200 300
void main()
}; else {
{
struct node *front=0; temp=front; enqueue(5);
struct node *rear=0;
while(temp!=0) { enqueue(0);
printf(“%d”, temp->data);
void display() { temp=temp->next;
enqueue(-3);
struct node *temp; } display();
if(front==0 && rear ==0) dequeue();
{ peek();
printf(“Queue is empty);
}
}
Implementation of Queue using Linked List
Front rear
struct node
{ The prev part of the first node and
struct node *prev; the next part of the last node will
always contain null indicating end in
int data; each direction.
struct node *next;
}
Doubly Linked List
In a singly linked list, we could traverse only in one direction,
because each node contains address of the next node and it
doesn't have any record of its previous nodes.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
Operations
Sl. No Operations Descriptions
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion at specified Adding the node into the linked list at the specified node.
node
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node Removing the node which is present just after the node
having given data containing the given data.
7 Searching Comparing each node data with the item to be searched
and return the location of the item in the list if the item
found else return null.
8 Traversing Visiting each node of the list at least once in order to
perform some specific operation like searching, sorting,
display, etc.
Insertion Operation
head
100
struct node{
int data; 0 5 200 100 4 250 200 2 0
struct node *prev;
head 100 200 250
struct node *next; tail
0 100 0 100 200 250
};
struct node *head, *tail; 0 100 6 0 tail
0 5 0 200
void createdll(){
struct node *newnode; 100 200
while(choice) {
newnode=(struct node*)malloc(size of(struct if(head==0){
node) head=tail=newnode;
head=0; }
printf(“Enter the data”); else{
scanf(“%d”, &newnode->data); tail->next=newnode;
newnode->prev=tail;
newnode->prev=0;
tail=newnode;
newnode->next=0; printf(“Do you want to continue(1,0)”);
scanf(“%d”, &choice);
Insertion Operation: Insert at the
beginning 150head
500
struct node{
int data; 0 500 5 100 150 4 200 100 1 0
struct node *prev; 200
150 100
struct node *next;
200
}; 0 6 0 150 500
struct node *head, *tail; tail
500 newnode
void insertatbeg(){
struct node *newnode;
newnode=(struct
node*)malloc(size of(struct head->prev=newnode;
node); newnode->next=head;
head=0; head=newnode;
printf(“Enter the data”); }
scanf(“%d”, &newnode-
>data);
newnode->prev=0;
newnode->next=0;
Insertion Operation: Insert at the
end head
500
tail
200
struct node{
int data; 500 5 100 150 4 200 100 1 0 400
struct node *prev; 200
150 100
struct node *next;
}; 0 6 150
struct node *head, *tail; 0 200 7 0
void insertatend(){ 500
400
struct node *newnode;
newnode=(struct node*)malloc(size of(struct 400
node); tail->next=newnode; newnode
head=0; newnode->prev=tail;
printf(“Enter the data”); tail=newnode;
scanf(“%d”, &newnode->data); }
newnode->prev=0;
newnode->next=0;
Insertion Operation: Insert at the
pos head
150
temp
150 100
tail
200
500 500
struct node{
int data; 500 5 100 150 4 200 100 1 0
struct node *prev; 200
150 100
struct node *next; 0 100 7 0 200
};
struct node *head, *tail; struct node *newnode, *temp; 500 newnode
void insertatpos() { temp=head;
int pos, i=1; newnode=(struct node*)malloc(size of(struct node);
printf(“Enter the position”); head=0;
scanf(“%d”, &pos); printf(“Enter the data”);
if(pos<=0 && pos>count){ scanf(“%d”, &newnode->data);
printf(“Invalid position”); newnode->prev=0;
newnode->prev=temp;
} newnode->next=0;
newnode->next=temp->next;
else if(pos==1){ while(i<pos-1) {
temp->next=newnode;
insertatbeg(); temp=temp->next;
newnode->next-
} i++;
>prev=newnode;
else{ }
}
Deletion Operation:
head temp
Delete from Beg deletefromBeg()
deletefromEnd()
deletefromPos()
200 400 200
struct node {
int data;
struct node *next;
};
struct node *head;
void display()
{
struct node *temp;
if(head==0) {
printf(“List is empty”);
}
else
{
temp=head;
while(temp->next!=head){
printf(“%d”, temp->data);
temp=temp->next;
}
printf(“%d”, temp->data);
}}
head tail
struct node 200 200 100 Create using head and tail
{
int data;
struct node *next; 7 100 1 200
};
struct node *head, *tail; 200 100
void createCLL(){
int choice=1;
struct node *newnode;
head=0;
while(choice){ else
newnode=(struct node *)malloc(sizeof(struct node)); {
printf(“Enter the data\n”); tail->next=newnode;
scanf(“%d”, &newnode->data); tail=newnode;
newnode->next=0; }
if(head==0) tail->next=head;
{ printf(“Enter your choice\n”);
head=tail=newnode; scanf(“%d”, &choice);
} }
printf(“%d”, tail->next->data);
}
Creation tail
100 500
struct node { 150
int data; 7 100 6 0 100 1 0 100
struct node *next;
}; 100 500 150
struct node *tail;
void createCLL(){
int choice=1;
struct node *newnode;
tail=0; else {
while(choice){ newnode->next=tail->next;
newnode=(struct node *)malloc(sizeof(struct node)); tail->next=newnode;
printf(“Enter the data\n”); tail=newnode;
scanf(“%d”, &newnode->data); }
newnode->next=0; printf(“Enter your choice\n”);
if(tail==0) scanf(“%d”, &choice);
{ }
tail=newnode; printf(“%d”, tail->next->data);
tail->next=newnode; } }
struct node { temp tail
150 200 Display
int data;
struct node *next; 1 100 2 200 3 150
}; 150 100 200
struct node *tail;
void displayCLL(){
struct node *temp;
if(tail==0)
{ while(temp->next!=tail->next)
{
printf(“List is empty”);
printf(“%d”, temp->data);
} temp=temp->next;
else{ }
temp=tail->next; printf(“%d”, temp->data);
}
}
Insertion at beginning
tail
tail
struct node { 0 500
int data;
struct node *next; 7 0 100 5 200 1 150 2 500 4 100 600
};
600 100 200 150 500
struct node *tail;
newnode
void insertatBeg(){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node)); else{
printf(“Enter the data”); newnode->next=tail->next;
tail->next=newnode;
scanf(“%d”, &newnode->data); }
newnode->next=0; printf(“%d”, tail->next->data);
if(tail==0) }
{
tail=newnode;
tail->next=newnode;
}
Insertion at End tail
struct node { 500
int data;
struct node *next; 7 100 5 200 1 150 2 500 4 600 700
};
600 100 200 150 500
struct node *tail;
void insertatEnd(){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node)); 10 0 600
printf(“Enter the data”); 700
else{
scanf(“%d”, &newnode->data); newnode
newnode->next=tail->next;
newnode->next=0; tail->next=newnode;
if(tail==0) tail=newnode;
}
{
printf(“%d”, tail->next->data);
tail=newnode; }
tail->next=newnode;
}
Insertion at Pos temp tail
struct node { 100 500
int data;
250
struct node *next; 5 200 1 150 2 500 4 100
};
100 200 150 500
struct node *tail;
void insertatPos(){
struct node *newnode, *temp; 7 0 150
int pos,i=1;
printf(“Enter the pos\n”); 250
newnode
scanf(“%d”, &pos); newnode=(struct node*)malloc(sizeof(struct
if(pos<0 || pos>count) node));
{ printf(“Enter the data”); newnode->next=temp->next;
printf(“Invalid position”);
scanf(“%d”, &newnode->data); temp->next=newnode;
} newnode->next=0; }
elseif(pos==1){ temp=tail->next;
insertatBeg(); } while(i<pos-1){
else{ temp=temp->next;
Deletion from Circular Linked List: Deletion from
Beginning tail
temp
500 200
struct node{
int data; 5 600 4 100 1 200 7 500
struct node *next;
}; 500 600 100 200
struct node *tail;
void deletefromBeg(){
struct node *temp;
temp=tail->next; temp tail
if(tail==0) 600 600
{
tail->next=temp->next;
printf(“List is empty”); 5 600
free(temp);
}
else{ 600
if(tail==temp)
{
tail=0;
free(temp);
}
else{
Deletion from Circular Linked List: Deletion from End
tail
temp
500 200
struct node{
int data; 5 600 4 100 1 200 7 500
struct node *next;
}; 500 600 100 200
struct node *tail;
void deletefromEnd(){
struct node *temp, *prev;
temp=tail->next; temp tail
if(tail==0) 600 600
{
while(temp->next!=tail->next){
printf(“List is empty”); 5 600
prev=temp;
}
temp=temp->next; 600
else if(tail==temp)
}
{
prev->next=tail->next;
tail=0;
tail=prev;
free(temp);
free(temp);
}
}
else{
Deletion from Circular Linked List: Deletion from Pos
tail
temp prev
500 100 200
struct node{
int data; 5 600 4 100 1 200 7 500
struct node *next;
}; 500 600 100 200
struct node *tail;
void deletefromEnd(){
struct node *temp, *prev;
int pos, i=1; temp tail
temp=tail->next; 600 600
printf(“Enter the position\n”);
while(i<pos-1){
scanf(“%d”, &pos); 5 600
temp=temp->next;
if(pos<1||pos>count)
i++; 600
{
}
printf(“Invalid position”);
prev=temp->next;
}
temp->next=prev->next;
else if(pos==1)
temp=prev;
{
free(temp);
deletefromBeg()
}
}
}
Circular Doubly Linked list
Circular doubly linked list is a more complexed type of data structure in
which a node contain pointers to its previous node as well as the next
node.
Circular doubly linked list doesn't contain NULL in any of the node.
The last node of the list contains the address of the first node of the list.
The first node of the list also contain address of the last node in its
previous pointer.
Due to the fact that a circular doubly linked list contains three parts in its
structure therefore, it demands more space per node and more expensive
basic operations.
Memory Management of Circular Doubly linked list
Operations on circular doubly linked
list :
SN Operation Description
1 Insertion at beginning Adding a node in circular doubly linked list at the
beginning.
2 Insertion at end Adding a node in circular doubly linked list at the end.
3 Insertion at Pos Adding a node at the specified position
4 Deletion at beginning Removing a node in circular doubly linked list from
beginning.
5 Deletion at end Removing a node in circular doubly linked list at the end.
6 Deletion at Pos Removing the node from the specified pos
Implementation of Doubly Circular
Linked
struct node{ List
int data;
head tail
struct node *next;
struct node *prev; 500 600
};
struct node *head, *tail;
void createDCLL() 600 7 300 500 6 600 300 1 500
{ 500 300 600
struct node *newnode;
head=0; int choice=1;
while(choice){ head tail
newnode=(struct node*)malloc(sizeof(struct else{ 500 500
node)); tail->next=newnode;
printf(“Enter the data\n”); newnode->prev=tail; 500 7 500
scanf(“%d”, &newnode->data); newnode->next=head;
newnode->next=0; newnode->prev=0; head->prev=newnode; 500
if(head==0) tail=newnode;
{ }
printf(“Do you want to continue (1 or 0)\n”);
head=tail=newnode;
scanf(“%d”, &choice);
tail->next=tail;
}}
Implementation of Doubly Circular Display
Linked List
head temp tail
struct node{
500 500 600
int data;
struct node *next;
struct node *prev;
600 7 300 500 6 600 300 1 500
};
struct node *head, *tail; 500 300 600
void display()
{
struct node *temp;
temp=head;
else{
if(head==0) while(temp!=tail) {
{ printf(“%d”, temp->data);
printf(“List is empty”); temp=temp->next;
} }
printf(“%d”, temp->data);
}
Insertion in Doubly Circular Linked List: Insert at beginning
head temp tail
struct node{
int data; 500 500 100
newnode
struct node *next; 0 100 7 0 200
struct node *prev;
}; 500 100 500 6 300 200 5 100 300 1 200 500
struct node *head, *tail; 200 300 100
void insertatBeg()
{
struct node *newnode; tail
head
newnode=(struct node*)malloc(sizeof(struct
node)); 500 500
else{
printf(“Enter the data\n”);
newnode->next=head;
scanf(“%d”,&newnode->data); newnode->prev=tail; 500 7 500
newnode->next=0; head->prev=newnode; 500
newnode->prev=0; tail->next=newnode;
if(head==0){ head=newnode;
head=tail=newnode; }
tail->next=newnode;
Insertion in Doubly Circular Linked List: Insert at end
struct node{ head temp tail
int data; 500 500 100
struct node *next;
700 7 200
struct node *prev;
}; 500 500 6 300 200 5 100 300 1 200 700
struct node *head, *tail; 200 100
300
void insertatEnd()
{
struct node *newnode; 0 100 8 0 500
newnode=(struct node*)malloc(sizeof(struct 700
node)); newnode
printf(“Enter the data\n”); else{
newnode->prev=tail; head tail
scanf(“%d”,&newnode->data);
newnode->next=0; tail->next=newnode;
newnode->next=head; 500 500
newnode->prev=0;
head->prev=newnode;
if(head==0){ tail=newnode; 500 7 500
head=tail=newnode; }
tail->next=newnode; 500
Insertion in Doubly Circular Linked List: Insert at pos
struct node{
int data; head temp tail
newnode
struct node *next; 200 100 100
300 5 100
struct node *prev;
}; 400
struct node *head, *tail; 100 6 300 200 5 100 400 300 400 1 200
void insertatPos() 200 300 100
{
struct node *newnode, *temp;
int pos, i=1;
temp=head; else{
newnode=(struct node*)malloc(sizeof(struct node));
printf(“Enter position\n”);
printf(“Enter the data\n”);
scanf(“%d”, &pos); newnode->prev=temp;
scanf(“%d”,&newnode->data);
if(pos<1 && pos>count){ newnode->next=temp->next;
newnode->next=0;
printf(“Invalid position”); temp->next->prev=newnode;
newnode->prev=0;
} temp->next=newnode;
while(i<pos-1){
}}
else if(pos==1){ temp=temp->next;
insertatBeg(); i++;
} }
Deletion from Doubly Linked List – Deletion from Beginning
head temp head tail
struct node{
int data; 200 200 100 250
struct node *next;
struct node *prev; 250 1 100 200 2 300 100 3 250 300 7 200
};
struct node *head, *tail; 200 100 300 250
void deletefromBeg(){
struct node *temp;
temp=head;
if(head==0){ head tail
printf(“List is empty\n”);
} 500 500
else if(head->next=head)
{ 500 7 500
head=tail=0;
free(temp);
}
else{
head=head->next;
head->prev=tail;
tail->next=head;
Deletion from Doubly Linked List – Deletion from End
head tail temp tail
struct node{
200 250 250 250
int data;
struct node *next;
struct node *prev; 250 1 100 200 2 300 100 3 250 300 7 200
};
struct node *head, *tail; 200 100 300 250
void deletefromEnd(){
struct node *temp;
temp=tail;
if(head==0){ head tail
printf(“List is empty\n”);
} 500 500
else if(head->next=head)
{ 500 7 500
head=tail=0;
free(temp);
}
else{
tail=tail->prev;
tail->next=head;
head->prev=tail;
Deletion from Doubly Linked List – Deletion from Pos
struct node{ temp tail
head
int data;
200 200 250
struct node *next;
struct node *prev;
}; 250 1 100 200 2 300 100 3 250 300 7 200
struct node *head, *tail;
void deletefromBeg(){ 200 100 300 250
struct node *temp;
int pos, i=1;
temp=head;
printf(“Enter the position\n”); head tail
scanf(“%d”, &pos); temp->prev->next=temp->next;
if(pos<1 ||pos>count){ 500 500
temp->next->prev=temp->prev;
printf(“Invalid position\n”); free(temp);
} } 500 7 500
else if(pos==1){
deletefromBeg();
}
else{
while(i<pos){
temp=temp->next;
i++; }
Hashing
• Storing and Retrieving data from database in order of O(1) time.
• Terminology:
• Search key: We will store the data in database based on the key.
• The search key will be stored in the hash table.
• Hash table: It is a data structure where we store the data.
• Hash Function: k mod 10, k mod n, mid square, folding method.
Example: search key – 24, 52, 91, 67, 48, 83 0
Hash function: k mod 10. 91 1
24 mod 10 = 4 52 2
52 mod 10 = 2 83 3
24 4
Storing 5
67 mod 10 = 7 Retrieving / Searching
6
67 7
48 8
9
Collision in Hashing
0
1 91
2 52 Suppose if we want to add 62 then how to store 62?
3 83
This process is called collision
4 24
0
5 Collision Resolution Techniques
1 91
6
2 52 62
7 67
3 83 Chaining Open addressing
8 48
4 24 (Open hashing) (Closed hashing)
9 • Linear Probing
5
• Quadratic Probing
6 • Double Probing
7 67
8 48
9
Chaining (Open hashing)
0
1
10
2 42
Keys: 42, 19, 10, 12 3
Hash Function: k mod 5 4 19
Advantages: Deletion is easy
Insertion is easy O(1)
0 10
1
Disadvantages: Searching is O(n) 2 12 42
Extra space 3
= 4/5
Linear Probing 0 19
1
2 72
• Keys: 43, 135, 72, 23, 99, 19, 82 3 43
• Hash Function h(k): k mod 10 4 23
5 135
• When collision occurs use hash function
6 82
• h(k)=(h(k)+i)mod 10) where i is the collision number
7
• 23 8
• h(k)=23 mod 10 9 99
=3
h(k)=(3+1)mod 10=4
Advantages: No extra space
Double hashing
56 mod 11 = 1
h2(k)=8-(56 mod 8)
=8-0 0
=8
1 34
1+1(8) mod 11
• h1(k)=k mod 11 1+8 mod 11 2
9 mod 11 = 9 3 56
• h2(k)=8-(k mod 8) 1+2(8) mod 11= 17 mod 11=6
4 45
• h1(k)+i h2(k)mod 11 1+3(8) mod 11=25 mod 11=3
5
• Keys: 20, 34, 45, 70, 56
6 70
20 70 7
20 mod 11 = 9 70 mod 11=4
8
h2(k)=8-(70 mod 8)
=8-6 9 20
=2 10
34 h2(k)=8-(45 mod 8) 4+1(2) mod 11
34 mod 11=1 =8-5 4+2 mod 11
=3 6 mod 11
h1(k)+i h2(k)mod 11 6
45 1+ 1(3) mod 11
1+3 mod 11
45 mod 11=1 4 mod 11= 4