0% found this document useful (0 votes)
22 views119 pages

DS - I Slips Solution

The document contains multiple programming solutions related to data structures, including linked lists, stacks, queues, and sorting algorithms. Each solution is presented with header files, main program files, and example outputs demonstrating their functionality. The solutions cover operations such as creating, displaying, sorting linked lists, implementing a stack for string reversal, and managing a priority queue.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views119 pages

DS - I Slips Solution

The document contains multiple programming solutions related to data structures, including linked lists, stacks, queues, and sorting algorithms. Each solution is presented with header files, main program files, and example outputs demonstrating their functionality. The solutions cover operations such as creating, displaying, sorting linked lists, implementing a stack for string reversal, and managing a priority queue.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 119

Slip:-1

Q1.
Solution:-
Header File:- doublylist.h
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev; int data;
struct node *next;
};
struct node *f;
void create()
{
int n,i;
struct node *s;
printf("enter number of nodes needed : ");
scanf("%d",&n);
f=(struct node *)malloc(sizeof(struct node));
printf("enter data : ");
scanf("%d",&f->data); f->prev=NULL;
s=f; for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;
printf("enter data :"); scanf("%d",&s->data);
}
s->next= NULL;
}
void display()
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf(" %d -> ",s->data);
}
}

Program File:-
#include <stdio.h>
#include "doublylist.h"
int main()
{
int ch;
do
{
printf("\n1.create\n2.display\n0.exit");
printf("\nenter choice :");
scanf("%d",&ch);
switch (ch)
{
case 1: create();
break;
case 2: display();
break;
case 0: ;
break;
default:printf("invalid choice ");
break;
}
}
while(ch!=0);
}

//output
/*
1.create
2.display
0.exit
enter choice :1
enter number of nodes needed : 3
enter data : 3
enter data :2
enter data :1

1.create
2.display
0.exit
enter choice :2
3 -> 2 -> 1 ->
1.create
2.display
0.exit
enter choice :0
*/
Q2.
Solution :-
// Sort linked list using bubble sort
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *f;
void create()
{
int i,n;
struct node *s;
printf("\nEnter no of nodes ");
scanf("%d",&n);
f=(struct node *)malloc(sizeof(struct node));
printf("\n Enter node ");
scanf("%d",&f->data);
s=f;

for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;
printf("\n Enter node "); scanf("%d",&s->data);
}
s->next=NULL;
}
void display()
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf("\t %d ->",s->data);
}
}
void sort()
{
struct node *p,*q; int temp;
for(p=f;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data > q->data)
{
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
}
int main()
{
create();
printf("\n Link list is : ");
display();
printf("\n After sorting Link list is = ");
sort();
display();
}

//output
/*
Enter no of nodes 3

Enter node 3

Enter node 2

Enter node 1

Link list is : 3 -> 2 -> 1 ->


After sorting Link list is = 1 -> 2 -> 3 ->
*/
Slip:-2
Q1.
Solution:-
Header File:- singlylist.h
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *f;
void create()
{
int n,i;
struct node *s;
printf("enter number of nodes needed : ");
scanf("%d",&n);
f=(struct node *)malloc(sizeof(struct node));
printf("enter data : ");
scanf("%d",&f->data);
s=f;
for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node));
s=s->next;
printf("enter data :");
scanf("%d",&s->data);
}
s->next= NULL;
}
void display()
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf("%d ->",s->data);
}
}

Program file:-
#include <stdio.h>
#include "singlylist.h"
int main()
{
int ch;
do
{
printf("\n1.create\n2.display\n3.exit");
printf("\nenter choice :");
scanf("%d",&ch);
switch (ch)
{
case 1: create();
break; case 2: display();
break; case 3: break;
default: printf("invalid input");
}
}
while(ch!=3);
}

//output
/*
1.create
2.display
3.exit
enter choice :1
enter number of nodes needed : 5
enter data : 21
enter data :23
enter data :21
enter data :36
enter data :32

1.create
2.display
3.exit
enter choice :2
21 ->23 ->21 ->36 ->32 ->
1.create
2.display
3.exit
enter choice :3
*/
Q2.
Solution :-
#include <stdio.h>
char s[20];
int top;
void init()
{
top=-1;
}
int isempty(){
if(top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char ch)
{
if(isfull()==1)
printf("stack is full");
else
{
top++;
s[top]=ch;
}
}
char pop()
{
char ch;
if(isempty()==1)
printf("stack is empty");
else
{
ch=s[top];
top--;
}
return ch;
}
main()
{
int i,k=0;
char temp[20];
init();
char str[20];
printf("enter string ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
push(str[i]);
}
while(!isempty())
{
temp[k]=pop();
k++;
}
temp[k]='\0';
//Again String push into stack
printf("second string :");
for(i=0;temp[i]!='\0';i++)
{
push(temp[i]);
}

while(!isempty())
{
printf("%c",pop());
}
}

//output
/*
enter string Rahul Shinde Wagh
second string :Rahul
*/
Slip:-3
Q1.
Solution:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10],i,j,n,key;
printf("Enter how many numbers: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
a[i]=rand()%100;
}
printf("\n Before sorting array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
} //Output
for(i=1; i<n; i++)
{ Enter how many numbers: 5
key = a[i];
for(j=i-1; j>=0; j--) Before sorting array is 83 86
{ 77 15 93
if(a[j] > key) After sort array is: 15 77 83
{ 86 93
a[j+1]=a[j];
}
else
break;
}
a[j+1]=key;
}
printf("\nAfter sort array is: ");
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
Q2.
Solution :-

#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

//output
/*
Enter the expression :: 1+2

The result of expression 1+2 = 2


*/
Slip:-4
Q1.
Solution :-

#include<stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]); //output
}
printf("Input array is \n"); Enter the value of num
for (i = 0; i < num; i++) 5
{ Enter the elements one by
printf("%d\n", array[i]); one
} 65
for (i = 0; i < num; i++) 36
{ 24
for (j = 0; j < (num - i - 1); j++) 51
{ 35
if (array[j] > array[j + 1]) Input array is
{ 65
temp = array[j]; 36
array[j] = array[j + 1]; 24
array[j + 1] = temp; 51
} 35
} Sorted array is...
} 24
printf("Sorted array is...\n"); 35
for (i = 0; i < num; i++) 36
{ 51
printf("%d\n", array[i]); 65
}
}
Q2.
Solution :-

#include<stdio.h>
int f = -1,r = -1;
int q[50];
void enqueue(int data,int l)
{
if(r==l-1){
printf("Queue is full");
}
else if((f==-1)&&(r==-1)){
f = r = 0;
q[r] = data;
}
else
{
r++;
q[r] = data;
}
}
void print()
{
int i;
for(i=f;i<=r;i++)
{
printf("\n%d",q[i]);
}
}
void reverse()
{
int i,j,t;
for(i=f,j=r;i<j;i++,j--){
t = q[i];
q[i] = q[j];
q[j] = t;
}
}
void main()
{
int n,i=0,t;
printf("Enter the size of Queue");
scanf("%d",&n);
printf("\nEnter the data for Queue");
while(i<n){
scanf("%d",&t);
enqueue(t,n);
i++;
}
printf("\nQueue which you have entered:-");
print();
reverse();
printf("\nQueue after reversing:-");
print();

//output
/*
Enter the size of Queue5

Enter the data for Queue6


5
4
1
2

Queue which you have entered:-


6
5
4
1
2
Queue after reversing:-
2
1
4
5
6
*/
Slip:-5
Q1.
Solution:-
#include<stdio.h>
void linearsearch(int a[10],int n,int sr)
{
int i,p,cnt=0;
for(i=0;i<n;i++)
{
if(a[i]==sr)
{
p=i;
cnt++;
break;
}
}
if(cnt>=1)
printf("element found at %d position",p);
else
printf("element NOT found "); //output
}
int main() enter how many values5
{ enter values6
int n,i,sr,a[10]; enter values8
printf("enter how many values"); enter values5
scanf("%d",&n); enter values3
for(i=0;i<n;i++) enter values2
{
printf("enter values"); enter search element3
scanf("%d",&a[i]); element found at 3 position
}
printf("\n enter search element");
scanf("%d",&sr);
linearsearch(a,n,sr);
}
Q2.
Solution:-
Header File:- PriorityQ.h

#include<stdio.h>
int Q[20];
int f,R;
void init()
{
f=R=-1;
}
int isempty()
{
if(f==R)
return 1;
else
return 0;
}
int isfull()
{
if(R==19)
return 1;
else return 0;
}
void Add(int no)
{
int i;
if(isfull()==1)
printf("Queue is Full ");
else
{
for(i=R;i>f;i--)
{
if(no<Q[i])
Q[i+1]=Q[i];
else
break;
}
Q[i+1]=no;
R++;
}
}
int Delete()
{
int no;
if(isempty()==1)
printf("Queue is empty ");
else
{
f++;
no=Q[f];
}
return no;
}
void display()
{
int i;
for(i=f+1;i<=R;i++)
{
printf("%d ",Q[i]);
}
}

Program File:-
#include<stdio.h>
#include "PriorityQ.h"
int main()
{
int n,ch;
init();
do
{
printf("\n\n1.Add \n2.Delete \n3.Display
\n0.EXit"); printf("\nEnter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter element "); scanf("%d",&n);
Add(n);
break;
case 2:if(isempty()==1)
printf("\nQueue is empty ");
else
printf("deleted elemet =%d ",Delete());
break;
case 3:display();
break;
case 0:break;
default:printf("\nInvalid choice ");
}
}
while(ch!=0);
}

//output
/*
1.Add
2.Delete
3.Display
0.EXit
Enter choice 1

Enter element 3

1.Add
2.Delete
3.Display
0.EXit
Enter choice 3
3

1.Add
2.Delete
3.Display
0.EXit
Enter choice 2
deleted elemet =3

1.Add
2.Delete
3.Display
0.EXit
Enter choice 0
*/
Slip:-6
Q1.
Solution:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,a[10],n,min,pos,j,temp;
printf("Enter how many elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
printf("\nBefore array sorting ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
for(j=0;j<n-1;j++)
{ //OUTPUT
min=a[j];
pos=j; Enter how many elements 5
for(i=j+1;i<n;i++)
{ Before array sorting 83 86 77 15 93
if(a[i]<=min) Sorted array is 15 77 83 86 93
{
min=a[i];
pos=i;
}
}
temp=a[j];
a[j]=min;
a[pos]=temp;
}
printf("\nSorted array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Q2.
Solution:-
Header File:- dyqueue.h
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *f,*r;
void init() {
f=NULL;
r=NULL;
}
int isempty()
{
if(f==NULL)
return 1;
else
return 0;
}
void enqueue() {
struct node*nw;
int n;
nw=(struct node*)malloc(sizeof(struct node));
nw->data=n;
nw->next=NULL;
if(f==NULL)
{
f=nw;
r=nw;
}
else
{
r->next=nw;
r=r->next;
}
}
int dequeue()
{
int n;
struct node *temp;
if(isempty()==1)
printf("queue is empty");
else //OUTPUT
{
temp=f; 1.enqueue
f=f->next; 2.dequeue
n=temp->data; 3.peek
free(temp); 0.exitenter choice1
} enter data:5
}
int peek() 1.enqueue
{ 2.dequeue
return f->data; 3.peek
} 0.exitenter choice3
top element =32723
1.enqueue
Program File:- 2.dequeue
//Program file .c 3.peek
#include<stdio.h> 0.exitenter choice2
#include"dyqueue.h" dequeue element=0
int main() 1.enqueue
{ 2.dequeue
int ch,no; 3.peek
init(); 0.exitenter choice0
do
{
printf("\n1.enqueue \n2.dequeue \n3.peek
\n0.exit"); printf("enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter data:");
scanf("%d",&no);
enqueue(no);
break;
case 2:if(isempty()==1)
printf("\n queue is empty");
else
printf("dequeue element=%d",dequeue());
break;
case 3:printf("top element =%d",peek());
break;
case 0:break;
}
}
while(ch!=0);
}
Slip:-7
Q1.
Solution:-

#include <stdio.h>
#include <stdlib.h>

// Function to partition the array and return the pivot index


int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (arr[j] <= pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// Swap arr[i+1] and arr[high]


int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

return i + 1;
}

// Function to perform quick sort


void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);

quickSort(arr, low, pivotIndex - 1);


quickSort(arr, pivotIndex + 1, high);
}
}

int main() {
int n;
printf("Enter the number of integers: ");
scanf("%d", &n);

if (n <= 0) {
printf("Invalid input. Number of integers must be greater than
0.\n");
return 1;
}

int arr[n];

printf("Enter %d integers:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Perform quick sort


quickSort(arr, 0, n - 1);

printf("Sorted Array in Ascending Order:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}

//output
/*
Enter the number of integers: 5
Enter 5 integers:
25
23
35
25
21
Sorted Array in Ascending Order:
21 23 25 25 35
*/
Q2.
Solution:-
Header File:- cststack
// Header File .h
#include<stdio.h>
char s[20];
int top;
void init()
{
top=-1;
}
int isempty()
{
if(top==-1)
return 1;
else return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char data)
{
if(isfull()==1)
printf("\nStack is full ");
else
{
top++;
s[top]=data;
}
}
char pop()
{
char data;
if(isempty()==1)
printf("\nStack is empty ");
else
{
data=s[top];
top--;
return data;
}
}
int peek()
{
return s[top];
}

Program File:-
#include<stdio.h> //output
#include"cststack.h"
#include<string.h> enter string ShreeRam
void main() The string is not
{ palindrome
char str[20]; OR
int count=0,i; enter string12321
char ch; The string is palindrome
printf("enter string");
scanf("%s",str);
init();
for(i=0;i<=str[i]!='\0';i++)
{
push(str[i]);
}
for (i=0;i<=strlen(str)/2;i++)
{
ch = pop();
if(ch!=str[i])
{
count++;
break;
}
}
if(count==0)
{
printf("The string is palindrome");
}
else
{
printf("The string is not palindrome");
}
}
Slip:-8
Q1.
Solution:-
Header File:- singlylist.h
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *f;
void create()
{
int n,i;
struct node *s;
printf("Enter how many nodes ");
scanf("%d",&n);

f= (struct node *)malloc(sizeof(struct node));


printf("Enter data ");
scanf("%d",&f->data);
s=f;
for(i=1;i<n;i++)
{
s->next=(struct node*)malloc(sizeof(struct node)); s=s->next;
printf("Enter data ");
scanf("%d",&s->data);
}
s->next=NULL;
}
void display()
{ struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf("| %d |-> ",s->data);
}
}
void Delete()
{ int p,cnt=0,i;
struct node *temp,*s;
printf("Enter position to delete a node ");
scanf("%d",&p);
for(s=f;s!=NULL;s=s->next)
{ cnt++;
//output
}
/*
if(p==1)
1.Create
{
2.Display
temp=f;
3.Delete
f=f->next;
0.ExitEnter choice 1
free(temp);
Enter how many nodes 5
}
Enter data 3
else if(p==cnt)
Enter data 6
{
Enter data 9
for(i=1,s=f;i<p-1;i++)
Enter data 12
{ s=s->next;
Enter data 15
}
temp=s->next;
1.Create
s->next=NULL;
2.Display
free(temp);
3.Delete
}
0.ExitEnter choice 2
else if(p>1 && p<cnt)
| 3 |-> | 6 |-> | 9 |-> | 12 |-> |
{
15 |->
for(i=1,s=f;i<p-1;i++)
1.Create
{ s=s->next;
2.Display
}
3.Delete
temp=s->next;
0.ExitEnter choice 3
s->next=temp->next;
Enter position to delete a node 5
free(temp);
}
1.Create
2.Display
else
3.Delete
printf("Invalid Position ");
0.ExitEnter choice 2
}
| 3 |-> | 6 |-> | 9 |-> | 12 |->
1.Create
Program File:- 2.Display
#include<stdio.h> 3.Delete
#include<stdlib.h> 0.ExitEnter choice 0
#include"singlylist.h" */
int main()
{
int ch;
do
{
printf("\n1.Create\n2.Display\n3.Delete \n0.Exit");
printf("Enter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:Delete();
break;
case 0:break;
default:printf("\nInvalid choice");
}
}
while(ch!=0);
}

Q2.
Solution:-
#include <stdio.h>
char s[20];
int top;
void init()
{
top=-1;
//output
}
int isempty() enter string Shree Ram
{ second string :Shree
if(top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char ch)
{
if(isfull()==1)
printf("stack is full");
else
{
top++;
s[top]=ch;
}
}
char pop()
{
char ch;
if(isempty()==1)
printf("stack is empty");
else
{
ch=s[top];
top--;
}
return ch;
}
int main() {
int i,k=0;
char temp[20];
init();
char str[20];
printf("enter string ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
push(str[i]);
}

while(!isempty())
{
temp[k]=pop();
k++;
}
temp[k]='\0';
printf("second string :");
for(i=0;temp[i]!='\0';i++)
{
push(temp[i]);
}

while(!isempty())
{
printf("%c",pop());
}
}
Slip:-9
Q1.
Solution:-
Header File:- stack.h
#include<stdio.h>
char s[20];
int top;
void init()
{
top=-1;
}
int isempty()
{
if(top==-1)
return 1;
else return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char data)
{
if(isfull()==1)
printf("\nStack is full ");
else
{
top++;
s[top]=data;
}
}
char pop()
{
char data;
if(isempty()==1)
printf("\nStack is empty ");
else
{ data=s[top];
top--;
return data;
}
}
int peek()
{
return s[top];
}

Program File:-
#include<stdio.h>
#include "stack.h"
int priority(char ch)
{ //OUTPUT
switch(ch)
{ Enter the infix expression
case '(':return 0; (A+B)*(C+D)
case '+':
case '-':return 1; Postfix string = AB+CD+*
case '*':
case '/':return 2;
case '^':
case '$':return 3;
}
return 0;
}
void convert(char str[20])
{
int i,j=0;
char post[20],ch,ch1;
init();
for(i=0;str[i]!='\0';i++)
{ ch=str[i];
switch(ch)
{
case '(':push(ch);
break;
case '+':
case '-':
case '*':
case '/':
case '$':
case '^':
while(!isempty() && (priority(peek())>=priority(ch))) {
post[j]= pop(); j++;
}
push(ch);
break;
case ')': while((ch1=pop())!='(')
{ post[j]=ch1; j++;
}
break;
default:post[j]=ch;
j++;
}
}
while(!isempty())
{
post[j]=pop();
j++;
}
post[j]='\0';
printf("\n Postfix string = %s ",post);
}
int main()
{
char infix[20];
printf("\nEnter the infix expression ");
scanf("%s",infix);
convert(infix);
}

Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[20];
int age;
}emp[10];
int readFile(struct employee a[])
{
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp, "%19s %d", a[i].name, &a[i].age);
i++;
}
}
return i-1;
}
void quicksort(struct employee a[10],int lb,int ub)
{
int i,j;
struct employee key,temp;
if(lb<ub)
{
i=lb+1;
key=a[lb];
j=ub;

while(i<=j)
{
while(a[i].age<=key.age && i<=ub)
i++;
while(a[j].age>key.age && j>=lb)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[lb];
a[lb]=temp;
quicksort(a,lb,j-1);
quicksort(a,j+1,ub);
}
}
void writeFile(struct employee a[],int n)
{
int i=0;
FILE *fp;
if((fp=fopen("sortedemp_quick_age.txt","w"))!=NULL) {

for(i=0;i<n;i++)
{
fprintf(fp,"%s %d\n",a[i].name,a[i].age);
}
}
}
int main()
{
int n;
n=readFile(emp);
if(n==-1)
printf("File not found ");
else
{
quicksort(emp,0,n-1);
writeFile(emp,n);
printf("File Sorted ");
}
}

//output
/*
File Sorted
file name=sortedemp_quick_age.txt
*/
Slip:-10
Q1.
Solution:-
Header File:- st_queue.h
#ifndef ST_QUEUE_H
#define ST_QUEUE_H

#define MAX_QUEUE_SIZE 100

typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
int size;
} Queue;

// Function to initialize a queue


void init(Queue* Q);

// Function to add an element to the queue


int add(Queue* Q, int value);

// Function to peek at the front element of the queue


int peek(const Queue* Q);

#endif

//output
Program File:- Adding elements to the
#include <stdio.h> queue:
#include "st_queue.h" Added: 1
Added: 2
// Function to initialize a queue Added: 3
void init(Queue* Q) { Added: 4
Q->front = 0; Added: 5
Q->rear = -1;
Q->size = 0; Peeking at the front
} element: 1
Adding one more
// Function to add an element to the element to the queue: 6
queue Added: 6
int add(Queue* Q, int value) {
if (Q->size >= MAX_QUEUE_SIZE) {
return 0; // Queue is full
}

Q->rear = (Q->rear + 1) % MAX_QUEUE_SIZE;


Q->data[Q->rear] = value;
Q->size++;
return 1; // Successfully added
}

// Function to peek at the front element of the queue


int peek(const Queue* Q) {
if (Q->size == 0) {
return -1; // Queue is empty
}

return Q->data[Q->front];
}

int main() {
Queue myQueue;
init(&myQueue);

printf("Adding elements to the queue:\n");


for (int i = 1; i <= 5; i++) {
add(&myQueue, i);
printf("Added: %d\n", i);
}

printf("\nPeeking at the front element: %d\n", peek(&myQueue));

printf("Adding one more element to the queue: 6\n");


if (add(&myQueue, 6)) {
printf("Added: 6\n");
} else {
printf("Queue is full. Cannot add 6.\n");
}

return 0;
}
Q2. //output
Solution:-
#include<stdio.h> File is found
#include<stdlib.h>
#include<string.h> Sorted
struct employee filename=bsort.txt
{
char name[20];
int age;
}emp[10];

int readfile(struct employee a[10])


{
int i=0;
FILE*fp;
if((fp=fopen("empl.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",a[i].name ,&a[i].age);
i++;
}
}
return i-1;
}
void writefile(struct employee a[],int n)
{
int i;
FILE *fp;
if((fp=fopen("bsort.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
{
fprintf(fp,"%s %d \n",a[i].name ,a[i].age);
}
}
}
void bubblesort(struct employee a[],int n)
{
int i,j;
struct employee temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(strcmp(a[j].name,a[j+1].name)>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int n;
n=readfile(emp);
if(n==-1)
printf("File is not found");
else
{
bubblesort(emp,n);
writefile(emp,n);
printf("File is found");
}

}
Slip:-11
Q1.
Solution:-
#include<stdio.h>
#include<string.h> //output
typedef struct city Enter city name
{ Nashik
char cname[20];
int stdcode; STD Code
}rec; 12345
rec city[100];
int read_file(rec *a)
{
int i=0;
FILE *fp;
if((fp=fopen("cities.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",a[i].cname,&a[i].stdcode);
i++;
}
}
return (i-1);
}
void l_search(rec *a,int n,char x[20])
{
int i;
for(i=0;i<n;i++)
{
if(strcmp(a[i].cname,x)==0)
{
printf("\nSTD Code\n%d\n",a[i].stdcode);
break;
}
}
if(i==n)
printf("\nCity not in the list\n");
}

void main()
{
char x[20];
int n;
n=read_file(city);
printf("Enter city name\n");
gets(x);
l_search(city,n,x);
}

Q2.
Solution:-
Header File:- PriorityQ.h
#include<stdio.h>
int Q[20];
int f,R;
void init()
{
f=R=-1;
}
int isempty()
{
if(f==R)
return 1;
else
return 0;
}
int isfull()
{
if(R==19)
return 1;
else return 0;
}
void Add(int no)
{
int i;
if(isfull()==1)
printf("Queue is Full ");
else
{
for(i=R;i>f;i--)
{ if(no<Q[i])
Q[i+1]=Q[i];
else
break;
}
Q[i+1]=no;
R++;
}
}
int Delete()
{
int no;
if(isempty()==1)
printf("Queue is empty ");
else
{
f++;
no=Q[f];
}
return no;
}
void display()
{
int i;
for(i=f+1;i<=R;i++)
{
printf("%d ",Q[i]);
}
}

Program File:-
#include<stdio.h>
#include "PriorityQ.h"
main()
{
int n,ch;
init();
do
{
printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit");
printf("\nEnter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter element ");
scanf("%d",&n);
Add(n);
break;
case 2:if(isempty()==1)
printf("\nQueue is empty ");
else
printf("deleted elemet =%d ",Delete());
break;
case 3:display();
break;
case 0:break;
default:printf("\nInvalid choice ");
}
}while(ch!=0);
}

//output
/*
1.Add
2.Delete
3.Display
0.EXit
Enter choice 1

Enter element 5

1.Add
2.Delete
3.Display
0.EXit
Enter choice 3
5

1.Add
2.Delete
3.Display
0.EXit
Enter choice 2
deleted elemet =5

1.Add
2.Delete
3.Display
0.EXit
Enter choice 3
*/
Slip:-12
Q1.
Solution:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct city
//output
{
char name[20]; Enter city name to
int code; search Nashik
}ct[10]; city is found and code is
int readFile(struct city a[]) 41
{
int i=0;
FILE *fp;
if((fp=fopen("city.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",&a[i].name,&a[i].code);
i++;
}
}
return i-1;
}
void linearsearch(struct city a[10],int n,char sr[20])
{ int i,p,cnt=0;
for(i=0;i<n;i++)
{

if(strcmp(a[i].name,sr)==0)
{ p=i; //store position
cnt++;
break;
}
}
if(cnt>=1)
printf("city is found and code is %d ",a[p].code);
else
printf("city NOT found ");
}
main()
{ int n;
char sr[20];
n=readFile(ct);
if(n==-1)
printf("File not found ");
else
{
printf("Enter city name to search ");
scanf("%s",sr);
linearsearch(ct,n,sr);
}
}

Q2.
Solution:-
Header File:- cir_queue.h
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *r;
void init()
{
r=NULL;
}
int isempty()
{
if(r==NULL)
return 1;
else
return 0;
}
void Add(int n)
{
struct node *nw;
nw=(struct node *)malloc(sizeof(struct node));
nw->data=n;
if(r==NULL)
{
r=nw;
r->next=r;
}
else
{
nw->next=r->next; Program File:-
r->next=nw;
r=r->next; #include<stdio.h>
} #include "cir_queue.h"
} main()
int Delete() {
{ int ch,no;
int no; init();
struct node *temp; do
temp=r->next; {
if(r==temp->next) printf("\n1.Add \n2.Delete
{ \n0.Exit");
r=NULL; printf("\nEnter choice");
} scanf("%d",&ch);
else switch(ch)
{ {
r->next=temp->next; case 1:printf("\n Enter
} element");
no=temp->data; scanf("%d",&no);
free(temp); Add(no);
return (no); break;
} case 2:if(isempty()==1)
int peek() printf("\n Queue is empty");
{ else
return r->next->data; {
} printf("deleted element is
%d",Delete());
//output
1.Add break;
2.Delete }
0.Exit case 0:break;
Enter choice1 case 4:printf("Elemenent at
Enter element5 peek %d ",peek());
break;
1.Add
2.Delete default:printf("Invalid choice");
0.Exit }
Enter choice2 }
deleted element is 5 while(ch!=0);
}
1.Add
2.Delete
0.Exit
Enter choice0
Slip:-13
Q1.
Solution:- Header File:- sstac.h
#include<stdio.h>
char s[20];
int top;
void init() {
top=-1;
}
int isempty()
{
if(top==-1)
return 1;
else return 0;
}
int isfull() { //output
if(top==19)
return 1;
1.push
else
2.pop
return 0; 3.chech stack is empty or not
} 4.chech stack is full or not
void push(char data) { 5.Peek
if(isfull()==1)
0.exit
printf("\nStack is full ");
eneter your choice 1
else enter elements5
{ top++;
s[top]=data; eneter your choice 2
}
deleted elements :5
}
char pop() { eneter your choice 3
char data; stack is empty
if(isempty()==1)
printf("\nStack is empty ");
eneter your choice 4
else stack is not full
{
data=s[top]; eneter your choice 5
top--;
top of elements:0
return data;
} eneter your choice 0
}
int peek()
{
return s[top];
}
Program File:-
#include<stdio.h>
#include<stdlib.h>
#include"sstack.h"
main()
{
int n,i=0,ch;
init();
do
{
printf("\n1.push \n2.pop \n3.chech stack is empty or not \n4.chech
stack is full or not \n5.Peek \n0.exit");

printf("\neneter your choice ");


scanf("%d",&ch);

switch(ch)
{
case 1: printf("enter elements");
scanf("%d",&n);
push(n);
break;
case 2:printf("\ndeleted elements :%d",pop());
break;

case 3:if(isempty()==1)
printf("stack is empty");
else
printf("stack is not empty");
break;

case 4:if(isfull()==1)
printf("stack is full");
else
printf("stack is not full");
break;

case 5:printf("\ntop of elements:%d",peek());


break;
case 0: break;
}
}while(ch!=0);
}
Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *f;
void create()
{
int i,n;
struct node *s;
printf("\nEnter no of nodes ");
scanf("%d",&n);
f=(struct node *)malloc(sizeof(struct node));
printf("\n Enter node ");
scanf("%d",&f->data);
s=f;

for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node));
s=s->next;
printf("\n Enter node ");
scanf("%d",&s->data);
}
s->next=NULL;

}
void display()
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf("\t %d ->",s->data);
}
}
void sort()
{
struct node *p,*q;
int temp;
for(p=f;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data > q->data)
{ temp = p->data;
p->data = q->data;
q->data = temp;

}
}
}
}
main()
{
create();
printf("\n Link list is : ");
display();
printf("\n After sorting Link list is = ");
sort();
display();
}

//output
/*
Enter no of nodes 5

Enter node 6

Enter node 9

Enter node 8

Enter node 7

Enter node 2

Link list is : 6 -> 9 -> 8 -> 7 -> 2 ->


After sorting Link list is = 2 -> 6 -> 7 -> 8 -> 9 ->

*/
Slip:-14
Q1.
Solution:-
#include<stdio.h>
void linearsearch(int a[10],int n,int sr)
{
int i,p,cnt=0;
for(i=0;i<n;i++)
{
if(a[i]==sr)
{
p=i; //store position
cnt++;
break;
}
}
if(cnt>=1)
printf("element found at %d position",p);
else
printf("element NOT found ");
}
main() //output
{
int n,i,sr,a[10];
enter how many values5
printf("enter how many values"); enter values6
scanf("%d",&n); enter values5
for(i=0;i<n;i++) enter values4
{
enter values3
printf("enter values");
enter values2
scanf("%d",&a[i]);
} enter search element3
printf("\n enter search element"); element found at 3 position
scanf("%d",&sr);
linearsearch(a,n,sr);
}

Q2.
Solution:- Header File:- dstqueue.h

#ifndef DSTQUEUE_H
#define DSTQUEUE_H

#define MAX_QUEUE_SIZE 10
typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
int count;
} Deque;

void initDeque(Deque* Q);


int isFull(const Deque* Q);
void addFront(Deque* Q, int value);
int getRear(const Deque* Q);
int deleteRear(Deque* Q);

#endif

Program File:-
#include "dstqueue.h" //Output
#include <stdio.h>
void initDeque(Deque* Q) { Is the queue full? No
Q->front = -1; Rear element: 10
Q->rear = -1; Deleted rear element: 10
Q->count = 0;
}
int isFull(const Deque* Q) {
return (Q->count == MAX_QUEUE_SIZE);
}
int isEmpty(const Deque* Q) {
return (Q->count == 0);
}
void addFront(Deque* Q, int value) {
if (!isFull(Q)) {
if (Q->front == -1) {
Q->front = 0;
Q->rear = 0;
} else if (Q->front == 0) {
Q->front = MAX_QUEUE_SIZE - 1;
} else {
Q->front--;
}
Q->data[Q->front] = value;
Q->count++;
} else {
printf("Queue is full. Cannot add to the front.\n");
}
}
int getRear(const Deque* Q) {
if (!isEmpty(Q)) {
return Q->data[Q->rear];
} else {
printf("Queue is empty. Cannot get the rear element.\n");
return -1; // You can choose a better way to handle this
}
}
int deleteRear(Deque* Q) {
if (!isEmpty(Q)) {
int deletedValue = Q->data[Q->rear];
if (Q->front == Q->rear) {
Q->front = -1;
Q->rear = -1;
} else if (Q->rear == 0) {
Q->rear = MAX_QUEUE_SIZE - 1;
} else {
Q->rear--;
}
Q->count--;
return deletedValue;
} else {
printf("Queue is empty. Cannot delete from the rear.\n");
return -1; // You can choose a better way to handle this
}
}
int main() {
Deque myQueue;
initDeque(&myQueue);

printf("Is the queue full? %s\n", isFull(&myQueue) ? "Yes" : "No");

addFront(&myQueue, 10);
addFront(&myQueue, 20);
addFront(&myQueue, 30);

printf("Rear element: %d\n", getRear(&myQueue));


printf("Deleted rear element: %d\n", deleteRear(&myQueue));
return 0;
}
Slip:-15
Q1.
Solution:-
#include<stdio.h>
main()
{
int i,a[10],n,min,pos,j,temp;
printf("Enter how many elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
printf("\nBefore array sorting ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
for(j=0;j<n-1;j++)
{
min=a[j]; //output
pos=j;
for(i=j+1;i<n;i++) Enter how many elements 5
{
if(a[i]<=min) Before array sorting 83 86 77 15 93
{ Sorted array is 15 77 83 86 93
min=a[i];
pos=i;
}
}
temp=a[j];
a[j]=min;
a[pos]=temp;
}
printf("\nSorted array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Q2.
Solution:- Header File:- cir_queue.h
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *r;
void init()
{
r=NULL;
}
int isempty()
{
if(r==NULL)
return 1;
else
return 0;
}
void Add(int n)
{
struct node *nw;
nw=(struct node *)malloc(sizeof(struct node));
nw->data=n;
if(r==NULL)
{
r=nw;
r->next=r;
}
else
{
nw->next=r->next;
r->next=nw;
r=r->next;
}
}
int Delete()
{
int no;
struct node *temp;
temp=r->next;
if(r==temp->next)
{
r=NULL; Program File:-
}
#include<stdio.h>
else
#include "cir_queue.h"
{
main()
r->next=temp->next;
{
}
int ch,no;
no=temp->data;
init();
free(temp);
do
return (no);
{
}
printf("\n1.Add \n2.Delete
int peek()
\n0.Exit");
{
printf("Enter choice");
return r->next->data;
scanf("%d",&ch);
}
switch(ch)
{
case 1:printf("\n Enter
element");
scanf("%d",&no);
Add(no);
break;
case 2:if(isempty()==1)
printf("\n Queue is empty");
else
//output {
printf("deleted element is
1.Add %d",Delete());
2.Delete break;
0.ExitEnter choice1 }
case 0:break;
Enter element5 case 4:printf("Elemenent at peek
%d ",peek());
1.Add break;
2.Delete default:printf("Invalid choice");
0.ExitEnter choice2 }
deleted element is 5 }while(ch!=0);
1.Add
2.Delete }
0.Exit
Enter choice0
Slip:-16
Q1.
Solution:-
#include<stdio.h>
void countingsort(int a[20],int n,int k)
{
int count[50],b[30],i;
for(i=0;i<=k;i++)
{
count[i]=0;
}
for(i=0;i<n;i++)
{
++count[a[i]];
}
for(i=1;i<=k;i++)
{
count[i]=count[i]+count[i-1];
}
for(i=n-1;i>=0;i--)
{
b[--count[a[i]]]=a[i];
//output
}
for(i=0;i<n;i++)
Enter how many elements 5
{
a[i]=b[i];
Before sort array is 3 6 7 5 3
}
Afer sorting array is 3 3 5 6 7
}
int main()
{
int a[20],n,i,max;
printf("Enter how many elements ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
a[i]=rand()%10;
}
printf("\n Before sort array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
countingsort(a,n,max);
printf("\n Afer sorting array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

Q2.
Solution:-
#include<stdio.h>
#include<string.h>
char s[20];
int top;
void init()
{
top=-1;
} //output
int isempty()
{
Enter postfix string (2+3)*(4+5)
if(top==-1)
Enter value of ( 5
return 1;
Enter value of 2 5
else return 0;
Enter value of 3 3
}
Enter value of ) 1
isfull()
Enter value of ( 8
{
Enter value of 4 9
if(top==19)
Enter value of 5 4
return 1;
Enter value of ) 7
else
Ans =7
return 0;
}
void push(char data)
{
if(isfull()==1)
printf("\nStack is full ");
else
{ top++;
s[top]=data;
}
}
char pop()
{ char data;
if(isempty()==1)
printf("\nStack is empty ");
else
{ data=s[top];
top--;
return data;
}
}
void postfix_eval(char str[20])
{
int i,op1,op2,val;
for(i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '+': op2=pop();
op1=pop();
push(op1+op2); break;
case '-': op2=pop();
op1=pop();
push(op1-op2); break;
case '*': op2=pop();
op1=pop();
push(op1*op2); break;
case '/': op2=pop();
op1=pop();
push(op1/op2); break;
default:printf("Enter value of %c ",str[i]); scanf("%d",&val);
push(val);

}
}
printf("Ans =%d ",pop());
}
int main()
{
char str[20];
printf("Enter postfix string ");
scanf("%s",str);
postfix_eval(str);
}
Slip:-17
Q1.
Solution:-Header File- singlylis.h
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *f;
void create()
{
struct node *s;
int n,i;
printf("Enter how many nodes");
scanf("%d",&n);
f=(struct node *)malloc(sizeof(struct node));
printf("Enter data");
scanf("%d",&f->data);
s=f;
for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node));
s=s->next;
printf("Enter data");
//output
scanf("%d",&s->data);
}
Enter how many nodes5
s->next=NULL;
Enter data6
}
Enter data4
void display()
Enter data2
{
Enter data3
struct node *s;
Enter data9
for(s=f;s!=NULL;s=s->next)
6 ->4 ->2 ->3 ->9 ->9 ->3 -
{
>2 ->4 ->6 ->
printf("%d ->",s->data);
}
}
void reverse()
{
int cnt=0,i;
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
cnt++; Program File :- .C
}
while(cnt>0) #include<stdio.h>
{ #include"singlylist.h"
for(i=1,s=f;i<cnt;i++) main()
{ {
s=s->next; create();
} display();
printf("%d ->",s->data); reverse();
cnt--; }
}
}

Q2.
Solution:-
#include <stdio.h>
char s[20];
int top;
void init()
{
top=-1;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char ch)
{
if(isfull()==1)
printf("stack is full");
else
{
top++;
s[top]=ch;
}
}
char pop()
{
//output
char ch;
enter string Shree RAm
if(isempty()==1)
second string :Shree
printf("stack is empty");
else
{
ch=s[top];
top--;

}
return ch;
}
main()
{
int i,k=0;
char temp[20];
init();
char str[20];
printf("enter string ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
push(str[i]);
}

while(!isempty())
{
temp[k]=pop();
k++;
}
temp[k]='\0';
printf("second string :");
for(i=0;temp[i]!='\0';i++)
{
push(temp[i]);
}
while(!isempty())
{
printf("%c",pop());
}
}
Slip:-18
Q1.
Solution:-
#include<stdio.h>
main()
{
int i,a[10],n,min,pos,j,temp;
printf("Enter how many elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
printf("\nBefore array sorting ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]); //output
}
for(j=0;j<n-1;j++) Enter how many elements 5
{
min=a[j]; Before array sorting 83 86 77
pos=j; 15 93
for(i=j+1;i<n;i++) Sorted array is 15 77 83 86 93
{
if(a[i]<=min)
{
min=a[i];
pos=i;
}
}
temp=a[j];
a[j]=min;
a[pos]=temp;
}
printf("\nSorted array is ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coeff,exp;
struct node *next;
};
struct node* create(struct node *f)
{
int i,n;
struct node *s;
printf("\nEnter no of terms ");
scanf("%d",&n);
printf("Enter term in descending order of power "); f=(struct node
*)malloc(sizeof(struct node));
printf("\n Enter coeff ");
scanf("%d",&f->coeff);
printf("\n Enter power ");
scanf("%d",&f->exp);
s=f;
for(i=1;i<n;i++)
{
s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;
printf("\n Enter coeff ");
scanf("%d",&s->coeff);
printf("\n Enter power ");
scanf("%d",&s->exp);
}
s->next=NULL;
return f;
}
void display(struct node *f)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf("%dx^%d ->",s->coeff,s->exp);
}
}
int length(struct node *p)
{
int len=0;
struct node *s;
for(s=p;s!=NULL;s=s->next)
{
len++;
}
return len;
}
struct node* Mult(struct node *p1,struct node *p2)
{
struct node *t1,*t2,*t3=NULL,*nw;
struct node *p3;

for(t1=p1;t1!=NULL;t1=t1->next)
{
for(t2=p2;t2!=NULL;t2=t2->next)
{
nw=(struct node*)malloc(sizeof(struct node)); nw->next=NULL;
nw->coeff=t1->coeff*t2->coeff;
nw->exp=t1->exp+t2->exp;
if(t3==NULL)
{
p3=nw;
t3=nw;
}
else
{
t3->next=nw;
t3=t3->next;
}
}
}
return p3;
}
main()
{ struct node *p1=NULL,*p2=NULL,*p3=NULL;
p1=create(p1);
p2=create(p2);

printf("\n 1st Polynomial is : ");


display(p1);
printf("\n 2nd Polynomial is : ");
display(p2);
p3=Mult(p1,p2);
printf("\n Multiplication of 2 Polynomial is "); display(p3);
}
//output

/*
Enter no of terms 5
Enter term in descending order of power
Enter coeff 10
Enter power 2
Enter coeff 12
Enter power 2
Enter coeff 36
Enter power 2
Enter coeff 65
Enter power 1
Enter coeff 32
Enter power 3

Enter no of terms 2
Enter term in descending order of power
Enter coeff 10
Enter power 1
Enter coeff 20
Enter power 2

1st Polynomial is : 10x^2 ->12x^2 ->36x^2 ->65x^1 ->32x^3 -


>
2nd Polynomial is : 10x^1 ->20x^2 ->
Multiplication of 2 Polynomial is 100x^3 ->200x^4 ->120x^3 -
>240x^4 ->360x^3 ->720x^4 ->650x^2 ->1300x^3 ->320x^4 -
>640x^5 ->

*/
Slip:-19
Q1.
Solution:-
#include<stdio.h>
main() {
int i,a[10],n,min,pos,j,temp;
printf("Enter how many elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
printf("\nBefore array sorting ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
//output
}
Enter how many elements 5
for(j=0;j<n-1;j++)
{
Before array sorting 83 86 77
min=a[j];
15 93
pos=j;
Sorted array is 15 77 83 86 93
for(i=j+1;i<n;i++)
{
if(a[i]<=min)
{
min=a[i];
pos=i;
}
}
temp=a[j];
a[j]=min;
a[pos]=temp;
}
printf("\nSorted array is ");
for(i=0;i<n;i++) {
printf("%d ",a[i]);
}
}
Q2.
Solution:-
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node *next;
struct Node *prev;
} Node;

Node *head = NULL;


Node *tail = NULL;

// Inserts a new node into the doubly linked list in ascending order.
void insert(int data) {
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
new_node->prev = NULL;

if (head == NULL) {
head = new_node;
tail = new_node;
return;
}

Node *current = head;

while (current != NULL && current->data < data) {


current = current->next;
}

if (current == head) {
new_node->next = head;
head->prev = new_node;
head = new_node;
} else if (current == NULL) {
new_node->prev = tail;
tail->next = new_node;
tail = new_node;
} else {
new_node->next = current;
new_node->prev = current->prev;
current->prev->next = new_node;
current->prev = new_node;
}
}

// Searches for an integer in the doubly linked list.


Node *search(int data) {
Node *current = head;

while (current != NULL && current->data < data) {


current = current->next;
}

if (current != NULL && current->data == data) {


return current;
} else {
return NULL;
}
}

// Displays the doubly linked list in ascending order.


void displayAscending() {
Node *current = head;

while (current != NULL) {


printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
// Insert some integers into the doubly linked list in ascending order.
insert(10);
insert(5);
insert(20);
insert(15);

// Display the doubly linked list in ascending order.


printf("Doubly Linked List (Ascending): ");
displayAscending();

// Search for an integer in the doubly linked list.


int searchValue = 15;
Node *node = search(searchValue);
if (node != NULL) {
printf("The integer %d was found in the doubly linked list.\n",
searchValue);
} else {
printf("The integer %d was not found in the doubly linked list.\n",
searchValue);
}

return 0;
}

//output
/*
Doubly Linked List (Ascending): 5 10 15 20
The integer 15 was found in the doubly linked list.
*/
Slip:-20
Q1.
Solution:-Header File:- sstack.h
#include<stdio.h>
char s[20];
int top;
void init() {
top=-1;
}
int isempty() {
if(top==-1)
return 1;
else return 0;
}
int isfull() {
if(top==19)
return 1;
else
return 0;
//output
}
void push(char data) {
1.push
if(isfull()==1)
2.pop
printf("\nStack is full ");
3.chech stack is empty or not
else
4.chech stack is full or not
{
5.Peek
top++;
0.exit
s[top]=data;
eneter your choice 1
}
enter elements 5
}
char pop() {
eneter your choice 2
char data;
deleted elements :5
if(isempty()==1)
printf("\nStack is empty ");
eneter your choice 3
else
stack is empty
{
data=s[top];
eneter your choice 4
top--;
stack is not full
return data;
}
eneter your choice 5
}
top of elements:0
int peek()
{
eneter your choice 0
return s[top];
}
Program File:-
#include<stdio.h>
#include<stdlib.h>
#include"sstack.h"
main()
{
int n,i=0,ch;
init();
do
{
printf("\n1.push \n2.pop \n3.chech stack is empty or not \n4.chech
stack is full or not \n5.Peek \n0.exit");
printf("\neneter your choice ");
scanf("%d",&ch);

switch(ch)
{
case 1: printf("enter elements");
scanf("%d",&n);
push(n);
break;
case 2:printf("\ndeleted elements :%d",pop());
break;

case 3:if(isempty()==1)
printf("stack is empty");
else
printf("stack is not empty");
break;

case 4:if(isfull()==1)
printf("stack is full");
else
printf("stack is not full");
break;

case 5:printf("\ntop of elements:%d",peek());


break;
case 0: break;
}
}while(ch!=0);
}
Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next,*prev;
};
struct node *f;
void create()
{
struct node *s;
int i,n;
printf("enter how many nodes");
scanf("%d",&n);
f=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&f->data);
s=f;
for(i=1;i<n;i++)
{
s->next=(struct node*)malloc(sizeof(struct node)); s=s->next;
printf("enter data");
scanf("%d",&s->data);
}
s->next=f;
}
void display()
{
struct node *s;
printf("\nCircular linked list is::");
s=f;
do
{
printf("%d->",s->data);
s=s->next;
}
while(s!=f);
}
void append()
{
struct node *nw,*s;
int n,i;
printf("\nenter how many new nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nw=(struct node*)malloc(sizeof(struct node));
printf("\nenter new node of data");
scanf("%d",&nw->data);
s=f;
do
{
s=s->next;
}while(s->next!=f);

s->next=nw;
nw->next=f;
}
}
main()
{
create();
display();
append();
display();
}

//output
/*
enter how many nodes5
enter data3
enter data6
enter data9
enter data8
enter data7

Circular linked list is::3->6->9->8->7->


enter how many new nodes2

enter new node of data21

enter new node of data23

Circular linked list is::3->6->9->8->7->21->23->


*/
Slip:-21
Q1.
Solution:-
Header File :-cststack.h
#include<stdio.h>
char s[20];
int top;
void init()
{
top==-1;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char ch)
{
if(isfull()==1)
printf("Stack is full");
else
{
top++;
s[top]=ch;
}
}
char pop()
{
char ch;
if(isempty()==1)
printf("Stack is empty");
else
{
ch=s[top];
top--;
return ch;
}
}

Program File:-
#include<stdio.h>
#include"cststack.h"
int main()
{
init();
char str[20];
int i;
printf("Enter String: ");
scanf("%s",&str);
for(i=0;str[i]!='\0';i++)
{
push(str[i]);
}
printf("Reversed string: ");
while(!isempty())
{
printf("%c",pop());
}
}

//Output
/*
Enter String: Microsoft
Reversed string: tfosorciM
*/
Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[20];
int age;
}emp[10];
int readfile(struct employee a[])
{
int i=0;
FILE*fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",&a[i].name,&a[i].age);
i++;
}
}
return i-1;
}
void InsertionSort(struct employee a[],int n)
{
int i,j;
struct employee key;
for(i=1; i<n; i++)
{
key=a[i];
for(j=i-1; j>=0; j--)
{
if(strcmp(a[j].name,key.name)>0)
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=key;
}
}
void writefile(struct employee a[],int n)
{
int i=0;
FILE*fp;
if((fp=fopen("insertionsort.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
{
fprintf(fp,"%s %d\n",a[i].name,a[i].age);
}
}
}
int main()
{
int n;
n=readfile(emp);
if(n==-1)
printf("File Not Found");
else
{
InsertionSort(emp,n);
writefile(emp,n);
printf("File Sorted");
}
}

//Output
/*
File sorted
file name=insertionsort.txt
*/
Slip:-22
Q1.
Solution:-
Header File :- st_queue.h
#ifndef ST_QUEUE_H
#define ST_QUEUE_H
#define MAX_QUEUE_SIZE 10
typedef struct
{
int data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;

void init(Queue *Q);


int isFull(const Queue *Q);
int isEmpty(const Queue *Q);
void AddQueue(Queue *Q, int x);
int DeleteQueue(Queue *Q);

#endif

Program File:-
#include <stdio.h>
#include "st_queue.h"
void init(Queue *Q)
{
Q->front = -1;
Q->rear = -1;
}
int isFull(const Queue *Q) {
return (Q->rear == MAX_QUEUE_SIZE - 1);
}
int isEmpty(const Queue *Q) {
return (Q->front == -1);
}
void AddQueue(Queue *Q, int x) {
if (isFull(Q)) {
printf("Queue is full. Cannot add %d.\n", x);
return;
}
if (isEmpty(Q)) {
Q->front = 0;
}
Q->rear++;
Q->data[Q->rear] = x;
}
int DeleteQueue(Queue *Q) {
int deletedValue;

if (isEmpty(Q)) {
printf("Queue is empty. Cannot delete.\n");
return -1; // You can choose a better way to handle this.
}

deletedValue = Q->data[Q->front];

if (Q->front == Q->rear) {
// If there is only one element in the queue.
Q->front = -1;
Q->rear = -1;
} else {
Q->front++;
//output
}
Queue contents: 10 20 30
Queue contents after adding 40: 40
return deletedValue;
}
int main() {
Queue myQueue;
init(&myQueue);
AddQueue(&myQueue, 10);
AddQueue(&myQueue, 20);
AddQueue(&myQueue, 30);

printf("Queue contents: ");


while (!isEmpty(&myQueue)) {
printf("%d ", DeleteQueue(&myQueue));
}
printf("\n");
AddQueue(&myQueue, 40);
printf("Queue contents after adding 40: ");
while (!isEmpty(&myQueue)) {
printf("%d ", DeleteQueue(&myQueue));
}
printf("\n");

return 0;
}
Q2.
Solution:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct city
{
char name[20];
int code;
}ct[10];
int readFile(struct city a[])
{
int i=0;
FILE *fp;
if((fp=fopen("city.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",&a[i].name,&a[i].code);
i++;
}
}
return i-1;
}
void linearsearch(struct city a[10],int n,char sr[20])
{ int i,p,cnt=0;
for(i=0;i<n;i++)
{

if(strcmp(a[i].name,sr)==0)
{ p=i; //store position
cnt++;
break;
}
}
if(cnt>=1)
printf("city is found and code is %d ",a[p].code);
else
printf("city NOT found ");
}
main()
{ int n;
char sr[20];
n=readFile(ct);
if(n==-1)
printf("File not found ");
else
{
printf("Enter city name to search ");
scanf("%s",sr);
linearsearch(ct,n,sr);
}
}

//Output
/*
Enter city name to search Nashik
city is found and code is 41
*/
Slip 23
Q1.
Solution
Header File :- PriorityQ.h
#include<stdio.h>
int Q[20];
int f,R;
void init() {
f=R=-1;
}
isempty()
{
if(f==R)
return 1;
else //Output
return 0;
} 1.Add
int isfull() 2.Delete
{ 3.Display
if(R==19) 0.EXit
return 1; Enter choice 1
else return 0;
} Enter element 23
void Add(int no) {
int i; 1.Add
if(isfull()==1) 2.Delete
printf("Queue is Full "); 3.Display
else 0.EXit
{ Enter choice 3
for(i=R;i>f;i--) 23
{
if(no<Q[i]) 1.Add
Q[i+1]=Q[i]; 2.Delete
else 3.Display
break; 0.EXit
} Enter choice 2
Q[i+1]=no; deleted elemet =23
R++;
} 1.Add
} 2.Delete
int Delete() 3.Display
{ 0.EXit
int no; Enter choice 0
if(isempty()==1)
printf("Queue is empty ");
else
{
f++;
no=Q[f];
}
return no;
}
void display()
{
int i;
for(i=f+1;i<=R;i++)
{
printf("%d ",Q[i]);
}
}

Program File:-
#include<stdio.h>
#include "PriorityQ.h"
main()
{
int n,ch;
init();
do
{
printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit");
printf("\nEnter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter element "); scanf("%d",&n);
Add(n);
break;
case 2:if(isempty()==1)
printf("\nQueue is empty "); else
printf("deleted elemet =%d ",Delete());
break;
case 3:display();
break;
case 0:break;
default:printf("\nInvalid choice "); }
}while(ch!=0);
}
Q2.
Solution:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct city
{
char name[20];
int code;
}ct[10];
int readFile(struct city a[])
{
int i=0;
FILE *fp;
if((fp=fopen("sortedfile.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",&a[i].name,&a[i].code); i++;
}
}
return i-1;
}
int binarysearch(struct city a[10],int lb,int ub,char sr[20])
{
int mid=0;
while(lb<=ub)
{
mid=(lb+ub)/2;
if(strcmp(a[mid].name,sr)==0)
return mid;
else if(strcmp(sr,a[mid].name)<0)
ub=mid-1;
else
lb=mid+1;
}
return -1;
}
main()
{
int n,p;
char sr[20];
n=readFile(ct);
if(n==-1)
printf("File not found ");
else
{
printf("Enter city name to search ");
scanf("%s",sr);
p=binarysearch(ct,0,n,sr);
if(p>=0)
printf("\nCity is found and code =%d ",ct[p].code); else
printf("\nCity not found ");
}
}

//Output
/*
Enter city name to search Nashik

City is found and code =4321


*/
Slip 24
Q1.
Solution
Header File:- cir_queue.h
#ifndef CIR_QUEUE_H
#define CIR_QUEUE_H

typedef struct Node {


int data;
struct Node* next;
} Node;

typedef struct {
Node* front;
Node* rear;
} Queue;

void init(Queue* Q);


void AddQueue(Queue* Q, int x);
int isEmpty(const Queue* Q);

#endif

Program file:-
#include <stdio.h>
#include <stdlib.h>
#include "cir_queue.h"
void init(Queue* Q) {
Q->front = NULL;
Q->rear = NULL;
}

void AddQueue(Queue* Q, int x) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = x;
newNode->next = NULL;

if (Q->rear == NULL) {
Q->front = newNode;
Q->rear = newNode;
newNode->next = Q->front;
} else {
Q->rear->next = newNode;
Q->rear = newNode;
Q->rear->next = Q->front;
}
}

int isEmpty(const Queue* Q) {


return (Q->front == NULL);
}

int main() {
Queue myQueue;
init(&myQueue);

int choice, x;

do {
printf("\nMenu:\n");
printf("1. Add to Queue\n");
printf("2. Check if Queue is Empty\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter element to add to the queue: ");
scanf("%d", &x);
AddQueue(&myQueue, x);
printf("Element %d added to the queue.\n", x);
break;
case 2:
if (isEmpty(&myQueue)) {
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}
break;
case 0:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 0);
return 0;
}

//Output
/*
Menu:
1. Add to Queue
2. Check if Queue is Empty
0. Exit
Enter your choice: 1
Enter element to add to the queue: 3
Element 3 added to the queue.

Menu:
1. Add to Queue
2. Check if Queue is Empty
0. Exit
Enter your choice: 2
Queue is not empty.

Menu:
1. Add to Queue
2. Check if Queue is Empty
0. Exit
Enter your choice: 0
Exiting the program.
*/
Q2.
Solution:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[20];
int age;
}emp[10];
int readfile(struct employee a[])
{
int i=0;
FILE*fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",&a[i].name,&a[i].age);
i++;
}
}
return i-1;
}
void InsertionSort(struct employee a[],int n)
{ int i,j;
struct employee key;
for(i=1; i<n; i++)
{
key=a[i];
for(j=i-1; j>=0; j--)
{
if(strcmp(a[j].name,key.name)>0)
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=key;
}
}
void writefile(struct employee a[],int n)
{
int i=0;
FILE*fp;
if((fp=fopen("insertionsort.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
{
fprintf(fp,"%s %d\n",a[i].name,a[i].age);
}
}
}
int main()
{
int n;
n=readfile(emp);
if(n==-1)
printf("File Not Found");
else
{
InsertionSort(emp,n);
writefile(emp,n);
printf("File Sorted");
}
}

//Output
/*
File Sorted
File Name=insertionsort.txt
*/
Slip:25
Q1.
Solution:-
#include <stdio.h>
#include <stdbool.h>
#define MAX_STACK_SIZE 100
typedef struct
{
//Output
char data[MAX_STACK_SIZE];
int top;
Infix Expression: (a*(b+c)*((d-
} Stack;
a)/b))
Postfix Notation: abc+*da-b/*
void initStack(Stack *S) {
S->top = -1;
}

bool isEmpty(const Stack *S) {


return (S->top == -1);
}

bool isFull(const Stack *S) {


return (S->top == MAX_STACK_SIZE - 1);
}

void push(Stack *S, char ch) {


if (!isFull(S)) {
S->top++;
S->data[S->top] = ch;
} else {
printf("Stack overflow\n");
}
}
char pop(Stack *S) {
char ch;
if (!isEmpty(S)) {
ch = S->data[S->top];
S->top--;
return ch;
} else {
printf("Stack underflow\n");
return '\0';
}
}
int precedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
bool isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}
void infixToPostfix(const char *infix) {
Stack operatorStack;
initStack(&operatorStack);

printf("Postfix Notation: ");


for (int i = 0; infix[i] != '\0'; i++) {
char ch = infix[i];
if (isalnum(ch)) {
printf("%c", ch);
} else if (ch == '(') {
push(&operatorStack, ch);
} else if (ch == ')') {
while (!isEmpty(&operatorStack) &&
operatorStack.data[operatorStack.top] != '(') {
printf("%c", pop(&operatorStack));
}
if (!isEmpty(&operatorStack) &&
operatorStack.data[operatorStack.top] == '(') {
pop(&operatorStack);
}
} else if (isOperator(ch)) {
while (!isEmpty(&operatorStack) &&
precedence(operatorStack.data[operatorStack.top]) >= precedence(ch))
{
printf("%c", pop(&operatorStack));
}
push(&operatorStack, ch);
}
}

while (!isEmpty(&operatorStack)) {
printf("%c", pop(&operatorStack));
}
printf("\n");
}
int main() {
char infix[] = "(a*(b+c)*((d-a)/b))";
printf("Infix Expression: %s\n", infix);
infixToPostfix(infix);
return 0;
}

Q2.
Solution:-
Header File :- stack.h
#include<stdio.h>
char s[20];
int top;
void init()
{
top=-1;
}
int isempty()
{
if(top==-1)
return 1;
else return 0;
}
int isfull()
{
if(top==19)
return 1;
else
return 0;
}
void push(char data)
{
if(isfull()==1)
printf("\nStack is full ");
else
{
top++;
s[top]=data;
}
}
char pop()
{
char data;
if(isempty()==1)
printf("\nStack is empty ");
else
{
data=s[top];
top--;
return data;
}
}
int peek()
{
return s[top];
}

Program file:-
#include<stdio.h>
#include "stack.h"
int priority(char ch)
{
switch(ch)
{
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':
case '$':return 3;
}
return 0;
}
void convert(char str[20])
{
int i,j=0;
char post[20],ch,ch1;
init();
for(i=0;str[i]!='\0';i++)
{
ch=str[i];
switch(ch)
{
case '(':push(ch);
break;
case '+':
case '-':
case '*':
case '/':
case '$':
case '^':
while(!isempty() && (priority(peek())>=priority(ch)))
{
post[j]= pop();
j++;
}
push(ch);
break;
case ')':while((ch1=pop())!='(')
{
post[j]=ch1; j++;
}
break;
default:post[j]=ch;
j++;
}
}
while(!isempty())
{
post[j]=pop();
j++;
}
post[j]='\0';
printf("\n Postfix string = %s ",post);
}
main()
{
char infix[20];
printf("\nEnter the infix expression ");
scanf("%s",infix);
convert(infix);
}

//OUTPUT
/*
Enter the infix expression (A+B)*(C+D)

Postfix string = AB+CD+*


*/
Slip:26
Q1.
Solution:-
Header File:- ststack.h
#ifndef STSTACK_H
#define STSTACK_H
#define MAX_STACK_SIZE 100
typedef struct
{
int data[MAX_STACK_SIZE];
int top;
} Stack;
void init(Stack *S);
Stack* Push(Stack *S, int x);
int isEmpty(const Stack *S);

#endif

Program File:-
#include <stdio.h>
#include "ststack.h"
void init(Stack *S)
{
S->top = -1;
}
int isFull(const Stack *S) {
return (S->top == MAX_STACK_SIZE - 1);
}
int isEmpty(const Stack *S) {
return (S->top == -1); //OUTPUT
}
Stack* Push(Stack *S, int x) { Pushing elements onto the
if (!isFull(S)) { stack:
S->top++; 12345
S->data[S->top] = x; Is the stack empty? No
} Popping elements from the
return S; stack:
} 54321
int main() Is the stack empty? Yes
{
Stack myStack;
init(&myStack);
printf("Pushing elements onto the stack:\n");
for (int i = 1; i <= 5; i++) {
Push(&myStack, i);
printf("%d ", i);
}
printf("\n");
printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :
"No");
printf("Popping elements from the stack:\n");
while (!isEmpty(&myStack)) {
printf("%d ", myStack.data[myStack.top]);
myStack.top--;
}
printf("\n");
printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :
"No");
return 0;
}

Q2.
Solution:-
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;

Node* insert(Node* head, int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;

if (head == NULL || value < head->data) {


newNode->next = head;
head = newNode;
} else {
Node* current = head;
while (current->next != NULL && current->next->data < value) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
return head;
}
Node* search(Node* head, int value) {
Node* current = head;
while (current != NULL && current->data < value) {
current = current->next;
}
if (current != NULL && current->data == value) {
return current;
} else {
return NULL;
}
}
void display(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data); //OUTPUT
current = current->next;
} Ordered Linked List: 5 10
printf("\n"); 15 20
} Element 15 found in the
int main() { linked list.
Node* head = NULL;
head = insert(head, 10);
head = insert(head, 5);
head = insert(head, 20);
head = insert(head, 15);
printf("Ordered Linked List: ");
display(head);

int searchValue = 15;


Node* result = search(head, searchValue);
if (result != NULL) {
printf("Element %d found in the linked list.\n", searchValue);
} else {
printf("Element %d not found in the linked list.\n", searchValue);
}
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
Slip:27
Q1.
Solution:-
#include <stdio.h>
#include <stdlib.h>
typedef struct Term
{
int coefficient;
int exponent;
struct Term* next;
} Term;

Term* createTerm(int coefficient, int exponent) {


Term* newTerm = (Term*)malloc(sizeof(Term));
if (newTerm == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}

void insertTerm(Term** poly, int coefficient, int exponent) {


Term* newTerm = createTerm(coefficient, exponent);
if (*poly == NULL || exponent > (*poly)->exponent) {
newTerm->next = *poly;
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL && exponent < current->next-
>exponent) {
current = current->next;
}
newTerm->next = current->next;
current->next = newTerm;
}
}
Term* addPolynomials(Term* poly1, Term* poly2) {
Term* result = NULL;

while (poly1 != NULL && poly2 != NULL) {


if (poly1->exponent > poly2->exponent) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
} else if (poly1->exponent < poly2->exponent) {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
} else {
int sum_coeff = poly1->coefficient + poly2->coefficient;
if (sum_coeff != 0) {
insertTerm(&result, sum_coeff, poly1->exponent);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != NULL) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
}

while (poly2 != NULL) {


insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}
return result;
}
void displayPolynomial(Term* poly) {Polynomial 1: 3x^2 + 4x^1 + 2
Polynomial 2: 5x^3 + 1x^2 -4x^1
Result: 5x^3 + 4x^2 + 2
if (poly == NULL) {
printf("0");
} else {
while (poly != NULL) {
printf("%d", poly->coefficient);
if (poly->exponent > 0) {
printf("x^%d", poly->exponent);
}
if (poly->next != NULL) {
if (poly->next->coefficient > 0) {
printf(" + ");
} else {
printf(" ");
}
}
poly = poly->next;
}
}
printf("\n");
}
//output
void freePolynomial(Term* poly) {
while (poly != NULL) { Polynomial 1: 3x^2 + 4x^1
Term* temp = poly; +2
poly = poly->next; Polynomial 2: 5x^3 + 1x^2
free(temp); -4x^1
} Result: 5x^3 + 4x^2 + 2
}
int main() {
Term* poly1 = NULL;
Term* poly2 = NULL;

// Add terms to the first polynomial


insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 4, 1);
insertTerm(&poly1, 2, 0);

// Add terms to the second polynomial


insertTerm(&poly2, 5, 3);
insertTerm(&poly2, 1, 2);
insertTerm(&poly2, -4, 1);

printf("Polynomial 1: ");
displayPolynomial(poly1);

printf("Polynomial 2: ");
displayPolynomial(poly2);

Term* result = addPolynomials(poly1, poly2);

printf("Result: ");
displayPolynomial(result);

// Free memory
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);

return 0;
}
Q2. //Output
Solution:-
#include <stdio.h> Data sorted and written to
#include <stdlib.h> 'sortedemponname.txt'
#include <string.h> file
Name=sortedemponname.txt
// Structure to represent an employee
typedef struct Employee {
char name[100];
int id;
} Employee;

// Function to perform Counting Sort based on employee names


void countingSort(Employee arr[], int n) {
Employee output[n];
int count[256] = {0};

for (int i = 0; i < n; i++) {


count[(int)arr[i].name[0]]++;
}

for (int i = 1; i < 256; i++) {


count[i] += count[i - 1];
}

for (int i = n - 1; i >= 0; i--) {


output[count[(int)arr[i].name[0]] - 1] = arr[i];
count[(int)arr[i].name[0]]--;
}

for (int i = 0; i < n; i++) {


arr[i] = output[i];
}
}

// Function to merge two subarrays based on employee names


void merge(Employee arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

Employee leftArr[n1], rightArr[n2];

for (int i = 0; i < n1; i++) {


leftArr[i] = arr[left + i];
}

for (int i = 0; i < n2; i++) {


rightArr[i] = arr[mid + i + 1];
}

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {


if (strcmp(leftArr[i].name, rightArr[j].name) <= 0) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = leftArr[i];
i++;
k++;
}

while (j < n2) {


arr[k] = rightArr[j];
j++;
k++;
}
}

// Function to perform Merge Sort based on employee names


void mergeSort(Employee arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

int main() {
// Open the input file for reading
FILE *inputFile = fopen("employee.txt", "r");
if (inputFile == NULL) {
printf("Error: Cannot open input file.\n");
return 1;
}

int n;
fscanf(inputFile, "%d", &n);

Employee employees[n];

// Read data from the input file


for (int i = 0; i < n; i++) {
fscanf(inputFile, "%s %d", employees[i].name, &employees[i].id);
}

// Close the input file


fclose(inputFile);

// Sort the data by names using Counting Sort


countingSort(employees, n);

// Open the output file for writing


FILE *outputFile = fopen("sortedemponname.txt", "w");
if (outputFile == NULL) {
printf("Error: Cannot open output file.\n");
return 1;
}

// Write the sorted data to the output file


fprintf(outputFile, "%d\n", n);
for (int i = 0; i < n; i++) {
fprintf(outputFile, "%s %d\n", employees[i].name,
employees[i].id);
}

// Close the output file


fclose(outputFile);

printf("Data sorted and written to 'sortedemponname.txt'\n");

return 0;
}
Slip 28
Q1.
Solution:-
Header file:- ststack.h
#ifndef STSTACK_H
#define STSTACK_H

#define MAX_STACK_SIZE 100

typedef struct {
int data[MAX_STACK_SIZE];
int top;
} Stack;

void init(Stack *S);


int isEmpty(const Stack *S);
int isFull(const Stack *S);
void push(Stack *S, int x);
int pop(Stack *S);
int top(const Stack *S);

#endif

Program File:-
#include <stdio.h> //output
#include "ststack.h"
Is the stack empty? No
void init(Stack *S) { Is the stack full? No
S->top = -1; Top element: 5
} Popping elements from the
stack:
int isEmpty(const Stack *S) { 54321
return (S->top == -1); Is the stack empty? Yes
}

int isFull(const Stack *S) {


return (S->top == MAX_STACK_SIZE - 1);
}

void push(Stack *S, int x) {


if (!isFull(S)) {
S->top++;
S->data[S->top] = x;
}
}

int pop(Stack *S) {


int x;
if (!isEmpty(S)) {
x = S->data[S->top];
S->top--;
return x;
} else {
return -1; // Stack is empty
}
}
int top(const Stack *S) {
if (!isEmpty(S)) {
return S->data[S->top];
} else {
return -1; // Stack is empty
}
}

int main() {
Stack myStack;
init(&myStack);
for (int i = 1; i <= 5; i++) {
push(&myStack, i);
}

printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :


"No");

printf("Is the stack full? %s\n", isFull(&myStack) ? "Yes" : "No");


printf("Top element: %d\n", top(&myStack));
printf("Popping elements from the stack:\n");
while (!isEmpty(&myStack)) {
printf("%d ", pop(&myStack));
}
printf("\n");

printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" :


"No");

return 0;
}
Q.2.
Solution:-
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee {
char name[100];
int age;
} Employee;

void merge(Employee arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

Employee leftArr[n1], rightArr[n2];

for (int i = 0; i < n1; i++) {


leftArr[i] = arr[left + i];
}

for (int i = 0; i < n2; i++) {


rightArr[i] = arr[mid + i + 1];
}

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i].age <= rightArr[j].age) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(Employee arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int partition(Employee arr[], int low, int high) {
int pivot = arr[high].age;
int i = (low - 1);

for (int j = low; j < high; j++) {


if (arr[j].age < pivot) {
i++;
Employee temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Employee temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

void quickSort(Employee arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

int main() {
FILE *inputFile = fopen("employee.txt", "r");
if (inputFile == NULL) {
printf("Error: Cannot open input file.\n");
return 1;
}
int n;
fscanf(inputFile, "%d", &n);
Employee employees[n];
for (int i = 0; i < n; i++) {
fscanf(inputFile, "%s %d", employees[i].name, &employees[i].age);
}
fclose(inputFile);
Employee mergeSortEmployees[n];
Employee quickSortEmployees[n];

for (int i = 0; i < n; i++) {


mergeSortEmployees[i] = employees[i];
quickSortEmployees[i] = employees[i];
}
mergeSort(mergeSortEmployees, 0, n - 1);
quickSort(quickSortEmployees, 0, n - 1);

FILE *outputFile = fopen("sortedemponage.txt", "w");


if (outputFile == NULL) {
printf("Error: Cannot open output file.\n");
return 1;
}

fprintf(outputFile, "%d\n", n);

for (int i = 0; i < n; i++) {


fprintf(outputFile, "%s %d\n", mergeSortEmployees[i].name,
mergeSortEmployees[i].age);
}

fclose(outputFile);

printf("Data sorted and written to 'sortedemponage.txt'\n");

return 0;
}

//output
/*
Data sorted and written to 'sortedemponage.txt'
file name=sortedemponage.txt
*/
Slip 29
Q1.
Solution:-
Header File:- ststack.h
#ifndef STSTACK_H
#define STSTACK_H

#define MAX_STACK_SIZE 100

typedef struct {
int data[MAX_STACK_SIZE];
int top;
} Stack;

void init(Stack *S);


void push(Stack *S, int x);
int peek(const Stack *S);

#endif

Program File:-
#include <stdio.h>
#include "ststack.h"

void init(Stack *S) {


S->top = -1;
}

void push(Stack *S, int x) {


if (S->top < MAX_STACK_SIZE - 1) {
S->data[++S->top] = x;
}
}

int peek(const Stack *S) {


if (S->top >= 0) {
return S->data[S->top];
} else {
return -1; // Stack is empty
}
}
int main() {
Stack myStack;
init(&myStack);

printf("Pushing elements onto the stack:\n");


for (int i = 1; i <= 5; i++) {
push(&myStack, i);
printf("Pushed: %d\n", i);
}

printf("Peek at the top element: %d\n", peek(&myStack));

printf("Popping elements from the stack:\n");


while (peek(&myStack) != -1) {
int x = peek(&myStack);
printf("Popped: %d\n", x);
myStack.top--;
}

return 0;
}

//output
/*
Pushing elements onto the stack:
Pushed: 1
Pushed: 2
Pushed: 3
Pushed: 4
Pushed: 5
Peek at the top element: 5
Popping elements from the stack:
Popped: 5
Popped: 4
Popped: 3
Popped: 2
Popped: 1
*/
Q2.
Solution:-
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a node in the circular list


typedef struct Node {
int data;
struct Node* next;
} Node;

// Function to create a new node with the given data


Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to append a new element to the end of the circular list


Node* append(Node* last, int data) {
Node* newNode = createNode(data);
if (last == NULL) {
newNode->next = newNode;
last = newNode;
} else {
newNode->next = last->next;
last->next = newNode;
last = newNode;
}
return last;
}

// Function to display the circular list


void display(Node* last) {
if (last == NULL) {
printf("The list is empty.\n");
return;
}
Node* current = last->next;
do {
printf("%d ", current->data);
current = current->next;
} while (current != last->next);
printf("\n");
}

int main() {
Node* last = NULL;
int n, data;
printf("Enter the number of integers to append: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter integer %d: ", i + 1);
scanf("%d", &data);
last = append(last, data);
}
printf("Circular List Contents: ");
display(last);

// Free memory
if (last != NULL) {
Node* current = last->next;
do {
Node* temp = current;
current = current->next;
free(temp);
} while (current != last->next);
}

//output
/*
Enter the number of integers to append: 5
Enter integer 1: 45
Enter integer 2: 25
Enter integer 3: 69
Enter integer 4: 32
Enter integer 5: 21
Circular List Contents: 45 25 69 32 21
free(): double free detected in tcache 2
Aborted (core dumped)
*/
return 0;
}
Slip 30
Q1.
Solution:-
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node in the linked list


typedef struct Node {
int data;
struct Node* next;
} Node;

// Function to insert a new node at the end of a linked list


Node* insert(Node* head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
return newNode;
}

Node* current = head;


while (current->next != NULL) {
current = current->next;
}
current->next = newNode;

return head;
}

// Function to merge two ordered linked lists while maintaining order


Node* mergeOrderedLists(Node* list1, Node* list2) {
Node* mergedList = NULL;

while (list1 != NULL && list2 != NULL) {


if (list1->data < list2->data) {
mergedList = insert(mergedList, list1->data);
list1 = list1->next;
} else {
mergedList = insert(mergedList, list2->data);
list2 = list2->next;
}
}

while (list1 != NULL) {


mergedList = insert(mergedList, list1->data);
list1 = list1->next;
}

while (list2 != NULL) {


mergedList = insert(mergedList, list2->data);
list2 = list2->next;
}

return mergedList;
}

// Function to display a linked list


void display(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
Node* list1 = NULL;
Node* list2 = NULL;

// Populate the first ordered list


list1 = insert(list1, 1);
list1 = insert(list1, 3);
list1 = insert(list1, 5);

// Populate the second ordered list


list2 = insert(list2, 2);
list2 = insert(list2, 4);
list2 = insert(list2, 6);

// Merge the two ordered lists


Node* mergedList = mergeOrderedLists(list1, list2);
printf("Merged List: ");
display(mergedList);

// Free allocated memory


while (mergedList != NULL) {
Node* temp = mergedList;
mergedList = mergedList->next;
free(temp);
}
return 0;
}

//output
/*
Merged List: 1 2 3 4 5 6
*/

Q2.
Solution:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Structure to represent an employee


typedef struct Employee {
char name[100];
int id;
} Employee;

// Function to perform bubble sort based on employee names


void bubbleSort(Employee employees[], int n) {
int i, j;
Employee temp;

for (i = 0; i < n - 1; i++) {


for (j = 0; j < n - i - 1; j++) {
if (strcmp(employees[j].name, employees[j + 1].name) > 0) {
temp = employees[j];
employees[j] = employees[j + 1];
employees[j + 1] = temp;
}
}
}
}
void selectionSort(Employee employees[], int n) {
int i, j, min;
Employee temp;

for (i = 0; i < n - 1; i++) {


min = i;
for (j = i + 1; j < n; j++) {
if (strcmp(employees[j].name, employees[min].name) < 0) {
min = j;
}
}
if (min != i) {
temp = employees[i];
employees[i] = employees[min];
employees[min] = temp;
}
}
}
int main() {
FILE* inputFile = fopen("employee.txt", "r");
if (inputFile == NULL) {
printf("Error: Cannot open input file.\n");
return 1;
}
int n;
fscanf(inputFile, "%d", &n);
Employee employees[n];

for (int i = 0; i < n; i++) {


fscanf(inputFile, "%s %d", employees[i].name, &employees[i].id);
}
fclose(inputFile);

Employee bubbleSortEmployees[n];
Employee selectionSortEmployees[n];

for (int i = 0; i < n; i++) {


bubbleSortEmployees[i] = employees[i];
selectionSortEmployees[i] = employees[i];
}
bubbleSort(bubbleSortEmployees, n);
selectionSort(selectionSortEmployees, n);

FILE* outputFileBubble = fopen("sortedemponname_bubble.txt", "w");


if (outputFileBubble == NULL) {
printf("Error: Cannot open bubble sort output file.\n");
return 1;
}

FILE* outputFileSelection = fopen("sortedemponname_selection.txt",


"w");
if (outputFileSelection == NULL) {
printf("Error: Cannot open selection sort output file.\n");
return 1;
}

fprintf(outputFileBubble, "%d\n", n);


fprintf(outputFileSelection, "%d\n", n);

for (int i = 0; i < n; i++) {


fprintf(outputFileBubble, "%s %d\n",
bubbleSortEmployees[i].name, bubbleSortEmployees[i].id);
fprintf(outputFileSelection, "%s %d\n",
selectionSortEmployees[i].name, selectionSortEmployees[i].id);
}

fclose(outputFileBubble);
fclose(outputFileSelection);

printf("Data sorted using bubble sort and selection sort and written to
files.\n");

return 0;
}

//output
/*
Data sorted using bubble sort and selection sort and written to
files.
file names=1)sortedemponname_bubble.txt
2)sortedemponname_selection.txt
*/

You might also like