DSA Unit 2 - Question Bank
DSA Unit 2 - Question Bank
Part A
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)
PART-B
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()
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())
11. What is a Deque? Explain its types and its operations with diagrammatic representation.
(Apr/May 2022)
Refer page no: 247 (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