Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Aim:
1
To create list using array and perform various operation.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[100],n,i,t,max=50,p,x;
int ch;
void main()
{
void create();
void insertion();
void deletion();
void find();
void display();
clrscr();
while(1)
{
printf("\n1.creation");
printf("\n2.insertion");
printf("\n3.deletion");
printf("\n4.find");
printf("\n5.display");
printf("\n6.exit");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insertion();
break;
2
case 3:
deletion();
break;
case 4:
find();
break;
case 5:
display();
break;
default:
exit(0);
}
getch();
}
}
void create()
{
printf("enter the n value:");
scanf("%d",&n);
printf("enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void insertion()
{
if(n==max)
printf("\narray is full");
else
{
printf("enter the position:");
scanf("%d",&p);
printf("\nenter the element:");
scanf("%d",&x);
for(i=n;i>=p;i--)
{
a[i]=a[i-1];
}
a[i]=x;
n++;
}
}
void deletion()
{
if(n==0)
printf("\narray is empty");
else
{
3
printf("\nenter the position to be deleted:");
scanf("%d",&p);
for(i=p;i<n;i++)
{
a[i-1]=a[i];
}
n--;
}
}
void find()
{
int t=0;
if(n==0)
printf("array is empty:");
else
{
printf("enter the element to find:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
t=1;
break;
}
}
if(t==1)
printf("\nthe element is present in location %d",i+1);
else
printf("\nthe element is not present");
}
}
void display()
{
if(n==0)
printf("\nthe array is empty");
else
{
printf("\narray elements are");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}}
Output:
4
Result:
Thus the list is created using array and its various operations are performed using C
program.
Aim:
5
To write a C program to perform a singly linked list
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. In the insert beginning function set y->link=head. For the insert middle check if c<pos if
so set z=x; and x=x->link;and increment c
5. In the delete beginning function check the above function but perform delete operation
6. In the display function display the array elements
7. Stop the program
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insmid();
void insend();
void delbeg();
void delmid();
void delend();
void display();
int ch;
clrscr();
while(1)
{
printf("\n 1.Creation");
printf("\n 2.Insertion at beginning");
printf("\n 3.Insertion at middle");
6
printf("\n 4.Insertion at end");
printf("\n 5.Deletion at beginning");
printf("\n 6.Deletion at middle");
printf("\n 7.Deletion at end");
printf("\n 8.Display");
printf("\n 9.Exit");
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insmid();
break;
case 4:
insend();
break;
case 5:
delbeg();
break;
case 6:
delmid();
break;
case 7:
delend();
break;
case 8:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
head=NULL;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
7
head=x;
printf("\n Do u wish to continue press 1 otherwise 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=NULL;
x->link=y;
x=y;
printf("\n Do u wish to continue press 1 otherwise 0:");
scanf("%d",&c);
}
}
void insbeg()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=head;
head=y;
}
void insmid()
{
int pos,c=1;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
y->link=x;
z->link=y;
}
void insend()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=NULL;
8
x=head;
while(x->link!=NULL)
{
x=x->link;
}
x->link=y;
}
void delbeg()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
head=x->link;
free(x);
}
}
void delmid()
{
int pos,c=1;
if(head==NULL)
printf("\n List is empty");
else
{
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
z->link=x->link;
free(x);
}
}
void delend()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
while(x->link!=NULL)
9
{
y=x;
x=x->link;
}
y->link=NULL;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
printf("\n List elements are \n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
}
Output:
10
Result:
Thus the list operations are performed using singly linked list in C perform.
11
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
struct node *head,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insmid();
void insend();
void delbeg();
void delmid();
void delend();
void display();
int ch;
clrscr();
while(1)
{
printf("\n 1.Creation");
printf("\n 2.Insertion at beginning");
printf("\n 3.Insertion at middle");
printf("\n 4.Insertion at end");
printf("\n 5.Deletion at beginning");
12
printf("\n 6.Deletion at middle");
printf("\n 7.Deletion at end");
printf("\n 8.Display");
printf("\n 9.Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insmid();
break;
case 4:
insend();
break;
case 5:
delbeg();
break;
case 6:
delmid();
break;
case 7:
delend();
break;
case 8:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
head=NULL;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&x->data);
x->llink=NULL;
x->rlink=NULL;
head=x;
13
printf("\n Do you wish to continue press 1 or 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
x->rlink=y;
y->llink=x;
y->rlink=NULL;
x=y;
printf("\n Do you wish to continue press 1 or 0:");
scanf("%d",&c);
}
}
void insbeg()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
y->llink=NULL;
y->rlink=head;
head->llink=y;
head=y;
}
void insmid()
{
int c=1,pos;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->rlink;
c++;
}
y->rlink=x;
y->llink=z;
x->llink=y;
z->rlink=y;
}
void insend()
{
14
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
y->rlink=NULL;
x=head;
while(x->rlink!=NULL)
{
x=x->rlink;
}
x->rlink=y;
y->llink=x;
}
void delbeg()
{
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
head=x->rlink;
head->llink=NULL;
free(x);
}
}
void delmid()
{
int pos,c=1;
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
while(c<pos)
{
z=x;
x=x->rlink;
c++;
}
z->rlink=x->rlink;
x->rlink->llink=z;
free(x);
}
}
void delend()
{
15
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
while(x->rlink!=NULL)
{
y=x;
x =x->rlink;
}
y->rlink=NULL;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
printf("\nThe list elements are");
while(x->rlink!=NULL)
{
printf("%d->",x->data);
x=x->rlink;
}
printf("%d",x->data);
}
}
Output:
16
Result:
Thus the list operations are executed using doubly linked list in C program.
17
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head=NULL,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insrem();
void delbeg();
void delrem();
void display();
int ch;
clrscr();
printf("\n 1.Creation \n 2.Insertion at beginning \n 3.Insertion at remaining");
printf("\n4.Deletion at beginning \n5.Deletion at remaining \n6.Display");
printf("\n7.Exit");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
18
create();
break;
case 2:
insbeg();
break;
case 3:
insrem();
break;
case 4:
delbeg();
break;
case 5:
delrem();
break;
case 6:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=x;
head=x;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
x->link=y;
y->link=head;
x=y;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d",&c);
}
}
void insbeg()
{
19
x=head;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
while(x->link!=head)
{
x=x->link;
}
x->link=y;
y->link=head;
head=y;
}
void insrem()
{
int c=1,pos;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
y->link=x;
z->link=y;
}
void delbeg()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
y=head;
while(x->link!=head)
{
x=x->link;
}
head=y->link;
x->link=head;
free(y);
}
}
20
void delrem()
{
if(head==NULL)
printf("\n List is empty");
else
{
int c=1,pos;
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
y=x;
x=x->link;
c++;
}
y->link=x->link;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
while(x->link!=head)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
}
Output:
21
Result:
Thus the program for circular linked list is executed and verified using C program
22
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int top=0, a[100], max=50;
void main()
{
void push();
void pop();
void display();
int ch;
clrscr();
printf(“\n1.Push \n2.Pop \n3.Display \n4.Exit”);
while(1)
{
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
23
break;
case 3:
display();
break;
default:
exit();
}
}
}
void push()
{
if(top==max)
printf(“\n Array is full”);
else
{
printf(“\n Enter the data:”);
scanf(“%d”,&x);
st[top++]=x;
}
}
void pop()
{
if (top==0)
printf(“\n Array is empty”);
else
{
top--;
}
}
void display()
{
int i;
if(top==0)
{
printf(“\n Stack is empty”);
else
{
printf(“\n Array elememts are:”);
for(i=0;i<top;i++)
printf(“%d \t”,st[i]);
}
}
Output:
24
Result:
Thus the stack is implemented using array and its operations are performed in C program.
25
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *top=NULL,*x;
void main()
{
void push();
void pop();
void display();
int ch;
clrscr();
while(1)
{
printf("\n OPTIONS");
printf("\n 1.Insertion (push)");
printf("\n 2.Deletion (pop)");
printf("\n 3.Display");
printf("\n 4.Exit");
26
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void push()
{
if(top==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
top=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=top;
top=x;
}
}
void pop()
{
if(top==NULL)
printf("\n Stack is empty");
else
{
x=top;
printf("\n Popped Element is %d",x->data);
top=top->link;
free(x);
27
}
}
void display()
{
x=top;
printf("\n Stack Elements are\n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
Output:
Result:
Thus the stack is implemented using linked list and its operations are performed in C
program.
Ex. No: 7 ARRAY IMPLEMENTATION OF QUEUE
Date:
28
Aim:
To write a C program to perform the array implementation of queue
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int ch,qu[50], max=50,front=0,rear=0;
void main()
{
void enqueue();
void dequeue();
void display();
clrscr();
printf(“\n1.Insertion(enqueue) \n2.Deletion(dequeue) \n3.Display \n4.Exit”);
while(1)
{
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
29
display();
break;
default:
exit(0);
}
}
}
void enqueue()
{
int ele;
if(rear==max)
printf(“\n queue is full”);
else
{
printf(“\n Enter the data:”);
scanf(“%d”,&ele);
qu[rear++]=ele;
}
}
void dequeue()
{
int i;
if (rear==0)
printf(“\n queue is empty”);
else
{
printf(“\n Deleted element is %d”,qu[front]);
front++;
}
}
void display()
{
int i;
if(rear==0)
printf(“\n Queue is empty”);
else
{
printf(“\n Queue elememts are:”);
for(i=front;i<rear;i++)
printf(“%d \t”,qu[i]);
}
}
Output:
30
Result:
Thus the queue is implemented using array and its operations are performed in C
program.
Aim:
31
To write a C program to perform linked list implementation of queue
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*x;
void main()
{
void enqueue();
void dequeue();
void display();
int ch;
clrscr();
while(1)
{
printf("\n OPTIONS");
printf("\n 1.Insertion (enqueue)");
printf("\n 2.Deletion (dequeue)");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter ur choice:");
scanf("%d",&ch);
32
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void enqueue()
{
if(rear==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
rear->link=x;
rear=x;
}
}
void dequeue()
{
if(front==NULL)
printf("\n Queue is empty");
else
{
x=front;
printf("\n Dequeued Element is %d",x->data);
front=x->link;
free(x);
33
}
}
void display()
{
x=front;
printf("\n Queue Elements are\n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
Output:
Result:
Thus the queue is implemented using linked list and its operations are performed in C program
Aim:
34
To write a C program to balance the symbols in an expression.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str1[50],str[50],str2[50];
int i,n,t1=0,t2=0;
void main()
{
clrscr();
printf("\nEnter the string:");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
{
printf("%c\t",str[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t",i+1);
}
for(i=0;i<n;i++)
{
if(str[i]=='(')
{
str1[t1++]=str[i];
str2[t2++]=i+1;
}
else
{
if(str[i]==')')
if(str1[--t1]=='(')
35
printf("\nMatch found for %d\t%d",str2[--t2],i+1);
else
printf("\nNo left match for %d",i+1);
}
}
for(i=0;i<t1;i++)
printf("\nNo right match for %d",str2[--t2]);
getch();
}
Output:
Result:
36
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char c;
struct node *rlink;
struct node *llink;
}*stc[30];
struct node *temp,*root;
char prefix[20],ch;
int topt=-1,max=50,len; //global declaration
void pusht(struct node *p);
struct node *popt();
void tredis(struct node *ptr,int level);
void exptree();
void post(struct node *p);
void main()
{
clrscr();
printf("Enter a prefix expression:");
37
scanf("%s",prefix);
exptree();
tredis(root,1);
printf("\nThe postfix expression is:");
post(root);
getch();
}
void post(struct node *p)
{
if(p!=NULL)
{
post(p->llink);
post(p->rlink);
printf("%c",p->c);
}
}
void exptree()
{
int i;
len=strlen(prefix);
i=len-1;
while(i>=0)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
temp=(struct node*)malloc(sizeof(struct node));
temp->c=prefix[i];
temp->llink=popt();
temp->rlink=popt();
pusht(temp);
break;
default :
temp=(struct node*)malloc(sizeof(struct node));
temp->c=prefix[i];
temp->rlink=NULL;
temp->llink=NULL;
pusht(temp);
}
i--;
}
root=stc[topt];
}
38
void pusht(struct node * p)
{
if(topt==max)
{
printf("/**Beyond Capacity**/");
}
else
{
stc[++topt]=p;
}
}
struct node *popt()
{
if(topt==-1)
printf("/**No Expression**/");
else
return(stc[topt--]);
}
void tredis(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
tredis(ptr->rlink, level+1);
printf(" ");
for (i = 0; i < level; i++)
printf(" ");
printf("%c", ptr->c);
tredis(ptr->llink, level+1);
}
}
Output:
Result:
39
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[50],stack1[50],stack2[50];
int i=0,j,n,top1=-1,top2=-1;
int prec(char);
void main()
{
int t=0;
clrscr();
printf("\nEnter the infix expression:");
scanf("%s",str);
printf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
{
t=0;
if(str[i]==')')
{
stack1[++top1]=stack2[top2--];
top2--;
t=1;
}
if(str[i]=='(')
{
stack2[++top2]='(';
40
t=1;
}
if(t==0)
{
if(((str[i]>='a')&&(str[i]<='z'))||(( str[i]>='A')&&(str[i]<='Z')))
stack1[++top1]=str[i];
else
{
if(top2==-1)
stack2[++top2]=str[i];
else
{
if(prec(str[i])>prec(stack2[top2]))
{
stack2[++top2]=str[i];
}
else
{
while(prec(str[i])<=prec(stack2[top2]))
stack1[++top1]=stack2[top2--];
stack2[++top2]=str[i];
}
}
}
}
}
printf("\nANSWER\n");
while(top2>=0)
stack1[++top1]=stack2[top2--];
for(i=0;i<=top1;i++)
printf("%c",stack1[i]);
getch();
}
int prec(char ch)
{
int tt;
if((ch=='+')||(ch=='-'))
tt=2;
if((ch=='*')||(ch=='/'))
tt=3;
if(ch=='(')
tt=1;
return tt;
}
Output:
41
Result:
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Using the while loop check if node->next!=NULL if so print the values of
node->coeff,node->pow
5. Perform the addition according to the conditions of polynomial addition.
6. print the result
7. Stop the program
Program:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n continue(y/n):");
ch=getch();
43
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
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->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
44
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
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);
printf("\nAdded polynomial:");
show(poly);
printf("\n add two more numbers:");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
Output:
45
Result:
Thus the program for polynomial addition using linked list is executed.
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*x;
void main()
{
int a;
void enqfro();
void deqrea();
void enqrea();
void deqfro();
void display1();
void display2();
clrscr();
47
while(1)
{
printf("\n1.insertion at front");
printf("\n2.insertion at rear ");
printf("\nenter choice");
scanf("%d",&a);
while(a==1)
{
int ch;
printf("\n1.insertion at front");
printf("\n2.deletion at rear ");
printf("\n3.display1");
printf("\n4.exit");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqfro();
break;
case 2:
deqrea();
break;
case 3:
display1();
break;
default:
exit(0);
}
}
while(a==2)
{
int ch;
printf("\n1.insertion at rear");
printf("\n2.deletion at front");
printf("\n3.display2");
printf("\n4.exit");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqrea();
break;
case 2:
deqfro();
break;
48
case 3:
display2();
break;
default:
exit(0);
}
}
}
}
void enqfro()
{
if(front==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
front->link=x;
front=x;
}
}
void deqrea()
{
if(rear==NULL)
printf("\nQueue is empty");
else
{
x=rear;
printf("\ndeleted elementis %d",x->data);
rear=x->link;
free(x);
}
}
void enqrea()
{
if(rear==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
49
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear->link=x;
rear=x;
}
}
void deqfro()
{
if(front==NULL)
printf("\nQueue is empty");
else
{
x=front;
printf("\ndeleted elementis %d",x->data);
front=x->link;
free(x);
}
}
void display2()
{
if(front==NULL)
printf("\nqueue is empty");
else
{
x=front;
printf("\nqueue elements are");
while(x->link!=NULL)
{
printf("%d\t",x->data);
x=x->link;
}
printf("%d",x->data);
}
}
void display1()
{
if(rear==NULL)
50
printf("\nqueue is empty");
else
{
x=rear;
printf("\nqueue elements are");
while(x->link!=NULL)
{
printf("%d\t",x->data);
x=x->link;
}
printf("%d",x->data);
}
}
Output:
Result:
51
Ex. No: 14 INSERTION IN AVL TREE
Date:
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);
main()
{
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
clrscr();
while(1)
52
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
53
struct node *aptr;
struct node *bptr;
if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
54
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1: /* Left heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = -1;
break;
case -1: /* Right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
55
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/
return(pptr);
}/*End of insert()*/
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
56
Output:
Result:
Thus the C program to perform insertion on an AVL tree is executed and verified
57
Ex. No: 15 BINARY SEARCH TREE
Date:
Aim:
Algorithm:
Program:
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct node
{
struct node *llink;
struct node *rlink;
int data;
};
void main()
{
struct node *head,*t;
int s,d;
struct node* finsert();
struct node* delenode(struct node*,int);
void insert(struct node *);
void inorder(struct node *);
clrscr();
58
head=NULL;
do
{
printf("\n1-Insertion");
printf("\n2-Deletion");
printf("\n3-Inorder");
printf("\n4-Exit");
printf("\nEnter Choice:");
scanf("%d",&s);
switch(s)
{
case 1://insertion
if(head==NULL)
{
head=finsert();
}
else
insert(head);
break;
case 2://Deletion
if(head==NULL)
printf("\nBinary Tree Empty.......");
else
{
printf("\nEnter Data to delete:");
scanf("%d",&d);
if(head->llink==NULL && head->rlink==NULL && head->data==d)
{
t=head;
head=NULL;
free(t);
}
else
head = delenode(head,d);
}
break;
case 3://to display
printf("\nIN-ORDER:");
if(head==NULL)
printf("\nBinary Tree Empty....");
else
inorder(head);
break;
case 4://exit
exit(0);
}
59
}while(s<5 ||s>0);
getch();
}
struct node* finsert()
{
struct node * head;
head=(struct node*)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&head->data);
head->llink=NULL;
head->rlink=NULL;
return head;
}
void insert(struct node *head)
{
struct node *t,*n;
t=head;
n=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&n->data);
n->llink=NULL;
n->rlink=NULL;
while(t->llink!=NULL || t->rlink!=NULL)
{
if(t->llink!=NULL)
if(n->data < t->data)
t=t->llink;
if(t->rlink!=NULL)
if(n->data>=t->data)
t=t->rlink;
if((t->llink==NULL) && (n->data < t->data) && (n->data <t->rlink->data))
break;
if((t->rlink==NULL) && (n->data >= t->data) && (n->data >t->llink->data))
break;
}
if((n->data < t->data) && (t->llink==NULL))
t->llink=n;
if((n->data > t->data) && (t->rlink==NULL))
t->rlink=n;
}
void inorder(struct node * head)
{
if(head!=NULL)
{
inorder(head->llink);
printf("%d\t",head->data);
inorder(head->rlink);
60
}
}
struct node * delenode(struct node *head,int d)
{
int f=0,f1=0;
struct node *p,*t,*t1,*x;
t=head;
//to search found or not
while(t!=NULL)
{
if(t->data==d)
{
f=1;
x=t;
break;
}
if(t->data > d)
{
p=t;
t=t->llink;
}
else if(t->data <= d)
{
p=t;
t=t->rlink;
}
}
if(f==0)
{
printf("\nGiven element not found.......");
return head;
}
//Deleted node has no child
if(x->llink==NULL && x->rlink==NULL)
{
if(p->rlink==x)
p->rlink=NULL;
else
p->llink=NULL;
free(x);
return head;
}
//deleted node has 2 children
if(x->llink!=NULL && x->rlink!=NULL)
{
p=x;
t1=x->rlink;
61
while(t1->llink!=NULL)
{
p=t1; f1=1;
t1=t1->llink;
}
if(t1->llink==NULL && t1->rlink==NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->llink;
if(f1==0)
x->rlink=t1->rlink;
free(t1);
return head;
}
if(t1->rlink!=NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->rlink;
if(f1==0)
p->rlink=t1->rlink;
free(t1);
return head;
}
}
//Deleted node has oniy right child
if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->rlink;
else
p->rlink=x->rlink;
free(x);
return head;
}
//Deleted node has oniy left child
if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->llink;
else
p->rlink=x->llink;
free(x);
return head;
}
if(x->llink!=NULL && x->rlink==NULL && x->data==head->data)
62
{
head=x->llink;
free(p);
return head;
}
if(x->llink==NULL && x->rlink!=NULL && x->data==head->data)
{
head=x->rlink;
free(p);
return head;
}
}
Output:
Result:
Thus the binary search tree is constructed and various operation are performed using C
program.
63