Data Structure and Application (1333203) - Winter 2024 Solution

Solution guide for Data Structure and Application (1333203) Winter 2024 exam

Question 1(a) [3 marks]

Write names of linear data structures.

Answer:

Linear Data Structures
1. Array
2. Stack
3. Queue
4. Linked List

Mnemonic: "All Students Queue Lazily"

Question 1(b) [4 marks]

Define Time and space complexity.

Answer:

Complexity TypeDefinitionNotation
Time ComplexityMeasures how execution time increases as input size growsO(n), O(1), O(log n)
Space ComplexityMeasures how memory usage increases as input size growsO(n), O(1), O(log n)

Diagram:

goat

Mnemonic: "Time Steps, Space Stores"

Question 1(c) [7 marks]

Explain concept of class & object with example.

Answer:

Diagram:

ConceptDefinitionExample
ClassBlueprint or template for creating objectsStudent class with properties (rollNo, name) and methods (setData, displayData)
ObjectInstance of a class with specific valuesstudent1 (rollNo=101, name="Raj")

Code Example:

Python

Mnemonic: "Class Creates, Objects Operate"

Question 1(c) OR [7 marks]

Develop a class for managing student records with instance methods for adding and removing students from a class.

Answer:

Diagram:

Code:

Python

Mnemonic: "Add Accumulates, Remove Reduces"

Question 2(a) [3 marks]

Explain the importance of constructor in class.

Answer:

Constructor Importance
1. Initializes object's data members
2. Automatically called when object is created
3. Can have different versions (default, parameterized, copy)

Mnemonic: "Initialization Always Creates"

Question 2(b) [4 marks]

Explain different operations on stack.

Answer:

OperationDescriptionExample
PushAdds element to toppush(5)
PopRemoves element from topx = pop()
Peek/TopViews top element without removingx = peek()
isEmptyChecks if stack is emptyif(isEmpty())

Diagram:

goat

Mnemonic: "Push Pop Peek Properly"

Question 2(c) [7 marks]

Describe evaluation algorithm of postfix expression A B C + * D /

Answer:

Diagram:

goat
StepSymbolActionStack
1APush onto stackA
2BPush onto stackA,B
3CPush onto stackA,B,C
4+Pop B,C; Push B+CA,B+C
5*Pop A,B+C; Push A*(B+C)A*(B+C)
6DPush onto stackA*(B+C),D
7/Pop A*(B+C),D; Push A*(B+C)/DA*(B+C)/D

Mnemonic: "Read, Push, Pop, Calculate"

Question 2(a) OR [3 marks]

Write difference between stack and queue.

Answer:

FeatureStackQueue
PrincipleLIFO (Last In First Out)FIFO (First In First Out)
OperationsPush/PopEnqueue/Dequeue
Access PointsSingle end (top)Two ends (front, rear)

Mnemonic: "Stack LIFO, Queue FIFO"

Question 2(b) OR [4 marks]

Explain concept of circular queue.

Answer:

Diagram:

FeatureDescription
StructureLinear data structure with connected ends
AdvantageEfficiently uses memory by reusing empty spaces
OperationsEnqueue, Dequeue with modulo arithmetic

Mnemonic: "Circular Connects Front to Rear"

Question 2(c) OR [7 marks]

Describe the procedure for inserting a new node after and before a given node in a singly linked list.

Answer:

Diagram:

goat
InsertionSteps
After Node X1. Create new node N
2. Set N's next to X's next
3. Set X's next to N
Before Node X1. Create new node N
2. Find node A pointing to X
3. Set N's next to X
4. Set A's next to N

Mnemonic: "After: Set Next Links, Before: Find Previous First"

Question 3(a) [3 marks]

Explain traversing a linked list.

Answer:

Diagram:

goat
StepAction
1Initialize pointer to head
2Access data at current node
3Move pointer to next node
4Repeat until NULL

Mnemonic: "Start, Access, Move, Repeat"

Question 3(b) [4 marks]

Explain expression conversion from infix to postfix.

Answer:

Diagram:

goat
StepActionStackOutput
1Scan from left to right
2If operand, add to outputA
3If operator, push if higher precedence+A
4Pop lower precedence operators+A B
5Push current operator*A B
6Continue until expression ends*A B C
7Pop remaining operatorsA B C * +

Mnemonic: "Operators Push Pop, Operands Output Directly"

Question 3(c) [7 marks]

Write a program to delete a node at the beginning and end of singly linked list.

Answer:

Diagram:

goat

Code:

Python

Mnemonic: "Delete First: Shift Head, Delete Last: Find Second-Last"

Question 3(a) OR [3 marks]

Explain searching an element in linked list.

Answer:

Diagram:

goat
StepDescription
1Start from head node
2Compare current node's data with key
3If match found, return true
4Else, move to next node and repeat

Mnemonic: "Start, Compare, Move, Repeat"

Question 3(b) OR [4 marks]

Explain concepts of circular linked lists.

Answer:

Diagram:

FeatureDescription
StructureLast node points to first node
AdvantageNo NULL pointers, efficient for circular operations
TraversalNeed extra condition to prevent infinite loop

Mnemonic: "Last Links to First"

Question 3(c) OR [7 marks]

Explain algorithm to search a particular element from list using Binary Search.

Answer:

Diagram:

Code:

Python

Mnemonic: "Middle, Compare, Eliminate Half"

Question 4(a) [3 marks]

Write applications of linked list.

Answer:

Applications of Linked List
1. Implementation of stacks and queues
2. Dynamic memory allocation
3. Image viewer (next/previous images)

Mnemonic: "Store Data Dynamically"

Question 4(b) [4 marks]

Differentiate between singly linked list and doubly linked list.

Answer:

FeatureSingly Linked ListDoubly Linked List
Node StructureOne pointer (next)Two pointers (next, prev)
TraversalForward onlyBoth directions
MemoryLess memoryMore memory
OperationsSimple, less codeComplex, more flexible

Diagram:

goat

Mnemonic: "Single Direction, Double Direction"

Question 4(c) [7 marks]

Write a program to sort numbers in ascending order using selection sort algorithm.

Answer:

Diagram:

goat

Code:

Python

Mnemonic: "Find Minimum, Swap Position"

Question 4(a) OR [3 marks]

Explain bubble sort algorithm.

Answer:

Diagram:

Key Points
Compare adjacent elements
Swap if they are in wrong order
Largest element bubbles to end in each pass

Mnemonic: "Bubble Bigger Elements Upward"

Question 4(b) OR [4 marks]

Differentiate Linear & Binary search.

Answer:

FeatureLinear SearchBinary Search
Working PrincipleSequential checkingDivide and conquer
Time ComplexityO(n)O(log n)
Data ArrangementUnsorted or sortedMust be sorted
Best ForSmall datasetsLarge datasets

Mnemonic: "Linear Looks at All, Binary Breaks in Half"

Question 4(c) OR [7 marks]

Explain Quick sort & Merge sort algorithm.

Answer:

Quick Sort:

Merge Sort:

AlgorithmPrincipleAverage TimeSpace Complexity
Quick SortPartitioning around pivotO(n log n)O(log n)
Merge SortDivide, conquer, combineO(n log n)O(n)

Mnemonic: "Quick Partitions, Merge Divides"

Question 5(a) [3 marks]

Define a complete binary tree.

Answer:

Diagram:

goat
PropertyDescription
All levels filledExcept possibly the last level
Last level filled from leftNodes added from left to right

Mnemonic: "Fill Left to Right, Level by Level"

Question 5(b) [4 marks]

Explain inorder traversal of a binary tree.

Answer:

Diagram:

goat
StepAction
1Traverse left subtree
2Visit root node
3Traverse right subtree

Code:

Python

Mnemonic: "Left, Root, Right"

Question 5(c) [7 marks]

Write a program to inserting a node into a binary search tree.

Answer:

Diagram:

Code:

Python

Mnemonic: "Compare, Move, Insert"

Question 5(a) OR [3 marks]

State the fundamental characteristic of a binary search tree.

Answer:

Characteristics of Binary Search Tree
1. Left child nodes < Parent node
2. Right child nodes > Parent node
3. No duplicate values allowed

Mnemonic: "Left Less, Right More"

Question 5(b) OR [4 marks]

Explain postorder traversal of a binary tree.

Answer:

Diagram:

goat
StepAction
1Traverse left subtree
2Traverse right subtree
3Visit root node

Code:

Python

Mnemonic: "Left, Right, Root"

Question 5(c) OR [7 marks]

Write a program to delete a node from a binary search tree.

Answer:

Diagram:

Code:

Python

Mnemonic: "Find, Replace, Reconnect"