Académique Documents
Professionnel Documents
Culture Documents
CANARA ENGINEERING
COLLEGE
(Affiliated to Visvesvaraya Technological University, Belgaum, Karnataka)
Benjanpadavu 574 219, Mangalore, Bantwal Taluk, D.K. District, Karnataka
Laboratory Manual
Name
USN
Staff Incharge
I.A. Marks: 25
Exam Hours: 03
Exam Marks: 50
1. Using circular representation for a polynomial, design, develop, and execute a program in C to
accept two polynomials, add them, and then print the resulting polynomial.
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct link
{
int coeff;
int exp;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\nEnter Coeff : ");
scanf("%d",&node->coeff);
printf("\nEnter power ");
scanf("%d",&node->exp);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\nContinue (y/n) ");
ch=getch();
}while(ch=='y'||ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->exp);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->exp > poly2->exp)
{
poly->exp=poly1->exp;
poly->coeff=poly1->coeff;
void main()
{
char ch;
clrscr();
do
{
poly1=(struct link*)malloc(sizeof(struct link));
poly2=(struct link*)malloc(sizeof(struct link));
poly=(struct link*)malloc(sizeof(struct link));
printf("\nEnter 1st number: ");
create(poly1);
printf("\nEnter 2nd number: ");
create(poly2);
printf("\n1st number: ");
show(poly1);
printf("\n2nd number: ");
show(poly2);
polyadd(poly1,poly2,poly);
postfix[j]=symb;
j++;
}
while(top>0)
{
temp=pop();
postfix[j]=temp;
j++;
}
postfix[j]='\0';
}
void push(char item)
{
top=top+1;
stack[top]=item;
}
char pop()
{
char item;
item=stack[top];
top=top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case'+':
case'-':
case'*':
case'/':
case'^':
case'$':
case'(':
case')':
case'#':
}
return p;
}
p=1; break;
p=2; break;
p=3; break;
p=0; break;
p=-1;
exit(0);
}
res=op1/op2;
push(res);
break;
res=pop();
printf("\n\nResult\t%f",res);
getch();
10
11
12
13
};
14
15
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define size 5
class stack
{
private:
int top;
int st[size];
public:
stack operator +(int);
stack operator --(int);
friend void operator <<(ostream&,stack);
stack()
{
top=-1;
}
};
stack stack :: operator +(int item)
{
if(top==size-1)
{
cout<<"\nStack Overflow\n";
}
else
{
top=top+1;
st[top]=item;
}
return *this;
}
stack stack :: operator --(int)
{
int item;
if(top==-1)
{
cout<<"\nStack Underflow\n";
}
else
{
item=st[top];
cout<<"Deleted element ="<<item<<endl;
top=top-1;
}
return *this;
}
16
17
18
19
20
21
22
23
24
25
}
a[i]=item;
}
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the no. of elements ");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapify(a,n);
printf("The elements of the array after heapify\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
26
27
getnode();
insert_front();
insert_left();
delete();
display();
28
if(prev==NULL)
{
first=temp->rlink;
first->llink=prev;
printf("Deleting %d\n",temp->data);
free(temp);
return;
}
prev->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
printf("Deleting %d",temp->data);
free(temp);
void display()
{
NODE temp;
if(first==NULL)
{
printf("No List exists\n");
return;
}
temp=first;
printf("The elements of the list are\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->rlink;
}
printf("\n");
return;
29
30
31
32
33
return d1;
count;
}
ostream& operator << (ostream& os,date d1)
{
os<<d1.d<<"/"<<d1.m<<"/"<<d1.y<<endl;
return os;
}
void date :: read()
{
cin>>d>>m>>y;
}
void main()
{
date d1,d2;
int choice, x;
clrscr();
for(;;)
{
cout<<"1.Subtraction\n2. Addition\n";
cout<<"Enter your choice
";
cin>>choice;
switch(choice)
{
34
}
}
35
int oct;
octal(int);
int operator + (int);
friend ostream& operator << (ostream&,octal);
};
octal :: octal (int x)
{
int base=1,rem=0;
oct=0;
while(x!=0)
{
rem=x%8;
oct+=rem*base;
x=x/8;
base=base*10;
}
}
int octal :: operator + (int k)
{
int base=1,rem=0,dec=0;
while(oct!=0)
{
rem=oct%10;
dec+=rem*base;
oct=oct/10;
base=base*8;
}
return (dec+k);
}
ostream & operator <<(ostream& os, octal x)
{
os<<x.oct;
return os;
}
void main()
{
int x,k;
clrscr();
cout<<"Enter the value of x ";
cin>>x;
octal h=x;
36
37
38
postorder(t->lchild);
postorder(t->rchild);
cout<<t->data<<"\t";
}
void bintree :: create()
{
node *temp,*t;
temp=new node;
cout<<"Enter the element to be inserted\n";
cin>>temp->data;
temp->lchild=NULL;
temp->rchild=NULL;
t=root;
if(root==NULL)
{
root=temp;
return;
}
while(1)
{
if(temp->data < t->data)
{
if(t->lchild==NULL)
{
t->lchild=temp;
break;
}
t=t->lchild;
}
else
{
if(t->rchild==NULL)
{
t->rchild=temp;
break;
}
t=t->rchild;
}
}
void main()
{
bintree b;
int choice;
clrscr();
for(;;)
{
cout<<"1. Create\n2. Preorder\n3. Inorder\n4. Postorder\n";
cout<<"Enter your choice\n";
cin>>choice;
switch(choice)
{
39
getch();
40
41