Moona Answer
Moona Answer
Define linear data structure and explain difference between queue and stack
with example.
Linear Data Structures are a type of data structure in computer science where data
elements are arranged sequentially or linearly. Each element has a previous and
next adjacent, except for the first and last elements.
A linear data
structure that
A linear data structure that follows the
Definition follows the Last In
F irst In First Out (FIFO) principle.
F irst Out (LIFO)
principle.
Function call
Scheduling processes in operating
management (call
Use Cases systems, managing requests in a printer
stack), expression
queue, breadth-first search in graphs.
evaluation and
syntax parsing, undo
mechanisms in text
editors.
Browser history
Customer service lines, CPU task
Examples (back button),
scheduling.
reversing a word.
A stack of plates:
Real-World A queue at a ticket counter: the first
you add and remove
Analogy person in line is the first to be served.
plates from the top.
Typically uses a
Memory contiguous block of Typically uses a circular buffer or linked
Structure memory or linked list.
list.
Can be implemented
Implementat Can be implemented using arrays, linked
using arrays or
ion lists, or circular buffers.
linked lists.
2.explain operation on non-linear data structure with example
non-linear data structure is a structure where data elements are not placed
A
sequentially. Instead, elements are connected in a hierarchical or networked manner,
where a single element can be connected to multiple elements.
efinition: A data structure where traversal is not done sequentially but hierarchically
D
or through links/branches is called a non-linear data structure.
3. Heaps
4. Tries
tree is a hierarchical structure where each node has a parent (except the root) and
A
can have zero or more children.
● Traversal: Visit all nodes (Preorder, Inorder, Postorder, Level Order)
50
/ \
30 70
/ \ / \
20 40 60 80
Inorder Traversal (Left → Root → Right): 20, 30, 40, 50, 60, 70, 80
def inorder(node):
if node is not None:
inorder(node.left)
print(node.data)
inorder(node.right)
Example Graph:
A -- B
| |
C -- D
while queue:
node = queue.popleft()
if node not in visited:
print(node)
visited.add(node)
queue.extend(graph[node] - visited)
Real-Life Applications
Conclusion
on-linear data structures allow us to model complex relationships. Trees are used
N
in hierarchical data representation like folder systems or expression trees. Graphs
are used in modeling networks and paths like road maps or web links. Key
operations include insertion, deletion, traversal, and searching. They are powerful
tools for solving real-world computational problems.
3. What is Stack Data Structure? Write Algorithm for Push Operation
stack is a linear data structure that follows the Last In, First Out (LIFO) principle. In
A
a stack, the element that is inserted last is the one that is removed first.
● The last item pushed into the stack is the first one to be popped out.
Real-Life Example
A stack of plates:
Implementation of Stack
topindex.
2. If not full, increment the
top
3. Assign the new element to the position pointed by .
Let:
●
MAXbe the maximum size of the stack.
●
stack[]be the array representing the stack.
●
topbe the index of the top element (initialized to -1).
Step 5: RETURN
Explanation of Algorithm
● Step 1 checks whether the stack is full. If it is, insertion cannot be done.
dataand a
● Each element is a node with nextpointer.
top
● The new node becomes the new .
Applications of Stack
Conclusion
stack is a fundamental data structure with LIFO behavior. The push operation adds
A
an element to the top. It is simple but powerful, forming the basis for many
system-level and application-level functionalities.
4. What is a Linked List? Give Memory Representation of Linked List
linked listis a linear data structure in which elements, callednodes, are stored in
A
a sequence, where each node contains:
● Next: A reference (or pointer) to the next node in the sequence.
nlike arrays, linked lists do not store elements in contiguous memory locations.
U
Instead, each element points to the next, allowing dynamic memory allocation. This
allows linked lists to efficiently grow or shrink in size.
1. S
ingly Linked List: Each node has a reference to the next node, and the last
node’s reference isNULL
, indicating the end of thelist.
2. D
oubly Linked List: Each node has references to both the next and the
previous node.
3. C
ircular Linked List: The last node points back tothe first node, creating a
circular reference.
he linked list typically has ahead pointer, which points to the first node in the list. If
T
NULL
the list is empty, the head pointer is .
Memory Representation of Linked List
In asingly linked list, the memory representation consists of individual nodes. Each
node is created dynamically (via pointers or references in high-level languages), and
the nodes are connected by the nextpointer.
Data = 10
1. Node 1: Next = address of Node 2
,
Data = 20
2. Node 2: Next = address of Node 3
,
Data = 30
3. Node 3: Next = NULL
,
In memory:
ode
N 10 ddress of Node
A
1 2
ode
N 20 ddress of Node
A
2 3
ode
N 30 NULL
3
he head of the list points to the first node (Node 1). If the list is empty, the head
T
NULL
pointer is .
Consider the following memory representation of a linked list with three nodes:
head -> [10 | *] -> [20 | *] -> [30 | NULL]
Where:
●
headpoints to the first node.
○ Data: The value stored in that node (e.g., 10, 20, 30).
1. Insertion: Add a node to the list (at the beginning, end, or middle).
nextpointers).
3. Traversal: Visit each node in the list (usually by following the
● D
ynamic Size: The size of the linked list can growor shrink dynamically,
making it more flexible than arrays.
● E
fficient Insertions/Deletions: Insertion and deletionoperations can be
performed efficiently, especially at the beginning or middle of the list.
● M
emory Overhead: Each node requires extra memory for the
pointer/reference to the next node.
● A
ccess Time: Accessing an element in a linked list requires traversal from
the head to the desired node (O(n) time complexity).
Conclusion
linked list is a flexible, dynamic data structure that consists of nodes, where each
A
node stores data and a reference to the next node. The memory representation of a
linked list is non-contiguous, with each node dynamically allocated and linked
through pointers. Linked lists are widely used in situations where the size of the data
structure is not known in advance or when frequent insertions and deletions are
required.