DATA STRUCTURES FILE Questions
DATA STRUCTURES FILE Questions
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node {
int data;
struct Node* next;
};
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("Node inserted.\n");
}
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty.\n");
} else {
struct Node* temp = head;
head = head->next;
free(temp);
printf("Node deleted from beginning.\n");
}
}
void display() {
if (head == NULL) {
printf("List is empty.\n");
} else {
void main() {
int choice, value;
clrscr();
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
while (1) {
printf("\n1. Insert at End\n2. Delete from Beginning\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
deleteAtBeginning();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
getch();
}
Output:
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node {
int data;
struct Node* next;
};
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct Node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
printf("Node inserted.\n");
}
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
head = head->next;
last->next = head;
}
free(temp);
printf("Node deleted from beginning.\n");
}
void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
void main() {
int choice, value;
clrscr();
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
while (1) {
printf("\n1. Insert at End\n2. Delete from Beginning\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
deleteAtBeginning();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
getch();
}
Output:
Solution:
#include <stdio.h>
Output:
/*Program 15: WAP to implement Polynomial addition operation using linked list.
Solution:
#include <stdio.h>
#include <stdlib.h>
struct polynode {
float coeff;
int exp;
struct polynode *next;
};
if (*q == NULL) {
*q = temp;
} else {
struct polynode *last = *q;
while (last->next != NULL) {
last = last->next;
}
last->next = temp;
}
}
void poly_add(struct polynode *x, struct polynode *y, struct polynode **s) {
if (*s == NULL) {
*s = temp;
z = *s;
} else {
z->next = temp;
z = z->next;
}
}
while (x != NULL) {
struct polynode *temp = (struct polynode *)malloc(sizeof(struct polynode));
temp->coeff = x->coeff;
temp->exp = x->exp;
temp->next = NULL;
if (*s == NULL) {
*s = temp;
z = *s;
} else {
z->next = temp;
z = z->next;
}
x = x->next;
}
while (y != NULL) {
struct polynode *temp = (struct polynode *)malloc(sizeof(struct polynode));
temp->coeff = y->coeff;
temp->exp = y->exp;
temp->next = NULL;
if (*s == NULL) {
*s = temp;
z = *s;
} else {
z->next = temp;
z = z->next;
}
y = y->next;
}
}
int main() {
struct polynode *first = NULL, *second = NULL, *total = NULL;
return 0;
}
Output:
/*Program 16: Write a C program to create two linked lists from a given list in
following way
INPUT List:- 1 2 3 4 5 6 7 8 9 10
OUTPUT:- First List:- 1 3 5 7 9 Second List:- 2 4 6 8 10
Solution:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
void createLists(struct Node* input, struct Node** list1, struct Node** list2)
{
int index = 1; // Start with index 1 for the first element
while (input != NULL)
{
if (index % 2 != 0)
{
append(list1, input->data);
}
else
{
append(list2, input->data);
}
input = input->next;
index++;
}
}
int main()
{
struct Node* inputList = NULL;
struct Node* list1 = NULL;
struct Node* list2 = NULL;
return 0;
}
Output:
Solution:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top = -1;
int main()
{
int choice;
char ch;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
do
{
printf("\n--- Static Stack using Array ---");
printf("\n1. PUSH ");
printf("\n2. POP ");
printf("\n3. TRAVERSE ");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
printf("\nThe deleted element is %d\n", pop());
break;
case 3:
traverse();
break;
default:
return 0;
}
void push()
{
int item;
if (Top == MAXSIZE - 1)
{
printf("\nThe Stack Is FULL (OVERFLOW)\n");
return;
}
else
{
printf("Enter the element to be inserted: ");
scanf("%d", &item);
Top = Top + 1;
stack[Top] = item;
printf("Element pushed successfully.\n");
}
}
int pop()
{
int item;
if (Top == -1)
{
printf("The Stack is EMPTY (UNDERFLOW)\n");
return -1;
}
else
{
item = stack[Top];
Top = Top - 1;
return item;
}
}
void traverse()
{
int i;
if (Top == -1)
{
Output:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct node
{
int data;
struct node *link;
};
// Function declarations
void push(struct node **, int);
int pop(struct node **);
void display(struct node *);
int main()
{
struct node *top = NULL;
int ch, item;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
do
{
printf("\n--- Dynamic Stack using Linked List ---");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter value to be inserted: ");
scanf("%d", &item);
push(&top, item);
break;
case 2:
item = pop(&top);
if (item != -1)
printf("Item popped: %d\n", item);
break;
case 3:
display(top);
break;
case 4:
printf("Exiting program...\n");
exit(0);
default:
return 0;
}
Output:
/*Program 18: Write a program to convert Infix to equivalent (i) Prefix expression (ii)
Postfix expression
Solution:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 50
struct infix {
char target[MAX];
char stack[MAX];
char *s, *t;
int top;
};
int main() {
struct infix p;
char expr[MAX];
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
initinfix(&p);
setexpr(&p, expr);
convert(&p);
return 0;
}
char opr;
while (*(p->s)) {
if (*(p->s) == ' ' || *(p->s) == '\t') {
p->s++;
continue;
}
if (isdigit(*(p->s)) || isalpha(*(p->s))) {
*(p->t) = *(p->s);
p->t++;
p->s++;
} else if (*(p->s) == ')') {
push(p, *(p->s));
p->s++;
} else if (*(p->s) == '*' || *(p->s) == '+' || *(p->s) == '/' || *(p->s) == '%' || *(p->s) == '-' ||
*(p->s) == '$') {
while (p->top != -1 && priority(p->stack[p->top]) > priority(*(p->s))) {
*(p->t) = pop(p);
p->t++;
}
push(p, *(p->s));
p->s++;
} else if (*(p->s) == '(') {
opr = pop(p);
while (opr != ')') {
*(p->t) = opr;
p->t++;
opr = pop(p);
}
p->s++;
} else {
p->s++;
}
}
else
return 0;
}
Output:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
struct infix {
char target[MAX];
char stack[MAX];
char *s, *t;
int top;
};
int main() {
struct infix p;
char expr[MAX];
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
initinfix(&p);
setexpr(&p, expr);
convert(&p);
return 0;
}
if (isdigit(*(p->s)) || isalpha(*(p->s))) {
while (isdigit(*(p->s)) || isalpha(*(p->s))) {
*(p->t) = *(p->s);
p->s++;
p->t++;
}
} else if (*(p->s) == '(') {
push(p, *(p->s));
p->s++;
} else if (*(p->s) == '*' || *(p->s) == '+' || *(p->s) == '/' || *(p->s) == '%' || *(p->s) == '-' ||
*(p->s) == '$') {
if (p->top != -1) {
opr = pop(p);
while (priority(opr) >= priority(*(p->s))) {
*(p->t) = opr;
p->t++;
opr = pop(p);
}
push(p, opr);
push(p, *(p->s));
} else {
push(p, *(p->s));
}
p->s++;
} else if (*(p->s) == ')') {
opr = pop(p);
while (opr != '(') {
*(p->t) = opr;
p->t++;
opr = pop(p);
}
p->s++;
} else {
p->s++;
}
}
Output:
/*Program 19: Write a program to evaluate (i) Prefix Expression (ii) Postfix Expression
using stack.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int stack[SIZE];
int top = -1;
switch (expression[i])
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
result = 0;
break;
}
push(result);
}
i++;
}
return pop();
}
int evaluatePrefix(char *expression)
{
int i = strlen(expression) - 1;
int operand1, operand2, result;
while (i >= 0)
{
if (isdigit(expression[i]))
{
push(expression[i] - '0'); // Push operand to stack
}
else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] ==
'/')
{
operand1 = pop();
operand2 = pop();
switch (expression[i])
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
result = 0;
break;
}
push(result);
}
i--;
}
return pop();
}
int main()
{
char postfix[SIZE], prefix[SIZE];
return 0;
}
Output:
/*Program 20: WAP to implement a (i) Static (ii) Dynamic Linear Queue
Abhijeet Singh [Roll No: 02914202024] Page 56
Data Structures and algorithms Lab [BCA-106P] 2025
Solution:
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
// Dynamic Queue
struct node {
int info;
struct node *link;
} *front_d = NULL, *rear_d = NULL;
void main() {
int mode, ch;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
while (1) {
printf("\nChoose Queue Type:\n1. Static Linear Queue\n2. Dynamic Linear Queue\n3.
Exit\nEnter your choice: ");
scanf("%d", &mode);
switch (mode) {
case 1:
do {
printf("\nStatic Queue Menu:\n1. Insert\n2. Delete\n3. Display\n4. Back to Main
Menu\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: static_insert(); break;
case 2: static_delete(); break;
case 3: static_display(); break;
case 4: break;
default: printf("Invalid choice.\n");
}
} while (ch != 4);
break;
case 2:
do {
printf("\nDynamic Queue Menu:\n1. Insert\n2. Delete\n3. Display\n4. Back to
Main Menu\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: dynamic_insert(); break;
case 2: dynamic_delete(); break;
case 3: dynamic_display(); break;
case 4: break;
default: printf("Invalid choice.\n");
}
} while (ch != 4);
break;
case 3:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice.\n");
}
}
}
void static_delete() {
if (front_s == -1 || front_s > rear_s) {
printf("Queue is empty.\n");
} else {
printf("Deleted: %d\n", queue[front_s]);
front_s++;
if (front_s > rear_s) front_s = rear_s = -1;
}
}
void static_display() {
if (front_s == -1) {
printf("Queue is empty.\n");
} else {
printf("Queue: ");
for (int i = front_s; i <= rear_s; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
if (rear_d == NULL) {
front_d = rear_d = temp;
} else {
rear_d->link = temp;
rear_d = temp;
}
void dynamic_delete() {
if (front_d == NULL) {
printf("Queue is empty.\n");
} else {
struct node *temp = front_d;
int item = front_d->info;
front_d = front_d->link;
if (front_d == NULL)
rear_d = NULL;
free(temp);
printf("Deleted: %d\n", item);
}
}
void dynamic_display() {
struct node *temp = front_d;
if (temp == NULL) {
printf("Queue is empty.\n");
} else {
printf("Queue: ");
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
printf("\n");
}
}
Output:
/*Program 21: WAP to implement a (i) Static (ii) Dynamic Circular Queue
Solution:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void insert();
void delet();
void display();
void main()
{
int ch;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert(); break;
case 2: delet(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nInvalid choice. Please try again...");
}
}
while(1);
}
void insert()
{
if((front == 0 && rear == SIZE - 1) || (front == rear + 1))
{
printf("\nQueue is full.");
}
else
{
printf("\nEnter ITEM: ");
scanf("%d", &item);
if(rear == -1)
front = rear = 0;
else if(rear == SIZE - 1)
rear = 0;
else
rear++;
queue[rear] = item;
printf("Item inserted: %d", item);
}
}
void delet()
{
if(front == -1)
{
printf("\nQueue is empty.");
}
else
{
item = queue[front];
if(front == rear)
front = rear = -1;
else if(front == SIZE - 1)
front = 0;
else
front++;
void display()
{
int i;
if(front == -1)
{
printf("\nQueue is empty.");
}
else
{
printf("\nQueue elements are: ");
if(front <= rear)
{
for(i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for(i = front; i < SIZE; i++)
printf("%d ", queue[i]);
for(i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
}
}
Output:
#include <stdio.h>
#include <stdlib.h>
void insert();
void delet();
void display();
int main()
{
int ch;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
if(queue == NULL)
{
printf("Memory allocation failed!");
return 1;
}
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert(); break;
case 2: delet(); break;
case 3: display(); break;
case 4: free(queue); exit(0);
default: printf("\nInvalid choice. Please try again...");
}
}
while(1);
return 0;
}
void insert()
{
if((front == 0 && rear == size - 1) || (rear + 1) % size == front)
{
printf("\nQueue is full.");
}
else
{
printf("\nEnter ITEM: ");
scanf("%d", &item);
if(rear == -1)
front = rear = 0;
else
rear = (rear + 1) % size;
queue[rear] = item;
printf("Item inserted: %d", item);
}
}
void delet()
{
if(front == -1)
{
printf("\nQueue is empty.");
}
else
{
item = queue[front];
if(front == rear)
front = rear = -1;
else
front = (front + 1) % size;
void display()
{
int i;
if(front == -1)
{
printf("\nQueue is empty.");
}
else
{
printf("\nQueue elements are: ");
i = front;
while(1)
{
printf("%d ", queue[i]);
if(i == rear)
break;
i = (i + 1) % size;
}
}
}
Output:
Solution:
1.Static De-Queue
#include <stdio.h>
#define MAX 10
void add_rear();
void add_front();
void delete_rear();
void delete_front();
void display();
void main()
{
int ch;
printf("\t\t Name: Abhijeet Singh \t\t Roll No: 02914202024\n\n");
do
{
printf("\n D-Queue Menu");
printf("\n--------------");
printf("\n 1. AddRear");
printf("\n 2. AddFront");
printf("\n 3. DeleteRear");
printf("\n 4. DeleteFront");
printf("\n 5. Display");
printf("\n 6. Exit");
printf("\n--------------");
switch(ch)
{
case 1:
add_rear();
printf("\n Queue after insert at rear");
display();
break;
case 2:
add_front();
case 3:
delete_rear();
printf("\n Queue after delete at rear");
display();
break;
case 4:
delete_front();
printf("\n Queue after delete at front");
display();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("\n Wrong Choice\n");
}
}
while(ch != 6);
}
void add_rear()
{
int no;
printf("\n Enter value to insert : ");
scanf("%d", &no);
if(rear == MAX - 1)
{
printf("\n Queue is Overflow");
return;
}
else
{
if(rear == -1)
rear = 0;
else
rear++;
if(front == -1)
front = 0;
q[rear] = no;
}
}
void add_front()
{
int no;
printf("\n Enter value to insert:-");
scanf("%d", &no);
if(front <= 0)
{
printf("\n Cannot add value at front end");
return;
}
else
{
front--;
q[front] = no;
}
}
void delete_front()
{
int no;
if(front == -1)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
no = q[front];
printf("\n Deleted element is %d\n", no);
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
}
}
void delete_rear()
{
int no;
if(rear == -1)
{
printf("\n Cannot delete value at rear end\n");
return;
}
else
{
no = q[rear];
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
rear--;
}
Output:
2.Dyanmic De-Queue
Solution:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
struct dqueue {
struct node *front;
struct node *rear;
};
int main()
{
struct dqueue dq;
int i, n;
initdqueue(&dq);
addqatend(&dq, 11);
addqatbeg(&dq, 10);
addqatend(&dq, 12);
addqatbeg(&dq, 9);
addqatend(&dq, 13);
addqatbeg(&dq, 8);
addqatend(&dq, 14);
addqatbeg(&dq, 7);
display(dq);
n = count(dq);
printf("\nTotal elements: %d", n);
i = delqatbeg(&dq);
printf("\nItem extracted = %d", i);
i = delqatbeg(&dq);
printf("\nItem extracted = %d", i);
i = delqatbeg(&dq);
printf("\nItem extracted = %d", i);
i = delqatend(&dq);
printf("\nItem extracted = %d", i);
display(dq);
n = count(dq);
printf("\nElements Left: %d\n", n);
deldqueue(&dq);
return 0;
}
if (p->rear == NULL) {
temp->next = temp->prev = temp;
p->front = p->rear = temp;
} else {
temp->next = p->front;
temp->prev = p->rear;
p->rear->next = temp;
p->front->prev = temp;
p->rear = temp;
}
}
if (p->front == NULL) {
free(temp);
return item;
}
if (p->front == p->rear) {
p->front = p->rear = NULL;
} else {
p->rear = p->rear->prev;
p->rear->next = p->front;
p->front->prev = p->rear;
}
free(temp);
return item;
}
do {
c++;
temp = temp->next;
} while (temp != dq.front);
return c;
}
Output: