Print DS
Print DS
Print DS
void main()
{
int a[10],n,x,i,e;
clrscr();
printf(“\n\nENTER THE DEGREE OF POLYNOMIAL\n\n”);
scanf(“%d”,&n);
printf(“\n\nENTER THE CO-EFFICIENT OF POLYNOMIAL\n\n”);
for(i=n;i>=0;i–)
{
printf(“\n\nCO-EFFICIENT OF A[%d]:-\t “,i);
scanf(“%d”,&a[i]);
}
printf(“\n\nENTERED POLYNOMAIL IS \n\n”);
for(i=n;i>0;i–)
{
if(a[i]!=0)
{
printf(“%dX^%d+”,a[i],i);
}
}
printf(“%d”,a[i]);
printf(“\n\nENTER THE VALUE FOR X”);
scanf(“%d”,&x);
e=eval(a,n,x);
printf(“\nEvaluation of Poly is \t %d”,e);
getch();
}
int eval(int a[],int n,int x)
{
int sum=0,i;
for(i=n;i>=0;i–)
{
sum=sum+a[i]*pow(x,i);
}
return sum;
}
Slip 3: ‘C’ program for implementing Linear Search method using function.
#include<stdio.h>
#include<conio.h>
#define MAX 20
int lsearch(int [],int,int);
void main()
{
int a[MAX],k,i,n,m;
clrscr();
printf(“\n\nENTER THE RANGE VALUE”);
scanf(“%d”,&n);
if(n>MAX)
{
printf(“\n\nINVALIED INDEX\n”);
}
else
{
printf(“\n\nENTER THE ARRAY ELEMENTS\n\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\n\nENTER THE ELEMENT FOR SEARCH\n”);
scanf(“%d”,&k);
m=lsearch(a,n,k);
if(m==-1)
{
printf(“\n\nELEMENT IS NOT FOUND IN LIST”);
}
else
{
printf(“\n\nELEMENT IS FOUND AT LOCATION %d”,m);
}
}
getch();
}
void main()
{
int a[27],b[27],c[54],m,n,i,j,z,y=0,t,s=0;
printf ("How many terms you want to add in the 1st polynomial ?? : ");
scanf ("%d",&n);
}
printf (" 0 \n");
printf ("How many terms you want to add in the 2nd polynomial ?? : ");
scanf ("%d",&m);
}
printf (" 0\n");
}
printf (" 0\n");
}
for (i=1;i<(m+n)*3;i=i+3)
{
for (j=4;j<(m+n)*3;j=j+3)
{
if (c[i]==c[j])
{ if(c[i+1]==c[j+1])
{
c[i-1]=c[i-1]+c[j-1];
c[j-1]=0;
}
}
}
}
printf ("ADDITION \n");
for (i=0;i<(m+n)*3;i=i+3)
if (c[i]!=0)
{
printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);
}
else
printf (" ");
printf (" 0 \n");
}
6.‘C’ program to search given elements into the list using Non-Recursive Binary Search
Method.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1);
else
printf(“the key element is not found”);
getch();
}
7.‘C’ program to search given elements into the list using Recursive Binary Search Method.
#include<stdio.h>
#include<conio.h>
#define MAX 20
int bsearch(int [],int,int,int);
void main()
{
int a[10],i,t,f=0,n,l,k;
int bsearch(int[],int,int,int);
clrscr();
printf(“\n\nENTER THE SIZE OF AN ARRAY”);
scanf(“%d”,&n);
if (n>MAX)
{
printf(“\n\nINVALIED INDEX \n\n”);
}
else
{
printf(“\n\nENTER THE ARRAY ELEMENTS\n\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\n\nENTER THE ELEMENT FOR SEARCH”);
scanf(“%d”,&t);
l=n-1;
k=bsearch(a,f,l,t);
if(k==-1)
{
printf(“\n\nELEMENT IS NOT FOUND\n”);
}
else
{
printf(“\n\nELEMENT IS FOUND AT LOCATION =%d”,k);
}
}
getch();
}
int bsearch(int list[],int f,int l,int t)
{
int m;
if(f<=l)
{
m=(f+l)/2;
if(list[m]==t)
return m;
else if(t<list[m])
bsearch(list,f,m-1,t);
else if(t>list[m])
bsearch(list,m+1,l,t);
}
else
{
return(-1);
}
}
#include<stdio.h>
#include<conio.h>
node *g[20];
int n,visited[20];
int indegree(int i);
int outdegree(int i);
void dfs(int i);
if(g[vi]==NULL)
g[vi]=q;
else
{
p=g[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
void readgraph()
{
int vi,vj,i,j,k,no_of_edges;
for(i=0;i<n;i++)
g[i]=NULL;
printf("\nEnter the no. of Vertices::");
scanf("%d",&n);
printf("\nEnter the no of Edges::");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("\nEnter the Edge(u,v)::");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
8.‘C’ program to count indegree and outdegree of each node in graph.
void main()
{
int i,j,k;
clrscr();
readgraph();
for(i=0;i<n;i++)
visited[i]=0;
/* printf("\n=====================================================");
printf("\nNode\tIndegree\tOutdegree");
printf("\n=====================================================");
for(i=0;i<n;i++)
{
j=indegree(i);
k=outdegree(i);
printf("\n%2d\t%4d\t\t%5d",i,j,k);
}
printf("\n-----------------------------------------------------");
*/
dfs(0);
getch();
}
int outdegree(int i)
{
int j=0;
node *p;
p=g[i];
while(p!=NULL)
{
p=p->next;
j++;
}
return(j);
}
int indegree(int v)
{
int i,j=0,k;
node *p;
for(i=0;i<n;i++)
{
p=g[i];
while(p!=NULL)
{
if(p->vertex==v)
j++;
p=p->next;
}
}
return(j);
}
void dfs(int i)
{
node *p;
p=g[i];
visited[i]=1;
printf("\nVisit->%d",i);
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
dfs(i);
p=p->next;
}
}
9.‘C’ program to create Circular Singly Link list and display it.
#include<stdio.h>
#include<conio.h>
struct circular
{
int i;
struct circular *next;
};
int cnt=0;
void create(void);
void insert(void);
void display(void);
void del(void);
void main()
{
int ch=0;
clrscr();
while(ch!=5)
{
printf("\n1.CREATE");
printf("\n2.INSERT");
printf("\n3.DELETE");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
scanf("%d",&ch);
if(ch==1)
{
create();
cnt++;
cnt++;
}
if(ch==2)
{
insert();
cnt++;
}
if(ch==3)
{
del();
cnt--;
}
if(ch==4)
{
display();
}
if(ch==5)
{
break;
}
}
getch();
}
void create()
{
head=(struct circular *)malloc(sizeof(struct circular));
head->next=head;
printf("ENETER THE DATA");
scanf("%d",&head->i);
temp=head;
}
void insert()
{
int add,t;
void display()
{
p=head;
printf("%d-->",p->i);
p=p->next;
while(p!=head)
{
printf("%d-->",p->i);
p=p->next;
}
}
void del(void)
{
int add,t;
}
10.‘C’ program to sort elements of a singly linked list in ascending order and display the
sorted List.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct NUM
{ int data;
struct NUM *link;
}num;
num *createnode()
{
num *nn=NULL;
nn=(num*)malloc(sizeof(num));
if(nn==NULL)
{
printf(“\n insufficient memory”);
exit(0);
}//end if
return(nn) ;
} //end createnode
num *create()
{ num *hn=NULL ,*cn=NULL ,*nn=NULL;
int i,n;
printf(“\n enter no of node “);
scanf(“%d”,&n);
printf(“\n enter data\t”);
for(i=0;i<n;i++)
{
nn=createnode();
scanf(“%d”,&nn->data);
nn->link=NULL;
if(hn==NULL)
{
hn=nn;
cn=nn;
}//end if
else
{
cn->link=nn;
cn=nn;
}
}//end for
return(hn);
}//end create
void display(num *hn)
{
num *cn=NULL;
num *temp,*j,*i;
printf(“\n original data present in link list is “);
for(cn=hn;cn!=NULL;cn=cn->link)
{
printf(” %d”,cn->data);
}//end for
/*following code counts the nonzero,even,odd*/
for(j=hn;j->link!=NULL;j=j->link)
{
for(i=hn;i->link!=NULL;i=i->link)
{
if(i->data > i->link->data)
{
temp->data=i->data;
i->data=i->link->data;
i->link->data=temp->data;
}
}
}
getch();
} //end main
11.‘C’ program to sort the element using Quick sort (recursive) method.
#include<stdio.h>
int main(){
int x[20],size,i;
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
12.‘C’ program to read a postfix expression, evaluate it and display the result.
#include<stdio.h>
#include<conio.h>
#include<math.h>
char st[100];
int top=-1;
void push(char c)
{
top++;
st[top]=c;
}
char pop()
{
char c;
c=st[top];
top–;
return c;
}
int main()
{
int i,j,p,a,b,temp;
char s1[30];
clrscr();
printf(“\nEnter the Postfix Expr”);
gets(s1);
j=0;
for(i=0;s1[i]!=”;i++)
{
if(s1[i]<=’9′ && s1[i]>=’0′)
push(s1[i]-48);
else
{
a=pop();
b=pop();
switch(s1[i])
{
case ‘+’:
temp=b+a;
break;
case ‘-‘:
temp=b-a;
break;
case ‘/’: temp=b/a;
break;
case ‘*’:
temp=b*a;
break;
case ‘^’:
temp=pow(b,a);
break;
case ‘%’:
temp=b%a;
break;
}
push(temp);
}
}
j=pop();
printf(“Result is %d “,j);
getch();
return;
}
13.‘C’ program to create two singly linked lists and concatenate one list at the end of
another list.
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
};
typedef struct list NODE;
NODE *create(NODE *);
void disp(NODE *);
NODE *concat(NODE *,NODE *);
void main()
{
NODE *l1=NULL,*l2=NULL;
clrscr();
l1=create(l1);
printf(“\n\nFIrst Created List Is :\t”);
disp(l1);
l2=create(l2);
printf(“\n\nSecond Created List Is :\t”);
disp(l2);
l1=concat(l1,l2);
printf(“\nconcatenated :”);
disp(l1);
getch();
}
NODE *create(NODE *l1)
{
NODE *tmp,*q;
int n,m,i;
printf(“\n\nHOW MANY NODES U WANT”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the Data”);
scanf(“%d”,&m);
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=m;
tmp->link=NULL;
if(l1==NULL)
{
l1=tmp;
}
else
{
q=l1;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
return l1;
}
void disp(NODE *l)
{
NODE *q;
if(l==NULL)
{
printf(“\nLIST IS EMPTY”);
}
else
{
q=l;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}
NODE *concat(NODE *l1,NODE *l2)
{
NODE *q,*p,*tmp;
q=l1;
p=l2;
while(q->link!=NULL)
{
q=q->link;
}
while(p!=NULL)
{
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=p->data;
tmp->link=NULL;
q->link=tmp;
q=tmp;
p=p->link;
}
return(l1);
}
14.Program to count leaf nodes in a binary tree
#include <stdio.h>
#include <stdlib.h>
return(node);
}
getchar();
return 0;
}
15.‘C’ program to count all non-zero elements, odd numbers and even numbers in the
singly linked list.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf(“\n\nMENU\n\n”);
printf(“\n1.CREATE\n”);
printf(“\n2.DISPLAY\n”);
printf(“\n3.COUNT\n”);
printf(“\n4.EXIT\n”);
printf(“\nENTER UR CHOICE\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nHOW MANY NODES U WANT TO CREATE\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void count()
{
struct node *q;
int nonz=0,eno=0,ono=0;
q=start;
while(q!=NULL)
{
if(q->data>0)
{
nonz++;
}
if(q->data%2==0)
{
eno++;
}
else
{
ono++;
}
q=q->link;
}
printf(“\n\nPOSITVE NO ARE %d EVEN NO ARE %d ODD NO ARE %d”,nonz,eno,ono);
}
16.‘C’ program to swap mth and nth element of singly linked list.
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void creat(int);
void swap();
void disp();
void main()
{
int ch,i,n,m;
clrscr();
do
{
printf(“\n1.create”);
printf(“\n2.display”);
printf(“\n3.Swap”);
printf(“\n4.exit”);
printf(“\nenter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\nHow many nodes”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the data”);
scanf(“%d”,&m);
creat(m);
}
break;
case 2:
disp();
break;
case 4:
exit(0);
case 3:
swap();
break;
}
}
while(ch!=4);
getch();
}
void creat(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf(“list is empty”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void swap()
{
int m,n,i,tmp;
struct list *q,*ptr,*ptr1;
printf(“\nEnter the mth and nth position”);
scanf(“%d%d”,&m,&n);
for(i=1,ptr=start;i<m && ptr!=NULL;ptr=ptr->link,i++);
for(i=1,ptr1=start;i<n && ptr1!=NULL;ptr1=ptr1->link,i++);
if(ptr!=NULL && ptr1!=NULL)
{
tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{
printf(“\nPosition Not Found”);
}
}
struct node
{
int a;
struct node *next;
};
int main()
{
struct node *head = NULL;
generate(&head);
printf("\nDisplaying the alternate nodes\n");
display(head);
delete(&head);
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int id;
char name[10],city[10];
struct node *next;
};
struct node *list=NULL;
void create()
{
int n,i;
struct node *newnode,*temp;
do
{
printf("ID NAME CITY?\n");
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
scanf("%d %s %s",&newnode->id,&newnode->name,&newnode->city);
if(list==NULL)
list=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
printf("\nDo you want to insert record again ?");
printf("\nIf yes press 1 else 0:");
scanf("%d",&n);
}while(n==1);
}
void display()
{
struct node *temp=list;
printf("\nRecords are\n");
printf("\n ID NAME CITY\n");
printf("------ ---------- ----------");
while(temp!=NULL)
{
printf("\n%3d %10s %10s",temp->id,temp->name,temp->city);
temp=temp->next;
}
}
void main()
{
clrscr();
create();
display();
getch();
}
/***************Output************************
ID NAME CITY?
1 Yasin Pune
Records are
ID NAME CITY
------ ---------- ----------
1 Yasin Pune
2 Hamid Pune
3 Kapil Delhi
4 Lalu Bihar
printf("\nNumber of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d]Enter the Data:",i+1);
newnode=getnode;
scanf("%d",&newnode->data);
newnode->next=NULL;
if(list==NULL)
list=t=newnode;
else
{
t->next=newnode;
t=newnode;
}
}
}
void display()
{
nodeptr t;
t=list;
printf("\nNodes are\n");
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void addfirst()
{
nodeptr newnode;
printf("\nData of the newnode:");
newnode=getnode;
scanf("%d",&newnode->data);
newnode->next=list;
list=newnode;
}
void delmid()
{
nodeptr t1,t2,t3;
int i,pos;
printf("Position of the node to be deleted:");
scanf("%d",&pos);
i=2;
t1=list;
while(i<pos)
{
t1=t1->next;
i++;
}
t2=t1->next;
t3=t2->next;
t1->next=t3;
i=t2->data;
free(t2);
printf("%d is deleted",i);
}
void main()
{
int ch;
clrscr();
up:
printf("\n1 to create");
printf("\n2 to display");
printf("\n3 to add the node at the first position");
printf("\n 4 to delete the node, which is in betwen the list");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : create(); break;
case 2 : display(); break;
case 3 : addfirst(); break;
case 4 : delmid(); break;
case 11: exit(0);
default: printf("Wrong choice");
}
getch();
goto up;
}
/***********************Output*************
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:1
Number of nodes:5
1]Enter the Data:1
2]Enter the Data:2
3]Enter the Data:3
4]Enter the Data:4
5]Enter the Data:5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2
Nodes are
1 2 3 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:3
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2
Nodes are
0 1 2 3 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:4
Position of the node to be deleted:4
3 is deleted
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:2
Nodes are
0 1 2 4 5
1 to create
2 to display
3 to add the node at the first position
4 to delete the node, which is in betwen the list
11 to exit
Enter your choice:11
*************************************/
Reverse is
tseB si egelloC anooP
*****************Output2************************
Reverse is
.yrtnuoC yM si aidnI
**************************************************/
20.Write a ‘C’ program to search givenelements into the list using linear
search method.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],h=0,i,n,f;
clrscr();
printf("\nHow many Elements:");
scanf("%d",&n);
printf("\nEnter the %d Elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Element to find:");
scanf("%d",&f);
for(i=0;i<n;i++)
{
if(a[i]==f)
{
h++;
printf("\n%d is present at location number:%d",f,i+1);
}
}
if(h==0)
printf("%d not Present",f);
getch();
}
************************Output1****************
How many Elements:10
Enter the 10 Elements:
1 5 9 10 11 5 2 0 1 5
**********************Output2********************
How many Elements:10
Enter the 10 Elements:
5
2
4
9
10
45
95
11
100
2
Enter the Element to find:3
3 not Present
**************************************************************************
**
21. Write menu driven program using ‘C’ for Static implementation of
Stack. The menu
includes
- push
- pop
- display
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int a[10];
int top;
}s;
int push(int x)
{
s.top++;
s.a[s.top]=x;
return s.a[s.top];
}
int pop()
{
int x;
x=s.a[s.top];
s.top--;
return x;
}
void display()
{
int t;
t=s.top;
while(t>=0)
{
printf("\n%d",s.a[t]);
t--;
}
}
void main()
{
int i,ch;
s.top=-1;
clrscr();
up:
printf("\n1 to push");
printf("\n2 to pop");
printf("\n3 to display");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: if(s.top==9)
printf("\nStack is full");
else
{
printf("\nEnter the element to insert:");
scanf("%d",&i);
ch=push(i);
printf("%d is inserted",ch);
}
break;
case 2: if(s.top==-1)
printf("\nStack is empty");
else
{
i=pop();
printf("\n%d is removed",i);
}
break;
case 3: if(s.top==-1)
printf("\nNo elements to display");
else
display();
break;
case 11: exit(0);
default: printf("\nWrong choice");
}
getch();
goto up;
}
/*********************OUTPUT*******************
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:1
6
5
4
3
2
1
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:2
6 is removed
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:2
5 is removed
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:3
4
3
2
1
1 to push
2 to pop
3 to display
11 to exit
Enter your choice:11
***********************************/
Element is present
******************Output2*******************
*/
23. program to create two singly linked lists and perform the union of two
lists and display it.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr q;
q=(nodeptr)malloc(sizeof(struct node));
q->next=NULL;
return q;
}
nodeptr list1=NULL,list2=NULL;
void create1()
{
nodeptr t,nn;
int n,i;
printf("\nHow many nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nn=getnode();
printf("\nEnter the data:");
scanf("%d",&nn->data);
if(list1==NULL)
list1=t=nn;
else
{
t->next=nn;
t=nn;
}
}
}
void display1()
{
nodeptr t;
t=list1;
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void create2()
{
nodeptr t,nn;
int n,i;
printf("\nHow many nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nn=getnode();
printf("\nEnter the data:");
scanf("%d",&nn->data);
if(list2==NULL)
list2=t=nn;
else
{
t->next=nn;
t=nn;
}
}
}
void display2()
{
nodeptr t;
t=list2;
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
}
}
void uni()
{
nodeptr t1,t2;
int a[20],flag=0,i=0,j=0,n=0;
for(t1=list1;t1!=NULL;t1=t1->next)
{
a[i]=t1->data;
i++;
}
n=i;
for(t2=list2;t2!=NULL;t2=t2->next)
{
flag=0;
for(i=0;i<n;i++)
{
if(t2->data==a[i])
{
flag=1;
break;
}
}
if(flag==0)
{
a[n++]=t2->data;
}
}
printf("\nThe Union of the two list is\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
}
void main()
{
int ch;
clrscr();
up:
printf("\n1 to create list1");
printf("\n2 to dislay list1");
printf("\n3 to create list2");
printf("\n4 to display list2");
printf("\n5 to get the union of the lists");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create1(); break;
case 2: display1(); break;
case 3: create2(); break;
case 4: display2(); break;
case 5: uni(); break;
case 11: exit(0);
default : printf("\nWrong choice");
}
getch();
goto up;
}
/**************OUTPUT****************
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:1
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:2
1 2 3 4 5
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:3
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:4
4 5 6 7 8
1 to create list1
2 to dislay list1
3 to create list2
4 to display list2
5 to get the union of the lists
11 to exit
Enter your choice:5
#include<stdio.h>
#include<conio.h>
struct yasin
{
int a[10];
}s;
int bin_search(int lb,int up,int x)
{
int mid;
if(lb<=up)
{
mid=(lb+up)/2;
if(s.a[mid]==x)
return mid+1;
else if(x<=s.a[mid])
bin_search(lb,mid-1,x);
else
bin_search(mid+1,up,x);
}
else
return -1;
}
void main()
{
int i,n,x;
/****************************************
25. Write menu driven program using ‘C’ for Static implementation of
Queue. The menu includes
- Insert
- Delete
- Display
- Exit
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct queue
{
int item[10];
int front,rear;
}q;
void ins(int x)
{
q.item[++q.rear]=x;
}
int del()
{
return (q.item[q.front++]);
}
void main()
{
int ch,x,i;
q.rear=-1;
q.front=0;
up:clrscr();
printf("\n ----MENU-----");
printf("\n1)Insert");
printf("\n2)Remove");
printf("\n3)Display");
printf("\n4)Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:if (q.rear==9)
printf("\n queue is full.");
else
{
printf("\n enter element to insert.");
scanf("%d",&x);
ins(x);
}
printf("\n %d is inserted in queue.",x);
break;
case 2:if (q.front > q.rear)
printf("\n queue is empty.");
else
{
x=del();
printf("\n the removed element is %d.",x) ;
}
break;
case 3: if (q.front > q.rear)
printf("\n queue is empty.");
else
{
printf("\n The Queue is-->");
for( i=q.front;i<=q.rear;i++)
printf("\n %d",q.item[i]);
}
break;
case 4:exit(0);
default:printf("\n wrong choice.");
}
getch();
goto up;
}
scanf("%d",&a[i]);
}
for(pass=1;pass<n-1;pass++)
{
for(i=0;i<n-pass;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
printf("\nArray in sorted order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
/****************output*****************
27. Write a menu driven program using ‘C’ for static implementation of
Circular Queue for characters. The menu includes
- Insert
- Delete
- Display
- Exit
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct queue
{
int item[10];
int front,rear;
}q;
void ins(int x)
{
q.rear=(q.rear+1)%10;
q.item[q.rear]=x;
}
int del()
{
q.front=(q.front+1)%10;
return (q.item[q.front]);
}
void main()
{
int ch,x,i,j;
q.rear=-1;
q.front=-1;
up:
printf("\n ----MENU----\n");
printf("\n1)Insert");
printf("\n2)Remove");
printf("\n3)Display");
printf("\n4)Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:if ((q.rear+1)%10==q.front)
printf("\n queue is full.");
else
{
printf("\n enter element to insert.");
scanf("%d",&x);
ins(x);
}
break;
case 3:
if (q.front==q.rear)
printf("\n queue is empty.");
else
{
j=(q.front+1)%10;
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
1 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
2 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
3 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
4 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
5 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
1)Insert
2)Remove
3)Display
4)Exit
1)Insert
2)Remove
3)Display
4)Exit
1)Insert
2)Remove
3)Display
4)Exit
Enter your choice:1
6 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
1)Insert
2)Remove
3)Display
4)Exit
7 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
8 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
9 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
10 is inserted in queue.
----MENU----
1)Insert
2)Remove
3)Display
4)Exit
post[j]=x;
j++;
x=pop();
}
}
else
{
post[j]=inf[i];
j++;
}
}
}
while (s.top!=-1)
{
x=pop();
post[j]=x;
j++;
}
post[j]='\0';
printf("\n\n Postfix expression is:\n%s",post);
for(j=0;post[j]!='\0';j++)
{
if (!isopr(post[j]))
{
chr[0]=post[j];
chr[1]='\0';
p=atoi(chr);
push(p);
}
else
{
op1=pop();
op2=pop();
switch(post[j])
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '%':push(op1%op2);
break;
case'$':push(pow(op1,op2));
break;
}
}
}
getch();
}
/*Output:
else if(newnode->data>temp->data)
{
if(temp->right==NULL)
{
temp->right=newnode;
break;
}
else
temp=temp->right;
}
}
}
//end of while
}
}
ptr=pop();
if(flag==0)
printf("%3d",ptr->data);
else
{
push(ptr);
ptr=ptr->right;
}
}
void main()
{
int ch;
clrscr();
s.top=-1;
up:
printf("\n1 to create the Binary Search tree");
printf("\n2 to display in preorder");
printf("\n3 to display in inorder:");
printf("\n4 to dislay in postorder");
printf("\n5 to npreorder");
printf("\n6 to ninorder");
printf("\n7 to npost");
printf("\n11 to exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: bst(); break;
case 2: preorder(root); break;
case 3: inorder(root); break;
case 4: postorder(root); break;
case 5: npreorder(root);break;
case 6: ninorder(root); break;
case 7:npost(root);break;
case 11: exit(0);
default: printf("\nWrong choice");
}
getch();
goto up;
}
/****************Output*******************
data:50
data:56
data:2
data:40
data:43
data:30
data:25
data:90