Ques8: Write C-functions for Isempty () and Stacktop () operations. Ans: Isempty () Stacktop () Ques9: Write C program to implement stack using linked list. Perform, PUSH, POP and Display operations. Ans:
Ques10: Assume that S1 and S2 are two stacks. Combine these two stacks by placing all elements of the second stack on top of the first stack (maintain the same order as it was). Ans: To combine two stacks (S1 and S2) by placing all elements of the second stack on top of the first stack while maintaining the same order, The following steps can be followed: - • Empty S1 into a temporary stack (TempStack). • Combine S2 and TempStack into S1.
Here’s a simple algorithm for it CombineStacks(S1, S2): // Step 1: Empty S1 into a temporary stack while S1 is not empty: TempItem = pop(S1) push(TempStack, TempItem) // Step 2: Combine S2 and TempStack into S1 while TempStack is not empty: TempItem = pop(TempStack) push(S1, TempItem) while S2 is not empty: Item = pop(S2) push(S1, Item) • We use a temporary stack (TempStack) to temporarily hold the elements of S1. • We empty S1 into TempStack by popping elements from S1 and pushing them onto TempStack. • Then, we combine S2 and TempStack into S1 by popping elements from both stacks and pushing them onto S1. • After executing this algorithm, S1 will contain all the elements from both S1 and S2, with the order maintained.
Ques11: Discuss the applications of stacks. Ans: There are several applications of stack like: - • Recursion • Reversal of a string • Checking the parenthesis matching • Postfix expression evaluation • Infinix to Postfix conversion • Infinix to prefix conversion Ques12: Write the prefix and postfix form of the following Infinix expressions i. ((A+B)*C-(D-E))$(F+G) ii. (A+B)*(C+D-E)*F iii. (A+B)*(C+D)$(A+B) iv. A+B*C-D/E*H v. (A+B^C^D)*(E+F/D) Ans: ((A+B)*C-(D-E))$(F+G) Prefix: $ - * + A B C - D E + F G Postfix: A B + C * D E - - F G + $ (A+B)*(C+D-E)*F Prefix: * + A B * + C - D E F Postfix: A B + C D E - + * F * (A+B)*(C+D)$(A+B) Prefix: $ * + A B + C D + A B Postfix: A B + C D + * A B + $
A+B*C-D/E*H Prefix: - + A * B C / D * E H Postfix: A B C * + D E / H * - (A+B^C^D)*(E+F/D) Prefix: * + A ^ B ^ C D + E / F D Postfix: A B C ^ D ^ + E F D / + * Ques13: Write a C-program to evaluate postfix expression Ans:
Ques14: Write a C-program to convert given Infinix expression into postfix expression Ans: 1. 2. 3.
Ques15: Show the stack contents after each operation for the following sequence of PUSH and POP operations, assuming that MAXSTK = 5; a. PUSH(10) b. PUSH(20) c. POP() d. PUSH(30) e. PUSH(40) f. POP() g. POP() h. PUSH(50) i. POP() j. PUSH(60) Ans: Initial Stack: (Empty) a. PUSH(10): Stack Contents: [10] b. PUSH(20): Stack Contents: [10, 20] c. POP(): Stack Contents: [10] d. PUSH(30): Stack Contents: [10, 30] e. PUSH(40): Stack Contents: [10, 30, 40] f. POP(): Stack Contents: [10, 30] g. POP(): Stack Contents: [10] h. PUSH(50): Stack Contents: [10, 50] i. POP(): Stack Contents: [10]
j. PUSH(60): Stack Contents: [10, 60] Ques16: What is Recursion? Ans: Recursion is a process of calling the function repeatedly in terms of itself until a specified condition is reached so that it reduces the complexity of the program. In Recursion, stack is used to store the current value and return address of the function call. At each recursive procedure call, the stack is pushed to save necessary values; upon exit from that level the stack is “popped” to restore the saved values of the preceding level. Ques17: Write a recursive program for Tower of Hanoi Problem. Ans:
Ques18: Write a recursive program to find GCD of three numbers. Ans: Ques19: Write a program to find the factorial of a number using recursion. Ans:
Ques20: Write a program to generate Fibonacci series using recursion. Ans: Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy
Unit 2 Chapter -7 Queues
Unit 2 Chapter -7 Queues Question & Answer: - Ques1: What is a queue? How queues are represented in memory? Ans: - Queue is defined as an ordered collection of items from which, the item from which, the item may be deleted at one end called FRONT end and into which the items may be inserted at the other end called REAR end. Ques2: What are the different types of queues? Ans: - Ques3: What are the operations performed on queues? And: -
Ques4: Explain linear/ordinary queue and write a C-program to implement the same. Ans: Explanation A linear queue is a data structure that follows the FIFO (first in, first out) principle. This means that the element that is added to the queue first is also the element that is removed first. Queues are often used to buffer data or to implement tasks that need to be completed in a specific order. Program to Implement Linear/Ordinary Queue #include <stdio.h> #include <stdlib.h> typedef struct { int data; } element; typedef struct { element *items; int front; int rear; int size; } queue; queue *createQueue(int size) { queue *q = (queue *)malloc(sizeof(queue)); q->items = (element *)malloc(size * sizeof(element)); q->front = -1; q->rear = -1; q->size = size;
return q; } void enqueue(queue *q, element item) { if (q->rear == q->size - 1) { printf("Queue is full\n"); return; } q->rear++; q->items[q->rear] = item; } element dequeue(queue *q) { if (q->front == q->rear) { printf("Queue is empty\n"); return; } q->front++; return q->items[q->front]; } int main() { queue *q = createQueue(5); enqueue(q, (element){1}); enqueue(q, (element){2}); enqueue(q, (element){3}); enqueue(q, (element){4});
enqueue(q, (element){5}); printf("The elements of the queue are:\n"); while (q->front != q->rear) { element item = dequeue(q); printf("%d ", item.data); } printf("\n"); return 0; } Ques5: Distinguish between linear queue and circular queues Ans:
Ques6: Write a C function to insert an element in linear queue. Ans: Ques7: Write an algorithm for queue insert and delete. Ans:
Ques8: Explain Queue as ADT. Ans: Ques9: What is underflow and overflow in queue? Ans: Underflow & Overflow: We know that the deletion or removal of item from the queue requires at least one item in the 'front.i.e., deletion is possible only if the queue is non- empty. It does not make sense to delete the item which is not present. So, the illegal attempt made to remove the element from an empty queue is called 'Underflow'. We know that if an array is used for a queue, then there is a limit for the insert operations to be performed. The possibility of a condition called 'Overflow' arises when the queue size reaches the maximum array size. In the above example when R=4, it has reached the maximum array size. Hence the insertions can not take place and there by Overflow occurs. Ques10: Write a C program to perform the following operation on linear queue. 1. Insert 2. Delete 3. Display Ans: 1. Insert
2. Delete 3. Display Ques11: Write a C program to perform the following operation on linear queue. 1. Insert 2. Delete 3. Display
Ans: 1. Insert 2. Delete
3. Display
Ques12: Write a short note on priority queues. Ans: A priority queue is a queue such that each element of the queue has been assigned with a priority & such that the order in which elements are processed (deletion and insertion) comes from the following rules: 1. An element of higher priority is processed before any element of lower priority. 2. if 2 elements have same priority, then the element which come first will be processed first. Ques13: What are the different types of priority queue? Ans: Ascending Priority Queue: It is a collection of elements into which items can be inserted arbitrarily, but deletion is possible only for the smallest element, it means that elements can be inserted in any order, but only the smallest element can be removed first from the queue. Descending Priority Queue: It is a collection of elements into which items can be inserted arbitrarily, but deletion is possible only for the largest elements can be inserted in any order, but only the largest element can be removed first from the queue.
Ques14: What are the different types of representing priority queues in memory? Ans: There are various ways of implementing a priority queue. They are: 1. Circular Queue. 2. Array. 3. Trees. 4. Heap. 5. Multi- Queue. Ques15: Write a C-program to implement ascending priority queue using arrays. Ans:
Ques16: How queues are represented using Linked List? Explain with an example. Ans: Each node of a linked queue consists of two fields: data and next(storing of next node). The data field of each node contains the assigned value, and the next points to the node containing the next item in the queue. A linked queue consists of two pointers, i.e., the front pointer and the rear pointer.
Ques17: Write a C-program to perform Insert and Delete Operations on Queue using Linked List. Ans: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *front = NULL; struct node *rear = NULL; void enqueue(int data) { struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; if (front == NULL) { front = new_node; rear = new_node; } else { rear->next = new_node; rear = new_node; } }
void dequeue() { if (front == NULL) { printf("Queue is empty\n"); return; } struct node *temp = front; front = front->next; if (front == NULL) { rear = NULL; } free(temp); } void printQueue() { struct node *temp = front; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { enqueue(1);
enqueue(2); enqueue(3); printQueue(); dequeue(); printQueue(); return 0; } Ques18: Write a C-program to implement priority queue by using 3 priority levels and 3 queues sequentially. Ans: #include <stdio.h> #include <stdlib.h> struct Node { int data; int priority; struct Node *next; }; struct Queue { struct Node *front; struct Node *rear; }; struct Queue queues[3]; void initQueue(struct Queue *queue) {
queue->front = queue->rear = NULL; } void enqueue(struct Queue *queue, int data, int priority) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = data; newNode->priority = priority; newNode->next = NULL; if (queue->rear == NULL) { queue->front = queue->rear = newNode; } else { queue->rear->next = newNode; queue->rear = newNode; } } int dequeue(struct Queue *queue) { if (queue->front == NULL) { return -1; } int data = queue->front->data; struct Node *temp = queue->front; queue->front = queue->front->next; if (queue->front == NULL) { queue->rear = NULL; }
free(temp); return data; } int main() { // Initialize the queues for (int i = 0; i < 3; i++) { initQueue(&queues[i]); } // Enqueue some data enqueue(&queues[0], 10, 1); enqueue(&queues[1], 20, 2); enqueue(&queues[2], 30, 3); // Dequeue the data in priority order while (1) { int data = dequeue(&queues[0]); if (data == -1) { break; } printf("%d\n", data); } return 0; }
Ques19: What is double ended queue or deque? What are the operations that can be performed on double ended queue? Ans: Deque (pronounced as “Deck”), in deque, both insertion and deletion can be made at either end but not in the middle. The term deque is a contraction of the name Double- ended Queue. Deque is maintained by a circular array; DEQUE with pointers FRONT and REAR, which points to the two ends of the deque. Operations performed on double- ended queue: 1. Insertion at Front. 2. Insertion at Rear. 3. Deletion at Front. 4. Deletion at Rear.
Ques20: Write a C- program to implement deque both input- restricted and output- restricted queue. Ans:
Ques21: Explain how elements can be arranged in ascending order in priority queue while inserting? After arranging them in ascending order, how insert and delete operations are performed? Ans: In a priority queue, elements are arranged based on their priorities. one way is that we can first sort the inserted elements in ascending order so that the FRONT element of the queue will be the smallest element. let’s consider a linear array of size = 5, 0 1 2 3 4 20 30 10 5 40 FRONT REAR Fig. a. Unsorted array 0 1 2 3 4 5 10 20 30 40 FRONT REAR Fig. b. Sorted array Now, we can see in Fig. b. that after sorting, ‘5’ element has come to FRONT from 3rd index position. Now, we can directly delete the element from the queue. This will save a lot time because there is no need to search the entire array elements. So, when we delete the front element of the queue, the highest priority element (smallest one) is deleted.
Ques22: If priority queue is not sorted, then how insert and delete operations are performed on priority queue. Ans: Starting from the FRONT, traversing the array for an element of the highest priority (the smallest element) and then deleting this element from the queue. If this is not the front most element, then shift all its trailing elements to left side, after the deleted element, one stroke each to fill up the vacant position. let’s consider a linear array of size = 5, 0 1 2 3 4 20 30 10 5 40 FRONT REAR Starting from ‘20’ all the remaining elements are said to be compared and the smallest item is to be found out. The smallest element is ‘5’. So, ‘5’ is to be deleted at 3rd position of the array and now 4th positions are to be shifted one stroke to the left side. 0 1 2 3 4 20 30 10 40 FRONT REAR Ques23: Explain the applications of queues. Ans: Applications of Queues: 1. Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU. 2. Queues are used in playlists for jukebox to add songs to the end, play from the front of the list. 3. Queues are used as buffers in most of the applications like MP3 media player, CD player, etc. 4. Used in call center systems for handling incoming calls in sequence. 5. To process orders in E-commerce in sequence.
Ques24: Write a C- program to perform Insert and Delete Operations in Circular Queue using Linked List. Ans:
Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy
Unit- 3 Chapter- 8 Binary Trees
Unit- 3 Chapter- 8 Binary Trees Question and Answers: - Ques1: Define a tree? Ans: Tree is a non- linear data structure, used to represent a relationship between elements such as records, nodes and table of contents. Ques2: Define a binary tree? Distinguish between tree and binary tree. Ans: A binary tree is a hierarchical data structure in which each node has at most two children, referred to as left child and the right child. The arrangement of nodes in a binary tree forms a tree- like structure where each node can have, at most, two subtrees. Distinguish between tree and binary tree: Ques3: Define the following: Ans: 1. Degree The maximum number of children that is possible for a node is known as degree. [or] The number of sub- trees of a node is called degree. Indegree: The number of edges entering into the node is known as indegree. outdegree: The number of edges coming out from the node is known as outdegree. 2. Leaf A node with no successors [nodes after it] is called a leaf node.
in simple words, the node which is at the end and having no children or a node whose degree is zero is called a leaf node. It is also known as terminal node or external node. 3. Level Level is the rank of hierarchy and root node is termed as in level 0. Similarly, each node in a tree has a level. If a node is at level p, then their children are at level [p+1] and parent is at level [p-1]. 4. Depth or height The depth or height of the tree is the maximum number of nodes that is possible in a path starting from root node to a leaf node. So, the maximum level of any leaf in the tree can be considered as the height. 5. Siblings the nodes which have the same parent are said to be siblings [or] children of the same parent are said to be siblings or brothers. 6. Forest A forest is a collection of disjoint binary trees. Each binary trees within the forest consists of nodes connected by edges, where each node has at most two children. The term ‘disjoint’ implies that there are no common nodes among the different trees in the forest. 7. Ancestor An ancestor of a node is any node that lies on the path from the root to that node. Specifically, it includes the node itself and all the nodes encountered while moving upward from the node toward the root of the tree. 8. Descendant A descendant of a node is any node that lies on the path from that node to any of its leaves. More precisely, it includes the node itself and all the nodes encountered while moving downward from that node toward the leaves of the tree. Each node in binary tree has zero, one or two descendants, depending on the structure of the tree.
Ques4: Explain the properties of Binary tree with suitable diagrams. Ans:
Ques5: Write a C- program to create a binary tree using sequential representation. Ans:
Ques6: Explain linked list representation of binary tree. Ans: Linked list representation of binary trees of each node is divided into three parts: 1. Info field. 2. Left link. 3. Right link. The info field is use to store the information of the node [element]. The left link is a link field which contains the address of its left node and the right link is a link field which contains the address of its right node. Representation: Ques7: Explain the different methods of representation of binary trees in C. Ans: a) Contiguous or Sequential representation 1. Elements are stored in a linear, continuous block of memory. 2. For a binary tree, you must use an array to represent the tree in a contiguous manner. Such that, each node is assigned an index, and the relationship between parent and child nodes can be determined by simple arithmetic calculation 3. This method is efficient in terms of memory usage, but it may require more space than actually needed if the tree is not fully populated.
b) Linked list representation Linked list representation of binary trees of each node is divided into three parts: 1. Info field. 2. Left link. 3. Right link. The info field is use to store the information of the node [element]. The left link is a link field which contains the address of its left node and the right link is a link field which contains the address of its right node.
Ques8: What is Binary Search Tree? Ans: A Binary Search Tree [BST] is a binary tree where each node has at most two children: a left child with values less than the node and a right child with the values greater than the node. No duplicate values are allowed. This structure allows for efficient search, insertion, and deletion operations with an average time complexity of O(log n), where ‘n’ is the number of nodes. Maintaining balance in the tree is crucial for optimal performance. A Binary Search Tree is also known as Ordered Binary Tree. Ques9: Write an algorithm for constructing a binary search tree. Ans: If node == NULL return createNode(data) if (data < node->data) node->left = insert(node->left, data); else if (data > node->data) node->right = insert(node->right, data); return node; Ques10: What is Tree sort? Write a program to sort elements in BST. Ans: Tree sort is a sorting algorithm that organizes elements into a binary search tree and then retrieves them in sorted order through an in- order traversal of the of the tree C-program: P.T.O
Ques11: Write a Program to create and search an element in BST. Ans:
Ques12: Write a program to construct BST and perform preorder, inorder, and postorder traversals. Ans: