Data Structures Coding Questions Asked In Online Test In 2022
Hello guys, welcome back to my blog. In this article, I will share data structures coding questions asked in an online test in 2022, or coding questions on data structures on algorithms.
If you have any doubts related to electrical, electronics, and computer science, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics And Chetan Shidling.Â
Also, read:
- Roadmap To Become A Blockchain Developer, Skills, Scope, Salary
- Top 14 Back-End Frameworks For Web And App Developers In 2022
- Top 10 Python Frameworks And Libraries For IoT Engineers
Data Structures Coding Questions
01. Design, Develop and Implement a menu-driven Program in C for the following Array operations
- a. Creating an Array of N Integer Elements
- b. Display of Array Elements with Suitable Headings
- c. Inserting an Element (ELEM) at a given valid Position (POS)
- d. Deleting an Element at a given valid Position(POS)
- e. Exit.
Support the program with functions for each of the above operations
Solution:
#include <stdio.h>
#include <stdlib.h>
int a[20];
int n, val, i, pos;
/*Function Prototype*/
void create();
void display();
void insert();
void Delete();
int main()
{
int choice = 1;
while (choice)
{
printf("\n\n--------MENU-----------\n");
printf("1.CREATE\n");
printf("2.DISPLAY\n");
printf("3.INSERT\n");
printf("4.DELETE\n");
printf("5.EXIT\n");
printf("-----------------------");
printf("\nENTER YOUR CHOICE:\t");
scanf("%d", &choice);
switch (choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
Delete();
break;
case 5:
exit(0);
default:
printf("\nInvalid choice:\n");
break;
}
}
return 0;
}
//creating an array
void create()
{
printf("\nEnter the size of the array elements:\t");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
}
//displaying an array elements
void display()
{
// int i;
printf("\nThe array elements are:\n");
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
//inserting an element into an array
void insert()
{
printf("\nEnter the position for the new element:\t");
scanf("%d", &pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d", &val);
for (i = n - 1; i >= pos; i--)
{
a[i + 1] = a[i];
}
a[pos] = val;
n = n + 1;
}
//deleting an array element
void Delete()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d", &pos);
val = a[pos];
for (i = pos; i < n - 1; i++)
{
a[i] = a[i + 1];
}
n = n - 1;
printf("\nThe deleted element is =%d", val);
}
Output:
inux:~/dslab # gedit array.c
linux:~/dslab # cc array.c
linux:~/dslab # ./a.out
MENU
1.CREATE
2.DISPLAY
3.INSERT
4.DELETE
5.EXIT
ENTER YOUR CHOICE: 1
Enter the size of the array elements: 3
Enter the elements for the array
10 25 30
ENTER YOUR CHOICE: 2 The
array elements are:
10 25 30
ENTER YOUR CHOICE: 3
Enter the position for the new element: 1
Enter the element to be inserted : 20
ENTER YOUR CHOICE: 2 The
array elements are:
10 20 25 30
ENTER YOUR CHOICE: 4
Enter the position of the element to be deleted: 3
The deleted element is =30
enter your choice: 5
02. Design, Develop and Implement a Program in C for the following operations on Strings
- a. Read the main String (STR), a Pattern String (PAT), and a Replace String (REP)
- b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR.
Support the program with functions for each of the above operations. Don’t use Built-in functions.
Solution:
#include <stdio.h>
void main()
{
char STR[100], PAT[100], REP[100], ans[100];
int i, j, c, m, k, flag = 0;
printf("\nEnter the MAIN string: \n");
gets(STR);
printf("\nEnter a PATTERN string: \n");
gets(PAT);
printf("\nEnter a REPLACE string: \n");
gets(REP);
i = m = c = j = 0;
while (STR[c] != '\0')
{
// Checking for Match
if (STR[m] == PAT[i])
{
i++;
m++;
if (PAT[i] == '\0')
{
//copy replace string in ans string
for (k = 0; REP[k] != '\0'; k++, j++)
{
ans[j] = REP[k];
flag = 1;
}
i = 0;
c = m;
}
}
else //mismatch
{
ans[j] = STR[c];
j++;
c++;
m = c;
i = 0;
}
}
if (flag == 0)
{
printf("Pattern doesn't found!!!");
}
else
{
ans[j] = '\0';
printf("\nThe RESULTANT string is:%s\n", ans);
}
}
Output:
linux:~/dslab # gedit string.c
linux:~/dslab # cc string.c
linux:~/dslab # ./a.out
Enter the MAIN string: good
morning
Enter a PATTERN string:
morning
Enter a REPLACE string:
evening
The RESULTANT string is: good evening
linux:~/dslab # ./a.out Enter
the MAIN string: hi SJCIT
Enter a PATTERN string: NICE
Enter a REPLACE string: hello
Pattern doesn't found!!!
03. Design, Develop and Implement a menu-driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX).
- a. Push an Element on to Stack
- b. Pop an Element from Stack
- c. Demonstrate how Stack can be used to check Palindrome
- d. Demonstrate Overflow and Underflow situations on Stack
- e. Display the status of Stack
- f. Exit
Support the program with appropriate functions for each of the above operations.
Solution:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int top = -1;
int overflow()
{
if (top == MAX - 1)
return 1;
else
return 0;
}
int underflow()
{
if (top == -1)
return 1;
else
return 0;
}
void push(int *s, int ele)
{
if(top!= MAX-1)
{
if (!overflow())
{
s[++top] = ele;
}
else
{
printf("stack overflow\n");
}
}
}
int pop(int *s)
{
if (!underflow())
return (s[top--]);
else
printf("Stack underflow\n");
return (0);
}
void display(int *s)
{
int i;
if (underflow())
{
printf("Stack underflow\n");
return;
}
printf("The stack contents are: \n");
for (i = 0; i <= top; i++)
{
printf("%d\t", s[i]);
}
}
int palin(int stack[MAX])
{
int i, count=0;
for(i=0; i<=(top/2); i++)
{
if(stack[i]==stack[top-i])
count++;
}
if((top/2+1)== count)
printf("\n Stack contents are palindrome");
else
printf("\n stack contents are mot a palindrome");
}
int main()
{
int stack[MAX], ch, i, j, ele, p;
char str[10];
while (1)
{
printf("\n Enter your choice: \n 1.PUSH \n 2.POP \n 3.CHECK PALINDROME \n 4.DISPLAY \n 5.EXIT\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter the elements to be pushed\n");
scanf("%d", &ele);
push(stack, ele);
break;
case 2:
ele = pop(stack);
if (ele != 0)
printf("\n The deleted element is: %d\n", ele);
break;
case 3:
palin(stack);
break;
case 4:
display(stack);
break;
default:
exit(0);
}
}
return 0;
}
Output:
linux:~/dslab # gedit stack.c
linux:~/dslab # cc stack.c
linux:~/dslab # ./a.out
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 2
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 5
Enter your choice: 2
The poped element: 5
Enter your choice: 4 The stack elements are:
1 2 1
Enter your choice:
3 Numbers= 1
Numbers= 2
Numbers= 1
reverse operation
: reverse array :
1
2
1
check for palindrome :
It is palindrome number
04. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix Expression. The program should support both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %(Remainder), ^(Power), and alphanumeric operands.
Solution:
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
#include <stdio.h>
char s[SIZE];
int top = -1; /* Global declarations */
int push (char elem) /* Function for PUSH operation */
{
s[++top] = elem;
}
char pop() /* Function for POP operation */
{
return (s[top--]);
}
int pr(char elem) /* Function for precedence */
{
switch (elem)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
}
}
void main() /* Main Program */
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nEnter the Infix Expression ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}
Output:
linux:~/dslab # gedit intopost.c
linux:~/dslab # cc intopost.c
linux:~/dslab # ./a.out
Enter the Infix Expression:
(a+b)*c/d^5%1
Given Infix Expn: (a+b)*c/d^5%1
Postfix Expn: ab+c*d5^/1%
05. Design, Develop and Implement a Program in C for the following Stack Applications
- a. Evaluation of Suffix expression with single-digit operands and operators: +, -, *, /, %, ^
- b. Solving Tower of Hanoi problem with n disks
Solution:
// Evaluation of Suffix Expression
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top = -1;
/*fUNCTION PROTOYPE */
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
printf("Insert a postfix notation :: ");
scanf("%s", post);
for (i = 0; i < strlen(post); i++)
{
if (post[i] >= '0' && post[i] <= '9')
{
pushstack(i);
}
if (post[i] == '+' || post[i] == '-' || post[i] == '*' || post[i] == '/' || post[i] == '^')
{
calculator(post[i]);
}
}
printf("\n\nResult :: %d", stack[top]);
}
void pushstack(int tmp)
{
top++;
stack[top] = (int)(post[tmp] - 48);
}
void calculator(char c)
{
int a, b, ans;
a = stack[top];
stack[top] = '\0';
top--;
b = stack[top];
stack[top] = '\0';
top--;
switch (c)
{
case '+':
ans = b + a;
break;
case '-':
ans = b - a;
break;
case '*':
ans = b * a;
break;
case '/':
ans = b / a;
break;
case '^':
ans = pow(b, a);
break;
default:
ans = 0;
}
top++;
stack[top] = ans;
}
Output:
linux:~/dslab #gedit posteval.c
linux:~/dslab #gcc posteval.c -lm
linux:~/dslab # ./a.out
Insert a postfix notation :: 22^32*+
Result :: 10
linux:~/dslab #gedit tower.c
linux:~/dslab #gcc tower.c
linux:~/dslab # ./a.out
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
06. Design, Develop and Implement a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
- a. Insert an Element on to Circular QUEUE
- b. Delete an Element from Circular QUEUE
- c. Demonstrate Overflow and Underflow situations on Circular QUEUE
- d. Display the status of Circular QUEUE
- e. Exit
Support the program with appropriate functions for each of the above operations.
Solution:
#include <stdio.h>
#include <stdlib.h>
#define max 10
int q[10], front = 0, rear = -1;
void main()
{
int ch;
/*FUNCTION PROTOTYPE */
void insert();
void delet();
void display();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while (1)
{
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if ((front == 0 && rear == max - 1) || (front > 0 && rear == front - 1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d", &x);
if (rear == max - 1 && front > 0)
{
rear = 0;
q[rear] = x;
}
else
{
if ((front == 0 && rear == -1) || (rear != front - 1))
q[++rear] = x;
}
}
}
void delet()
{
int a;
if ((front == 0) && (rear == -1)) //empty stack
{
printf("Queue is underflow\n");
exit(1);
}
if (front == rear) // only 1 element
{
a = q[front];
rear = -1;
front = 0;
}
else if (front == max - 1) // only 1 elements at the end of queue
{
a = q[front];
front = 0;
}
else
a = q[front++];
printf("Deleted element is:%d\n", a);
}
void display()
{
int i, j;
if (front == 0 && rear == -1)
{
printf("Queue is underflow\n");
exit(1);
}
if (front > rear)
{
for (i = 0; i <= rear; i++)
printf("\t%d", q[i]);
for (j = front; j <= max - 1; j++)
printf("\t%d", q[j]);
printf("\nrear is at %d\n", q[rear]);
printf("\nfront is at %d\n", q[front]);
}
else
{
for (i = front; i <= rear; i++)
{
printf("\t%d", q[i]);
}
printf("\nrear is at %d\n", q[rear]);
printf("\nfront is at %d\n", q[front]);
}
printf("\n");
}
Output:
linux:~/dslab #gedit cirQ.c
linux:~/dslab #gcc cirQ.c
linux:~/dslab # ./a.out
Circular Queue
operations 1.insert
2.delete
3.display
4.exit
Enter your choice:1
Enter element to be insert:10
Enter your choice:1
Enter element to be insert:20
Enter your choice:1
Enter element to be insert:30
Enter your
choice:3 10 20 30
rear is at 30
front is at 10
Enter your choice:2
Deleted element is:10
Enter your
choice:3
20 30
rear is at 30
front is at 20
Enter your choice:4
07. Design, Develop and Implement a menu-driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: U SN, Name, Branch, Sem, PhNo.
- a. Create a SLL of N Students Data by using front insertion.
- b. Display the status of SLL and count the number of nodes in it
- c. Perform Insertion and Deletion at End of SLL
- d. Perform Insertion and Deletion at Front of SLL
- e. Demonstrate how this SLL can be used as STACK and QUEUE
- f. Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
int sem, phno;
char name[20], usn[20], branch[20];
struct node *next;
} *first=NULL , *last=NULL, *temp=NULL, *temp1, *d;
void create();
void insert_atfirst();
void insert_atlast();
void display();
int delete_end();
int delete_first();
void create()
{
int sem, phno;
char usn[20], name[20], branch[20];
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the USN, Name, Branch, Sem and phno");
scanf("%s %s %s %d %d", usn, name, branch, &sem, &phno);
strcpy(temp->usn, usn);
strcpy(temp->name, name);
strcpy(temp->branch, branch);
temp->sem=sem;
temp->phno=phno;
temp->next=NULL;
count++;
}
void insert_atfirst()
{
if(first==NULL)
{
create();
first=temp;
last=first;
}
else
{
create();
temp->next=first;
first=temp;
}
}
void insert_atlast()
{
if (first==NULL)
{
create();
first=temp;
last=first;
}
else
{
create();
last->next=temp;
last=temp;
}
}
void display()
{
//int n=0;
temp1=first;
if(temp1==NULL)
{
printf("List is empty to display\n");
return;
}
printf("The linked list from beginning :\n");
while(temp1!=NULL)
{
printf("%s %s %s %d %d\n", temp1->usn, temp1->name, temp1->branch, temp1->sem, temp1->phno);
temp1=temp1->next;
// count++;
}
printf("Number of students are %d", count);
}
int delete_end()
{
struct node *p;
p=first;
if(p->next==NULL)
{
free(p);
first=NULL;
}
else
{
while(p->next!=last)
p=p->next;
printf("%s %s %s %d %d\n", last->usn, last->name, last->branch, last->sem, last->phno);
free(last);
p->next=NULL;
last=p;
}
count--;
return 0;
}
int delete_first()
{
struct node*p;
p=first;
if(p->next==NULL)
{
free(p);
first=NULL;
return 0;
}
else
{
first=temp->next;
printf("%s %s %s %d %d",last->usn, last->name, last->branch, last->sem, last->phno);
free(p);
}
count--;
return 0;
}
void main()
{
int ch, n, i;
first=NULL;
temp=temp1=NULL;
printf("----MENU----\n");
printf("1. Create a SLL of N employees\n");
printf("2. Display from beginning\n");
printf("3. Insert at end\n");
printf("4. Delete at end\n");
printf("5. Insert at Beginning\n");
printf("6. Delete at Beginning\n");
printf("7. Exit\n");
printf("--------------\n");
while(1)
{
printf("\n Enter your choice\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter no of students\n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
insert_atfirst();
}
break;
case 2:
display();
break;
case 3:
insert_atlast();
break;
case 4:
delete_end();
break;
case 5:
insert_atfirst();
break;
case 6:
delete_first();
break;
case 7:
exit (0);
default:
printf("Wrong choice\n");
break;
}
}
}
Output:
linux:~/dslab #gedit slink.c
linux:~/dslab #gcc slink.c
linux:~/dslab # ./a.out
– ------
MENU -
1 – create a SLL of n emp
2 -Display from beginning
3 -Insert at end
4 -delete at end
5 -Insert at beg
6 -delete at beg
7 -exit
Enter choice : 1
Enter no of students : 2
Enter usn,name, branch, sem, phno of student : 007 vijay CSE 3 121
Enter usn,name, branch, sem, phno of student : 100 yashas CSE 3 911
Enter choice : 2
Linked list elements from begining
: 100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 3
Enter usn,name, branch, sem, phno of student : 001 raj CSE 3 111
Enter choice : 2
Linked list elements from begining
: 100 yashas CSE 3 911
007 vijay CSE 3 121
001 raj CSE 3
111 No of
students = 3
Enter choice :4
001 raj CSE 3
111
Enter choice :2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 5
Enter usn,name, branch, sem, phno of student : 003 harsh cse 3 111
Enter choice : 2
Linked list elements from begining
: 003 harsh cse 3 111
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 3
Enter choice : 6
003 harsh cse 3 111
Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3
121 No of students
= 2
Enter choice : 7
08. Design, Develop and Implement a menu driven Program in C for the following operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
- a. Create a DLL of N Employees Data by using end insertion.
- b. Display the status of DLL and count the number of nodes in it
- c. Perform Insertion and Deletion at End of DLL
- d. Perform Insertion and Deletion at Front of DLL
- e. Demonstrate how this DLL can be used as Double Ended Queue
- f. Exit
Solution:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int count=0;
struct node
{
struct node *prev;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void create()
{
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &ssn, name,dept,desg,&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void insertbeg()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insertend()
{
if(h==NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void displaybeg()
{
temp2 =h;
if(temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp2!= NULL)
{
printf("%d %s %s %s %f %d\n", temp2->ssn, temp2->name,temp2->dept,
temp2->desg,temp2->sal, temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
return 0;
}
else
{
temp2=temp1->prev;
temp2->next=NULL;
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,
temp1->desg,temp1->sal, temp1->phno );
free(temp1);
temp1=temp2;
}
count--;
return 0;
}
int deletebeg()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
}
else
{
h=h->next;
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept,
temp->desg,temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
h=NULL;
temp = temp1 = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
}
}
}
Output:
09. Design, Develop and Implement a Program in C for the following operations o Singly Circular Linked List (SCLL) with header nodes
- a. Represent and Evaluate a Polynomial P(x,y,z) = 6x y z-4yz +3x yz+2xy z-2xyz
- b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct term
{
int coeff;
int pow_x;
int pow_y;
int pow_z;
};
typedef struct term TERM;
struct node
{
TERM info;
struct node* next;
};
typedef struct node* Node;
Node Create(TERM t)
{
Node temp = ( Node)malloc(sizeof(struct node));
temp->info=t;
temp->next = temp;
return temp;
}
Node MKHeader()
{
TERM x;
Node temp;
x.coeff=-1;
x.pow_x=-1;
x.pow_y=-1;
x.pow_z=-1;
temp=Create(x);
return temp;
}
Node Insert(Node p,TERM t)
{
Node temp = Create(t),cur=p;
while(cur->next!=p)
cur=cur->next;
cur->next=temp;
temp->next=p;
return p;
}
double Compute(Node temp,int x,int y,int z)
{
double ret;
TERM t=temp->info;
ret=t.coeff * pow(x, t.pow_x) * pow(y, t.pow_y) * pow(z,t.pow_z);
return ret;
}
double Evaluate(Node p, int x, int y, int z)
{
Node po = p->next;
double sum = 0;
while (po!=p)
{
sum += Compute(po,x,y,z);
po = po->next;
}
return sum;
}
void DispTerm(TERM t)
{
printf(" %dx^%dy^%dz^%d ", t.coeff, t.pow_x, t.pow_y, t.pow_z);
}
void PrintPoly(Node p)
{
Node po = p->next;
while (po!=p)
{
DispTerm(po->info);
if(po->next!=p)
printf("+");
po = po->next;
}
printf("\n");
}
void ReadPoly(Node t1,int n)
{
TERM t;
int i;
for(i=1;i<=n;i++)
{
printf("Enter the value of coefficent and powers of x,y and z");
scanf("%d%d%d%d",&t.coeff,&t.pow_x,&t.pow_y,&t.pow_z);
t1 = Insert(t1, t);
}
PrintPoly(t1);
}
int ComparePower(TERM m,TERM n)
{
if(m.pow_x==n.pow_x && m.pow_y==n.pow_y && m.pow_z==n.pow_z)
return 1;
else
return 0;
}
TERM AddTerms(TERM m,TERM n)
{
TERM temp;
temp.coeff=m.coeff+n.coeff;
temp.pow_x = m.pow_x;
temp.pow_y = m.pow_y;
temp.pow_z = m.pow_z;
return temp;
}
Node AddPoly(Node p1,Node p2)
{
Node Newlist=MKHeader();
Node t1=p1->next,t3;
Node t2=p2->next,t4;
TERM res;
int i,flag;
t3=t1;
t4=t2;
while(t1!=p1)
{
t2=p2->next;
flag=1;
while(t2!=p2 && flag)
{
if(ComparePower(t1->info,t2->info))
{
res=AddTerms(t1->info,t2->info);
Newlist=Insert(Newlist,res);
flag=0;
}
t2=t2->next;
}
if (flag==1)
Newlist=Insert(Newlist,t1->info);
t1=t1->next;
}
while(t4!=p2)
{
t3=Newlist->next;
flag=1;
while(t3!=Newlist && flag)
{
if(ComparePower(t3->info,t4->info))
flag=0;
t3=t3->next;
}
if (flag==1)
Newlist=Insert(Newlist,t4->info);
t4=t4->next;
}
return Newlist;
}
int main()
{
int n,x,y,z,ch,i,coeff;
Node polysum;
Node poly1 = MKHeader();
Node poly2 = MKHeader();
while(1)
{
printf("\nMenu\n 1:Evaluate Polynomial \n 2:Add\n 3:Exit\n Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the terms in the polynomial");
scanf("%d",&n);
ReadPoly(poly1,n);
printf("Enter the values of x,y and z");
scanf("%d%d%d",&x,&y,&z);
printf("%.2f\n", Evaluate(poly1, x, y, z));
break;
case 2: printf("Enter the terms in the polynomial 2");
scanf("%d",&n);
ReadPoly(poly2,n);
polysum = AddPoly(poly1, poly2);
PrintPoly(polysum);
break;
case 3: exit(0);
}
}
return 0;
}
Output:
linux:~/dslab #gedit poly.c
linux:~/dslab #gcc poly.c
linux:~/dslab # ./a.out
<< MENU >>
Polynomial Operations :
1.Add 2.Evaluate
3.Exit
Enter your choice==>1
Enter no of terms of polynomial==>
3
Enter coef & expo==>
4 3
Enter coef & expo==>
2 2
Enter coef & expo==> 5 1
The polynomial is==>5x^(1) + 2x^(2) +
4x^(3)
Enter no of terms of polynomial==>3
Enter coef & expo==>
4 1
Enter coef & expo==>
3
2
Enter coef & expo==>
5 3
The polynomial is==>4x^(1) + 3x^(2) + 5x^(3)
Addition of polynomial==>
The polynomial is==>9x^(1) + 5x^(2) + 9x^(3)
Enter your choice==>2
Enter no of terms of polynomial==>3
Enter coef & expo==>
3 1
Enter coef & expo==> 4
2
Enter coef & expo==> 5 4
The polynomial is==>3x^(1) + 4x^(2) + 5x^(4)
Enter the value of x=2
Value of polynomial=102
Enter your choice==>3
exit
10. Design, Develop and Implement a menu-driven Program in C for the following operations on Binary Search Tree (BST) of Integers.
- a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
- b. Traverse the BST in Inorder, Preorder, and Post Order
- c. Search the BST for a given element (KEY) and report the appropriate message
- d. Delete an element(ELEM) from BST
- e. Exit
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
typedef struct node* Node;
Node newNode(int item)
{
Node temp = (Node)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
Node insert(Node node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->data)
node->left = insert(node->left, key);
else if (key > node->data)
node->right = insert(node->right, key);
return node;
}
int search(Node root, int key)
{
if (root == NULL)
return -1;
if(root->data == key)
return 1;
if (root->data < key)
return search(root->right, key);
return search(root->left, key);
}
void inorder(Node root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d \t", root->data);
inorder(root->right);
}
}
void preorder(Node root)
{
if (root != NULL)
{
printf("%d \t", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d \t", root->data);
}
}
int main()
{
int n, i, ch, ch1, key, pos;
Node root=NULL;
printf("Enter the no of nodes in the BST\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the element to be iserted\n");
scanf("%d",&key);
root=insert(root,key);
}
while(1)
{
printf("\nEnter the choice\n1: Insert Node\n2: Traversal\n3: Search for key\n4: Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to be iserted\n");
scanf("%d",&key);
root=insert(root,key);
break;
case 2:
printf("Enter your choice\n1: Preorder\n2: Inorder\n3: Postorder\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
preorder(root);
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
default:
printf("\n Make Correct Choice");
}
break;
case 3:
printf("Enter the key to be searched\n");
scanf("%d",&key);
pos=search(root,key);
if (pos==-1)
{
printf("\n Key is not found\n");
}
else
{
printf("\n Key is found\n");
}
break;
case 4: exit(0);
}
}
return 0;
}
Output:
linux:~/dslab #gedit bst.c
linux:~/dslab #gcc bst.c
linux:~/dslab # ./a.out
Program For Binary Search Tree
1.Create
2.Search
3.Recursive
Traversals 4.Exit
Enter your choice :1
Enter The Element 15
Want To enter More Elements?(1/0)1
Enter The Element 25
Want To enter More Elements?(1/0)1
Enter The Element 35
Want To enter More Elements?(1/0)1
Enter The Element 45
Want To enter More Elements?(1/0)1
Enter The Element 5
Want To enter More Elements?(1/0)1
Enter The Element 7
Want To enter More Elements?(1/0)0
Enter your choice :2
Enter Element to be searched :7
The 7 Element is Present
Parent of node 7 is 5
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :2
Enter Element to be searched :88
The 88 Element is not Present
Enter your choice :3
The Inorder display : 5 7 15 25 35 45
The Preorder display : 15 5 7 25 35 45
The Postorder display : 7 5 45 35 25 15
Enter your choice :4
11. Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using the BFS method.
c. Check whether a given graph is connected or not using the DFS method.
Solution:
#include <stdio.h>
#include <stdlib.h>
int a[20][20], q[20], visited[20], reach[10], n, i, j, f = 0, r = -1, count = 0;
void bfs(int v)
{
for (i = 1; i <= n; i++)
if (a[v][i] && !visited[i])
q[++r] = i;
if (f <= r)
{
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void dfs(int v)
{
int i;
reach[v] = 1;
for (i = 1; i <= n; i++)
{
if (a[v][i] && !reach[i])
{
printf("\n %d->%d", v, i);
count++;
dfs(i);
}
}
}
void main()
{
int v, choice;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
q[i] = 0;
visited[i] = 0;
}
for (i = 1; i <= n - 1; i++)
reach[i] = 0;
printf("\n Enter graph data in matrix form:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("1.BFS\n 2.DFS\n 3.Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n Enter the starting vertex:");
scanf("%d", &v);
bfs(v);
if ((v < 1) || (v > n))
{
printf("\n Bfs is not possible");
}
else
{
printf("\n Bfs is not possible");
printf("\n The nodes which are reachable from %d:\n", v);
for (i = 1; i <= n; i++)
if (visited[i])
printf("%d\t", i);
}
break;
case 2:
dfs(1);
if (count == n - 1)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
break;
case 3:
exit(0);
}
}
Output:
linux:~/dslab #gedit bfs.c
linux:~/dslab #gcc bfs.c
linux:~/dslab # ./a.out
Enter the number of vertices:5
Enter graph data in matrix
form: 0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 0
0 1 0 0
0
1.BFS
2.DFS
3.Exit 21->2 2->3 3->4 2->5
Graph is connected
Enter the number of
vertices:5
Enter graph data in matrix
form: 0 1 0 1 0
1 0 1 0 0
0 1 0 1 0
1 0 1 0 0 0 0 0 0 0
1.BFS
2.DFS
3.Exit 21->2 2->3 3->4
Graph is not connected
Enter the number of vertices:5
Enter graph data in matrix
form: 0 1 1 0 0
0 0 0 1 0
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
1.BFS
2.DFS
3.Exit
1
Enter the starting vertex:1
The nodes which are reachable from
1: 2 3 4
Enter graph data in matrix
form: 0 1 1 0 0
0 0 0 1 0
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
1.BFS
2.DFS
3.Exit
1
Enter the starting
vertex:0 BFS is not
possible
12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L be Integers. Design and develop a program in C that uses Hash function H: K → L as H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing
Solution:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
/*FUNCTION PROTOTYPE */
int create(int);
void linear_prob(int[], int, int);
void display (int[]);
void main()
{
int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
scanf("%d",&ans);
}while(ans);
display(a);
}
int create(int num)
{
int key;
key=num%100;
return key;
}
void linear_prob(int a[MAX], int key, int num)
{
int flag, i, count=0;
flag=0;
if(a[key]== -1)
{
}
else
{
a[key] = num;
printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
{
if (a[i]!=-1)
count++;
i++;
}
printf("Collision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;
break;
}
//for(i=0;i<key;i++)
i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
void display(int a[MAX])
{
int i,choice;
printf("1.Display ALL\n 2.Filtered Display\n");
scanf("%d",&choice);
if(choice==1)
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
printf("\n %d %d ", i, a[i]);
}
else
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
if(a[i]!=-1)
{
printf("\n %d %d ", i, a[i]);
continue;
}
}
}
Output:
linux:~/dslab #gedit hash.c
linux:~/dslab #gcc hash.c
linux:~/dslab # ./a.out
collision handling by linear probing :
Enter the data1234
Do you wish to continue ? (1/0) 1
Enter the data2548
Do you wish to continue ? (1/0) 1
Enter the data3256
Do you wish to continue ? (1/0) 1
Enter the data1299
Do you wish to continue ? (1/0) 1
Enter the data1298
Do you wish to continue ? (1/0) 1
Enter the data1398
Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING
Do you wish to continue ? (1/0)
0 1.Display ALL
2.Filtered
Display 2
the hash table is
0 1398
34 1234
48 2548
56 3256
98 1298
99 1299
This was about “Data Structures Coding Questions”. I hope this article “Data Structures Coding Questions” may help you all a lot. Thank you for reading.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs