❤️ DS ASSIGNMENT 4 💙
// Q1: Polynomial representation using structure
#include <stdio.h>
struct Term {
int coeff;
int exp;
};
int main() {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
struct Term poly[n];
printf("Enter coefficient and exponent for each term:\n");
for (int i = 0; i < n; i++) {
printf("Term %d: ", i + 1);
scanf("%d %d", &poly[i].coeff, &poly[i].exp);
printf("Polynomial: ");
for (int i = 0; i < n; i++) {
printf("%dx^%d", poly[i].coeff, poly[i].exp);
if (i < n - 1) printf(" + ");
return 0;
Q2. What is stack? Describe its applications
Stack is a linear data structure that follows LIFO (Last In First Out).
Operations:
Push() → Insert element
Pop() → Remove element
Peek() → Get top element
Applications
1. Expression evaluation (infix → postfix).
2. Function calls (recursion).
3. Undo/Redo in editors.
4. Browser back/forward history.
5. Parentheses matching.
Q3. What is queue? Explain its applications
Queue is a linear data structure that follows FIFO (First In First Out).
Operations:
Enqueue() → Insert at rear
Dequeue() → Delete from front
Applications
1. Printer task scheduling.
2. OS process scheduling.
3. Call center waiting lines.
4. Data buffering in networks.
// Q4: Queue implementation using array
#include <stdio.h>
#define SIZE 50
Int queue[SIZE], front = -1, rear = -1;
Void enqueue(int val) {
If (rear == SIZE – 1) {
Printf(“Queue Overflow\n”);
Return;
}
If (front == -1) front = 0;
Queue[++rear] = val;
Void dequeue() {
If (front == -1 || front > rear) {
Printf(“Queue Underflow\n”);
Return;
Printf(“Deleted: %d\n”, queue[front++]);
Void display() {
If (front == -1 || front > rear) {
Printf(“Queue is empty\n”);
Return;
Printf(“Queue: “);
For (int I = front; I <= rear; i++)
Printf(“%d “, queue[i]);
Printf(“\n”);
Int main() {
Int n, val, choice;
Printf(“Enter number of elements: “);
Scanf(“%d”, &n);
Printf(“Enter %d values:\n”, n);
For (int I = 0; I < n; i++) {
Scanf(“%d”, &val);
Enqueue(val);
Display();
Dequeue();
Display();
Return 0;
---------------------------------------------------------------------------------------------------------
// Q5: Stack using linked list
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node *next;
};
Struct Node *top = NULL;
Void push(int val) {
Struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = top;
top = newNode;
Void pop() {
If (top == NULL) {
Printf(“Stack Underflow\n”);
Return;
}
Printf(“Popped: %d\n”, top->data);
Top = top->next;
Void display() {
Struct Node *temp = top;
If (temp == NULL) {
Printf(“Stack is empty\n”);
Return;
Printf(“Stack: “);
While (temp != NULL) {
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
Push(10);
Push(20);
Push(30);
Display();
Pop();
Display();
Return 0;
Q6. Write the infix, prefix, and postfix forms of a given expression.
Examples
For expression: (A + B) * (C – D)
Infix: (A + B) * (C – D)
Prefix: * + A B – C D
Postfix: A B + C D - *
Q8. Define queue and explain its types
Queue: Linear data structure using FIFO.
Types of Queue
1. Simple Queue → Normal FIFO operations.
2. Circular Queue → Last position connected back to first.
3. Double-Ended Queue (Deque) → Insertion/deletion from both ends.
4. Priority Queue → Elements served based on priority.
// Q7. Convert given infix expression to its postfix form
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
Char stk[MAX];
Int top = -1;
Int isEmpty() {
Return top == -1;
Int isFull() {
Return top == MAX – 1;
Char peek() {
Return stk[top];
Char pop() {
If (isEmpty())
Return -1;
Return stk[top--];
Void push(char oper) {
If (isFull())
Printf(“Stack Overflow\n”);
Else
Stk[++top] = oper;
Int checkIfOperand(char ch) {
Return (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’) || (ch >= ‘0’
&& ch <= ‘9’);
Int precedence(char ch) {
Switch (ch) {
Case ‘+’:
Case ‘-‘: return 1;
Case ‘*’:
Case ‘/’: return 2;
Case ‘^’: return 3;
Return -1;
Void convertInfixToPostfix(char* expression) {
Int I, j = 0;
Char postfix[MAX];
For (I = 0; expression[i]; ++i) {
If (checkIfOperand(expression[i]))
Postfix[j++] = expression[i];
Else if (expression[i] == ‘(‘)
Push(expression[i]);
Else if (expression[i] == ‘)’) {
While (!isEmpty() && peek() != ‘(‘)
Postfix[j++] = pop();
If (!isEmpty() && peek() == ‘(‘)
Pop();
Else {
While (!isEmpty() && precedence(expression[i]) <=
precedence(peek()))
Postfix[j++] = pop();
Push(expression[i]);
While (!isEmpty())
Postfix[j++] = pop();
Postfix[j] = ‘\0’; // Null terminate
Printf(“Postfix Expression: %s\n”, postfix);
Int main() {
Char expression[MAX];
Printf(“Enter Infix Expression: “);
Scanf(“%s”, expression);
convertInfixToPostfix(expression);
return 0;
}