Post on 14-Apr-2018
transcript
7/29/2019 Ds Lab Printouts
1/35
Ashwin Kumar.S
312212104017
#include
struct node
{
int element[5];
int next[5];
}s;
static int i=0;
void create()
{
int x,cont;
do
{
printf("\nenter the element:");
scanf("%d",&x);
s.element[i] = x;
s.next[i]=i+1;
i++;
if(i
7/29/2019 Ds Lab Printouts
2/35
Ashwin Kumar.S
312212104017
s.next[i+1]='\0';
i++;
}
else
{
printf("\nthe position before the given position is vacant");
}
}
else
printf("\nno space for insertion");
}
void display()
{
int y;
for(y=0;y
7/29/2019 Ds Lab Printouts
3/35
Ashwin Kumar.S
312212104017
else
return 0;
}
int find(int x)
{
int y=0;
while(y
7/29/2019 Ds Lab Printouts
4/35
Ashwin Kumar.S
312212104017
scanf("%d",&x);
y=retreive(x);
if(y!=0)
printf("\nthe element is %d",y);
else
printf("\nthere is no element in the given position");
}
if(choice==4)
{
printf("\nenter the element to be found:");
scanf("%d",&x);
y=find(x);
if(y!=0)
printf("\nthe given element is in position no: %d",y);
else
printf("\nthe given element is not present");
}
printf("\ncontinue?\n1.yes\n2.no: ");
scanf("%d",&cont);
}while(cont==1);
printf("\nelements in the list are:");
display();
getch();
}
7/29/2019 Ds Lab Printouts
5/35
Ashwin Kumar.S
312212104017
SAMPLE INPUT AND OUTPUT
Enter the element: 3
Continue?
1.yes
2.no: 1
Enter the element: 4
Continue?
1.yes
2.no: 2
Enter your choice:
1.insert
2.delete
3.retreive
4.find
1
Enter the element and the position : 8
4
The position before the given position is vacant
Continue?
1.yes
2.no:1
Enter your choice:1.insert
2.delete
3.retreive
4.find
1
Enter the element and the position : 5
1
Continue?
1.yes2.no:1
Enter your choice:
1.insert
2.delete
3.retreive
4.find
3
Enter the position of the element to be retrieved : 3
The element is 4
Continue?
7/29/2019 Ds Lab Printouts
6/35
Ashwin Kumar.S
312212104017
1.yes
2.no:1
Enter your choice:
1.insert
2.delete
3.retreive
4.find
2
Enter the position of the element to be deleted : 1
Continue?
1.yes
2.no:1
Enter your choice:
1.insert
2.delete
3.retreive
4.find
4
Enter the element to be found : 3
The given element is in position no : 1
Continue?
1.yes
2.no: 2
The elements in the list are:
3
4
7/29/2019 Ds Lab Printouts
7/35
Ashwin Kumar.S
312212104017
#include
#include
#include
struct node
{
int element;
struct node * next;
};
struct node *ptr;
struct node *header;
static int count=0;
void create(int z)
{
int i,x;
struct node *temp,*temp1;
temp=(struct node *)malloc(sizeof(struct node));
header=(struct node *)malloc(sizeof(struct node));
header->next=temp;
for(i=0;ielement = x;
if (inext=temp1;
temp=temp->next;}
else
temp->next=NULL;
}
}
int find(int x)
{
int i=0;ptr=header->next;
7/29/2019 Ds Lab Printouts
8/35
Ashwin Kumar.S
312212104017
while(ptr!=NULL)
{i++;
if(ptr->element==x)
return i;
else
ptr=ptr->next;
}
return 0;
}
int retreive(int p)
{
int i=1;
ptr=header->next;
if(p==1)
return ptr->element;
while(ptr->next!=0)
{
i++;
ptr=ptr->next;
if(i==p)
{
return ptr->element;
}
}
return 0;
}
void insert(int x,int p)
{
int i=1;struct node * insert;
insert=(struct node *)malloc(sizeof(struct node));
insert->element=x;
ptr=header->next;
if(p==1)
{
insert->next=ptr;
header->next=insert;
count++;}
7/29/2019 Ds Lab Printouts
9/35
Ashwin Kumar.S
312212104017
else
{
while(inext;
i++;
}
if(countnext=ptr->next;
ptr->next=insert;
count++;
}
}
}
void delete(int p)
{
int i=1;
struct node * temp;
if(count!=0&&count>=p)
{
ptr=header->next;
temp=ptr->next;
if(p==1)
{
header->next=temp;
free(ptr);
count--;}
else
{
while(inext;
temp=ptr->next;
printf("\n%d\t%d",ptr,temp);
i++;}
7/29/2019 Ds Lab Printouts
10/35
Ashwin Kumar.S
312212104017
if(count!=p)
{
ptr->next=temp->next;
printf("\n%d\t%d",temp->next,ptr->next);
free(temp);
count--;
}
if(count==p)
{
ptr->next=NULL;
free(temp);
count--;
}
}
}
else
printf("\ndeletion is not possible");
}
void display()
{
printf("\nelements in the list and their addresses are:");
ptr=header->next;
while(ptr!=NULL)
{
printf("\n%d\t%d",ptr->element,ptr);
ptr=ptr->next;
}
}
void empty(){
ptr=header->next;
struct node * temp;
while(ptr!=NULL)
{temp=ptr;
ptr=ptr->next;
temp->element='\0';
}
printf("\nthe list is empty");}
7/29/2019 Ds Lab Printouts
11/35
Ashwin Kumar.S
312212104017
void reverse()
{
ptr=header->next;
struct node *q = NULL,*r;
while (ptr!= NULL)
{
r = q;
q = ptr;
printf("\n%d\t%d",ptr->element,ptr->next);
ptr = ptr->next;
q->next = r;
}
header->next=q;
}
void sort()
{
int swap=1,temp;
while(swap==1)
{
swap=0;
ptr=header->next;
while(ptr->next!=NULL)
{
if(ptr->element>ptr->next->element)
{
temp=ptr->element;
ptr->element=ptr->next->element;
ptr->next->element=temp;
swap=1;}
ptr=ptr->next;
}
}
}
main()
{
int choice,cont,x,y,z;printf("enter the no of nodes to be created in the list :");
7/29/2019 Ds Lab Printouts
12/35
Ashwin Kumar.S
312212104017
scanf("%d",&z);
create(z);
do
{
printf("\nenter your
choice:\n1.insert\n2.delete\n3.retreive\n4.find\n5.sort\n6.reverse:");
scanf("%d",&choice);
if(choice==1)
{
printf("\nenter the element and the position:");
scanf("%d%d",&x,&y);
insert(x,y);
display();
}
if(choice==2)
{
printf("\nenter the position of the element to be deleted:");
scanf("%d",&x);
delete(x);
display();
}
if(choice==3)
{
printf("\nenter the position of the element to be retrieved:");
scanf("%d",&x);
y=retreive(x);
if(y!=0)
printf("\nthe element is %d",y);
else
printf("\nthere is no element in the given position");
}if(choice==4)
{
printf("\nenter the element to be found:");
scanf("%d",&x);
y=find(x);
if(y!=0)
printf("\nthe given element is in position no: %d",y);
else
printf("\nthe given element is not present");}
7/29/2019 Ds Lab Printouts
13/35
Ashwin Kumar.S
312212104017
if(choice==5)
{
sort();
printf("\nthe elements have been sorted");
display();
}
if(choice==6)
{
reverse();
printf("\nthe elements have been reversed");
display();
}
printf("\ncontinue?\n1.yes\n2.no:");
scanf("%d",&cont);
}while(cont==1);
getch();
}
SAMPLE INPUT AND OUTPUT
Enter the number of nodes to be created in the list: 2
Enter the element : 5Enter the element : 2
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
1Enter the element and the position: 3
2
The elements in the list and their addresses are:
5 4751008
3 4751104
2 4751072
Continue?
1.yes
2.no: 1
Enter your choice :
7/29/2019 Ds Lab Printouts
14/35
Ashwin Kumar.S
312212104017
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
2
Enter the position of the element to be deleted: 1
The elements in the list and their addresses are:
3 4751104
2 4751072
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
3
Enter the position of the element to be retrieved: 3
There is no element in the given position
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive4.find
5.sort
6.reverse
4
Enter the element to be found: 2
The given element is in position no: 2
Continue?
1.yes
2.no: 1Enter your choice :
7/29/2019 Ds Lab Printouts
15/35
Ashwin Kumar.S
312212104017
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
5
The elements have been sorted
Elements in the list and their addresses are:
2 4751104
3 4751072
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
6
The elements have been reversed
Elements in the list and their addresses are:
3 4751072
2 4751104
Continue?
1.yes
2.no: 2
7/29/2019 Ds Lab Printouts
16/35
Ashwin Kumar.S
312212104017
#include
#include
#include
struct node
{
int element;
struct node * next,*previous;
};
struct node *header,*front,*back;
static int count=0;
void create(int z)
{
int i,x;
struct node *temp,*temp1;
temp=(struct node *)malloc(sizeof(struct node));
header=(struct node *)malloc(sizeof(struct node));
header->next=temp;
header->previous=NULL;
temp->previous=header;
for(i=0;ielement = x;
if (inext=temp1;temp1->previous=temp;
temp=temp->next;
}
else
temp->next=NULL;
}
}
int find(int x){
7/29/2019 Ds Lab Printouts
17/35
Ashwin Kumar.S
312212104017
int i=0;
front=header->next;
while(front!=NULL)
{i++;
if(front->element==x)
return i;
else
front=front->next;
}
return 0;
}
int retreive(int p)
{
int i=1;
front=header->next;
if(p==1)
return front->element;
while(front->next!=0)
{
i++;
front=front->next;
if(i==p)
{
return front->element;
}
}
return 0;
}
void insert(int x,int p){
int i=1;
struct node * insert;
insert=(struct node *)malloc(sizeof(struct node));
insert->element=x;
front=header->next;
if(p==1)
{
insert->next=front;header->next=insert;
7/29/2019 Ds Lab Printouts
18/35
Ashwin Kumar.S
312212104017
insert->previous=header;
front->previous=insert;
count++;
}
else
{
while(inext;
i++;
}
if(countnext=front->next;
insert->previous=front;
front->next=insert;
count++;
if(count!=p-1)
insert->next->previous=insert;
}
}
}
void delete(int p)
{
int i=1;
struct node * temp;
if(count!=0&&count>=p)
{front=header->next;
temp=front->next;
if(p==1)
{
header->next=temp;
temp->previous=header;
free(front);
count--;
}else
7/29/2019 Ds Lab Printouts
19/35
Ashwin Kumar.S
312212104017
{
while(inext;
temp=front->next;
printf("\n%d\t%d",front,temp);
i++;
}
if(count!=p)
{
front->next=temp->next;
temp->next->previous=front;
free(temp);
count--;
}
if(count==p)
{
front->next=NULL;
free(temp);
count--;
}
}
}
else
printf("\ndeletion is not possible");
}
void display()
{
printf("\nelement\tprevious address\tnext address ");
front=header->next;while(front!=NULL)
{
printf("\n%d\t\t%d\t\t%d",front->element,front->previous,front->next);
front=front->next;
}
}
void empty()
{front=header->next;
7/29/2019 Ds Lab Printouts
20/35
Ashwin Kumar.S
312212104017
struct node * temp;
while(front!=NULL)
{temp=front;
front=front->next;
temp->element='\0';
}
printf("\nthe list is empty");
}
void reverse()
{
front=header->next;
struct node *q = NULL,*r;
while (front!= NULL)
{
r = q;
q = front;
printf("\n%d\t%d\t\t%d",front->element,front->previous,front->next);
front = front->next;
q->next = r;
q->previous=front;
}
header->next=q;
}
void sort()
{
int swap=1,temp;
while(swap==1)
{
swap=0;front=header->next;
while(front->next!=NULL)
{
if(front->element>front->next->element)
{
temp=front->element;
front->element=front->next->element;
front->next->element=temp;
swap=1;}
7/29/2019 Ds Lab Printouts
21/35
Ashwin Kumar.S
312212104017
front=front->next;
}
}
}
main()
{
int choice,cont,x,y,z;
printf("enter the no of elements to be created :");
scanf("%d",&z);
create(z);
do
{
printf("\nenter your
choice:\n1.insert\n2.delete\n3.retreive\n4.find\n5.sort\n6.reverse:");
scanf("%d",&choice);
if(choice==1)
{
printf("\nenter the element and the position:");
scanf("%d%d",&x,&y);
insert(x,y);
display();
}
if(choice==2)
{
printf("\nenter the position of the element to be deleted:");
scanf("%d",&x);
delete(x);
display();
}
if(choice==3){
printf("\nenter the position of the element to be retrieved:");
scanf("%d",&x);
y=retreive(x);
if(y!=0)
printf("\nthe element is %d",y);
else
printf("\nthere is no element in the given position");
}if(choice==4)
7/29/2019 Ds Lab Printouts
22/35
Ashwin Kumar.S
312212104017
{
printf("\nenter the element to be found:");
scanf("%d",&x);
y=find(x);
if(y!=0)
printf("\nthe given element is in position no: %d",y);
else
printf("\nthe given element is not present");
}
if(choice==5)
{
sort();
printf("\nthe elements have been sorted");
display();
}
if(choice==6)
{
reverse();
printf("\nthe elements have been reversed");
display();
}
printf("\ncontinue?\n1.yes\n2.no:");
scanf("%d",&cont);
}while(cont==1);
getch();
}
SAMPLE INPUT AND OUTPUT
Enter the number of nodes to be created in the list: 2
Enter the element : 5
Enter the element : 2Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
1
Enter the element and the position: 3
2
7/29/2019 Ds Lab Printouts
23/35
Ashwin Kumar.S
312212104017
Element previous address next address
5 3112640 3112704
3 3112608 3112672
2 3112704 0
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
2
Enter the position of the element to be deleted: 1
Element previous address next address
3 3112640 3112672
2 3112704 0
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
3
Enter the position of the element to be retrieved: 3There is no element in the given position
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find5.sort
7/29/2019 Ds Lab Printouts
24/35
Ashwin Kumar.S
312212104017
6.reverse
4
Enter the element to be found: 2
The given element is in position no: 2
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
5
The elements have been sorted
Element previous address next address
2 3112640 3112672
3 3112704 0
Continue?
1.yes
2.no: 1
Enter your choice :
1.insert
2.delete
3.retreive
4.find
5.sort
6.reverse
6
The elements have been reversedElement previous address next address
3 0 3112704
2 3112672 0
Continue?
1.yes
2.no: 2
7/29/2019 Ds Lab Printouts
25/35
Ashwin Kumar.S
312212104017
#include
#include
struct poly
{
int coeff,degree;
struct poly *next;
};
struct poly * create()
{
int i,x,y,z;
printf("\nenter the no of terms: ");
scanf("%d",&z);
struct poly *temp,*temp1,*header;
temp=malloc(sizeof(struct poly));
header=malloc(sizeof(struct poly));
header->next=temp;
for(i=0;icoeff = x;
temp->degree=y;
if (inext=temp1;
temp=temp->next;
}
elsetemp->next=NULL;
}
return header;
}
void sort(struct poly * p)
{
struct poly * ptr;
int swap=1,tempc,tempd;while(swap==1)
7/29/2019 Ds Lab Printouts
26/35
Ashwin Kumar.S
312212104017
{
swap=0;
ptr=p->next;
while(ptr->next!=NULL)
{
if(ptr->degreenext->degree)
{
tempc=ptr->coeff;
tempd=ptr->degree;
ptr->coeff=ptr->next->coeff;
ptr->degree=ptr->next->degree;
ptr->next->coeff=tempc;
ptr->next->degree=tempd;
swap=1;
}
ptr=ptr->next;
}
}
}
struct poly* addpoly(struct poly *p1,struct poly *p2)
{
struct poly *p3,*p4;
p1=p1->next;
p2=p2->next;
p3=(struct poly *)malloc(sizeof(struct poly));
p4=(struct poly *)malloc(sizeof(struct poly));
p4->next=p3;
while( p1!=NULL && p2!=NULL )
{
if( p1->degree > p2->degree ){
p3->degree = p1->degree;
p3->coeff = p1->coeff;
p1 = p1->next;
}
else if( p1->degree < p2->degree )
{
p3->degree = p2->degree;
p3->coeff = p2->coeff;p2 = p2->next;
7/29/2019 Ds Lab Printouts
27/35
Ashwin Kumar.S
312212104017
}
else
{
p3->degree = p1->degree;
p3->coeff = p1->coeff + p2->coeff;
p1 = p1->next;
p2 = p2->next;
}
if(p1!=NULL&&p2!=NULL)
{
p3->next = (struct poly*)malloc(sizeof(struct poly));
p3 = p3->next;
}
p3->next = NULL;
}
while( p1!= NULL || p2!= NULL )
{
p3->next = (struct poly*)malloc(sizeof(struct poly));
p3 = p3->next;
if( p1!= NULL )
{
p3->degree = p1->degree;
p3->coeff = p1->coeff;
p1 = p1->next;
}
if( p2!= NULL )
{
p3->degree = p2->degree;
p3->coeff = p2->coeff;
p2 = p2->next;
}p3->next = NULL;
}
return p4;
}
struct poly * subpoly(struct poly * p1,struct poly * p2)
{
struct poly *p3,*p4;
p3=p2;p2=p2->next;
7/29/2019 Ds Lab Printouts
28/35
Ashwin Kumar.S
312212104017
while(p2!=NULL)
{
p2->coeff=-p2->coeff;
p2=p2->next;
}
p4=addpoly(p1,p3);
return p4;
}
struct poly * difpoly(struct poly *p)
{
struct poly *p1,*p2;
p1=p;
p=p->next;
while(p!=NULL)
{
if(p->degree!=0)
{
p->coeff=p->coeff*p->degree;
p->degree--;
p2=p;
p=p->next;
}
else
{
p2->next=NULL;
free(p);
p=p2->next;
}
}
return p1;}
struct poly * multipoly(struct poly *p1,struct poly *p2)
{
int swap=1;
struct poly *temp,*p4,*p5,*p6;
struct poly *p3=malloc(sizeof(struct poly));
p4=p3;
p5=p3;p6=p2;
7/29/2019 Ds Lab Printouts
29/35
Ashwin Kumar.S
312212104017
for(p1=p1->next;p1!=NULL;p1=p1->next)
{
for(p2=p2->next;p2!=NULL;p2=p2->next)
{
temp=malloc(sizeof(struct poly));
p3->next=temp;
temp->coeff=p1->coeff*p2->coeff;
temp->degree=p1->degree+p2->degree;
p3=p3->next;
}
p2=p6;
}
p3->next=NULL;
sort(p4);
while(p4->next!=NULL)
{
temp=p4->next;
if(p4->degree==temp->degree)
{
p4->coeff=p4->coeff+temp->coeff;
p4->next=temp->next;
free(temp);
}
else
p4=p4->next;
}
return p5;
}
void display(struct poly *p)
{struct poly *ptr;
printf("\nthe polynomial is:\n");
ptr=p->next;
while(ptr!=NULL)
{
printf("%d^%d ",ptr->coeff,ptr->degree);
ptr=ptr->next;
}
}
7/29/2019 Ds Lab Printouts
30/35
Ashwin Kumar.S
312212104017
main()
{
struct poly *p1,*p2,*p3;
int choice;
do
{
printf("\nenter your
choice\n1.addition\n2.subtraction\n3.multiplication\n4.differentiation\n");
scanf("%d",&choice);
if(choice==1)
{
p1=create();
p2=create();
sort(p1);
sort(p2);
display(p1);
display(p2);
p3=addpoly(p1,p2);
printf("\nafter addition ");
display(p3);
}
if(choice==2)
{
p1=create();
p2=create();
sort(p1);
sort(p2);
display(p1);
display(p2);
p3=subpoly(p1,p2);
printf("\nafter subtraction ");display(p3);
}
if(choice==3)
{
p1=create();
p2=create();
sort(p1);
sort(p2);
display(p1);display(p2);
7/29/2019 Ds Lab Printouts
31/35
Ashwin Kumar.S
312212104017
p3=multipoly(p1,p2);
printf("\nafter multiplication ");
display(p3);
}
if(choice==4)
{
p1=create();
sort(p1);
display(p1);
p3=difpoly(p1);
printf("\nafter differentiation ");
display(p3);
}
printf("\nperform another operation?\n1.yes\n2.no\n");
scanf("%d",&choice);
}while(choice==1);
}
SAMPLE INPUT AND OUTPUT
enter your choice
1.addition
2.subtraction
3.multiplication
4.differentiation
1
enter the no of terms: 3
enter the coefficient and the degree:
4
3
Enter the coefficient and the degree:6
5
enter the coefficient and the degree:
1
2
enter the no of terms: 2
enter the coefficient and the degree:
6
2enter the coefficient and the degree:
7/29/2019 Ds Lab Printouts
32/35
Ashwin Kumar.S
312212104017
4
5
the polynomial is:
6^5 4^3 1^2
the polynomial is:
4^5 6^2
after addition
the polynomial is:
10^5 4^3 7^2
perform another operation?
1.yes
2.no
1
enter your choice
1.addition
2.subtraction
3.multiplication
4.differentiation
2
enter the no of terms: 2
enter the coefficient and the degree:
3
4
enter the coefficient and the degree:
6
0
enter the no of terms: 3
enter the coefficient and the degree:
1
2
enter the coefficient and the degree:5
4
enter the coefficient and the degree:
2
1
the polynomial is:
3^4 6^0
the polynomial is:
5^4 1^2 2^1after subtraction
7/29/2019 Ds Lab Printouts
33/35
Ashwin Kumar.S
312212104017
the polynomial is:
-2^4 -1^2 -2^1 6^0
perform another operation?
1.yes
2.no
1
enter your choice
1.addition
2.subtraction
3.multiplication
4.differentiation
3
enter the no of terms: 2
enter the coefficient and the degree:
5
3
enter the coefficient and the degree:
4
1
enter the no of terms: 2
enter the coefficient and the degree:
6
2
enter the coefficient and the degree:
8
0
the polynomial is:
5^3 4^1
the polynomial is:
6^2 8^0
after multiplicationthe polynomial is:
30^5 64^3 32^1
perform another operation?
1.yes
2.no
1
enter your choice
1.addition
2.subtraction3.multiplication
7/29/2019 Ds Lab Printouts
34/35
Ashwin Kumar.S
312212104017
4.differentiation
4
enter the no of terms: 4
enter the coefficient and the degree:
2
4
enter the coefficient and the degree:
7
5
enter the coefficient and the degree:
4
8
enter the coefficient and the degree:
2
9
the polynomial is:
2^9 4^8 7^5 2^4
after differentiation
the polynomial is:
18^8 32^7 35^4 8^3
perform another operation?
1.yes
2.no
2
7/29/2019 Ds Lab Printouts
35/35
Ashwin Kumar.S
312212104017