Ds - Question Bank Solutions-Output
Ds - Question Bank Solutions-Output
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 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.
Step 3 -If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
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.
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
Advantages
They are a dynamic in nature which allocates the memory when required.
if(rear == SIZE-1)
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
void deQueue(){
if(front == rear)
else{
front++;
if(front == rear)
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int 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)
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{
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 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).
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
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 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
if(head->next == NULL)
head = NULL;
free(temp);
else
head = temp->next;
free(temp);
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 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.
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
else
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
if(rear == SIZE-1)
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
void deQueue(){
if(front == rear)
else{
front++;
if(front == rear)
}
Short Answers Questions
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
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%10
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.
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
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.
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.
Deletion Operation:
Delete7.
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
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
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.
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
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.
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
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)
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)
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
CHAINING
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
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.
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
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
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)
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.
Compare the key value of ‘curr’ and ‘New’ node. If New->key >Curr->key then
attach New node to‘curr’ node.
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.
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.
Case2:
i.e..if(curr==head)
Then, simply make ‘head’ node as next node and delete ‘curr’
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=currnext;
}
Printf(“display the elements is the dictionary:”);
Return count;
}
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)