0% found this document useful (0 votes)
22 views12 pages

DS Unit1,2 QB Short

The document provides an overview of data structures, including definitions, types (linear and non-linear), and specific structures like linked lists, stacks, and queues. It discusses operations, advantages, and applications of these data structures, as well as collision resolution techniques in hashing. Additionally, it covers concepts like abstract data types and conditions for stack and queue operations.

Uploaded by

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

DS Unit1,2 QB Short

The document provides an overview of data structures, including definitions, types (linear and non-linear), and specific structures like linked lists, stacks, and queues. It discusses operations, advantages, and applications of these data structures, as well as collision resolution techniques in hashing. Additionally, it covers concepts like abstract data types and conditions for stack and queue operations.

Uploaded by

sravyasharmaa18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is a data structure?Why we use data structures?

What are the types of data


structures? Give examples..
Ans: Data Structure:
A data structure is a systematic way of organizing data in computer memory and accessing
it.
Why Data Structures?

1. Data structures study how data is stored in a computer so that operations can be
implemented efficiently
2. Data structures are especially important when we have large amounts of information

Data structures can be classified into two types


 Linear Data Structures
 Non Linear Data Structures

Linear Data Structures:


Linear data structures are those data structures in which data elements are accessed (read and
written) in sequential fashion ( one by one)
Eg: Arrays, Stacks , Queues, Linked list
Non Linear Data Structures:
Non Linear Data Structures are those in which data elements are not accessed in sequential
fashion.
Eg: trees, graphs
2. Difference Between Primitive and Non Primitive Data Structure
Below is a table highlighting the differences between primitive and non primitive data
structure.
3.Define Linked list.List out its operations.
The linked list is a linear data structure that contains a sequence of elements such that each
element links to its next element in the sequence. Each element in a linked list is called
"Node".
Linked List is a collection of nodes linked together.
Operations:
a.Create( )
b.insert( )
c.Delete( )
d.Display( )
e.Search( )
4.Describe abstract data type with example.
Abstraction means “Hiding”.
An abstract data type (ADT) specifies the List of operations which are able to perform
upon data Structure with out its implementation details.
Example:
AbstractDataType StackADT
{
isFull( );
isEmpty( );
push( );
pop( );
display( );(or) peek( );
};
5.Write a structure representation of a node in SLL.

struct sll
{
int data;
struct sll *next;
};
typedef struct sll node;
node *start=NULL;

6.write The advantages and disadvantages of a singly linked list.

Advantages:

 Memory allocation: One of the biggest advantages of a singly linked list is dynamic memory
allocation. Memory is allocated only when it is required.
 Time: Singly linked list requires less access time which is also a great advantage of a singly
linked list.

Dis-Advantages:

 Traversing: The singly list can only move forward so it is very difficult to traverse in a
reverse way. This is a big disadvantage of singly link list.
 Space: Since we have to use pointers and store data, much more space is used up in a linked
list. This is also a disadvantage of the singly linked list.

7.What are the advantages linked list over array?


Linked lists have several advantages over arrays, including:

 Dynamic size: Linked lists can change size during a program's execution, while arrays have a
fixed size.
 Efficient memory use: Linked lists are more memory efficient than arrays because they don't
waste memory.
 Faster insertion and deletion: Linked lists are faster for inserting and deleting data than
arrays.
 Easier implementation of stacks and queues: Linked lists make it easier to implement stacks
and queues.
 Reduced access time: Linked lists can help reduce access time.
8.Write the applications of linked list.
Applications of linked list in computer science:
1. Implementation of stacks and queues
2. Implementation of graphs: Adjacency list representation of graphs is the most popular
which uses a linked list to store adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free blocks.
4. Maintaining a directory of names
5. Performing arithmetic operations on long integers
6. Manipulation of polynomials by storing constants in the node of the linked list
7. Representing sparse matrices
Applications of linked list in the real world:
1. Image viewer – Previous and next images are linked and can be accessed by the next
and previous buttons.
2. Previous and next page in a web browser
3. Music Player – Songs in the music player are linked to the previous and next songs.
4. GPS navigation systems- Linked lists can be used to store and manage a list of locations
and routes, allowing users to easily navigate to their desired destination.
5. Task Scheduling- Operating systems use linked lists to manage task scheduling, where
each process waiting to be executed is represented as a node in the list.
6. File Systems- File systems use linked lists to represent the hierarchical structure of
directories, where each directory or file is represented as a node in the list.
7. Symbol Table- Compilers use linked lists to build a symbol table, which is a data
structure that stores information about identifiers used in a program.
8. Undo/Redo Functionality- Many software applications implement undo/redo
functionality using linked lists,

9.Write Short notes on conditions of a stack.

Conditions of Stack:

1.Stack Overflow: Push of an element into the stack when it is full is called as Stack
overflow. When TOP==Size-1

2.Stack Underflow: Pop of an element from the stack when it is empty is called as Stack
Underflow. When Top=-1

10.List out the applications of a stack.

1. Expression Conversion
2. Expression Evaluation
3. Passing well-formed Parenthesis
4. Reversing a String
5. Used in Recursion
11.Define Expression.List out its types with suitable examples.

An expression is a collection of operators and operands that represents a specific value.

Types of Expressions
Based on the operator position, expressions are divided into THREE types. They are

1. Infix Expression a+b


2. Prefix Expression +ab
3. Postfix Expression ab+

12.How does a stack differs from a queue.

Parameter Stack Data Structure Queue Data Structure

Basics It is a linear data structure. The It is also a linear data structure. The objects are
objects are removed or inserted at removed and inserted from two different ends.
the same end.

Working It follows the Last In, First Out It follows the First In, First Out (FIFO) principle. It
Principle (LIFO) principle. It means that the means that the first added element gets removed
last inserted element gets deleted at first from the list.
first.

Pointers It has only one pointer- the top. It uses two pointers (in a simple queue) for reading
This pointer indicates the address of and writing data from both the ends- the front and
the topmost element or the last the rear. The rear one indicates the address of the
inserted one of the stack. last inserted element, whereas the front pointer
indicates the address of the first inserted element in
a queue.

Operations Stack uses push and pop as two of Queue uses enqueue and dequeue as two of its
its operations. The pop operation operations. The dequeue operation deletes the
functions to remove the element elements from the queue, and the enqueue
from the list, while the push operation inserts the elements in a queue.
operation functions to insert the
element in a list.

Structure Insertion and deletion of elements It uses two ends- front and rear. Insertion uses the
take place from one end only. It is rear end, and deletion uses the front end.
called the top.

Full Condition When top== max-1, it means that When rear==max-1, it means that the queue is full.
Examination the stack is full.

Empty When top==-1, it indicates that the When front = rear+1 or front== -1, it indicates that
Condition stack is empty. the queue is empty.
Examination
13.Evaluate the below postfix expression.

Result of Above postfix Expression after Evaluation is 7.

14. Evaluate the prefix expression. +, -, *, 2, 2, /, 16, 8, 5.

Step 1:First, we will reverse the expression given above.

Expression: 5, 8, 16, /, 2, 2, *, -, +

Step 2: Evaluate it using stack.


Stpe 3:Result of Postfix Expression after Evaluation is 7 will be the result for prefix
expression also..

15.Check whether the following braces sequence are balanced or not.

a. {[]{()}}
b. [{}{}(]

Ans: a.{[]{()}} is balanced.

b. [{}{}(] is Un-balanced.
16. Write Short notes on conditions of a Queue.

Conditions of Queue:

2 conditions to check while implementation of Queue.

1.Queue Overflow:Trying to insert an element in to the queue when it is full.

2.Queue Underflow: Trying to delete an element from the queue when it is empty.

17.List out the applications of Queues.

1. Task Scheduling: Queues can be used to schedule tasks based on priority or the order
in which they were received.
2. Resource Allocation: Queues can be used to manage and allocate resources, such as
printers or CPU processing time.
3. Batch Processing: Queues can be used to handle batch processing jobs, such as data
analysis or image rendering.
4. Message Buffering: Queues can be used to buffer messages in communication
systems, such as message queues in messaging systems or buffers in computer
networks.
5. Event Handling: Queues can be used to handle events in event-driven systems, such as
GUI applications or simulation systems.
6. Traffic Management: Queues can be used to manage traffic flow in transportation
systems, such as airport control systems or road networks.
7. Operating systems: Operating systems often use queues to manage processes and
resources. For example, a process scheduler might use a queue to manage the order in
which processes are executed.
8. Network protocols: Network protocols like TCP and UDP use queues to manage
packets that are transmitted over the network. Queues can help to ensure that packets
are delivered in the correct order and at the appropriate rate.
9. Printer queues :In printing systems, queues are used to manage the order in which
print jobs are processed. Jobs are added to the queue as they are submitted, and the
printer processes them in the order they were received.
10. Web servers: Web servers use queues to manage incoming requests from clients.
Requests are added to the queue as they are received, and they are processed by the
server in the order they were received.
11. Breadth-first search algorithm: The breadth-first search algorithm uses a queue to
explore nodes in a graph level-by-level. The algorithm starts at a given node, adds its
neighbors to the queue, and then processes each neighbor in turn.

18.What LIFO & FIFO stands for.Specify the Data structures which follows
LIFO,FIFO.

LIFO:Last In First out-Stack data structure.


In stack data structure, the insertion and deletion operations are performed based on LIFO
(Last In First Out) principle.
The element which is inserted Last is the element which will delete first.

FIFO:First In First out-Queue data structure.


In queue data structure, the insertion and deletion operations are performed based on FIFO
(First In First Out) principle.
The element which is inserted first is the element which will delete first.

19.The following sequence of operations is performed on a stack:

PUSH (10), PUSH (20), POP, PUSH (10), PUSH (20), POP, POP, POP, PUSH (20), POP

The sequence of values popped out is:

Ans:20,20,10,10,20
20. How to find length of Single Linked list.

Length means Counting of nodes in Linked List.

Void count()

Node *temp;

Int count=0;

temp = start;

while (temp != NULL)

temp = temp->next;

count++;

Printf(“Length of SLL is %d”,count);

}
UNIT 2

1 Define Collision. List the collision resolution Techniques used in Hashing.


The situation in which the hash function return the same hash key (home bucket) for more than one record
is called collision.
1. Chaining
2. Linear probing(Open addressing)
3. Quadratic probing
4. Double hashing
5. Rehashing
2 Explain Linear Probing Collision resolution technique.
When two records demands for same home bucket in the hash table, then collision will occur. It can be
solved by placing the second linearly down whenever the empty bucket is found.

3 Explain Quadratic Probing Collision resolution technique.


If collision occurs then it should be handled by applying some techniques. One of such technique is called
collision handling technique.
Hi (key)=Hash(key)+i² )%M M is Table Size i is 0,1,2,,3,4,…..
The probing sequence is determined by a quadratic formula. For a given key, if the initial index is h(k), the
sequence of indices checked will be
 First probe: h(k)+12 = h(k) + 1
 Second probe: h(k)+22= h(k) + 4
 Third probe: h(k)+32 = h(k) + 9
and so on.
4 Explain Double Hashing Collision resolution technique.
Double hashing is a collision resolution technique used in open addressing schemes for hash tables. It
involves using a second hash function to determine the step size when probing for the next available slot
after a collision occurs. This helps reduce clustering and improves the distribution of entries in the hash
table.
 Primary Hash Function (h1(key)): This function computes the initial index for the key.
H1(key)=Key%Table Size

 Secondary Hash Function (h2(key)): This function computes the step size for probing when a
collision occurs. It must ensure that it returns a value that is relatively prime to the table size to
ensure that all slots can be probed.
H2(key)=M-(Key%M)
M is Prime Number smaller than Table size.
5 Explain Re-Hashing Collision resolution technique.
When a collision occurs, rehashing involves computing a new hash index for the holliding key, helping to
place it in a different location in the hash table.
In Rehashing, New hash Table is generated to the size of prime number greater than the double of hash
table.
Ex:If Initial Hash Table size is 10,then the new Hash table size will be 23 in Rehashing.

6 What are the differences between the open addressing and the closed addressing

Feature Open Addressing Closed Addressing (Chaining)


Storage In the array In linked lists or structures
Collision Probing for next slot Adding to the list
Resolution
Load Factor Sensitive to high load Handles higher load well
Memory Usage Efficient but fixed size More overhead due to pointers
Implementation Simpler More complex
Deletion Complicated Straightforward
Complexity
8 Place the following keys in hash table using Linear Probing collision Resolution Technique. Hash table
size=10,
16, 4, 6, 22, 24, 10, 31, 7, 9, 20, 26
9 Place the following keys in hash table using Quadratic Probing collision Resolution Technique. Hash
table size=10,
16, 4, 6, 22, 24, 10, 31, 7, 9, 20, 26
10 Place the following keys in hash table using Double Hashing collision Resolution Technique. Hash table
size=10,
16, 4, 6, 22, 24, 10, 31, 7, 9, 20, 26
11 Write short note on Dictionary ADT
A Dictionary ADT is a collection of key-value pairs, where each key is unique and is used to retrieve the
corresponding value. It is a fundamental data structure that supports efficient data retrieval, insertion, and
deletion operations based on keys.
Key Characteristics:
1. Key-Value Pairs: Each entry in a dictionary consists of a key and its associated value. Keys are used
for accessing values, which can be of any data type.
2. Uniqueness of Keys: Keys in a dictionary are unique. If a new key-value pair is inserted with a key
that already exists, the existing value is typically updated.
3. Dynamic Size: Dictionaries can grow and shrink dynamically as items are added or removed.
Common Operations:
. Insert(key, value): Adds a new key-value pair to the dictionary. If the key already exists, updates the
value.
. Delete(key): Removes the key-value pair associated with the specified key.
. Find(key): Retrieves the value associated with the given key. If the key does not exist, it typically returns
a null or an indication that the key was not found.
. Keys(): Returns a collection of all keys in the dictionary.
. Values(): Returns a collection of all values in the dictionary.

10 What is the advantage of the hash table over a linked list


1. Fast Access Time
2. Efficiency in Searching
3. Better Handling of Collisions
4. Dynamic Size Management
5. No Need for Sequential Traversal
6. Data Retrieval by Key
Feature Hash Table Linked List
Access Time Average O(1) O(n)
Search Efficiency Fast, direct access Sequential traversal
Collision Handling Handles collisions well No collisions; slower access

Dynamic Resizing Efficient, based on load factor Grows dynamically, but inefficient
space use
Data Retrieval by Key Supports key-based retrieval Generally access by position
12 What is the hash function used in Double hashing?
In Double hashing, two hash functions are used to resolve collisions in a hash table. The primary hash
function determines the initial index for a key, while the second hash function provides the step size for
probing when a collision occurs.
1. Primary Hash function - This function computes the initial index where the key should be placed in
the hash table. Its typically defined as:
h1(k) = k mod m
k is the key and m is the size of the hash size.
2. Secondary Hash function – This function determines the increment (or step size) used for probing
in the case of a collision. It should produce a positive step size that is less than the size of the table.
h2(k) = 1 + ( k mod (m-1) )
13 List out Hash functions
1. Division method – The hash function depends upon the remainder of division. Typically the divisor
is the table length.
2. Mid square – In this method, the key is squared and the middle or mid part of the result is used as
the index.
If the key is a string, it has to be preprocessed to produce a number.
3. Multiplicative hash function – The given record is multiplied by some constant value.
The formula for computing the hash key is-
H(key) = floor (p *(fractional part of key* A)) where p is integer constant and A is constant real
number.
4.Digit Folding
5.Digit Analysis
14 What are the various ways to implement a Dictionary?
Dictionaries can be implemented in several ways, each with its own advantages and use cases.
1. Hash Tale
2. Binary Search Tree
3. Balanced Search Trees (eg. AVAL / Red-Black Tree)
4. Trie (Prefix Tree)
5. Skip List
6. Linear List
7. Database Management Systems (DBMS)

You might also like