Documente Academic
Documente Profesional
Documente Cultură
Result :
The polynomial is represented as a linked list and polynomial addition is performed.
3. Infix to postfix
Aim:
To implement a stack and use it to convert the infix to Postfix expression.
Source Code :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40// Left parentheses precedence. Minimum of all
#define LPP 0// Addition Subtraction precedence. Minimum among all operator precedence
#define AP 1
#define SP AP// Multiplication divisor precedence.
#define MP 2
#define DP MP// Remainder precedence.
#define REMP 2
#define NONE 9
static char infix[N+1],stack[N],postfix[N+1];
static int top;
void infixtopostfix(void); /** POSTFIX CONVERSION FUNCTION **/
int gettype(char); /** TYPE OF EXPRESSION GENERATOR **/
void push(char); /** PUSH FUNCTION **/
char pop(void); /** POP FUNCTION **/
int getprec(char); /** PRECEDENCE CHECKER FUNCTION **/
void main()
{ char ch;
do { top=-1;
printf("\nEnter an infix expression\n");
fflush(stdin);
gets(infix);
infixtopostfix();
printf("\ninfix = %s\npost fix =%s\n",infix,postfix);
printf("\nDo you wish to continue\n");
ch=getche(); }while(ch=='Y' || ch=='y');}
void infixtopostfix(void)
{ int i,p,l,type,prec;
char next; i=p=0;
l=strlen(infix);
while(i<l)
{
type=gettype(infix[i]);
switch(type)
{
case LP: push(infix[i]);
break;
case RP:
while((next=pop())!='(')
postfix[p++]=next;
break;
case OPERAND:
postfix[p++]=infix[i];
break;
case OPERATOR:
prec=getprec(infix[i]);
while(top>-1 && prec <= getprec(stack[top]))
postfix[p++]=pop();
push(infix[i]); break;
} i++;
} while(top>-1)
postfix[p++]=pop();
postfix[p]='\0';
}
int gettype(char sym)
{
switch(sym)
{ case '(':
return(LP);
case ')':
return(RP);
case '+':
case '-':
case '*':
case '/':
case '%':
return(OPERATOR);
default : return(OPERAND);
}
}
Aim:
To implement a circular queue and perform its operation.
Source Code :
/*Circular queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
const max_length=4;
void dele();
void insert();
void display();
void working();
void main( )
{ int queue[max_length];
int rear; int front,key;
clrscr( );
rear=-1;
front=-1;
for(int count=0;count<max_length;count++)
queue[count]=0;
do
{ printf("\n\nIMPLEMENTATION OF CIRCULAR QUEUE\n");
printf("\n SELECT UR CHOICE: ");
input:
printf("\n 1.INSERT \n 2.DELETE \n 3.DISPLAY \n 4.EXIT \n");
printf("ENTER UR CHOICE: \n");
scanf(“%d”,&key);//=getch();
if(key=='4')
break;
else if(key=='1')
insert();
else if(key=='2')
delete();
else if(key=='3')
display();
else
goto input;
} while(1);
getch( );
}
void insert()
{ int item;
printf("enter value to insert : ");
scanf(“%d”,&item);
if((rear==front-1)||(front==0 && rear==max_length-1))
printf("\n\n queue is full\n\n");
else if(rear==-1 && front==-1)
{
rear++;
queue[rear]=item;
front=rear;
printf("\nitem is inserted into queue \n");
}
else if(rear==max_length-1 && front!=0)
{
rear=0;
queue[rear]=item;
printf(“item is inserted into queue");
}
else
{
rear++;
queue[rear]=item;
printf(“itemis inserted into queue");
}
}
void delete()
{
if(rear==-1 && front==-1)
printf("queue is empty");
else
{
printf( “%d”,queue[front],"is deleted from queue");
queue[front]=0;
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==max_length-1)
front=0;
else
front++;
}
getch();
}
void display()
{
if(front!=-1 && rear!=-1)
{
printf(" The circular queue is \n ");
for(int count=0;count<max_length;count++)
printf(“%d”,queue[count],\t");
}
else
printf("no element in queue");
getch();
}
Another Circular queue program
#include<stdio.h>
int front=-1;
int rear=-1;
int size=10;
void cq_insert(int * cq,int v) // insert element into circular queue
{
int i;
if((front-rear)==1||(front==0&&rear==size-1))
{
printf("overflow!");
getch();
return;
}
else if((front==-1)&&(rear==-1))
{
front=0;
rear=0;
cq[rear]=v;
}
else if(rear==(size-1))
{
rear=0;
cq[rear]=v;
}
else
{
rear++;
cq[rear]=v;
}
printf("\n\ncircular Queue is:\n\n"); // display
if(front<=rear)
{
i=front;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
else if(rear<front)
{
i=front;
while(i<=size-1)
{
printf("%d ",cq[i]);
i++;
}
i=0;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
getch();
}
void cq_delet(int *cq) //delee the first element in circular queue
{
int i;
if((front==-1)&&(rear==-1))
{
printf("underflow!");
return;
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==size-1)
front=0;
else
front++;
do
{
clrscr();
printf("CIRCULAR QUEUE\n");
printf("Enter a choice:\n");
printf("1.Insertion\n");
printf("2.Deletion\n");
printf("3.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the element to be inserted:");
scanf("%d",&v);
cq_insert(cqueue,v);
break;
}
case 2:
{
cq_delet(cqueue);
break;
}
case 3:
exit(0);
default:
printf("Wrong choice!");
}
}while(ch!=3);
getch();
}
Aim:
To perform In-order, Preorder and Post order traversals in Expression tree.
Algorithm:
1. Create the binary search tree
2. To perform in-order traversals
a. process the left sub tree
b. process the root
c. process the right sub-tree
3. To perform preorder traversal
a. process the root node
b. process the left
c. process the right
4. To perform post-order traversal
a. process the left node
b. process the right node.
c. process the root node.
Source Code :
#include<stdio.h>
#include<conio.h>
struct node
{ int data;
struct node*left,*right;
};
typedef struct node * nodeptr;
nodeptr root,t,p,q;
void inorder(nodeptr);
void preorder(nodeptr);
void postorder(nodeptr);
void disp(nodeptr,int,int,int);
void add();
void main()
{ int ch;
while(1)
{ printf("\n1.add\n2.disp\n3.preorder\n4.inorder\n5.Post order\n");
printf("Enter ur choice");
scanf("%d",&ch);
switch(ch)
{ case 1: add(); break;
case 2: clrscr(); disp(root,40,7,16); break;
case 3: preorder(root); break;
case 4: inorder(root); break;
case 5: postorder(root); break;
case 6: exit(0);
}
}
getch();
}
void disp(nodeptr root,int col,int row,int wid)
{gotoxy(col,row);
if(root!=NULL)
{printf("%d\t",root->data);
disp(root->left,col-wid,row+2,wid/2);
disp(root->right,col+wid,row+2,wid/2);
}
}
void preorder(nodeptr root)
{ if(root!=NULL)
{printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(nodeptr root)
{ if(root!=NULL)
{ inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
}
void postorder(nodeptr root)
{ if(root!=NULL)
{postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
}
void add()
{ int x;
printf("Enter the data");
scanf("%d",&x);
t=(nodeptr)malloc(sizeof(struct node));
t->data=x;
t->right=NULL;
t->left=NULL;
if(root==NULL)
root=t;
else
{ p=q=root;
while(q!=NULL && p->data!=x)
{ p=q;
if(x<p->data) q=p->left;
else q=p->right;
}
if(p->data==x)
printf("%d is duplicate",x);
else if(x<p->data)
p->left=t;
else p->right=t;
}
}
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 3
2 5 8
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 4
2 5 8
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 5
8 5 2
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 6
ALGORITHM:
1. Define the BST structure & read the choice to perform the operations.
2. If the choice is insert,
a. Create a node and assign input value in it.
b. Make its left and right child pointer as NULL.
c. If the inserted value is less than the root, then, place it in the left subtree.
d. Else, place it in the right subtree.
3. If the choice is findmin,
a. Traverse the left subtree.
b. Last value in the left subtree is the minimum value.
4. If the choice is findmax,
a. Traverse the right subtree.
b. Last value in the right subtree is the maximum value.
5. If the choice is find,
a. Read the element to be searched.
b. If the search element is less than root value, continue the search in the
left sub tree.
c. If the search element is greater than root value, continue the search in
the right sub tree.
d. If search element found, then, display “Element found”.
6. If the choice is display, then display all elements in BST from root to leaves.
4
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:2
Enter the elt to search 4
Element is present
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:4
minimum 1
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:5
MAXIMUM: 4
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:6
Source Code:
/*Open Hashing*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<malloc.h>
struct node
{
int num,tr;
struct node *flink,*blink;
}*p;
struct node *temp,*te;
void create(int n)
{
int i;
temp=(struct node*)malloc(sizeof(struct node));
temp->flink=NULL;
temp->blink=NULL;
temp->tr=0;
if(p==NULL)
p=temp;
for(i=1;i<n;i++)
{
te=(struct node*)malloc(sizeof(struct node));
te->flink=NULL;
te->blink=NULL;
te->tr=i;
temp->flink=te;
temp=temp->flink;
}
}
void deletehash(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while((dwn->blink->num!=no) && (dwn->blink!=NULL))
dwn=dwn->blink;
if(dwn->blink==NULL)
{
printf("The record doesn't exist.You cannot delete it.\n");
break;
}
else
{
dwn->blink=dwn->blink->blink;
break;
}//inner else
}//outer else
}
void search(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while((dwn->blink->num!=no) && (dwn->blink!=NULL))
dwn=dwn->blink;
if(dwn->blink==NULL)
{
printf("The record doesn't exist\n");
break;
}
else
{
printf("The element is found");
break;
}//inner else
}//outer else
}
void insert(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
{
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while(dwn->blink!=NULL)
dwn=dwn->blink;
temp=(struct node*)malloc(sizeof(struct node));
temp->flink=temp->blink=NULL;
temp->num=no;
dwn->blink=temp;
f=f->flink;
}
}
}
void print()
{
struct node *t=p;
printf("\n\n OPEN HASHING\n\n The hash table\n");
while(t!=NULL)
{
struct node *tt=t;
printf("hash number= %d",tt->tr);
tt=tt->blink;
printf("\n");
while(tt!=NULL)
{
printf("element= %d\n",tt->num);
tt=tt->blink;
printf("\n");
}//inner while
t=t->flink;
}//outer while
}
void main()
{
int n,num,mod,op;
char ch='y';
p=NULL;
clrscr();
printf("\n\n enter the hash size\n");
scanf("%d",&n);
create(n);
while(ch=='y')
{
printf("\n 1.insert\n 2.print\n 3.delete\n 4.search\n 5.exit\n enter ur option");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n enter the element");
scanf("%d",&num);
mod=num%n;
insert(mod,num);
break;
case 2:
print();
break;
case 3:
printf("enter the element");
scanf("%d",&num);
mod=num%n;
deletehash(mod,num);
break;
case 4:
printf("enter the element");
scanf("%d",&num);
mod=num%n;
search(mod,num);
break;
case 5:
exit(1);
} //switch
} //while
} //main
Output:
enter the hash size
5
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 1
enter the element 2
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 1
enter the element 3
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 2
OPEN HASHING
The hash table
hash number= 0
hash number= 1
hash number= 2
element= 2
hash number= 3
element= 3
hash number= 4
1.insert
2.print
3.delete
4.search
5.exit
enter ur option5
9. PRIMS algorithm:
Source Code:
/*prims algorithm */
#include<stdio.h>
#include<conio.h>
#include<process.h>
float lcost[100],a[100][100];
int closest[100],i,j,k,min,n,c=0;
void get_mat()
{
printf("Enter the No of Vertices:");
scanf("%d",&n);
printf("enter 1000 for no path \n");
printf("enter weighted matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("cost between the edge\t%d,%d:\t",i,j);
scanf("%f",&a[i][j]);
}//inner for
}//outer for
}//fn
void prim1()
{ for(i=2;i<=n;i++)
{
lcost[i]=a[1][i];
closest[i]=1;
}
printf("minimum cost spanning tree edges are \n");
for(i=2;i<=n;i++)
{
min=lcost[2];
k=2;
for(j=3;j<=n;j++)
{
if(lcost[j]<min)
{
min=lcost[j];
k=j;
}
}
c=c+min;
printf("(%d,%d)\tcost=%d\t",closest[k],k,min);
lcost[k]=2000;
for(j=2;j<=n;j++)
if((a[k][j]<lcost[j])&&(lcost[j]<2000))
{
lcost[j]=a[k][j];
closest[j]=k;
}
printf("\n");
}//outer for
printf("\n\nWeight of minimum cost spanning tree :%d",c);
getch();
}
void main()
{int ch;
clrscr();
do
{
printf("\n1.get\n2.find path with minimum cost\n3.exit\nenter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
get_mat();
break;
case 2:
prim1();
break;
case 3:
exit(0);
break;
}//switch
}while(ch<=3);//do
getch();
}//main
OUTPUT:
1.get
2.find path with minimum cost
3.exit
enter ur choice
1
Enter the No of Vertices:3
enter 1000 for no path
enter weighted matrix
cost between the edge 1,1: 1000
cost between the edge 1,2: 6
cost between the edge 1,3: 2
cost between the edge 2,1: 6
cost between the edge 2,2: 1000
cost between the edge 2,3: 5
cost between the edge 3,1: 2
cost between the edge 3,2: 5
cost between the edge 3,3: 1000
1.get
2.find path with minimum cost
3.exit
enter ur choice
2
minimum cost spanning tree edges are
(1,3) cost=2
(3,2) cost=5