1). WAP to exchanging values of two variable with
pointer.
#include<stdio.h>
#include<conio.h>
int swap (int x,int y);
void main()
{
int
x,y;
clrscr();
printf("\n\t
enter the value of no1=");
scanf("%d",&x);
printf("\n\t
enter the value of no2=");
scanf("%d",&y);
swap(x,y);
getch();
}
int swap (int x,int y)
{
int
c;
c=x;
x=y;
y=c;
printf("\n\t
the value of no1=%d",x);
printf("\n\t
the value of no2=%d",y);
return
0;
}
OUTPUT
enter the value of no1=10
enter the value of no2=15
the value of
no1=15
the value of
no1=10
2).WAP for finding
the MAX and MIN number from N number.
#include<stdio.h>
#include<conio.h>
void main()
{
int
n,i,a[' '],max,min,*p;
clrscr();
printf("\n
insert the no. whitch u want :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\t
enter data=");
scanf("%d",&a[i]);
}
p=&a[0];
max=*p;
min=*p;
for(i=0;i<n;i++,p++)
{
if(max
< *p)
{
max=*p;
}
if(min
> *p)
{
min=*p;
}
}
printf("\n
maximum value is :-->%d",max);
printf("\n
minimum value is :-->%d",min);
getch();
}
OUTPUT
insert the no.
whitch u want :5
10
20
30
90
100
maximum value
is :-->100
minimum value
is :-->10
3). WAP to take array
and manipulate difference value with increment and
Decrement of pointer.
#include<conio.h>
#include<stdio.h>
void main()
{
int
i,a[10],n,*p;
clrscr();
printf("\n\t
enter size of array:-");
scanf("%d",&n);
p=&a[0];
for(i=0;i<n;i++)
{
printf("\n
enter element:-");
scanf("%d",p);
p++;
}
p=p-n;
printf("\n\t
before increment or decrement.....\n");
for(i=0;i<n;i++)
{
printf("\n\t
%d",*p);
p++;
}
p=p-n;
printf("\n\t
after the increment..... \n");
for(i=0;i<n;i++)
{
printf("\n\t
%d",++(*p));
p++;
}
printf("\n\t
before the decrement....\n");
p=p-n;
for(i=0;i<n;i++)
{
printf("\n\t
%d",--(*p));
p++;
}
getch();
}
OUTPUT
enter size of array:-3
enter element:-12
enter element:-52
enter element:-30
before increment or decrement.....
12
52
30
after the increment.....
13
53
31
before the decrement....
12
52
30
4). WAP to perform Copy, length & concatenate on given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char h[100],hh[100],final[200];
int i,n,n1,c=0,j,ch;
do
{
printf("\n\t
***********start****************");
printf("\n\t
1). copy of string");
printf("\n\t
2). length of given string");
printf("\n\t
3). concatenate string");
printf("\n\t
4). exit");
printf("\n\t
************end*****************");
printf("\n\t
enter your choice from given menu:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
printf("\n\t enter string:-");
scanf("%s",&h);
n=strlen(h);
for(i=0;i<n;i++)
{
hh[i]=h[i];
}
hh[i]=NULL;
puts(hh);
break;
case
2:
printf("\n\t enter string:-");
scanf("%s",&h);
n=strlen(h);
for(i=0;i<n;i++)
{
c++;
}
printf("\n\t total character is:-%d",c);
break;
case
3:
printf("\n\t enter string:-");
scanf("%s",&h);
n=strlen(h);
printf("\n\t enter string:-");
scanf("%s",&hh);
n1=strlen(hh);
for(i=0;i<n;i++)
{
final[i]=h[i];
}
final[i]=' ';
for(j=0;j<n1;j++)
{
final[i+1+j]=hh[j];
}
final[i+1+j]=NULL;
puts(final);
break;
case
4:
printf("\n\t
program exit");
break;
default:
printf("\n\t
your choice is wrong");
}
}while(ch!=4);
getch();
}
OUTPUT:-
enter the string :-->MAHESH
1.
length of string :
2. copy
one string to another :
3.
concatinat two string :
enter the choice:1
length
is 7
5). WAP to sort an
alphabet in a string using pointer.
#include<conio.h>
#include<stdio.h>
sort(char *p);
int main()
{
char
n[' '],t;
clrscr();
printf("enter
the string=");
gets(n);
sort(n);
getch();
return
0;
}
sort(char *p)
{
char
t;
int
i=0,j,l=0;
while(*(p+i)!='\0')
{
l++;
i++;
}
for(i=0;i<l;i++)
{
for(j=i+1;j<l;j++)
{
if(*(p+i)>*(p+j))
{
t=*
(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
}
printf("sorted
string is=");
for(i=0;i<l;i++)
{
printf("%c",*(p+i));
}
getch();
return
0;
}
OUTPUT:
Enter the string:- BHUMI
BHIMU
6).WAP using structure with pointer.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int
roll_no;
char
name[20];
}s1;
void main()
{
s1
*s,temp;
int
i,n;
clrscr();
printf("\n\t
enter no of student:->");
scanf("%d",&n);
s=(s1*)malloc(n*sizeof(s1));
printf("\n\t
enter information..\n");
printf("\n\t
------------------------\n");
for(i=0;i<n;i++)
{
printf("\n\t
enter roll no:->");
scanf("%d",&s->roll_no);
printf("\n\t
enter name:->");
scanf("%s",&s->name);
s++;
}
s=s-n;
printf("\n\t
roll no:\t name:");
for(i=0;i<n;i++)
{
printf("\n");
printf("\n\t
%d \t\t %s",s->roll_no,s->name);
s++;
}
getch();
}
OUTPUT
insert the no which u want:3
insert the roll no :32
insert the name:KHUSHBU
insert the the
semester:3
insert the
division:1
insert the roll no :57
insert the name:SNEHAL
insert the the
semester:4
insert the
division:2
insert the roll no :23
insert the name:NIDHI
insert the the
semester:6
insert the
division:3
~~~~~~~~~~~~~~~student
information~~~~~~~~~~~~~~~~~~~~~~~
roll no:46
student
name:KHUSHBU
semester:3
division:1
roll no:57
student
name:SNEHAL
semester:4
division:2
roll no:23
student
name:NIDHI
semester:6
division:3
7). WAP to implement
a program for pointer to pointer.
#include<conio.h>
#include<stdio.h>
void main()
{
int *p,**q,a[' '],n,i;
clrscr();
printf("\n Insert The Value :=->\n");
scanf("%d",&n);
p=&a[0];
**q=*p;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Array Is
:=->\n");
for(i=0;i<n;i++)
{
printf("%d\t%u",*(p+i));
printf("\n");
}
getch();
}
OUTPUT
insert the value :-->
5 1 6
9 7 8
array
is:-->
1
6
9
7
8
8). WAP to check wether the
string is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int palindrome(char *a);
main()
{
char
a[100];
clrscr();
printf("\n\n
Enter String");
scanf("%s",&a);
if(palindrome(a)==0)
printf("\n\n
Given String Is Palindrome");
else
printf("\n\n
Given String Is Not Palindrome");
getch();
}
int palindrome(char *a)
{
char
c[100];
strcpy(c,a);
c==strrev(a);
if(strcmp(a,c)==0)
return 0;
else
return
1;
}
OUTPUT
enter any string=madam
inputed string
is palindrom
9). W.A.P to perform push, pop, peep and update operation on stack.
#include<stdio.h>
#include<conio.h>
#define max 5
int stack[max],top=-1;
void push();
void pop();
void peep();
void update();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t====================================");
printf("\n\t
1).insert");
printf("\n\t
2).delete");
printf("\n\t
3).search");
printf("\n\t
4).update");
printf("\n\t
5).display");
printf("\n\t
6>.exit");
printf("\n\t=====================================");
printf("\n\t
enter your choice:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
{
push();
display();
break;
}
case
2:
{
pop();
display();
break;
}
case
3:
{
peep();
printf("\n\t=============================");
display();
break;
}
case
4:
{
update();
printf("\n\t==============================");
display();
break;
}
case
5:
{
display();
break;
}
case
6:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong please enter proper choice.......");
break;
}
}
}while(ch!=6);
getch();
}
void push()
{
int
data;
if(top==max-1)
{
printf("\n\t
stack is overflow...........");
}
else
{
top++;
printf("\n\t
enter data:-");
scanf("%d",&data);
stack[top]=data;
}
}
void pop()
{
if(top==-1)
{
printf("\n\t
stack is underflow...........");
}
else
{
printf("\n\t
%d is delete",stack[top]);
top--;
}
}
void peep()
{
int
d,f=0,i;
if(top==-1)
{
printf("\n\t
stack is underflow..........");
}
else
{
printf("\n\t
enter no you want to search it:-");
scanf("%d",&d);
for(i=0;i<=top;i++)
{
if(i==d)
{
printf("\n\t
stack[%d]=%d",i,stack[i]);
f=1;
break;
}
else
{
f=0;
}
}
}
if(f==0)
{
printf("\n\t
this no stack is not available......");
}
}
void update()
{
int
d,value,f=0,i;
if(top==-1)
{
printf("\n\t
stack is underflow.........");
}
else
{
printf("\n\t
enter position you want to update:-");
scanf("%d",&d);
for(i=0;i<=top;i++)
{
if(i==d)
{
printf("\n\t
enter value:-");
scanf("%d",&value);
stack[i]=value;
f=1;
break;
}
else
{
f=0;
}
}
}
if(f==0)
{
printf("\n\t
this no stack is not available.....");
}
}
void display()
{
int
i;
if(top==-1)
{
printf("\n\t
stack is underflow");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n\t
stack[%d]=%d",i,stack[i]);
}
}
}
OUTPUT
MENU
=====
1.Push
2.Pop
3.Peep
4.Update
5.display
6.Exit
Enter your choice:-->1
Push
Operation selected
Enter
value to be Pushed::11
Stack values
stack[1]=11
10). WAP to evaluate
postfix expression.
#include<stdio.h>
#include<conio.h>
#define size 10
int top=size;
int stack=[size];
void push(int);
int pop();
void main()
{
char
postfix[100],c;
int
i=0,r1,r2,r,x;
printf("enter
exp");
scanf("%s",&postfixed);
while(postfix[i]!='\0')
{
switch(c):
{
case '+':
r1=pop();
r2=pop();
r=r2+r1;
push(r);
break;
case '-':
r1=pop();
r2=pop();
r=r2-r1;
push(r);
break;
case '*':
r1=pop();
r2=pop();
r=r2*r1;
push(r);
break;
case '/':
r1=pop();
r2=pop();
r=r2/r1;
push(r);
break;
default:
printf("enter
the value of%c",c);
scanf("%d",&x);
push(x);
}
i++;
}
void
push(int x)
{
int
(top==0)
{
printf("stack is fall");
}
Else
{
top=top-1;
stack[top]=x;
}
}
int
pop()
{
int
x;
if(top==size)
{
printf("stack is empty");
}
Else
{
x=stack[top];
top=top+1;
}
Return
(x);
}
11).
WAP to convert infix expression
to postfix.
#include<conio.h>
#include<stdio.h>
void main()
{
int
top=-1,i;
char
in[50],stack[50];
clrscr();
printf("\n\t
enter notation:-");
gets(in);
stack[++top]='(';
printf("\n\t
postfix:\n");
for(i=0;in[i]!=NULL;i++)
{
switch
(in[i])
{
case
'(':
{
stack[++top]='(';
break;
}
case
')':
{
while(stack[top]!='(')
{
printf("%c",stack[top--]);
}
top--;
break;
}
case
'^':
{
while(stack[top]=='^')
{
printf("%c",stack[top--]);
}
stack[++top]=in[i];
break;
}
case
'*':
case
'/':
{
while(stack[top]=='^'
|| stack[top]=='*' || stack[top]=='/')
{
printf("%c",stack[top--]);
}
stack[++top]=in[i];
break;
}
case
'+':
case
'-':
{
while(stack[top]=='^'
|| stack[top]=='*' || stack[top]=='/' || stack[top]=='+' || stack[top]=='-')
{
printf("%c",stack[top--]);
}
stack[++top]=in[i];
break;
}
default:
{
printf("%c",in[i]);
break;
}
}
}
while(stack[top]!='(')
{
printf("%c",stack[top--]);
}
getch();
}
12). Write a program to convert
infix expression to prefix.
#include<stdio.h>
#include<conio.h>
void push();
int
pri(char);
void print();
char
stack[50],infix[50],ans[50];
int
i,top=0,j,a,to;
void main()
{
clrscr();
printf("\n Enter Ur Infix Notation :");
for(i=1;(infix[i]=getchar())!='\n';i++)
infix[0]='(';
infix[i]=')';
for(a=i;a>=0;a--)
{
if(toupper(infix[a])>=65
&& toupper(infix[a])<=180) ans[j++]=infix[a];
else
{
if(infix[a]=='(')
{
while(stack[top]!=')')
if(stack[top]!=')')
ans[j++]=stack[top--];
top--;
}
else
{
if(infix[a]!=')')
{
while(stack[to]!=')')
{
if(pri(stack[top])>pri(infix[a]))
ans[j++]=stack[top--];
to--;
}
}
push();
}
to=top;
}
}
print();
getch();
}
int pri(char
op)
{
if(top=='^') return (3);
else if(op=='*' || op=='/') return (2);
else if(op=='+' || op=='-') return (1);
}
void push()
{
stack[++top]=infix[a];
return;
}
void print()
{
printf("\n\n\t The Prifix Notation
Is :");
for(i=j-1;i>=0;i--)
printf("%c",ans[i]);
return;
}
OUTPUT
enter ur
infix notation: a+b-c
the prifix
notation is :-+abc
13). WAP to merge two stacks.
#include<conio.h>
#include<stdio.h>
#define max1 5
#define max2 6
#define max3 11
int s1[max1],s2[max2],s3[max3];
int top1=-1,top2=-1,top3=-1;
void push1();
void push2();
void display();
void merge();
void push();
void pop();
void peep();
void update();
void disp();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t=================================");
printf("\n\t
1>.insert first stack");
printf("\n\t
2>.insert second stack");
printf("\n\t
3>.display");
printf("\n\t
4>.mearge two stack");
printf("\n\t
5>.inser");
printf("\n\t
6>.delete");
printf("\n\t
7>.searching");
printf("\n\t
8>.update");
printf("\n\t
9>.merge stack display");
printf("\n\t
10>.exit");
printf("\n\t=================================");
printf("\n\t
enter your choice:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
{
push1();
display();
break;
}
case
2:
{
push2();
display();
break;
}
case
3:
{
display();
break;
}
case
4:
{
merge();
disp();
break;
}
case
5:
{
push();
disp();
break;
}
case
6:
{
pop();
disp();
break;
}
case
7:
{
peep();
break;
}
case
8:
{
update();
disp();
break;
}
case
9:
{
disp();
break;
}
case
10:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong......");
break;
}
}
}while(ch!=10);
getch();
}
void push1()
{
int
data;
if(top1==max1-1)
{
printf("\n\t
stack first is overflow....");
return;
}
top1++;
printf("\n\t
enter data:-");
scanf("%d",&data);
s1[top1]=data;
}
void push2()
{
int
data;
if(top2==max2-1)
{
printf("\n\t
stack first is overflow....");
return;
}
top2++;
printf("\n\t
enter data:-");
scanf("%d",&data);
s2[top2]=data;
}
void display()
{
int
i;
for(i=0;i<=top1;i++)
{
printf("\n\t
s1[%d]=%d",i,s1[i]);
}
printf("\n\t=================================\n\n");
for(int
j=0;j<=top2;j++)
{
printf("\n\t
s2[%d]=%d",j,s2[j]);
}
}
void merge()
{
if(top1==-1 && top2==-1)
{
printf("\n\t
both stack is empty");
return;
}
if(top3==-1)
{
for(int
i=0;i<=top1;i++)
{
top3++;
s3[top3]=s1[i];
}
for(int
j=0;j<=top2;j++)
{
top3++;
s3[top3]=s2[j];
}
}
}
void push()
{
int
data;
if(top3==max3-1)
{
printf("\n\t
stack is underflow...");
return;
}
top3++;
printf("\n\t
enter data:-");
scanf("%d",&data);
s3[top3]=data;
}
void pop()
{
if(top3==-1)
{
printf("\n\t
stack is underflow...");
return;
}
printf("\n\t
%d is deleted \n",s3[top3]);
top3--;
}
void peep()
{
int
d,f=0,i;
if(top3==-1)
{
printf("\n\t
stack is underflow..........");
return;
}
printf("\n\t
enter no you want to search it:-");
scanf("%d",&d);
for(i=0;i<=top3;i++)
{
if(i==d)
{
printf("\n\t
stack[%d]=%d",i,s3[i]);
f=1;
break;
}
Else
{
f=0;
}
}
if(f==0)
{
printf("\n\t
this no stack is not available......");
}
}
void update()
{
int
d,value,f=0,i;
if(top3==-1)
{
printf("\n\t
stack is underflow.........");
return;
}
printf("\n\t
enter position you want to update:-");
scanf("%d",&d);
for(i=0;i<=top3;i++)
{
if(i==d)
{
printf("\n\t
enter value:-");
scanf("%d",&value);
s3[i]=value;
f=1;
break;
}
else
f=0;
}
if(f==0)
{
printf("\n\t
this no stack is not available.....");
}
}
void disp()
{
if(top3==-1)
{
printf("\n\t
stack is underflow");
return;
}
for(int
i=0;i<=top3;i++)
{
printf("\n\t
stack[%d]=%d",i,s3[i]);
}
}
OUTPUT
Operation on 2 different stack:-
1.insert
2.display
3.merge
4.merge sort
5.exit
Enter the choice:1
Enter the nos.in which u wanna insert (1/2):-1
Enter the elements:10
Enter the nos.in which u wanna insert (1/2):-2
Enter the elements:5
Operation on 2 different stack:-
1.insert2.display3.merge4.merge sort5.exit
Enter the choice:2
Display stack
1or 2 (1/2):2
Elements of stack 25
14). WAP to perform insert,delete,update,search
operation on simple queue.
#include<stdio.h>
#include<conio.h>
#define max 5
int queue[max],rear=-1,front=-1;
void insert();
void delet();
void update();
void search();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t**************************");
printf("\n\t
1>.insert queue");
printf("\n\t
2>.delete queue");
printf("\n\t
3>.update queue");
printf("\n\t
4>.searching queue");
printf("\n\t
5>.display queue");
printf("\n\t
0>.exit");
printf("\n\t**************************");
printf("\n\t
enter your choice:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
{
insert();
display();
break;
}
case
2:
{
delet();
display();
break;
}
case
3:
{
update();
display();
break;
}
case
4:
{
search();
display();
break;
}
case
5:
{
display();
break;
}
case
0:
{
printf("\n\t
exit........");
break;
}
default:
{
printf("\n\t
your choice is wrong...........");
break;
}
}
}while(ch!=0);
getch();
}
void insert()
{
int
data;
if(rear==max-1)
{
printf("\n\t
queue is overflow.......");
}
else
{
printf("\n\t
enter data:-");
scanf("%d",&data);
rear++;
queue[rear]=data;
}
if(front==-1)
{
front++;
}
}
void delet()
{
if(front==-1)
{
printf("\n\t queue is
underflow...........");
}
else
{
printf("%d
is deleted",queue[front]);
}
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
}
void update()
{
int
data,po,i,f=0;
if(front==-1)
{
printf("\n\t
queue is underflow.........");
return;
}
printf("\n\t
enter data you want to change it=");
scanf("%d",&data);
for(i=front;i<=rear;i++)
{
if(queue[i]==data)
{
printf("\n\t enter data you want to
update=");
scanf("%d",&po);
queue[i]=po;
printf("\n\t=============================");
printf("\n\t
queue[%d]=%d",i,queue[i]);
printf("\n\t=============================");
f=1;
return;
}
}
if(f==0)
printf("\n\t
data not found");
}
void search()
{
int
data,i,f;
if(front==-1)
{
printf("\n\t
queue is underflow.........");
return;
}
printf("\n\t
enter position you want to search it=");
scanf("%d",&data);
for(i=front;i<=rear;i++)
{
if(i==data)
{
printf("\n\t=============================");
printf("\n\t
queue[%d]=%d",i,queue[i]);
printf("\n\t=============================");
f=0;
return;
}
else
f=1;
}
if(f==1)
{
printf("\n\t
data not found");
}
}
void display()
{
int
i;
if(front==-1)
printf("\n\t
queue is underflow..........");
else
{
for(i=front;i<=rear;i++)
{
printf("\n\t
queue[%d]=%d",i,queue[i]);
}
}
}
15). WAP to perform insert, delete, update, search operation on simple
circular
Queue.
#include<stdio.h>
#include<conio.h>
#define max 5
int queue[max],rear=-1,front=-1;
void insert();
void delet();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t**************************");
printf("\n\t1>.insert");
printf("\n\t2>.delete");
printf("\n\t3>.display");
printf("\n\t0>.exit");
printf("\n\t**************************");
printf("\n\t
enter your choice:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
{
insert();
display();
break;
}
case
2:
{
delet();
display();
break;
}
case
3:
{
display();
break;
}
case
0:
{
printf("\n\t
exit........");
break;
}
default:
{
printf("\n\t
your choice is wrong...........");
break;
}
}
}while(ch!=0);
getch();
}
void insert()
{
int
data;
if((rear==max-1
&& front==0) || (front==rear+1))
{
printf("\n\t
c-queue is overflow.......");
return;
}
if(front==-1
&& rear==-1)
{
front=0;
rear=0;
}
else
{
if(rear==max-1)
{
rear=0;
}
Else
{
rear++;
}
}
printf("\n\t
enter data:-");
scanf("%d",&data);
queue[rear]=data;
}
void delet()
{
if(front==-1)
{
printf("\n\t queue is
underflow...........");
}
else
{
printf("%d
is deleted",queue[front]);
}
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
if(front==max-1)
front=0;
else
front++;
}
}
void display()
{
int
i;
if(front==-1)
{
printf("\n\t
queue is underflow..........");
}
if(rear>=front)
{
for(i=front;i<=rear;i++)
{
printf("\n
%d",queue[i]);
}
}
else
{
for(i=front;i<=max-1;i++)
{
printf("\t
%d",queue[i]);
}
for(i=0;i<=rear;i++)
{
printf("\t
%d",queue[i]);
}
}
}
OUTPUT
Circular queue operation
1.Queue insert
2.Delete element
3.Display queue
4.Exit
Enter your choice:: 1
Enter
Element:-
11
Enter your choice:: 1
Enter Element:-:
22
Enter your choice:: 3
Element stored in circular queue11 22
11 22 0
0
0
16). WAP to perform
insert,delete operation on doubly queue.
#include<conio.h>
#include<stdio.h>
#define max 5
int queue[max],left=-1,right=-1;
char opt;
void insert();
void del ();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t***********************************");
printf("\n\t
1>.insert");
printf("\n\t
2>.delete");
printf("\n\t
3>.display");
printf("\n\t
0>.exit");
printf("\n\t***********************************");
printf("\n\t
enter your choice:-");
scanf("%d",&ch);
switch
(ch)
{
case
1:
insert();
display();
break;
case
2:
display();
break;
case
3:
display();
break;
case
0:
printf("\n\t
exit");
break;
default:
printf("\n\t
your choice is wrong..........");
break;
}
}while(ch!=0);
getch();
}
void insert()
{
int
data;
if((left==0
&& right==max-1)||(left==right+1))
{
printf("\n\t
de-queue is overflow...........");
return;
}
printf("\n\t enter data:-");
scanf("%d",&data);
printf("\n\t
enter choice:-");
flushall();
scanf("%c",&opt);
if(left==-1
&& right==-1)
{
left=0;
right=0;
queue[left]=data;
return;
}
if(opt=='l'
|| opt=='L')
{
if(left==0)
{
left=max-1;
}
else
{
left--;
}
queue[left]=data;
return;
}
if(opt=='r'
|| opt=='R')
{
if(right==max-1)
{
right=0;
}
else
{
right++;
}
queue[right]=data;
return;
}
}
void display()
{
int
i,j;
if(left==-1
&& right==-1)
{
printf("\n\t
deque is underflow....");
return;
}
if(left>right)
{
for(i=0;i<=right;i++)
{
printf("\n\t
queue[%d]=%d",i,queue[i]);
}
for(j=left;j<=max-1;j++)
{
printf("\n\t
queue[%d]=%d",j,queue[j]);
}
}
Else
{
for(i=left;i<=right;i++)
{
printf("\n\t
queue[%d]=%d",i,queue[i]);
}
}
}
void del ()
{
printf("\n\t
enter option:-");
flushall();
scanf("%c",&opt);
if(left==-1)
{
printf("\n\t
dequeue is underflow.....");
return;
}
if(left==right)
{
printf("%d
is deleted",queue[left]);
right=-1;
left=-1;
return;
}
if(opt=='l' || opt=='L')
{
printf("%d
is deleted",queue[left]);
if(left==max-1)
{
left=0;
}
else
{
left++;
}
return;
}
if(opt=='r'
|| opt=='R')
{
printf("%d
is deleted",queue[right]);
if(right==0)
{
right=max-1;
}
else
{
right--;
}
return;
}
}
17). WAP to perform create
SLL,insert node and delete node in singly linked list.
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
struct link
{
int
data;
struct
link *next;
}*first=NULL, *curr=NULL;
typedef struct link node;
node *temp;
void create();
void infirst();
void inlast();
void inposi();
void delfirst();
void dellast();
void delposi();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t
===================================");
printf("\n\t
1>.create");
printf("\n\t
2>.insert first");
printf("\n\t
3>.insert last");
printf("\n\t
4>.insert position");
printf("\n\t
5>.delete first");
printf("\n\t
6>.delet last");
printf("\n\t
7>.delete position");
printf("\n\t
8>.display");
printf("\n\t
0>.exit");
printf("\n\t=====================================");
printf("\n\t
enter choice=");
scanf("%d",&ch);
switch(ch)
{
case
1:
{
create();
display();
break;
}
case
2:
{
infirst();
display();
break;
}
case
3:
{
inlast();
display();
break;
}
case
4:
{
inposi();
display();
break;
}
case
5:
{
delfirst();
display();
break;
}
case
6:
{
dellast();
display();
break;
}
case
7:
{
delposi();
display();
break;
}
case
8:
{
display();
break;
}
case
0:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong");
break;
}
}
}while(ch!=0);
getch();
}
void create()
{
int
i,no;
printf("\n\t
enter size of node:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first==NULL)
{
first=temp;
curr=temp;
}
else
{
curr->next=temp;
curr=temp;
}
}
curr->next=NULL;
}
void infirst()
{
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first==NULL)
{
temp->next=NULL;
first=temp;
curr=temp;
}
else
{
temp->next=first;
first=temp;
}
}
void inlast()
{
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
curr=first;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=temp;
temp->next=NULL;
curr=temp;
}
void inposi()
{
int
i,f=0,no;
curr=first;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter position:-");
scanf("%d",&no);
if(no==1)
{
infirst();
return;
}
for(i=1;i<no-1;i++)
{
if(curr==NULL)
{
f=1;
break;
}
else
{
curr=curr->next;
}
}
if(f==1)
{
printf("\n\t
position not found");
}
else
{
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
temp->next=curr->next;
curr->next=temp;
}
}
void delfirst()
{
curr=first;
curr=curr->next;
printf("%u %d %u
is deleted",first,first->data,first->next);
free(first);
first=curr;
}
void dellast()
{
curr=first;
while(curr->next
!=NULL)
{
temp=curr;
curr=curr->next;
}
printf("%u %d %u
is deleted",curr,curr->data,curr->next);
free(curr);
temp->next=NULL;
curr=temp;
}
void delposi()
{
int
i,f=0,no;
curr=first;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter position:-");
scanf("%d",&no);
if(no==1)
{
delfirst();
return;
}
for(i=0;i<no-1;i++)
{
if(curr->next==NULL)
{
f=1;
break;
}
Else
{
temp=curr;
curr=curr->next;
}
}
if(f==1)
{
printf("\n\t
position not found");
}
else
{
printf("[%u] %d
[%u]===== is deleted",curr,curr->data,curr->next);
temp->next=curr->next;
free(curr);
}
}
void display()
{
curr=first;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
while(curr)
{
printf("[%u]
[%d] [%u]====",curr,curr->data,curr->next);
if(curr->next==NULL)
{
return;
}
curr=curr->next;
}
}
OUTPUT
Link list functioning as a stack
1.push
2.pop
3.display
4.exit
Enter the choice:-3
The linked list is:-
1
2
3
Enter the choice :-2
The node deleted is
3
18). WAP to perform create, insert node and delete node in circular
singly linked list.
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
struct link
{
int
data;
struct
link *next;
}*first=NULL, *curr=NULL;
typedef struct link node;
void create();
void infirst();
void inlast();
void inmiddle();
void delfirst();
void dellast();
void delmiddle();
void search();
void sorting();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t
===================================");
printf("\n\t
1>.create node");
printf("\n\t
2>.insert first");
printf("\n\t
3>.insert last");
printf("\n\t
4>.insert middle");
printf("\n\t
5>.delete first");
printf("\n\t
6>.delet last");
printf("\n\t
7>.delete middle");
printf("\n\t
8>.display");
printf("\n\t
0>.exit");
printf("\n\t=====================================");
printf("\n\t
enter choice=");
scanf("%d",&ch);
switch(ch)
{
case
1:
{
create();
display();
break;
}
case
2:
{
infirst();
display();
break;
}
case
3:
{
inlast();
display();
break;
}
case
4:
{
inmiddle();
display();
break;
}
case
5:
{
delfirst();
display();
break;
}
case
6:
{
dellast();
display();
break;
}
case
7:
{
delmiddle();
display();
break;
}
case
8:
{
display();
break;
}
case
0:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong");
break;
}
}
}while(ch!=0);
getch();
}
void create()
{
int
i,no;
printf("\n\t
enter size of link:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first==NULL)
{
first=temp;
curr=temp;
}
else
{
curr->next=temp;
curr=temp;
}
}
curr->next=first;
}
void infirst()
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first==NULL)
{
first=temp;
curr=temp;
temp->next=first;
}
else
{
temp->next=first;
curr->next=temp;
first=temp;
}
}
void inlast()
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
curr=first;
while(curr->next!=first)
{
curr=curr->next;
}
curr->next=temp;
temp->next=first;
curr=temp;
}
void inmiddle()
{
int
i,f=0,no;
curr=first;
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter position:-");
scanf("%d",&no);
if(no==1)
{
infirst();
return;
}
for(i=1;i<no-1;i++)
{
if(curr->next==first)
{
f=1;
break;
}
else
{
curr=curr->next;
}
}
if(f==1)
{
printf("\n\t
position not found");
}
else
{
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
temp->next=curr->next;
curr->next=temp;
}
}
void delfirst()
{
node
*temp;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
curr=first;
while(curr->next!=first)
{
curr=curr->next;
temp=curr;
}
curr=first;
curr=curr->next;
printf("%u %d %u
is deleted",first,first->data,first->next);
free(first);
if(curr==first)
{
printf("\n\t
link list is empty");
first=NULL;
return;
}
first=curr;
temp->next=first;
}
void dellast()
{
curr=first;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
node
*temp;
while(curr->next
!=first)
{
temp=curr;
curr=curr->next;
}
printf("%u %d %u
is deleted",curr,curr->data,curr->next);
free(curr);
if(curr==first)
{
printf("\n\t
link list is empty");
first=NULL;
return;
}
temp->next=first;
curr=temp;
}
void delmiddle()
{
int
i,f=0,no;
node
*temp;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
curr=first;
printf("\n\t
enter position:-");
scanf("%d",&no);
if(no==1)
{
delfirst();
return;
}
for(i=0;i<no-1;i++)
{
if(curr->next==first)
{
f=1;
break;
}
else
{
temp=curr;
curr=curr->next;
}
}
if(f==1)
printf("\n\t
position not found");
else
{
printf("[%u] %d
[%u]is deleted",curr,curr->data,curr->next);
temp->next=curr->next;
free(curr);
}
}
void display()
{
curr=first;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
while(curr)
{
printf("[%u]
[%d] [%u]====",curr,curr->data,curr->next);
if(curr->next==first)
{
return;
}
curr=curr->next;
}
}
OUTPUT
1.Create
2,Display
3.Add before element
4.Add after element
5.Delete
6.Exit
19). WAP to perform create,insert node and delete node in doubly linked
list.
#include<stdio.h>
#include<conio.h>
static int
item,p,i;
struct node
{
int info;
struct node*link;
struct node*linkp;
}*ptr,*newnode,*start,*null,*malloc,*loc,*locp;
void create()
{
start=null;
printf("\n enter element:");
scanf("%d",&item);
while(item!=-1)
{
newnode=(struct
node*)malloc(sizeof(struct node));
newnode->info=item;
if(start==null)
start=newnode;
else
ptr->link=newnode;
ptr=newnode;
printf("\n enter
element:");
scanf("%d",&item);
}
ptr->link=null;
}
void display()
{
ptr=start;
do
{
printf("%4d",ptr->info);
ptr=ptr->link;
}
while(ptr!=null);
}
void insert()
{
printf("\n enter element:");
scanf("%d",&item);
printf("\n enter position:");
scanf("%d",&p);
newnode=(struct
node*)malloc(sizeof(struct node));
newnode->info=item;
newnode->link=null;
if(p==1)
{
newnode->link=start;
newnode->linkp=null;
start->linkp=newnode;
start=newnode;
}
Else
{
ptr=start;
for(i=1;i<p&&ptr->link!=null;i++)
ptr=ptr->link;
newnode->link=ptr->link;
newnode->linkp=ptr;
ptr->link->linkp=newnode;
ptr->link=newnode;
}
}
void addlast()
{
printf("\n enter
element:");
scanf("%d",&item);
newnode=(struct
node*)malloc(sizeof(struct node));
newnode->info=item;
ptr=start;
for(i=1;ptr->link!=null;i++)
ptr=ptr->link;
newnode->link=null;
ptr->link=newnode;
newnode->linkp=ptr;
}
void dellast()
{
ptr=start;
if(ptr==null)
{
printf("\n
underflow \n");
return;
}
do
{
locp=ptr;
ptr=ptr->link;
}while(ptr->link!=null);
start->linkp=locp;
locp->link=start;
}
void delloc()
{
printf("\n enter position:");
scanf("%d",&p);
if(p==1)
{
ptr=start;
start->linkp->link=start->link;
start=start->link;
}
else
{
i=1;
ptr=start;
do
{
i++;
locp=ptr;
ptr=ptr->link;
}while(i<p);
locp->link=ptr->link;
}
}
void main()
{
int ch;
do
{
clrscr();
printf("\n
1 insert element\n");
printf("\n
2 insert at any position\n");
printf("\n
3 insert at last position\n");
printf("\n
4 delete from any position\n");
printf("\n
5 delete from last position\n");
printf("\n
6 display list\n");
printf("\n
7 exit\n");
printf("\n
enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case
1:
create();
break;
case
2:
insert();
break;
case
3:
addlast();
break;
case
4:
delloc();
break;
case
5:
dellast();
break;
case
6:
display();
break;
case
7:
printf("\n
exit\n");
break;
default:
printf("\n
invalid choice\n");
}
getch();
}while(ch!=7);
}
20). WAP to perform create, insert node and delete node in doubly
circular linked list.
#include<conio.h>
#include<stdio.h>
# define mem (struct node *)malloc(sizeof (struct
node))
struct node
{
int
info;
struct node
*next;
struct
node *prev;
} *first, *newnode, *q, *last,*t;
void create()
{
int
data;
first=NULL;
printf("\nEnter
data:-- ");
scanf("%d",&data);
while(data!=0)
{
newnode=mem;
newnode->info=data;
newnode->next=NULL;
newnode->prev=NULL;
if(first==NULL)
{
first=newnode;
}
else
{
q->next=newnode;
newnode->prev=q;
break;
}
q=newnode;
scanf("%d",&data);
}
last=newnode;
last->next=first;
First->prev=last;
}
void display()
{
if(first==NULL)
{
printf("\nLinked
list is empty");
}
else
{
printf("\nThe
linked list is:-\n\n");
q=first;
do
{
printf("\n\t%d",q->info);
q=q->next;
}
while(q!=first);
}
}
void delbyele()
{
int e;
printf("\nEnter
the element :- ");
scanf("%d",&e);
if(first->info==e)
{
if(first==last)
{
q=first;
free(first);
first=last=NULL;
printf("\nElement
is deleted");
}
else
{
q=first;
first=first->next;
last->next=first;
first->prev=last;
free(q);
printf("\nElement
is deleted");
}
}
else
{
q=first;
while(q->info==e
&& q!=last)
q=q->next;
if(q==first
&& q->info!=e)
{
printf("\nElement
not found ");
Return;
}
else
{
q->next->prev=q->prev;
q->prev->next=q->next;
}
if(q==last)
last=last->prev;
printf("\nElement
is deleted");
}
free(q);
}
void addafter()
{
int
data ,e;
printf("\nEnter
element after which u want to add:-");
scanf("%d",&e);
printf("\nEnter
data:- ");
scanf("%d",&data);
newnode=mem;
newnode->prev=NULL;
newnode->next=NULL;
newnode->info=data;
q=first;
while(q->info!=e
&& q!=last)
q=q->next;
if(q==last
&& q->info!=e)
{
printf("\nElement
not found");
}
else
{
newnode->prev=q;
newnode->next=q->next;
q->next->prev=newnode;
q->next=newnode;
printf("\nNode
is deleted");
}
if(q==last
&& q->info==e)
last=last->next;
}
void addbefore()
{
int
e,data;
printf("\nEnter
element before which u wann insert:-");
scanf("%d",&e);
printf("\nEnter
data:-");
scanf("%d",&data);
if(first->info==e)
{
newnode=mem;
newnode->info=data;
newnode->next=first;
newnode->prev=last;
last->next=newnode;
first->prev=newnode;
first=newnode;
}
else
{
newnode=mem;
newnode->info=data;
q=first;
while(q->info!=e
&& q!=last)
q=q->next;
if(q->info!=e
&& q!=last)
{
printf("\nElemnent
not found");
}
else
{
newnode->next=q;
newnode->prev=q->prev;
q->prev=newnode;
newnode->prev->next=newnode;
}
}
}
void main()
{
int ch;
do
{
printf("\n1.Create");
printf("\n2,display");
printf("\n3.delete
by element");
printf("\n4.Add
before element");
printf("\n5.Add
after element");
printf("\n6.Exit");
printf("\n\nEnter
your choice.......");
scanf("%d",&ch);
switch(ch)
{
case
1:
create();
break;
case
2:
display();
break;
case
3:
delbyele();
break;
case
4:
addbefore();
break;
case
5:
addafter();
break;
case
6:
printf("Bye
Bye.......");
default:
printf("\nWring
choice");
}
getch();
}while(ch!=6);
}
OUTPUT
1.Create
2,display
3.delete by element
4.Add before element
5.Add after element
6.Exit
21). WAP link list acting as a stack.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
# define mem (struct node*) malloc(sizeof(struct
node))
struct node
{
int
info;
struct
node *next;
} *newnode,*q,*first,*last;
void push()
{
int
data;
printf("\nEnter
data:-");
scanf("%d",&data);
q=last;
newnode=mem;
newnode->info=data;
newnode->next=NULL;
if(first==NULL)
first=newnode;
else
q->next=newnode;
q=newnode;
last=newnode;
}
void pop()
{
struct
node *t;
if(first==NULL)
printf("\nLinked
list is empty:-");
else
{
q=first;
while(q->next!=NULL)
{
t=q;
q=q->next;
}
t->next=NULL;
last=t;
printf("\nThe
node deleted is:-%d",q->info);
free(q);
if(q==first)
first=NULL;
}
}
void display()
{
if(first==NULL)
printf("\nLinked
list is empty:");
else
printf("\nThe
linked list is:-\n");
q=first;
do
{
printf("\n\t%d",q->info);
q=q->next;
}while(q!=NULL);
}
}
void main()
{
int ch;
char
ch1;
do
{
clrscr();
printf("\nLinked
list functioning as a stack");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.display");
printf("\n4.Exit");
printf("\n\nEnter
your choice:-- ");
scanf("%d",&ch);
switch(ch)
{
case
1:
push();
break;
case
2:
pop();
break;
case
3:
display();
break;
case
4:
printf("\nBye
Bye......");
break;
default:
printf("\nWrong
choice");
}
getch();
}while(ch!=4);
}
OUTPUT
Linked list functioning as a stack
1.Push
2.Pop
3.display
4.Exit
Enter your choice:-- 1
Enter data:-11
Enter your choice:-- 2
The node deleted is:-11
22). WAP to merge two singly
linked list.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct link
{
int
data;
struct
link *next;
}*first1=NULL,*first2=NULL,*curr1=NULL,*curr2=NULL;
typedef struct link node;
void create1();
void create2();
void join();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t==================================");
printf("\n\t1>.
create first link list");
printf("\n\t2>.
create second link list");
printf("\n\t3>.
join two link list");
printf("\n\t4>.
display");
printf("\n\t5>.
exit");
printf("\n\t==================================");
printf("\n\t
enter choice=");
scanf("%d",&ch);
switch
(ch)
{
case
1:
{
create1();
display();
break;
}
case
2:
{
create2();
display();
break;
}
case
3:
{
join();
break;
}
case
4:
{
display();
break;
}
case
5:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong......");
break;
}
}
}while(ch!=5);
getch();
}
void create1()
{
int
i,no;
printf("\n\t
enter no1:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first1==NULL)
{
first1=temp;
curr1=temp;
}
Else
{
curr1->next=temp;
curr1=temp;
}
}
curr1->next=NULL;
}
void create2()
{
int
i,no;
printf("\n\t
enter no:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first2==NULL)
{
first2=temp;
curr2=temp;
}
else
{
curr2->next=temp;
curr2=temp;
}
}
curr2->next=NULL;
}
void join()
{
curr1=first1;
while(curr1->next!=NULL)
{
printf("\n\t %d
%u
",curr1->data,curr1->next);
curr1=curr1->next;
}
curr1->next=first2;
printf("\n\t %d %u
",curr1->data,curr1->next);
curr2=first2;
while(curr2)
{
printf("\n\t %d
%u",curr2->data,curr2->next);
curr2=curr2->next;
}
}
void display()
{
printf("\n\t
first link list...");
curr1=first1;
if(first1==NULL)
{
printf("\n\t
link list is empty");
}
while(curr1)
{
printf("\n\t %d
%u",curr1->data,curr1->next);
curr1=curr1->next;
}
printf("\n\t
second link list \n");
curr2=first2;
if(first2==NULL)
{
printf("\n\t
link list is empty");
}
while(curr2)
{
printf("\n\t
%d
%u",curr2->data,curr2->next);
curr2=curr2->next;
}
}
OUTPUT
1.Create
2. Display
3.After Merging
4.Exit
Enter your choice:: 1
Enter data for 1st linked list(0 to exit) :-1
Enter data for 1st linked-list(0 to exit) :-2
Enter data 2nd linked list (0 to exit):-3
Enter data for 2nd linked list(0 to exit):-4
Enter your choice:: 2
The 1st linked list is:-
1
2
The 2nd linked list is:-
3
4
23). WAP to sort the singly linked list. Using bubble sort.
#include<stdio.h>
#include<malloc.h>
struct link
{
int
data;
struct
link *next;
}*first=NULL, *curr=NULL;
typedef struct link node;
void create();
void sorting();
void display();
void main()
{
int
ch;
clrscr();
do
{
printf("\n\t
===================================");
printf("\n\t
1>.create link list");
printf("\n\t
2>.sorting");
printf("\n\t
3>.display");
printf("\n\t
0>.exit");
printf("\n\t=====================================");
printf("\n\t
enter choice=");
scanf("%d",&ch);
switch(ch)
{
case
1:
{
create();
display();
break;
}
case
2:
{
sorting();
display();
break;
}
case
3:
{
display();
break;
}
case
0:
{
printf("\n\t
exit");
break;
}
default:
{
printf("\n\t
your choice is wrong");
break;
}
}
}while(ch!=0);
getch();
}
void create()
{
int
i,no;
printf("\n\t
enter size of link:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
node
*temp;
temp=(node*)malloc(sizeof(node));
printf("\n\t
enter data:-");
scanf("%d",&temp->data);
if(first==NULL)
{
first=temp;
curr=temp;
}
else
{
curr->next=temp;
curr=temp;
}
}
curr->next=NULL;
}
void sorting()
{
node
*temp;
int
t;
curr=first;
for(temp=first;temp->next!=NULL;temp=temp->next)
{
for(curr=temp->next;curr!=NULL;curr=curr->next)
{
if(temp->data
> curr->data)
{
t=temp->data;
temp->data=curr->data;
curr->data=t;
}
}
}
}
void display()
{
curr=first;
if(first==NULL)
{
printf("\n\t
link list is empty");
return;
}
while(curr)
{
printf("[%u]
[%d] [%u]====",curr,curr->data,curr->next);
if(curr->next==NULL)
{
return;
}
curr=curr->next;
}
}
OUTPUT:-
enter the element in link list:12
enter the element in link list:5
enter the element in link list:10
enter the element in link list:0
link list before sorting is:
12
5
10
0
link list after sorting is:
5
10
12
24). WAP to link list contain details of all student of both division
and display
Division wise.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
# define mem (struct division *)malloc(sizeof (struct
division))
struct division
{
int
roll;
char
name[10];
int
age;
char
div;
struct
division *next;
} *newnode,*first,*t;
void create()
{
char
data[10],d;
int
r,a;
printf("\nEnter
name of the student('Exit' to exit):-");
flushall();
scanf("%s",data);
printf("\nEnter
roll no and age of the student('0 0'to exit):-");
scanf("%d
%d",&r,&a);
printf("\nentre
division of the student('N' to exit):-");
flushall();
scanf("%c",&d);
while(strcmp(data,"exit")!=0&&r!=0&&a!=0
&& (d!='N'||d!='n'))
{
newnode=mem;
newnode->roll=r;
strcpy(newnode->name,data);
newnode->age=a;
newnode->next=NULL;
if(first==NULL)
{
first=newnode;
}
Else
{
t->next=newnode;
break;
}
t=newnode;
printf("\nEnter
name of the student('exit' to exit):-");
flushall();
scanf("%s",data);
printf("\nEnter
Roll no and age of the student('0 0' to exit):-");
scanf("%d
%d",&r,&a);
printf("\nEntre
division of the student('N' to exit):-");
flushall();
scanf("%c",&d);
}
}
void display()
{
if(first==NULL)
printf("\nMerged
link list is empty");
else
{
printf("\nThe
merged detail of all student is:--\n\n");
printf("\nRollno Name
Age Div");
printf("\n=================================\n");
t=first;
do
{
printf("\n%5d
%10s %5d %5c",t->roll,t->name,t->age,t->div);
t=t->next;
}while(t!=NULL);
}
}
void dis_choice()
{
char
ch1;
int
c=0;
printf("\nWhich
division's student details u want to display(A/B):-");
flushall();
scanf("%c",&ch1);
t=first;
printf("\nRollno Name
Age Div ");
printf("\n================================\n");
while(t!=NULL)
{
if(t->div==ch1)
{
printf("\n%5d
%10s %5d %5c",t->roll,t->name,t->age,t->div);
c++;
}
t=t->next;
}
if(c==0)
printf("\nNo
records found of %c division",ch1);
}
void main()
{
int
ch;
char
ch1,d;
do
{
clrscr();
printf("\n1.Create");
printf("\n2.display
all records");
printf("\n3.Display
according to the choice");
printf("\n4.Exit");
printf("\n\nEnter
your choice::");
scanf("%d",&ch);
switch(ch)
{
case
1:
create();
break;
case
2:
display();
break;
case
3:
dis_choice();
break;
case
4:
printf("\nBye
Bye");
break;
default:
printf("\nWrong
choice");
}
getch();
}while(ch!=4);
}
OUTPUT
1.Create
2.display all records
3.Display according to the choice
4.Exit
Enter your choice::1
Enter name of the student('Exit' to exit):-KHUSHBU
Enter roll no and age of the student('0 0'to
exit):-46 19
entre division of the student('N' to exit):-3
Enter name of the student('exit' to exit):-NIDHI
Enter Roll no and age of the student('0 0' to
exit):-23 19
Entre division of the student('N' to exit):-3
Enter your choice::2
The merged detail of all student is:--
Rollno Name
Age Div
=================================
46 KHUSHBU 19 3
23 NIDHI 19 3
25). WAP to
perform binary search.
#include<conio.h>
#include<stdio.h>
void main()
{
int
i,no,aa[100],po,a,b;
clrscr();
printf("\n\t
enter size of array:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("\n\t
enter element:-");
scanf("%d",&aa[i]);
}
printf("\n\t
enter position which you want to find it:-");
scanf("%d",&po);
if(po<0)
{
printf("\n\t
not found");
return;
}
if(po>no)
{
printf("\n\t
not found");
return;
}
a=no/2;
b=no-a;
if(po>a)
{
for(i=b;i<=no;i++)
{
if(po==i)
{
printf("\n\t
%d",aa[i]);
return;
}
}
}
if(po<b)
{
for(i=1;i<=a;i++)
{
if(po==i)
{
printf("\n\t
%d",aa[i]);
}
}
}
getch();
}
OUTPUT
Enter 10 value in array::
Enter value::1
Enter value::2
Enter value::6
Enter value::4
Enter value::8
Enter value::8
Enter value::8
Enter value::9
Enter value::8
Enter value::4
Enter number of elements:8
Search is successfull
found at5location::8
26). WAP to Heap sort with
array.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void heap_s(int list[],int n);
void max_heap(int list[],int n);
void heap_s(int list[],int n)
{
int
i,t;
max_heap(list,n);
for(i=n-1;i>=1;i--)
{
t=list[0];
list[0]=list[i];
list[i]=t;
max_heap(list
,i);
}
}
void max_heap (int list[],int n)
{
int
pos,s,val,f;
for(pos=n-1;pos>=1;pos--)
{
s=pos;
val=list[s];
f=(s-1)/2;
while
(s>=1)
{
if(list[f]
> val)
break;
else
{
list[s]=list[f];
f=s;
f=(s-1)/2;
}
s--;
}
list[s]=val;
}
}
void main()
{
int
*a,n,i;
clrscr();
printf("\n
enter size of the array:-");
scanf("%d",&n);
a=(int
*)malloc(sizeof (int) *n);
for(i=0;i<n;i++)
{
printf("\nEnter
%d element:-",i+1);
scanf("%d",&a[i]);
}
printf("\nThe
element of the array before sorting is:-\n");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
printf("\nThe
element of the array after sorting is:-\n");
heap_s(a,n);
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
getch();
}
OUTPUT
enter size of the array:-3
Enter 1 element:-2
Enter 2 element:-3
Enter 3 element:-1
The element of the array before sorting is:-
2
3
1
The element of the array after sorting is:-
1
2
3
27). WAP to merge
sort with array.
#include<conio.h>
#include<stdio.h>
void main()
{
int
i,j,k=1,a[100],b[100],c[200],no1,no2,temp;
clrscr();
printf("\n\t
enter size of array one:-");
scanf("%d",&no1);
for(i=1;i<=no1;i++)
{
printf("\n\t
enter element:-");
scanf("%d",&a[i]);
}
printf("\n\t
enter size of array two:-");
scanf("%d",&no2);
for(i=1;i<=no2;i++)
{
printf("\n\t
eter element:-");
scanf("%d",&b[i]);
}
for(i=1;i<=no1;i++)
{
c[k]=a[i];
k++;
}
for(i=1;i<=no2;i++)
{
c[k]=b[i];
k++;
}
for(i=1;i<=k-1;i++)
{
for(j=1;j<=i;j++)
{
if(c[i]<c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
for(i=1;i<=k-1;i++)
{
printf("\n\t
%d",c[i]);
}
getch();
}
28). WAP to quick sort with array.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
void
qsort (int ,int, int []);
int
partition (int ,int ,int []);
int
*a,n,i;
clrscr();
printf("\n
enter size of array:-");
scanf("%d",&n);
a=(int
*)malloc(sizeof (int) *n);
for(i=0;i<n;i++);
{
printf("\n
enter %d element :-",i+1);
scanf("%d",&a[i]);
}
printf("\n
the element of the array before sorting is:-\n");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
qsort(0,n,a);
printf("\n
the array after quick sort is :-\n");
for(i=0;i<n;i++)
{
printf("\n\t%d",a[i]);
}
getch();
}
void qsort (int l,int u, int a[])
{
int i;
if(l<u)
{
i=partition(l,
u,a);
qsort(l,i,a);
qsort(i+1,u,a);
}
}
int partition(int l,int u,int a[])
{
int
p,q,i,t;
p=l;
q=u;
i=a[l];
while
(p<q)
{
while(a[++p]<i);
while(a[--q]>i);
if(p<q)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[l];
a[l]=a[q];
a[q]=t;
return
q;
}
29). WAP to of
selection sort with array.
#include<conio.h>
#include<stdio.h>
void main()
{
int sml,log,a[10],n,i,j;
clrscr();
printf("\n\t how many no you want to enter in array:-");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n\t
enter data:-");
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
sml=a[i];
log=i;
for(j=i+1;j<=n;j++)
{
if(a[j]<sml)
{
sml=a[j];
log=j;
}
if(a[i]!=sml)
a[log]=a[i];
a[i]=sml;
}
}
for(i=1;i<=n;i++)
{
printf("\n\t
%d",a[i]);
}
getch();
}
OUTPUT
HOW MANY DO YOU WANT:5
Enter any number:11
Enter any number:12
Enter any number:14
Enter any number:15
Enter any number:16
SELECTION SORTED ARRAY
DATA:
11
12
14
15
16
30). WAP to sequential search with arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int
i,no,po,a[100],f;
clrscr();
printf("\n\t
enter the no:-");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("\n\t
enter the element:-");
scanf("%d",&a[i]);
}
printf("\n\t
enter the no which you want to search:-");
scanf("%d",&po);
for(i=1;i<=no;i++)
{
if(a[i]==po)
{
printf("\n\t
no is searching");
f=0;
break;
}
Else
{
f=1;
}
}
if(f==1)
{
printf("\n\t
this no is not found");
}
getch();
}
OUTPUT
Enter number of element
you want to enter in array::5
Enter value:1
Enter value:2
Enter value:6
Enter value:9
Enter value:4
Enter teh value to be searched::9
Search successful
Element found at 4
location::9
31). WAP to perform various binary tree operations.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
{
int info;
struct tree*left;
struct tree*right;
};
typedef struct
tree node;
node*search
node(node*int);
node*delete
node(node*,node*,node*);
node*delete
nodepredecessor(node*,node*,node*);
void
print(node*,int);
int depth of
tree(node*,int);
node*root=null;
node*newnode=null;
node*parent=null;
int depth=0;
void main()
{
Int key;
Int choice;
Node*n;
Clrscr();
Do
{
Clrscr();
Printf(“\n 1 create\n2.preorder\n3.post
order\n4.in order\n5.search\n6.inseert\n7
delete(predessor)\n8.delete(successor)\n9.depth\n11.exit”);
Printf(“\n your choice is:”);
switch(choice)
{
Case
1:
Root=null;
Create();
Break;
Case 2:
Printf(“\npreorder\n”);
Preorder(root);
Getch();
Break;
Case 3:
Printf(“\npreorder\n”);
Preorder(root);
Getch();
Break;
Case 4:
Printf(“\n ineorder\n”);
inorder(root);
Getch();
Break;
Case 5:
Printf(“\n enter key:”);
Scanf(“%d”,&key);
If(search(root,key))
{
Printf(“\n successful……..”);
}
Else
{
Printf(“\n unsuccessfull…..”);
}
Getch();
Break;
Case 6:
Newnode=(node*)malloc(sizeof(node));
Printf(“enter no:”);
Scanf(“%d”,&newnode->info);
Newnode->left=newnode->right=null;
Insert(root,newnode->info);
Case 7:
Printf(“\n enter key:”);
Scanf(“%d”,&key);\
N=search node(root,key);
If(key==root->info)
Parent=null;
Root=delete node
predecessor(root,n,parent);
Break;
Case 8:
Printf(“\n enter key:”);
Scanf(“%d”,&key);\
N=search node(root,key);
If(key==root->info)
Parent=null;
Root=delete node (root,n,parent);
Break;
Case 9:
Printf(root,0);
Getch();
Break;
Case 10:
Depth=0;
Depth=depth of
tree(root,0);
Printf(“\n depth of a tree
is:%d”,depth);
Getch();
Break;
Case 11:
Exit(0);
}
}while(choice!=11);
}
Comment Here