0% found this document useful (0 votes)
102 views11 pages

DSA Unit 2 - Question Bank

A data structure is a method for organizing and storing data to allow efficient retrieval and usage. Common data structures include stacks, queues, lists, trees, graphs, and tables. Data structures can be linear or non-linear. Linear data structures like arrays, stacks, and queues arrange data in a sequential manner where each item is related to the previous and next item. Non-linear data structures like trees and graphs do not arrange data sequentially. An abstract data type (ADT) defines a set of operations on a data structure without specifying how it is implemented. Lists are a common ADT that can be implemented using arrays or linked lists. Linked lists use nodes connected by pointers to store data sequentially. Singly linked lists use forward

Uploaded by

vengai marbhan
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)
102 views11 pages

DSA Unit 2 - Question Bank

A data structure is a method for organizing and storing data to allow efficient retrieval and usage. Common data structures include stacks, queues, lists, trees, graphs, and tables. Data structures can be linear or non-linear. Linear data structures like arrays, stacks, and queues arrange data in a sequential manner where each item is related to the previous and next item. Non-linear data structures like trees and graphs do not arrange data sequentially. An abstract data type (ADT) defines a set of operations on a data structure without specifying how it is implemented. Lists are a common ADT that can be implemented using arrays or linked lists. Linked lists use nodes connected by pointers to store data sequentially. Singly linked lists use forward

Uploaded by

vengai marbhan
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
You are on page 1/ 11

UNIT-II- LINEAR STRUCTURES

Part A

1. What is data structure?


 A data structure is a method for organizing and storing data which would allow efficient data
retrieval and usage.
 A data structure is a way of organizing data that considers not only the items stored, but also
their relationships to each other.
2. Why do we need data structures?
● Data structures allow us to achieve an important goal: component reuse.
● Once data structure has been implemented, it can be used again and again in various
applications
3. List some common data structures.
● Stacks
● Queues
● Lists
● Trees
● Graphs
● Tables
4. How data structures are classified?
Data structures are classified into two categories based on how the data items are
operated:
i. Primitive data structure
ii. Non-Primitive data structure
5. Differentiate linear and non-linear data structure.
linear data structure Non-linear data structure
Data are arranged in linear or Data are not arranged in linear
sequential manner manner
Every items is related to its previous Every item is attached with many
and next item other items
Data items can be traversed in a Data items cannot be traversed
single run. in a single run.
Implementation is easy Implementation is difficult.
Example: array, stack, queue, linked Example: tree, graph
List
6. Define ADT (Abstract Data Type)
An abstract data type (ADT) is a set of operations and mathematical abstractions , which can be
viewed as how the set of operations is implemented. Objects like lists, sets and graphs, along with
their operation, can be viewed as abstract data types, just as integers, real numbers and Booleans.
7. Mention the features of ADT.
a. Modularity
i. Divide program into small functions
ii. Easy to debug and maintain
iii. Easy to modify
b. Reuse
i. Define some operations only once and reuse them in future
c. Easy to change the implementation
8. Define List ADT
A list is a sequence of zero or more elements of a given type. The list is represented as
sequence of elements separated by comma. A1, A2, A3…..AN
Where N>0 and A is of type element
9. What are the types of linked lists?
There are three types
Singly linked list
Doubly linked list
Circularly linked list
10. What are the ways of implementing linked list?
The list can be implemented in the following ways:
i. Array implementation
ii. Linked-list implementation
11. How the singly linked lists can be represented?

Each node has two elements that is data and next


12. How the doubly linked list can be represented?

Doubly linked list is a collection of nodes where nodes are connected by forwarded and
backward link.
Each node has three fields:
1. Address of previous node
2. Data
3. Address of next node.
13. What are benefits of ADT?
a. Code is easier to understand
b. Implementation of ADT can be changed without requiring changes to the program
that uses the ADT
14. When singly linked list can be represented as circular linked list?
In a singly linked list, all the nodes are connected with forward links to the next nodes in
the list. The last node has a next field, NULL. In order to implement the circularly linked lists from
singly linked lists, the last node’s next field is connected to the first node.

15. When doubly linked list can be represented as circular linked list?
In a doubly linked list, all nodes are connected with forward and backward links to the
next and previous nodes respectively. In order to implement circular linked lists from
doubly linked lists, the first node’s previous field is connected to the last node and the
last node’s next field is connected to the first node.
16. List down the applications of List.
a. Representation of polynomial ADT
b. Used in radix and bubble sorting
c. In a FAT file system, the metadata of a large file is organized as a linked list of FAT entries.
d. Simple memory allocators use a free list of unused memory regions, basically a linked list with the
list pointer inside the free memory itself
17. What are the advantages of linked list?
a. Save memory space and easy to maintain
b. It is possible to retrieve the element at a particular index
c. It is possible to traverse the list in the order of increasing index
d. It is possible to change the element at a particular index to a different value, without affecting any
other value.
18. Mention the demerits of linked list
a. It is not possible to go backwards through the list
b. Unable to jump to the beginning of list from the end.
19. What are the operations performed in list?
The following operations can be performed on a list
a. Insertion
b. Insert at beginning
c. Insert at end
d. Insert after specific node
e. Insert before specific node
e. Deletion
f. Delete at beginning
g. Delete at end
h. Delete after specific node
i. Delete before specific node
20. What is a circular linked list?
A circular linked list is a special type of linked list that supports traversing from the end
of the list to the beginning by making the last node point back to
the head of the list.
21. List three examples that uses linked list?
a. Polynomial ADT b. Radix sort c. Multi lists
22. List out the different ways to implement the list?
1. Array Based Implementation
2. Linked list Implementation
i. Singly linked list
ii. Doubly linked list
25. Write the advantages of Linked List over Array.
1. Size of the list doesn't need to be mentioned at the beginning of the program.
2. As the linked list doesn't have a size limit, we can go on adding new nodes (elements) and increasing
the size of the list to any extent.
26. Write the disadvantages of Linked List over Array.
1. Nodes do not have their own address. Only the address of the first node is stored and in order to
reach any node, we need to traverse the whole list from beginning to the desired node.
2. As all Nodes don't have their particular address, BINARY SEARCH cannot be performed
27. Define Stack.
A stack is an ordered list in which all insertions and deletions are made at one end, called
the top. It is an abstract data type and based on the principle of
LIFO (Last In First Out).
28. What are the operations of the stack?
a. CreateStack/ InitStack(Stack) – creates an empty stack
b. Push(Item) – pushes an item on the top of the stack
c. Pop(Item) – removes the top most element from the stack
d. Top(Stack) – returns the first element from the stack
e. IsEmpty(Stack) – returns true if the stack is empty
29. Write the routine to push a element into a stack.
Push(Element X, Stack S)
{ if(IsFull(S)
{ Error(“Full Stack”); } else S→Array[+
+S→TopOfStack]=X;
}
30. How the operations performed on linked list implementation of stack?
a. Push and pop operations at the head of the list.
b. New nodes should be inserted at the front of the list, so that they become the top of the stack.
c. Nodes are removed from the front(top) of the stack.
31. What are the applications of stack?
The following are the applications of stacks
Evaluating arithmetic expressions
Balancing the paranthesis
Tower of Hanoi
Function calls
Tree Traversal
32. How the stack is implemented by linked list?
It involves dynamically allocating memory space at run time while performing stack operations.
Since it consumes only that much amount of space is required for holding its data elements, it prevents wastage of
memory space.
33. Write the routine to pop a element from a stack.
stack = ["Amar", "Akbar", "Anthony"]
stack.append("Ram")
stack.append("Iqbal")
print(stack)

# Removes the last item


print(stack.pop())
print(stack)

# Removes the last item


print(stack.pop())
print(stack)
34. Define queue.
It is a linear data structure that maintains a list of elements such that insertion happens at
rear end and deletion happens at front end. FIFO – First In First Out principle
35. What are the operations of a queue?
The operations of a queue are
● isEmpty()
● isFull()
● insert()
● delete()
● display()
36. What are the types of queue?
The following are the types of queue:
● Double ended queue
● Circular queue
● Priority queue
37. Define double ended queue
 It is a special type of queue that allows insertion and deletion of elements at both Ends.
● It is also termed as DEQUE.

38. How the queue is implemented by linked list?


• It is based on the dynamic memory management techniques which allow allocation and
De-allocation of memory space at runtime.
Insert operation
It involves the following subtasks:
1. Reserving memory space of the size of a queue element in memory
2. Storing the added value at the new location
3. Linking the new element with existing queue
4. Updating the rear pointer
Delete operation
It involves the following subtasks:
1. Checking whether queue is empty
2. Retrieving the front most element of the queue
3. Updating the front pointer
Returning the retrieved value
41. Write the routine to delete a element from a queue
del()
if(front == NULL) /*checking whether the queue is empty*/
return(-9999)
else
i = front→element;front = front→next;return i;}
42. What are the applications of queue?
The following are the areas in which queues are applicable
a. Simulation
b. Batch processing in an operating systems
c. Multiprogramming platform systems
d. Queuing theory
e. Printer server routines
f. Scheduling algorithms like disk scheduling , CPU scheduling
g. I/O Buffer Requests
43. What are push and pop operations?
• Push – adding an element to the top of stack
• Pop – removing or deleting an element from the top of stack
44. Convert the infix (a+b)*(c+d)/f into postfix & prefix expression
Postfix :ab+cd+*f/
Prefix: / * + a b + c d f
45. Write postfix from of the expression –A+B-C+D?
A-B+C-D+
46. How do you test for an empty queue?
To test for an empty queue, we have to check whether READ=HEAD where REAR is a pointer pointing to the last
node in a queue and HEAD is a pointer that pointer to the dummy header. In the case of array implementation of
queue, the condition to be checked for an empty queue is READ<FRONT.
47. What are the postfix and prefix forms of the expression?
A+B*(C-D)/(P-R)
Postfix form: ABCD-*PR-/+ Prefix form: +A/*B- CD-PR

48. How do you test for an empty stack?


To check if the stack is empty, we only need to check whether top and bottom are the same number.
bool stack_empty(stack S) //@requires is_stack(S); return S->top == S->bottom;
49. What are the features of stacks?
● Dynamic data structures
● Do not have a fixed size
● Do not consume a fixed amount of memory
● Size of stack changes with each push() and pop() operation.
Each push() and pop() operation increases and decreases the size of the stack by 1, respectively.
50. Write a routine for IsEmpty condition of queue.
If a queue is empty, this function returns 'true', else
it returns 'false'.

PART-B

1. Explain the various operations of the list ADT.

Refer page no: 202 (Michael .Goodrich)

2. Write the program for array implementation of lists

my_list = ['p', 'r', 'o', 'b', 'e']

# first item print(my_list[0])


#p
# third item print(my_list[2])
#o
# fifth item
print(my_list[4]) # e
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]
# Nested indexing
print(n_list[0][1])
print(n_list[1][3])
# Error! Only integer can be used for indexing print(my_list[4.0])

3. Explain the operations of singly linked lists


Refer page no: 256 (Michael .Goodrich)
Traversal - access each element of the linked list.
Insertion - adds a new element to the linked list.
Deletion - removes the existing elements.
Search - find a node in the linked list.
Sort - sort the nodes of the linked list.

4. Explain the operations of doubly linked lists


Refer page no: 281 (Michael .Goodrich)
Following are the basic operations supported by a list.
 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Insert Last − Adds an element at the end of the list.
 Delete Last − Deletes an element from the end of the list.
 Insert After − Adds an element after an item of the list.
 Delete − Deletes an element from the list using the key.
 Display forward − Displays the complete list in a forward manner.
 Display backward − Displays the complete list in a backward manner.

5. Explain the operations of circularly linked lists.


Refer page no: 266 (Michael .Goodrich)

6. Explain the steps involved in insertion and deletion into a doubly linked list.
Write a Python program for linked list implementation of list.
1. Inserting Items to Empty List
2. Inserting Items at the End
3. Deleting Elements from the Start
4. Deleting Elements from the End
5. Traversing the Linked List
# Initialise the Node
class Node:
def init (self, data):
self.item = data
self.next = None
self.prev = None
# Class for doubly Linked List
class doublyLinkedList:
def init (self):
self.start_node = None
# Insert Element to Empty list
def InsertToEmptyList(self, data):
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
else:
print("The list is empty")
# Insert element at the end
def InsertToEnd(self, data):
# Check if the list is empty
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
return
n = self.start_node
# Iterate till the next reaches NULL
while n.next is not None:
n = n.next
new_node = Node(data)
n.next = new_node
new_node.prev = n
# Delete the elements from the start
def DeleteAtStart(self):
if self.start_node is None:
print("The Linked list is empty, no element to delete")
return
if self.start_node.next is None:
self.start_node = None
return
self.start_node = self.start_node.next
self.start_prev = None;
# Delete the elements from the end
def delete_at_end(self):
# Check if the List is empty
if self.start_node is None:
print("The Linked list is empty, no element to delete")
return
if self.start_node.next is None:
self.start_node = None
return
n = self.start_node
while n.next is not None:
n = n.next
n.prev.next = None
# Traversing and Displaying each element of the list
def Display(self):
if self.start_node is None:
print("The list is empty")
return
else:
n = self.start_node
while n is not None:
print("Element is: ", n.item)
n = n.next
print("\n")
# Create a new Doubly Linked List
NewDoublyLinkedList = doublyLinkedList()
# Insert the element to empty list
NewDoublyLinkedList.InsertToEmptyList(10)
# Insert the element at the end
NewDoublyLinkedList.InsertToEnd(20)
NewDoublyLinkedList.InsertToEnd(30)
NewDoublyLinkedList.InsertToEnd(40)
NewDoublyLinkedList.InsertToEnd(50)
NewDoublyLinkedList.InsertToEnd(60)
# Display Data
NewDoublyLinkedList.Display()
# Delete elements from start
NewDoublyLinkedList.DeleteAtStart()
# Delete elements from end
NewDoublyLinkedList.DeleteAtStart()
# Display Data
NewDoublyLinkedList.Display()

7. Explain Stack ADT and its operations


Refer page no: 229 (Michael .Goodrich)

8. Explain array based implementation of stacks


Refer page no: 231 (Michael .Goodrich)

9. Explain linked list implementation of stacks


class Node:
# Class to create nodes of linked list
# constructor initializes node automatically
def __init (self,data):
self.data = data
self.next = None
class Stack:
# head is default NULL
def __init (self):
self.head = None
# Checks if stack is empty
def isempty(self):
if self.head == None:
return True
else:
return False
# Method to add data to the stack
# adds to the start of the stack
def push(self,data):
if self.head == None:
self.head=Node(data)
else:
newnode = Node(data)
newnode.next = self.head
self.head = newnode
# Remove element that is the current head (start of the stack)
def pop(self):
if self.isempty():
return None
else:
# Removes the head node and makes
#the preceding one the new head
poppednode = self.head
self.head = self.head.next
poppednode.next = None
return poppednode.data
# Returns the head node data
def peek(self):
if self.isempty():
return None
else:
return self.head.data
# Prints out the stack
def display(self):
iternode = self.head
if self.isempty():
print("Stack Underflow")

else:

while(iternode != None):
print(iternode.data,"->",end = " ")
iternode = iternode.next
return

# Driver code
MyStack = Stack()
MyStack.push(11)
MyStack.push(22)
MyStack.push(33)
MyStack.push(44)
# Display stack elements
MyStack.display()
# Print top element of stack
print("\nTop element is ",MyStack.peek())
# Delete top elements of stack
MyStack.pop()
MyStack.pop()
# Display stack elements
MyStack.display()
# Print top element of stack
print("\nTop element is ", MyStack.peek())

10. Explain the applications of queues


Queue is used when things don’t have to be processed immediately, but have to be
processed in First In First Out order like Breadth First Search. This property of Queue
makes it also useful in following kind of scenarios.
1) When a resource is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
5) Variations: ( Deque, Priority Queue, Doubly Ended Priority Queue )
Some other applications of Queue:
Applied as waiting lists for a single shared resource like CPU, Disk, Printer.
Applied as buffers on MP3 players and portable CD players.
Applied on Operating system to handle interruption.
Applied to add song at the end or to play from the front.

11. What is a Deque? Explain its types and its operations with diagrammatic representation.
(Apr/May 2022)
Refer page no: 247 (Michael .Goodrich)

12. Explain how to reverse data using Stack ADT


Refer page no: 235 (Michael .Goodrich)

13. Explain the concept of matching parenthesis in Stack ADT.


Refer page no: 235 (Michael .Goodrich)

14. Trace the algorithm to convert the infix expression A-(B/c+(D%E*F)/G)*H to a postfix expression. Evaluate
the given postfix expression 9 3 4 *8 + 4 /-.(Nov/Dec,2018)
Refer Class notes

15. Write an algorithm to convert infix expression to a postfix expression.


 Scan the infix expression from left to right. 
 If the scanned character is an operand, output it. 
 Else, 
 If the precedence and associativity of the scanned operator are greater than the precedence and
associativity of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), then push it.
 ‘^’ operator is right associative and other operators like ‘+’,’-‘,’*’ and ‘/’ are left-associative. Check
especially for a condition when both,  operator at the top of the stack and the scanned operator are ‘^’. In
this condition, the precedence of the scanned operator is higher due to its right associativity. So it will be
pushed into the operator stack. In all the other cases when the top of the operator stack is the same as the
scanned operator, then pop the operator from the stack because of left associativity due to which the
scanned operator has less precedence. 
 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the
scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis
while popping then stop there and push the scanned operator in the stack.) 
 If the scanned character is an ‘(‘, push it to the stack. 
 If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the
parenthesis. 
 Repeat steps 2-6 until the infix expression is scanned. 
 Print the output 
 Pop and output from the stack until it is not empty.

You might also like