0% found this document useful (0 votes)
58 views27 pages

DSU Super 25 by Rajan SirUpdated V2V

Imp DSU msbte Related

Uploaded by

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

DSU Super 25 by Rajan SirUpdated V2V

Imp DSU msbte Related

Uploaded by

Himesh Kochale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

1.

Differentiate between stack and queue


Ans.

2. Convert infix expression into prefix expression or postfix.


Ans.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
3. Describe working of linear search with an example.
Ans.
In linear search, search element is compared with each element from the list in a sequence.
Comparison starts with first element from the list and continues till number is found or comparison
reaches to the last element of the list.
As each element is checked with search element, the process of searching requires more time. Time
complexity of linear search is O (n) where n indicates number of elements in list.
Linear search on sorted array:-On sorted array search takes place till element is found or comparison
reaches to an element greater than search element.

Example:- Using array representation Input list 10, 20, 30, 40, 50 and Search element 30, Index =0
Iteration 1

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
4. Find the position of element 29 using binary search method in an array ‘A’ given below. Show each step.
A={11,5,21,3,29,17,2,43}
Ans.
An array which is given A[]= {11,5,21,3,29,17,2,43} is not in sorted manner, first we need to sort them in
order; So an array will be A[ ]={2,3,5,11,17,21,29,43} and the value to be searched is VAL = 29. The binary
search algorithm will proceed in the following manner.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
So, Element 29 is found at 6th location in given array A[]={2,3,5,11,17,21,29,43}.

5.Explain Double Ended Queue with example


Ans.

Deque (or double-ended queue)


The deque stands for Double Ended Queue. Deque is a linear data structure where the insertion and
deletion operations are performed from both ends. We can say that deque is a generalized version of
the queue.

Though the insertion and deletion in a deque can be performed on both ends, it does not follow the FIFO
rule. The representation of a deque is given as follows -

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Types of deque
There are two types of deque -

o Input restricted queue


o Output restricted queue
Input restricted Queue

In input restricted queue, insertion operation can be performed at only one end, while deletion can be
performed from both ends.

Output restricted Queue

In output restricted queue, deletion operation can be performed at only one end, while insertion can be
performed from both ends.

Operations performed on deque


There are the following operations that can be applied on a deque -

o Insertion at front
o Insertion at rear
o Deletion at front
o Deletion at rear
We can also perform peek operations in the deque along with the operations listed above. Through peek
operation, we can get the deque's front and rear elements of the deque. So, in addition to the above
operations, following operations are also supported in deque -

o Get the front item from the deque


Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
o Get the rear item from the deque
o Check whether the deque is full or not
o Checks whether the deque is empty or not

6. Describe working of bubble sort , insertion , selection with example.


Ans.

1. Bubble Sort

Working:

• Bubble sort compares adjacent elements in an array and swaps them if they are in the wrong order.
• This process is repeated for each element, and after each complete pass, the largest unsorted
element "bubbles" up to its correct position.
• The algorithm continues until no more swaps are needed, indicating that the array is sorted.

Steps:

1. Compare the first two elements. If the first is greater than the second, swap them.
2. Move to the next pair and repeat until you reach the end of the array.
3. After each full pass through the array, the largest element gets placed in its correct position.
4. Repeat the process for the remaining unsorted part of the array until the entire array is sorted.

Example (Sorting the array [5, 3, 8, 4, 2]):

1. First pass:
o Compare 5 and 3 → Swap them → [3, 5, 8, 4, 2]
o Compare 5 and 8 → No swap → [3, 5, 8, 4, 2]
o Compare 8 and 4 → Swap them → [3, 5, 4, 8, 2]
o Compare 8 and 2 → Swap them → [3, 5, 4, 2, 8]
o After the first pass, 8 is in its correct position.
2. Second pass:
o Compare 3 and 5 → No swap → [3, 5, 4, 2, 8]
o Compare 5 and 4 → Swap them → [3, 4, 5, 2, 8]
o Compare 5 and 2 → Swap them → [3, 4, 2, 5, 8]
o After the second pass, 5 is in its correct position.
3. Third pass:
o Compare 3 and 4 → No swap → [3, 4, 2, 5, 8]
o Compare 4 and 2 → Swap them → [3, 2, 4, 5, 8]
o After the third pass, 4 is in its correct position.
4. Fourth pass:
o Compare 3 and 2 → Swap them → [2, 3, 4, 5, 8]
o Now the array is sorted.

Time Complexity:

• Best case: O(n)O(n)O(n) (if the array is already sorted)


• Average and worst case: O(n2)O(n^2)O(n2)

2. Insertion Sort

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Working:

• Insertion sort builds the final sorted array one element at a time.
• It works by picking elements from the unsorted part of the array and inserting them into the correct
position in the sorted part of the array.
• It shifts elements in the sorted part to the right to make space for the new element.

Steps:

1. Start with the second element (since the first is considered sorted).
2. Compare the current element with the elements in the sorted part.
3. Move elements that are greater than the current element to one position ahead.
4. Insert the current element in the correct position.
5. Repeat until the entire array is sorted.

Example (Sorting the array [5, 3, 8, 4, 2]):

1. Insert 3: Compare with 5 → Move 5 to the right → [3, 5, 8, 4, 2]


2. Insert 8: No change as 8 is greater than 5 → [3, 5, 8, 4, 2]
3. Insert 4: Compare with 8 → Move 8 → Compare with 5 → Move 5 → Insert 4 → [3, 4, 5, 8, 2]
4. Insert 2: Compare with 8, 5, 4, and 3 → Move all elements → Insert 2 → [2, 3, 4, 5, 8]

Time Complexity:

• Best case: O(n)O(n)O(n) (if the array is already sorted)


• Average and worst case: O(n2)O(n^2)O(n2)

3. Selection Sort

Working:

• Selection sort works by selecting the smallest (or largest) element from the unsorted part of the
array and swapping it with the first unsorted element.
• It repeats this process until the entire array is sorted.

Steps:

1. Find the smallest (or largest) element in the unsorted part of the array.
2. Swap it with the first unsorted element.
3. Move the boundary of the sorted part of the array to the right and repeat until the array is sorted.

Example (Sorting the array [5, 3, 8, 4, 2]):

1. First pass: Find the smallest element (2) and swap it with the first element → [2, 3, 8, 4, 5]
2. Second pass: Find the smallest element in the remaining unsorted part ([3, 8, 4, 5]), which is 3 →
No swap → [2, 3, 8, 4, 5]
3. Third pass: Find the smallest element in the remaining unsorted part ([8, 4, 5]), which is 4 → Swap
it with 8 → [2, 3, 4, 8, 5]
4. Fourth pass: Find the smallest element in the remaining unsorted part ([8, 5]), which is 5 → Swap it
with 8 → [2, 3, 4, 5, 8]
5. Now the array is sorted.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
7. Construct a binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 show each step of
construction of BST
Ans.
Stepwise construction of Binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 is as
follows:

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
8. Describe circular linked list with suitable diagram. Also state advantage of circular linked list over linear
linked list.
Ans.
Circular Linked List A circular linked list is a variation of linked list in which the last element is linked to the first
element. This forms a circular loop.

A circular linked list can be either singly linked or doubly linked. for singly linked list, next pointer of last item
points to the first item In doubly linked list, prev pointer of first item points to last item as well. We declare
the structure for the circular linked list in the same way as follows:
Struct node
{
int data;
Struct node *next;
};
Typedef struct node *Node;
Node *start = null;
Node *last = null;
For example:

Advantages of Circular Linked Lists:


1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to stop
when the first visited node is visited again.
2) Useful for implementation of queue. Unlike this implementation, we don’t need to maintain two pointers for front
and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can always be
obtained as next of last.
3) Circular lists are useful in applications to repeatedly go around the list. For example, when multiple applications
are running on a PC, it is common for the operating system to put the running applications on a list and then to cycle
through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to
another application. It is convenient for the operating system to use a circular list so that when it reaches the end of
the list it can cycle around to the front of the list.
4) Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.

9. Write a program to traverse a linked list.


Ans.
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
};

struct Node* createNode(int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}

// Function to traverse and print the singly linked list


void traverseList(struct Node* start) {

while (start != NULL) {

printf("%d ", start->data);

start = start->next;
}
printf("\n");
}

int main() {

struct Node* start = createNode(10);


start->next = createNode(20);
start->next->next = createNode(30);
start->next->next->next = createNode(40);
traverseList(head);

return 0;
}

10. Write C program for performing following operations on array: insertion, display.
Ans.

#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
for (i = 0; i < 10; i++)
arr[i] = i + 1;

for (i = 0; i < n; i++)


printf("%d ", arr[i]);
printf("\n");

x = 50;

pos = 5;

n++;
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
// shift elements forward
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];

// insert x at pos
arr[pos - 1] = x;

for (i = 0; i < n; i++)


printf("%d ", arr[i]);
printf("\n");

return 0;
}

11. Differentiate between binary search and sequential search.


Ans.

12. Evaluate the following prefix expression: - * + 4 3 2 5 and 5, 6, 2, +, *, 12, 4, /, - show diagrammatically
Ans.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Result of above prefix expression evaluation – 37

13. Sort the given number in ascending order using Radix sort: 348, 14, 641, 3851, 74.
Ans.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
14. Write an algorithm to delete and insert a node from the beginning and end of a circular linked list.
Ans.

Delete at Beginning (Circular Queue):

Step 1: IF START = NULL


Write UNDERFLOW
Go to Step 8
[END OF IF]

Step 2: SET PTR = START


Step 3: Repeat Step 4 while PTR → NEXT != START
Step 4: SET PTR = PTR → next
[END OF LOOP]

Step 5: SET PTR → NEXT = START → NEXT


Step 6: FREE START
Step 7: SET START = PTR → NEXT
Step 8: EXIT

Delete at End (Circular Queue):

Step 1: IF START = NULL


Write UNDERFLOW
Go to Step 8
[END OF IF]

Step 2: SET PTR = START


Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]

Step 6: SET PREPTR -> NEXT = START


Step 7: FREE PTR
Step 8: EXIT

Insert at Beginning (Circular Queue):

Step 1 - Create a newNode with given value.


Step 2 - Check whether list is Empty (START == NULL)
Step 3 - If it is Empty then, set START = newNode and newNode→next = START .
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'START'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp → next ==
START').
Step 6 - Set 'newNode → next =START', 'START = newNode' and 'temp → next = START'.

Inserting At End of the list

Step 1 - Create a newNode with given value.


Step 2 - Check whether list is Empty (START == NULL).
Step 3 - If it is Empty then, set START = newNode and newNode → next = START.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with START.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next
== START).
Step 6 - Set temp → next = newNode and newNode → next = START.

15. Draw an expression tree for the following expression: (a-2b+5e) 2 * (4d=6e) 5
Ans.

16. Write an algorithm to insert an element at the beginning and end of linked list
Ans.
Inserting a Node at the Beginning of a Linked List

Step 1: IF AVAIL = NULL

Write OVERFLOW

Go to Step 7

[END OF IF]

Step 2: SET NEW_NODE = AVAIL

Step 3: SET NEW_NODE->DATA = VAL

Step 4: SET NEW_NODE-> NEXT = START

Step 5: SET START = NEW_NODE

Step 6: EXIT

Inserting a Node at the End of a Linked List

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Step 1: IF AVAIL = NULL

Write OVERFLOW

Go to Step 7

[END OF IF]

Step 2: SET NEW_NODE = AVAIL

Step 3: SET NEW_NODE->DATA = VAL

SET NEW_NODE->NEXT = NULL

Step 4: PTR=START

Step 5: Repeat Step 6 while PTR->NEXT != NULL

Step 6: SET PTR = PTR->NEXT

[END OF LOOP]

Step 5: SET PTR->NEXT = NEW_NODE

Step 6: EXIT

17. Write an algorithm for performing push and pop operations on stack.
Ans.
Push algorithm:
Step 1: IF TOP = MAX-1
PRINT OVERFLOW
Goto step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END

Pop algorithm:
Step 1: IF TOP = NULL
PRINT UNDERFLOW
Go to step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END

18. Write an algorithm to search a particular node in the given linked list.
Ans.
Step 1: SET PTR = HEAD
Step 2: Set I = 0
STEP 3: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF

STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL


Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
STEP 5: if ptr → data = item
write i+1
End of IF

STEP 6: I = I + 1
STEP 7: PTR = PTR → NEXT
[END OF LOOP]

STEP 8: EXIT

19. Create a singly linked list using data fields 90, 25, 46, 39, 56. Search a node 40 from the SLL and show
the procedure step-by-step with the help of a diagram from start to end
Ans.
To Search a data field in singly linked list, need to start searching the data field from first node of singly
linked list. ORIGINAL LIST:

SEARCHING A NODE STEP 1:


Compare 40 with 90
40!=90

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
20. Write an algorithm to count the number of nodes in a singly linked list
Ans.
0. Set Count =0
1. Initialize a node pointer, ptr = start.
2. Do following while ptr is not NULL
a. ptr = ptr -> next
b. Increment count by 1.
3. Return count.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
21. Traverse a tree in inorder preorder and post order
Ans.

Inorder Traversal: Q,E,F,R,D,H,B,A,I,J,K,C,L,P


Preorder Traversal: A,B,D,E,Q,F,R,H,C,I,J,K,L,P
Postorder Traversal: Q,R,F,E,H,D,B,K,J,I,P,L,C,A

22. Compare Linked List and Array (any 4 points)


Ans.

Criteria Use Arrays When... Use Linked Lists When...

Memory Allocation Memory needs to be allocated in a Memory can be allocated dynamically


contiguous block. and non-contiguously.

Access Speed Fast access to elements by index is Access speed is not the primary
required (O(1) access). concern, and traversal is acceptable.

Size of Data Structure The size of the data structure is fixed or The size of the data structure is
known in advance. dynamic or frequently changing.

Insertion/Deletion Insertions and deletions are infrequent Frequent insertions and deletions are
and mostly at the end. needed, especially at the beginning or
middle.

Memory Efficiency Memory efficiency is important, and Slightly higher memory usage is
overhead from pointers should be acceptable due to the overhead of
minimized. pointers.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Complexity of Simplicity and ease of implementation Flexibility and dynamic memory
Implementation are priorities. management are priorities.

23. Explain circular Queue with it advantage and need


Ans.
A Circular Queue is a linear data structure that follows the FIFO (First In, First Out) principle but differs
from a regular queue in its way of handling the overflow condition. In a regular queue, when the queue is
full, no more elements can be added. However, in a circular queue, the last position of the queue is
connected back to the first position, forming a circle. This allows the queue to efficiently use space by
reusing the vacant positions created when elements are dequeued.

Advantages of Circular Queue:


Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
1. Efficient Space Utilization: Unlike a regular queue, a circular queue allows the reuse of space that
becomes available once elements are dequeued, leading to better space utilization.
2. No Wasted Space: It ensures that there is no unused space, even if there are empty spots in the
queue.
Need of Circular Queue:
1. Fixed-Size Buffer: It is especially useful in scenarios like CPU scheduling or memory management
where a fixed-size queue is required, and elements are processed in a continuous loop.
2. Queue Overflow Prevention: It prevents overflow from happening prematurely as long as there is
available space in the queue.

24. Explain Priority queue with example


Ans.
A priority queue is an abstract data type that behaves similarly to the normal queue except that each
element has some priority, i.e., the element with the highest priority would come first in a priority
queue. The priority of the elements in a priority queue will determine the order in which elements are
removed from the priority queue.

Operations of a Priority Queue:


1) Insertion in a Priority Queue
When a new element is inserted in a priority queue, it moves to the empty slot from top to bottom and
left to right. However, if the element is not in the correct place then it will be compared with the parent
node. If the element is not in the correct order, the elements are swapped. The swapping process
continues until all the elements are placed in the correct position.
2) Deletion in a Priority Queue
As you know that in a max heap, the maximum element is the root node. And it will remove the
element which has maximum priority first. Thus, you remove the root node from the queue. This
removal creates an empty slot, which will be further filled with new insertion. Then, it compares the
newly inserted element with all the elements inside the queue to maintain the heap invariant.

There are two types of priority queue:

o Ascending order priority queue: In ascending order priority queue, a lower priority number is given
as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in an
ascending order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest priority

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
in a priority queue.

o Descending order priority queue: In descending order priority queue, a higher priority number is
given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in
descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as the highest priority
in a priority queue.

25. Explain Recursion with Factorial Program


Ans.
Recursion is a programming technique where a function calls itself to solve a problem by breaking it down
into smaller sub-problems. Each recursive function has:
1. Base Case: A condition to stop the recursion.
2. Recursive Case: The part where the function calls itself with a smaller or simpler version of the
problem.
Factorial Program Using Recursion:
The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It
is defined as:
• n! = n * (n-1) * (n-2) * ... * 1
• Base case: 0! = 1
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Recursive Formula:
• For n > 0, n! = n * (n-1)!
• Base case: 0! = 1
C Program to Compute Factorial Using Recursion:

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Explanation:
• Base Case: When n == 0, the function returns 1 (since 0! = 1).
• Recursive Case: For n > 0, the function calls itself with n-1 and multiplies it by n.
• Example: For factorial(5), the function will compute 5 * 4 * 3 * 2 * 1.

26. Define and Explain term

1. Leaf Node: A leaf node is a node in a tree that has no children.


2. Root Node: The root node is the topmost node in a tree, from which all other nodes originate.
3. Path: A path is a sequence of nodes connected by edges in a tree or graph.
4. Ancestor: An ancestor of a node is any node on the path from the root to that node.
5. Descendants: Descendants are all the nodes that can be reached by traversing down from a given
node.
6. Level of Node: The level of a node is the number of edges from the root to that node.
7. Application of Queue: A queue is used in scenarios like task scheduling, BFS in graphs, and
request handling.
8. Algorithm: An algorithm is a set of step-by-step instructions designed to solve a problem or perform
a task.
9. Queue Operation: Queue operations include enqueue (insertion), dequeue (removal), and peek
(viewing front element).
10. Stack Operation: Stack operations include push (insertion), pop (removal), and peek (viewing top
element).
11. Linked List Operation: Linked list operations include insertion, deletion, traversal, and search of
nodes.
12. Array Operation: Array operations include insertion, deletion, traversal, and search of elements.
13. Data Structure Operation: Data structure operations involve manipulating the elements of
structures like arrays, stacks, queues, etc.
14. Overflow of Stack: Stack overflow occurs when an attempt is made to push an element onto a full
stack.
15. Underflow of Queue: Queue underflow occurs when an attempt is made to dequeue from an empty
queue.
16. List any 4 applications of queue.
Ans: • In computer system to maintain waiting list for single shared resources such as printer, disk, etc.
• It is used as buffers on MP3 players, iPod playlist, etc.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
• Used for CPU scheduling in multiprogramming and time sharing systems.
• In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a
service representative is free.
• Handling of interrupts in real-time systems.
17. Explain Algorithm & its Characteristics of an Algorithm
An algorithm is a methodical process used to solve a task or solve a problem. Several
important factors impact an algorithm's effectiveness:

1. Finiteness

An algorithm must always have a finite number of steps before it ends. When the operation is
finished, it must have a defined endpoint or output and not enter an endless loop.

2. Definiteness

An algorithm needs to have exact definitions for each step. Clear and straightforward directions
ensure that every step is understood and can be taken easily.

3. Input

An algorithm requires one or more inputs. The values that are first supplied to the algorithm
before its processing are known as inputs. These inputs come from a predetermined range of
acceptable values.

4. Output

One or more outputs must be produced by an algorithm. The output is the outcome of the
algorithm after every step has been completed. The relationship between the input and the
result should be clear.

5. Effectiveness

An algorithm's stages must be sufficiently straightforward to be carried out in a finite time


utilizing fundamental operations. With the resources at hand, every operation in the algorithm
should be doable and practicable.

18. Explain Underflow & Overflow for Stack


The underflow condition checks if there exists any item before popping from the stack. An empty
one cannot be popped further.

if (top == -1) {
// underflow condition
}
The overflow condition checks if the stack is full (or more memory is available) before pushing any
element. This prevents any error if more space cannot be allocated for the next item.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
if (top == sizeOfStack) {
// overflow condition
}

19. Write any four operations that can be performed on data structure.
Ans Data structure operations (Non-Primitive)
1. Inserting: Adding a new data in the data structure is referred as insertion.
2. Deleting: Removing a data from the data structure is referred as deletion.
3. Sorting: Arranging the data in some logical order (ascending or descending, numerically or alphabetically).
4. Searching: Finding the location of data within the data structure which satisfy the searching condition.
5. Traversing: Accessing each data exactly once in the data structure so that each data item is traversed or
visited.
20. Describe the concept of linked list with the terminologies: node, next Pointer, null pointer and empty list.

Node: Each data element in a linked list is represented as a node. Node contains two parts one is info (data)
and other is next pointer (address). Info part stores data and next pointer stores address of next node.

Next pointer: It is a pointer that holds address of next node in the list i.e. next pointer points to next node in
the list

Null pointer: It is a pointer that does not hold any memory address i.e. it is pointing to nothing. It is used to
specify end of the list. The last element of list contains NULL pointer to specify end of list.
Empty list: Each linked list has a header node. When header node contains NULL value, then that list is said
to be empty list.

21. Give classification of data structure.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Explain Time & Space Complexity of Algorithm
The valid algorithm takes a finite amount of time for execution. The time required by the algorithm to solve
given problem is called time complexity of the algorithm. Time complexity is very useful measure in
algorithm analysis.
It is the time needed for the completion of an algorithm. To estimate the time complexity, we need to
consider the cost of each fundamental instruction and the number of times the instruction is executed.
Example 1: Addition of two scalar variables.
Algorithm ADD SCALAR(A, B)
//Description: Perform arithmetic addition of two numbers
//Input: Two scalar variables A and B
//Output: variable C, which holds the addition of A and B
C <- A + B
return C
The addition of two scalar numbers requires one addition operation. the time complexity of this algorithm is
constant, so T(n) = O(1) .

Space Complexity:
Problem-solving using computer requires memory to hold temporary data or final result while the program
is in execution. The amount of memory required by the algorithm to solve given problem is called space
complexity of the algorithm.
The space complexity of an algorithm quantifies the amount of space taken by an algorithm to run as a
function of the length of the input. Consider an example: Suppose a problem to find the frequency of array
elements.
It is the amount of memory needed for the completion of an algorithm.
To estimate the memory requirement we need to focus on two parts:
(1) A fixed part: It is independent of the input size. It includes memory for instructions (code), constants,
variables, etc.
(2) A variable part: It is dependent on the input size. It includes memory for recursion stack, referenced
variables, etc.

Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy