0% found this document useful (0 votes)
16 views37 pages

Ds - Question Bank Solutions-Output

The document discusses stack and queue operations using arrays and linked lists, detailing functions like push, pop, enqueue, and dequeue. It explains the advantages of linked lists over arrays, particularly in handling dynamic data sizes. Additionally, it covers algorithms for inserting and deleting elements in linked lists, as well as collision handling techniques in hash tables.

Uploaded by

br420389
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

Ds - Question Bank Solutions-Output

The document discusses stack and queue operations using arrays and linked lists, detailing functions like push, pop, enqueue, and dequeue. It explains the advantages of linked lists over arrays, particularly in handling dynamic data sizes. Additionally, it covers algorithms for inserting and deleting elements in linked lists, as well as collision handling techniques in hash tables.

Uploaded by

br420389
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Stack Operations using Array

A stack can be implemented using array as follows...


push(value) - Inserting value into the stack
In a stack, push() is a function used to insert an element into the stack. In a stack, the new
element is always inserted at top position. Push function takes one integer value as parameter
and inserts that value into the stack. We can use the following steps to push an element on to
the stack...
Step 1 -Check whether stack is FULL. (top == SIZE-1)
Step 2 -If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!"
and terminate the function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set
stack[top] to value (stack[top] = value).

pop() - Delete a value from the Stack


In a stack, pop() is a function used to delete an element from the stack. In a stack, the element
is always deleted from top position. Pop function does not take any value as parameter. We
can use the following steps to pop an element from the stack...
Step 1 -Check whether stack is EMPTY. (top == -1)

Step 2 -If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not


possible!!!"and terminate the function.

Step 3 -If it is NOT EMPTY, then delete stack[top] and decrement top value by one
(top--).

2.Write queue abstract data types using linked list with examples.

The major problem with the queue implemented using an array is, It will work for an only fixed number
of data values. That means, the amount of data must be specified at the beginning itself. Queue using
an array is not suitable when we don't know the size of data which we are going to use. A queue data
structure can be implemented using a linked list data structure. The queue which is implemented using
a linked list can work for an unlimited number of values. That means, queue using linked list can work
for the variable size of data (No need to fix the size at the beginning of the implementation). The Queue
implemented using linked list can organize as many data values as we want.

In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the first
node is always pointed by 'front'.
Example

In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted
node is 10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
Operations
enQueue(value) - Inserting an element into the Queue
We can use the following steps to insert a new node into the queue...
Step 1 -Create a newNode with given value and set 'newNode → next' to NULL.

Step 2 -Check whether queue is Empty(rear==NULL)

Step 3 -If it is Empty then, set front =newNode and rear =newNode.

Step 4 -If it is Not Empty then, set rear → next=newNode and rear=newNode.

deQueue() - Deleting an Element from Queue


We can use the following steps to delete a node from the queue...
Step 1 -Check whether queue is Empty (front == NULL).

Step 2 -If it is Empty, then display"Queue is Empty!!! Deletion is not possible!!!"and


terminate from the function

Step 3 -If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.

Step 4 -Then set 'front=front → next' and delete 'temp' (free(temp)).

3. Explain the problem to insert the N=5elements-10, 20, 30, 40, 50 in the top of
the stack.
4. Explain the problem to Identify and delete the 30 element from the following
10,20,30,40,50 using Queue.

5. Explain Linked List.write Advantages of linked list.

A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations.
The elements in a linked list are linked using pointers
a linked list consists of nodes where each node contains a data field and a reference(link) to the next
node in the list.
When we want to work with an unknown number of data values, we use a linked list data structure to
organize that data

Types of Linked list

Single Linked List

Doubly Linked List

Circular Linked List

Advantages

They are a dynamic in nature which allocates the memory when required.

Insertion and deletion operations can be easily implemented.

Stacks and queues can be easily executed.

Linked List reduces the access time

6.Write a C program for queue operations using array.

void enQueue(int value){

if(rear == SIZE-1)

printf("\nQueue is Full!!! Insertion is not possible!!!");

else{

if(front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("\nInsertion success!!!");

}
void deQueue(){

if(front == rear)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else{

printf("\nDeleted : %d", queue[front]);

front++;

if(front == rear)

front = rear = -1;

void display(){

if(rear == -1)

printf("\nQueue is Empty!!!");

else{

int i;

printf("\nQueue elements are:\n");

for(i=front; i<=rear; i++)

printf("%d\t",queue[i]);

}
Long Answer Questions

1.Write c program for push and pop operation in stack using linked list.
void push(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if(top == NULL)

newNode->next = NULL;

else

newNode->next = top;

top = newNode;

printf("\nInsertion is Success!!!\n");

void pop()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else{

struct Node *temp = top;

printf("\nDeleted element: %d", temp->data);

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

2.Write an algorithm to insert the element 25 at the end of a singly linked list with
node values 2->10->18.

We can use the following steps to insert a new node at end of the single linked list...
Step 1 -Create a newNode with given value and newNode → next as NULL.

Step 2 -Check whether list is Empty (head==NULL).

Step 3 -If it is Empty then, set head=newNode.

Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 -Keep moving the temp to its next node until it reaches to the last node in the
list (until temp → next is equal to NULL).

Step 6 -Set temp → next=newNode.

void insertAtEnd(int value)

struct Node *newNode;


newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if(head == NULL)

head = newNode;

else

struct Node *temp = head;

while(temp->next != NULL)

temp = temp->next;

temp->next = newNode;

printf("\nOne node inserted!!!\n");

3. Write an algorithm to delete the element 4 from the beginning of a singly linked
list with node values 4->8->15->20.

We can use the following steps to delete a node from beginning of the single linked list...
Step 1 -Check whether list is Empty (head==NULL)

Step 2 -If it is Empty then, display'List is Empty!!! Deletion is not possible'and


terminate the function.
Step 3 -If it is Not Empty then, define a Node pointer 'temp'and initialize with head.

Step 4 -Check whether list is having only one node (temp → next==NULL)

Step 5 -If it is TRUE then set head= NULL and delete temp (Setting Empty list
conditions)

Step 6 -If it is FALSE then set head = temp → next, and delete temp.

void removeBeginning()

if(head == NULL)

printf("\n\nList is Empty!!!");

else

struct Node *temp = head;

if(head->next == NULL)

head = NULL;

free(temp);

else

head = temp->next;

free(temp);

printf("\nOne node deleted!!!\n\n");

4. Write an algorithm to insert the element 25 at a specific position of a singly


Linked list with node values 1->5->9->21->41->51.

We can use the following steps to insert a new node after a node in the single linked list...
Step 1 -Create a newNode with given value.

Step 2 -Check whether list is Empty (head==NULL)

Step 3 -If it is Empty then, set newNode → next = NULL and head =newNode.

Step 4 -If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 -Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location
is the node value after which we want to insert the newNode).

Step 6 -Every time check whether temp is reached to last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!'and terminate the function. Otherwise move the temp to next node.

Step 7 -Finally, Set 'newNode → next=temp → next' and 'temp → next=newNode'

void insertBetween(int value, int loc1, int loc2)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if(head == NULL)
{

newNode->next = NULL;

head = newNode;

else

struct Node *temp = head;

while(temp->data != loc1 && temp->data != loc2)

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

printf("\nOne node inserted!!!\n");

5.Write an algorithm for enqueue and dequeue in a queue using array.

void enQueue(int value){

if(rear == SIZE-1)

printf("\nQueue is Full!!! Insertion is not possible!!!");

else{

if(front == -1)

front = 0;

rear++;

queue[rear] = value;
printf("\nInsertion success!!!");

void deQueue(){

if(front == rear)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else{

printf("\nDeleted : %d", queue[front]);

front++;

if(front == rear)

front = rear = -1;

}
Short Answers Questions

1) Compare Linear list and skip lists techniques in Dictionary.

Linear list :
Linear Lists are straightforward and work well for small datasets or simple applications where the
dataset size is manageable. Their simplicity and low memory overhead make them easy to
implement and use, but they suffer from poor search and update performance as the dataset
grows.
Skip list :
Skip Lists are more complex but offer significantly better performance for search, insertion, and
deletion operations, especially in larger datasets. They achieve this through a probabilistic
balancing mechanism and multi-level indexing, which reduces the average search time to
logarithmic complexity. However, they come with higher memory overhead and implementation
complexity.
Simple list (array or
Structure linked list) Multi-level linked list
Search Time Complexity O(n) O(log n)
O(n) (requires shifting
Insertion Time Complexity elements) O(log n) average
O(n) (requires shifting
Deletion Time Complexity elements) O(log n) average
Space Complexity O(n) O(n log n)
Ease of Implementation Easy Moderate to Complex
Higher (due to multiple
Memory Overhead Low levels)
Performance Consistency Poor for large datasets Good
Efficient multi-level
Indexing No efficient indexing indexing
Balance Maintenance No automatic balancing Probabilistic balancing
Small datasets, simple Large datasets requiring
Best Use Case applications fast searches
Worst-case Search Time O(n) O(n)
Average-case Search Time O(n/2) O(log n)
Flexibility in
Insertions/Deletions Low High

2) Describe any three collision handling techniques.


COLLISIONRESOLUTIONTECHNIQUES

If collision occurs then it should be handled by applying some techniques. Such a


technique is called collision handling technique.
1. Chaining
2. Open addressing (linear probing)
3. Quadratic probing
4. Double hashing
CHAINING

In collision handling method chaining is a concept which introduces an additional field


with data i.e. chain. A separate chain table is maintained for colliding data.When collision
occurs then a linked list (chain)is maintained at the home bucket.
Foreg;
Consider the keys to be placed in their home buckets are131,3,4,21,61,7,97,8,9

Then we will apply a hash function as H(key)=key%D Where D is the size of table. The
hash table will be-Here D=10

0
1 131 21 61 NULL
2
3 3 NULL

4
5 131 61 NULL

6
7 97 NULL
7
8
9

A chain is maintained for colliding elements. for instance 131 has a home
bucket(key)1.similarlykey 21 and 61 demand for home bucket 1.Hence a chain is
maintained at index 1.

OPENADDRESSING– LINEARPROBING
This is the easiest method of handling collision. When collision occurs i.e. when two
records demand for the same home bucket in the hash table then collision can be solved by
placing the second record linearly down whenever the empty bucket is found. When use
linear probing (open addressing), the hash table is represented as a one-dimensional array
with indices that range from0 to the desired table size-1.Before inserting any elements into
this table, we must initialize the table to represent the situation where all slots are empty.
This allows us to detect overflows and collisions when we insert elements into the table.
Then using some suitable hash function the elements can be inserted into the hash table.
For example:
Consider that following keys are to be inserted in the hash table131,4,8,7,21,5,31,61,9,29
Initially, we will put the following keys in the hash table.
We will use Division hash function. That means the keys are placed using the formula

H(key) = key % tablesize

H(key)=key%10

For instance, the element 131 can be placed at

H(key)=131%10=1

Index 1will be the home bucket for 131. Continuing in this fashion we will place 4,8,7.
Now the next key to be inserted is21. According to the hash function
H(key)=21%10
H(key)=1
But the index 1 location is already occupied by 131 i.e. collision occurs. To resolve this
collision we will linearly move down and at the next empty location we will prob the
element. Therefore 21 will be placed at the index 2. If the next element is 5 then we get
the home bucket for 5 as index 5 and this bucket is empty so we will put the element 5 at
index 5.

Index Key Key Key

0 NULL NULL NULL

1 131 131 131

NULL 21 21
2
NULL NULL 31
3
4 4 4
4
NULL 5 5
5
NULL NULL 61
6
7 7 7
7
8 8 8
8 NULL NULL NULL

After placing keys 31,61


The next record key is 9. According to decision hash function it demands for the home
bucket 9. Hence we will place 9 at index 9. Now the next final record key 29 and it hashes
a key 9. But home bucket 9 is already occupied. And there is no next empty bucket as the
table size is limited to index 9. The overflow occurs. To handle it we move back to bucket
0 and is the location over there is empty 29 will be placed at 0 th index.

3) Discuss extendible hashing with example.


Ans) EXTENSIBLEHASHING

 Extensible hashing is a technique which handles a large amount of data. The data to
be placed in the hash table is by extracting certain number of bits.

 Extensible hashing grow and shrink similar to B-trees.

 In extensible hashing referring the size of directory the elements are to be placed in
buckets. The levels are indicated in parenthesis.

For Eg:

 The bucket can hold the data of its global depth. If data in bucket is more than global
depth then, split the bucket and double the directory.

Consider we have to insert1,4,5,7,8,10. Assume each page can


hold 2 data entries (2 is the depth).
Step2:Insert7
7=111
But as depth is full we can not insert 7 here. Then double the directory and split the bucket.
After insertion of 7. Now consider last two bits.

Step3: Insert8 i.e.1000


Thus the data is inserted using extensible hashing.

Deletion Operation:

If we want to delete10 then, simply make the bucket of 10 empty.

Delete7.

Delete8. Remove entry from directory 00.


Applications of hashing:
 In compilers to keep track of declared variables.
 For online spelling checking the hashing functions are used.
 Hashing helps in Game playing programs to store the moves made.
 For browser program while caching the web pages, hashing is used.
 Construct a message authentication code (MAC)
 Digital signature.
 Time stamping
 Key updating: key is hashed at specific intervals resulting in new key

4) Describe hashing? Discuss different types of collision resolution Technical


with example for each.

Ans) Hash function is a function which is used to put the data in the hash table. Hence one
can use the same hash function to retrieve the data from the hash table. Thus hash function is
used to implement the hash table.

i) Division method
ii) Multiplicative hash function method
iii) Mid square method
iv) Digit folding

COLLISION RESOLUTION TECHNIQUES

If collision occurs then it should be handled by applying some techniques. Such a


technique is called collision handling technique.
1. Chaining
2. Open addressing (linear probing)
3. Quadratic probing
4. Double hashing
CHAINING

In collision handling method chaining is a concept which introduces an additional field


with data i.e. chain. A separate chain table is maintained for colliding data.When collision
occurs then a linked list (chain)is maintained at the home bucket.
Foreg;
Consider the keys to be placed in their home buckets are131,3,4,21,61,7,97,8,9

Then we will apply a hash function as H(key)=key%D Where D is the size of table. The
hash table will be-Here D=10

0
1 131 21 61 NULL
2
3 3 NULL

4
5 131 61 NULL

6
7 97 NULL
7
8
9

A chain is maintained for colliding elements. for instance 131 has a home
bucket(key)1.similarlykey 21 and 61 demand for home bucket 1.Hence a chain is
maintained at index 1.

OPENADDRESSING– LINEARPROBING
This is the easiest method of handling collision. When collision occurs i.e. when two
records demand for the same home bucket in the hash table then collision can be solved by
placing the second record linearly down whenever the empty bucket is found. When use
linear probing (open addressing), the hash table is represented as a one-dimensional array
with indices that range from0 to the desired table size-1.Before inserting any elements into
this table, we must initialize the table to represent the situation where all slots are empty.
This allows us to detect overflows and collisions when we insert elements into the table.
Then using some suitable hash function the elements can be inserted into the hash table.
For example:
Consider that following keys are to be inserted in the hash table131,4,8,7,21,5,31,61,9,29
Initially, we will put the following keys in the hash table.
We will use Division hash function. That means the keys are placed using the formula
H(key) = key % tablesize

H(key)=key%10

For instance, the element 131 can be placed at

H(key)=131%10=1

Index 1will be the home bucket for 131. Continuing in this fashion we will place 4,8,7.
Now the next key to be inserted is21. According to the hash function
H(key)=21%10
H(key)=1
But the index 1 location is already occupied by 131 i.e. collision occurs. To resolve this
collision we will linearly move down and at the next empty location we will prob the
element. Therefore 21 will be placed at the index 2. If the next element is 5 then we get
the home bucket for 5 as index 5 and this bucket is empty so we will put the element 5 at
index 5.

Index Key Key Key

0 NULL NULL NULL

1 131 131 131

NULL 21 21
2
NULL NULL 31
3
4 4 4
4
NULL 5 5
5
NULL NULL 61
6
7 7 7
7
8 8 8

8 NULL NULL NULL

After placing keys 31,61

The next record key is 9. According to decision hash function it demands for the home
bucket 9. Hence we will place 9 at index 9. Now the next final record key 29 and it hashes
a key 9. But home bucket 9 is already occupied. And there is no next empty bucket as the
table size is limited to index 9. The overflow occurs. To handle it we move back to bucket
0 and is the location over there is empty 29 will be placed at 0 th index.

Problem with linear probing:

One major problem with linear probing is primary clustering. Primary clustering is a process
in which a block of data is formed in the hash table when collision is resolved.
19%10=9 cluster is formed Key

18%10=8 39

29
39%10=9
8
29%10=9
8%10=8

rest of the table is empty this cluster problem can be solved


by quadratic probing.

18
QUADRATIC PROBING:
19

Quadratic probing operates by taking the original hash value and adding successive values of
an arbitrary quadratic polynomial to the starting value. This method uses following formula.

H(key)=(Hash(key)+i2)%m)

where m can be table size or any prime number.

foreg; If we have to insert following elements inthehashtablewithtablesize10:

37,90,55,22,17,49,87
37% 10=7
90% 10=0
55% 10=5
22% 10=2
11%10=1

Now if we want to place17 a collision will occur as17%10=7and bucket 7 has already an
element 37. Hence, we will apply quadratic probing to insert this recording the hash table.
Hi(key)= (Hash(key)+ i2)%m
Consider i=0then
(17+02) % 10=7
(17+12) %10=8,wheni=1
The bucket 8 is empty hence we will place the element at index 8. 0 90
Then comes 49 which will be placed at index 9. 1 11
2 22
49 % 10 = 9 3
4
5 55
6
7 37
8 49
9
Now to place 87 we will use quadratic probing.

(87+0)% 10=7
(87+1)% 10 = 8…but already occupied
(87+22) % 10=1..already occupied
(87+32) % 10=6
It is observed that if we want place all the necessary elements in
The hash table the size of divisor(m) should be twice as large as
Total number of elements.
5.Create hash table for 10, 23, 34, 56, 67, 88, 567, and 897 using division
method and use any collision handling technique if required.
6. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty
hash table of length 10 using open addressing with hash function h(k) = k mod
10 and linear probing. What is the resultant hash table?
Long Answer Questions(10 Marks)

1) Describe hashing? Discuss any two types of collision resolution


technique with example for each.

Ans) Hash function is a function which is used to put the data in the hash table. Hence one
can use the same hash function to retrieve the data from the hash table. Thus hash function is
used to implement the hash table.

i) Division method
ii) Multiplicative hash function method
iii) Mid square method
iv) Digit folding

COLLISION RESOLUTION TECHNIQUES

If collision occurs then it should be handled by applying some techniques. Such a


technique is called collision handling technique.
1. Chaining
2. Open addressing (linear probing)
3. Quadratic probing
4. Double hashing

CHAINING

In collision handling method chaining is a concept which introduces an additional field


with data i.e. chain. A separate chain table is maintained for colliding data.When collision
occurs then a linked list (chain)is maintained at the home bucket.
Foreg;
Consider the keys to be placed in their home buckets are131,3,4,21,61,7,97,8,9

Then we will apply a hash function as H(key)=key%D Where D is the size of table. The
hash table will be-Here D=10
0
1 NULL
131 21 61
2
3
3 NULL
4
5 131 61 NULL

6
7
7 97 NULL
8
9

A chain is maintained for colliding elements. for instance 131 has a home
bucket(key)1.similarlykey 21 and 61 demand for home bucket 1.Hence a chain is
maintained at index 1.

OPENADDRESSING– LINEARPROBING
This is the easiest method of handling collision. When collision occurs i.e. when two
records demand for the same home bucket in the hash table then collision can be solved by
placing the second record linearly down whenever the empty bucket is found. When use
linear probing (open addressing), the hash table is represented as a one-dimensional array
with indices that range from0 to the desired table size-1.Before inserting any elements into
this table, we must initialize the table to represent the situation where all slots are empty.
This allows us to detect overflows and collisions when we insert elements into the table.
Then using some suitable hash function the elements can be inserted into the hash table.
For example:
Consider that following keys are to be inserted in the hash table131,4,8,7,21,5,31,61,9,29
Initially, we will put the following keys in the hash table.
We will use Division hash function. That means the keys are placed using the formula
H(key) = key % tablesize

H(key)=key%10

For instance, the element 131 can be placed at

H(key)=131%10=1

Index 1will be the home bucket for 131. Continuing in this fashion we will place 4,8,7.
Now the next key to be inserted is21. According to the hash function
H(key)=21%10
H(key)=1
But the index 1 location is already occupied by 131 i.e. collision occurs. To resolve this
collision we will linearly move down and at the next empty location we will prob the
element. Therefore 21 will be placed at the index 2. If the next element is 5 then we get
the home bucket for 5 as index 5 and this bucket is empty so we will put the element 5 at
index 5.

Index Key Key Key

0 NULL NULL NULL

1 131 131 131

NULL 21 21
2
NULL NULL 31
3
4 4 4
4
NULL 5 5
5
NULL NULL 61
6
7 7 7
7
8 8 8
8 NULL NULL NULL

After placing keys 31,61

The next record key is 9. According to decision hash function it demands for the home bucket
9. Hence we will place 9 at index 9. Now the next final record key 29 and it hashes a key 9.
But home bucket 9 is already occupied. And there is no next empty bucket as the table size is
limited to index 9. The overflow occurs. To handle it we move back to bucket 0 and is the
location over there is empty 29 will be placed at 0th index.
Problem with linear probing:
One major problem with linear probing is primary clustering. Primary clustering is a process
in which a block of data is formed in the hash table when collision is resolved.
19%10=9 cluster is formed Key

18%10=8 39

29
39%10=9
8
29%10=9
8%10=8

rest of the table is empty this cluster problem can be solved


by quadratic probing.

18
QUADRATICPROBING:
19

Quadratic probing operates by taking the original hash value and adding successive values of
an arbitrary quadratic polynomial to the starting value. This method uses following formula.

H(key)=(Hash(key)+i2)%m)

where m can be table size or any prime number.

foreg; If we have to insert following elements inthehashtablewithtablesize10:

37,90,55,22,17,49,87
37% 10=7
90% 10=0
55% 10=5
22% 10=2
11%10=1

Now if we want to place17 a collision will occur as17%10=7and bucket 7 has already an
element 37. Hence, we will apply quadratic probing to insert this recording the hash table.
Hi(key)= (Hash(key)+ i2)%m
Consider i=0then
(17+02) % 10=7
(17+12) %10=8,wheni=1
The bucket 8 is empty hence we will place the element at index 8. 90 0
Then comes 49 which will be placed at index 9. 11 1
22 2
49 % 10 = 9 3
4
55 5
6
37 7
49 8
9
Now to place 87 we will use quadratic probing.

(87+0)% 10=7
(87+1)% 10 = 8…but already occupied
(87+22) % 10=1..already occupied
(87+32) % 10=6
It is observed that if we want place all the necessary elements in
The hash table the size of divisor(m) should be twice as large as
Total number of elements.
2) a) Discuss dictionary representations with relevant examples .

Ans) It is a collection of pairs of (key and Value) When Every value is associated with the
corresponding key.

There are 2 methods for representing the dictionaries: -


1. Sorted array
2. Sorted chain (Linear Linked List)
The Representation of linear list is dictionary is :-

Basic Operations can be performed on Dictionary are:


1. Insertion of value in the dictionary.
2. Deletion of particular value from the dictionary.
3. Searching of a Specific value with the help of key.

Structure of linear list for dictionary:


class dictionary
{
private:
int k,data;
structnode
{
private: intkey;
int value;
structnode*next;
} *head;
public:
dictionary();
void insert_d();
void delete_d();
void display_d();

Insertion of new node in the dictionary:


Consider that initially dictionary is empty then
head = NULL
We will create an new node with some key and value contained in it.
Now as head is NULL, this new node becomes head. Hence the dictionary contains
only one record. this node will be ‘curr’ and ‘prev’ as well. The ‘cuur’ node will always
point to current visiting node and ‘prev’ will always point to the node previous to
‘curr’ node. As now there is only one node in the list mark as ‘curr’ node as ‘prev’
node.

Insert a record, key=4 and value=20,


NEW
4 20 NULL

Compare the key value of ‘curr’ and ‘New’ node. If New->key >Curr->key then
attach New node to‘curr’ node.

prev/head New curr->next=New

1 10 4 20 NULL
prev=c
urr

Addanew node<7,80>then
head/prev curr
New 4 7 80 NULL
1 10 20

If we insert <3,15> then we have to search for it proper position by comparing key value.

(curr->key<New->key)is false. Hence else part will get executed.


The delete operation:
Case 1:

 Initially assign ‘head’ node as ‘curr’ node. Then ask for a key value of the node which
is to be deleted.

 Then starting from head node key value of each node is checked and compared with
the desired node’s key value. We will get node which is to be deleted invariable
‘curr’.

 The node given by variable ‘prev’ keeps track of previous node of ‘cuu’ node.

For example delete node with keyvalue4then

Case2:

If the node to be deleted is head node.

i.e..if(curr==head)

Then, simply make ‘head’ node as next node and delete ‘curr’

Hence the list becomes

Head

3 15 4 20 7 80 ULL
Searching &Display operations:

Initially Count=0,
Cuur=Head
int dictionary:
length()
{
Struct node *curr;
int count;
count=0; // next iteration count=1
curr=head;
if (curr==Null)// head itself is null (key value=null)
{
printf(“The list is empty”)
return 0;
}
while(curr!=null) // it will travel until the last node i.e, tail
node(key=1/key=2/key=3/key=null)
{
count++;
curr=currnext;
}
Printf(“display the elements is the dictionary:”);
Return count;
}

b) Discuss Rehashing with an example.

Ans) Rehashing is a technique in which the table is resized, i.e., the size of table is doubled by
creatinga
new table. It is preferable is the total size of table is a prime number. There are situations in
which
the rehashing is required.
When table is completely full
With quadratic probing when the table is filled half.
When insertions fail due to overflow.

Key
90 17 22 45 37 49 90 17 22 45 55 37 49
In such situations, we have to transfer entries from old table to the new table by re computing
their positions using hash functions.
Consider we have to insert the elements 37, 90, 55, 22, 17, 49, and 87. the table size is 10 and
will
use hash function.,
H(key) = key mod tablesize
37 % 10 = 7
90 % 10= 0
55 % 10 = 5
22 % 10 = 2
17 % 10 = 7
49 % 10 = 9
Now this table is almost full and if we try to insert more elements collisions will occur and
eventually further insertions will fail. Hence we will rehash by doubling the table size. The old
table size is 10 then we should double this size for new table, that becomes 20. But 20 is not a
prime number, we will prefer to make the table size as 23. And new hash function will be

Advantages:

1. This technique provides the programmer a flexibility to enlarge the table size if required.
2. Only the space gets doubled with simple hash function which avoids occurrence of
collisions.
3. Insert the keys 37, 90, 45, 22, 49, 17, 55 into the hash table of size 10.Resolve
all collisions using Double Hashing where first hash-functionis h1(k) = k mod 10
and second hash function is h2(k) = 7 - (k mod 7).The collision function is
hnew(k)=(h1(k)+i*h2(k))mod10.
4. The elements 37,90,55,22,17,49,87 are inserted into an empty hash table of
size10. Create the hash table and count the maximum number of collisions
when the method use quadratic probing.(h(k)=kmod10)

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