FDS 2023 Codes
FDS 2023 Codes
Write C++ pseudo code to implement linked queue. Write a function to insert a node in linked list.
Step 1: Create a new node with data 'x' struct QueueNode { class Node:
Step 2: Set a pointer 'current' to head of the linked list int data; def _init_(self, data=None, next=None):
Step 3: Traverse the list until the current node's position equals 'P' QueueNode *next; self.data = data
Step 4: Set the new node's next pointer to current node's next }; self.next = next
Step 5: Set the current node's next pointer to the new node class Queue { def insert_node(head, data):
Step 6: Return the updated linked list. private: new_node = Node(data)
QueueNode *front, *rear; if not head:
Write a pseudo code for basic operation of stack. public: return new_node
Step 1: Create an empty stack Queue() { front = rear = nullptr; } else:
Step 2: Check if stack is empty: void enqueue(int x); new_node.next = head
- If yes, return "Stack is empty" int dequeue(); return new_node
- If no, perform the following operations: bool isEmpty();
- Push: Add an element to the top of the stack }; Write a method to create a doubly link list.
- Pop: Remove an element from the top of the stack void Queue::enqueue(int x) { class Node:
- Peek: Return the element at the top of the stack without removing it QueueNode *temp = new QueueNode(); def _init_(self, data=None, next=None, prev=None):
- Size: Return the number of elements in the stack temp->data = x; self.data = data
Step 3: Repeat Step 2 until stack is empty temp->next = nullptr; self.next = next
if (rear == nullptr) { self.prev = prev
Write a function for addition of two polynomials. front = rear = temp; class DoublyLinkedList:
def add_polynomials(p1, p2): return; def _init_(self):
result = [0] * max(len(p1), len(p2)) } self.head = None
for i in range(len(result)): rear->next = temp; def add_node(self, data):
if i < len(p1): rear = temp; new_node = Node(data)
result[i] += p1[i] } if not self.head:
if i < len(p2): int Queue::dequeue() { self.head = new_node
result[i] += p2[i] if (front == nullptr) { else:
return result cout << "Queue is empty" << endl; new_node.next = self.head
return INT_MIN; self.head.prev = new_node
Write a pseudo code to convert infix to postfix expression. } self.head = new_node
INFIX_TO_POSTFIX(expression) QueueNode *temp = front;
stack = new Stack front = front->next; Write a pseudo code to insert a node in linked list
postfix = "" if (front == nullptr) rear = nullptr; INSERT_NODE(head, data, position)
for each character in expression int data = temp->data; newNode = new Node(data)
if character is a digit or a letter delete temp; if position is 1
postfix = postfix + character return data; newNode.next = head
else if character is an operator } head = newNode
while stack is not empty and stack.top() has higher or equal precedence than character bool Queue::isEmpty() { else
postfix = postfix + stack.pop() return front == nullptr; current = head
stack.push(character) } for i = 1 to position - 2
else if character is a left parenthesis current = current.next
stack.push(character) newNode.next = current.next
else if character is a right parenthesis current.next = newNode
while stack.top() is not a left parenthesis return head
postfix = postfix + stack.pop()
stack.pop() // remove the left parenthesis from the stack
while stack is not empty
postfix = postfix + stack.pop()
return postfix
Write a C++ function to delete a node from doubly link list. Write a c++ pseudo code to implement circular queue using array.
#include <iostream> int queue[SIZE]; //SIZE is the maximum size of the queue
struct Node {
int front = 0, rear = -1, count = 0;
int data;
Node *next; void enqueue(int item)
Node *prev; {
}; if(count == SIZE)
void deleteNode(Node *head, Node *to_delete) { {
if (!head || !to_delete) { cout << "Queue is full. Cannot insert element." << endl;
return;
return;
}
if (head == to_delete) { }
head = head->next; rear = (rear + 1) % SIZE; //Move the rear pointer to the next position
} queue[rear] = item; //Insert the element at the rear
if (to_delete->next) { count++; //Increment the count
to_delete->next->prev = to_delete->prev; }
}
int dequeue()
if (to_delete->prev) {
to_delete->prev->next = to_delete->next; {
} if(count == 0)
delete to_delete; {
} cout << "Queue is empty. Cannot delete element." << endl;
return -1;
Write a function to convert infix to postfix expression.
}
def infix_to_postfix(infix_expression):
precedence = {'(': 0, '+': 1, '-': 1, '*': 2, '/': 2, '^': 3} int item = queue[front]; //Get the element at the front
stack = [] front = (front + 1) % SIZE; //Move the front pointer to the next position
postfix = [] count--; //Decrement the count
for char in infix_expression: return item; //Return the deleted item
if char.isalnum(): }
postfix.append(char)
void display()
elif char == '(':
stack.append(char)
{
elif char == ')': if(count == 0)
while stack and stack[-1] != '(': {
postfix.append(stack.pop()) cout << "Queue is empty. No elements to display." << endl;
stack.pop() return;
else:
}
while stack and precedence[stack[-1]] >= precedence[char]:
postfix.append(stack.pop()) int i = front;
stack.append(char) while(i != rear)
while stack: {
postfix.append(stack.pop()) cout << queue[i] << " ";
return "".join(postfix) i = (i + 1) % SIZE;
}
cout << queue[rear] << endl;
}