DS - I Slips Solution
DS - I Slips Solution
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
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
#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
#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>
return i + 1;
}
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("\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);
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
typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
int size;
} Queue;
#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
}
return Q->data[Q->front];
}
int main() {
Queue myQueue;
init(&myQueue);
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];
}
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");
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;
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
*/
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;
#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);
addFront(&myQueue, 10);
addFront(&myQueue, 20);
addFront(&myQueue, 30);
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);
/*
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
*/
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>
// 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;
}
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;
}
}
int main() {
// Insert some integers into the doubly linked list in ascending order.
insert(10);
insert(5);
insert(20);
insert(15);
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;
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
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;
#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);
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
typedef struct {
Node* front;
Node* rear;
} Queue;
#endif
Program file:-
#include <stdio.h>
#include <stdlib.h>
#include "cir_queue.h"
void init(Queue* Q) {
Q->front = NULL;
Q->rear = 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 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;
}
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)
#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;
printf("Polynomial 1: ");
displayPolynomial(poly1);
printf("Polynomial 2: ");
displayPolynomial(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;
int i = 0, j = 0, k = left;
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];
return 0;
}
Slip 28
Q1.
Solution:-
Header file:- ststack.h
#ifndef STSTACK_H
#define STSTACK_H
typedef struct {
int data[MAX_STACK_SIZE];
int top;
} Stack;
#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 main() {
Stack myStack;
init(&myStack);
for (int i = 1; i <= 5; i++) {
push(&myStack, i);
}
return 0;
}
Q.2.
Solution:-
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee {
char name[100];
int age;
} Employee;
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;
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];
fclose(outputFile);
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
typedef struct {
int data[MAX_STACK_SIZE];
int top;
} Stack;
#endif
Program File:-
#include <stdio.h>
#include "ststack.h"
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>
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>
if (head == NULL) {
return newNode;
}
return head;
}
return mergedList;
}
int main() {
Node* list1 = NULL;
Node* list2 = NULL;
//output
/*
Merged List: 1 2 3 4 5 6
*/
Q2.
Solution:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Employee bubbleSortEmployees[n];
Employee selectionSortEmployees[n];
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
*/