Sei sulla pagina 1di 30

//Program for Circular Queue implementation through Array

#include <stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXSIZE 5
int cq[MAXSIZE];
int front,rear;
void main()
{
void add(int,int);
void del(int);
int will=1,i,num;
front = -1;
rear = -1;
clrscr();
printf("\nProgram for Circular Queue demonstration through array");
while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
add(num,MAXSIZE);
break;
case 2:
del(MAXSIZE);
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}
} //end of outer while
}
//end of main
void add(int item,int MAX)
{
//rear++;
//rear= (rear%MAX);
if(front ==(rear+1)%MAX)
{
printf("\n\nCIRCULAR QUEUE IS OVERFLOW");

}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAX;
cq[rear]=item;
printf("\n\nRear = %d Front = %d ",rear,front);
}
}
void del(int MAX)
{
int a;
if(front == -1)
{
printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");
}
else
{
a=cq[front];
if(front==rear)
front=rear=-1;
else
front = (front+1)%MAX;
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a);
printf("\n\nRear = %d Front = %d ",rear,front);
}}
OUTPUT:
1.insertion
2.deletion
3.exit
enter ur choice:1
enter the queue element:12
rear=0 front=0
enter ur choice:1
enter the queue element:2
rear=1 front=0
1.insertion
2.deletion
3.exit
enter ur choice:2
deleted element from queue is:12
rear=1 front=1

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0
struct linkedlist
{
int item;
struct linkedlist *right,*left;
};
typedef struct linkedlist node;
void main()
{
node *start,*end;
int choice;
int menu(void);
node *create(node **lastnode);
void display(node *first,node *last);
void insert(node **first,node **last);
void del(node **first,node **last);
clrscr();
printf("\n DOUBLY LINKED LIST");
printf("\n ******************");
do
{
printf("\n\nMain menu");
printf("\n\n1.Create \n2.Insert \n3.Delete \n4.Display \n5.Exit");
choice =menu();
switch(choice)
{
case 1:
printf("\n Enter the data(-999 to stop):");
start=create(&end);
continue;
case 2:
insert(&start,&end);
printf("\n");
continue;
case 3:

del(&start,&end);
printf("\n");
continue;
case 4:
display(start,end);
printf("\n");
continue;
case 5:
exit(0);
default:
printf("\n\nINVALID CHOICE...");
}
}while(1);
}
int menu()
{
int choice;
do
{
printf("\n Enter your choice:");
scanf("%d",&choice);
if(choice<1||choice>5)
printf("\n Wrong choice");
}while(choice<1||choice>5);
printf("\n");
return(choice);
}
node *create(node **lastnode)
{
node *temp,*firstnode;
int info;
*lastnode=NULL;
firstnode=NULL;
scanf("%d",&info);
while(info!=-999)
{
temp=(node *)malloc(sizeof(node));
temp->item=info;
temp->right=NULL;
if(firstnode==NULL)
{
temp->left=NULL;

firstnode=temp;
}
else
{
temp->left=(*lastnode);
(*lastnode)->right=temp;
}
(*lastnode)=temp;
scanf("%d",&info);
}
if(firstnode!=NULL)
(*lastnode)=temp;
return(firstnode);
}
void display(node *first,node *last)
{
printf("\n Forward traversal\n");
while(first!=NULL)
{
printf("%d\t",first->item);
first=first->right;
}
printf("\n Backward traversal\n");
while(last!=NULL)
{
printf("%d\t",last->item);
last=last->left;
}
return;
}
void insert(node **first,node **last)
{
node *newnode;
int newitem;
int position;
node *temp;
int i;
printf("\n New data item:");
scanf("%d",&newitem);
do
{
printf("\n Position of insertion:");
scanf("%d",&position);
}while(position<=0);

if(((*first)==NULL)||(position==1))
{
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=*first;
newnode->left=NULL;
if((*first)!=NULL)
(*first)->left=newnode;
else
(*last)=newnode;
*first=newnode;
}
else
{
i=1;
temp=*first;
while((i<position-1)&&(temp->right!=NULL))
{
i++;
temp=temp->right;
}
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=temp->right;
if(temp->right!=NULL)
temp->right->left=newnode;
newnode->left=temp;
temp->right=newnode;
}
if(newnode->right==NULL)
*last=newnode;
}
void del(node **first,node **last)
{
node *temp,*prev;
int target;
printf("\n Enter the data to be deleted:");
scanf("%d",&target);
if(*first==NULL)
printf("\n List is empty");
else if((*first)->item==target)
{
if((*first)->right==NULL)
*first=*last=NULL;
else

{
*first=(*first)->right;
(*first)->left=NULL;
}
}
else
{
temp=*first;
prev=NULL;
while((temp->right!=NULL)&&(temp->item!=target))
{
prev=temp;
temp=temp->right;
}
if(temp->item!=target)
printf("\n Element not found");
else
{
if(temp==*last)
*last=prev;
else
temp->right->left=temp->left;
prev->right=temp->right;
}
}Aa
}

OUTPUT:
1.create
2.insert
3.delete
4.diplay
5.exit
enter ur choice:1
enter the data(-999 tostop):5
10
15
-999
main menu
1.create
2.insert
3.delete
4.display
5.exit
enter ur choice:2
new data item:20
position of insertion:4
1.create
2.insert
3.delete
4.display
5.exit
enter ur choice:4
forward traversal
5 15 10 5
1.create
2.insert
3.delete
4.display
5.exit
ener ur choice:3
enter the data to be deleted:2
element not found

#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);
}
}
void push(char sym)
{
if(top>N)
{
printf("\nStack is full\n");
exit(0);
}
else
stack[++top]=sym;
}
char pop(void)
{
if(top<=-1)
{
printf("\nStack is empty\n");
exit(0);
}
else
return(stack[top--]);
}
int getprec(char sym)
{
switch(sym)
{
case '(':
return(LPP);
case '+':
return(AP);

case '-':
return(SP);
case '*':
return(MP);
case '/':
return(DP);
case '%':
return(REMP);
default :
return(NONE);
}
}

OUTPUT:
enter an infix expression
3*6+4*2/5
infix=36*6+4*2/5
postfix=36*42*5/+
do you wish to continue
y
enter an infix expression
a*bgd
infix=a*bgd
postfix=abgd*

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int data;
struct node *left,*right;
};
typedef struct node *nodeptr;
nodeptr root,t,p,q;
void add();
void search();
void findmin();
void findmax();
void disp(nodeptr,int,int,int);
void main()
{
int ch;
root=NULL;
while(1)
{
printf("\n1.add\n2.search\n3.display\n4.findmin\n5.findmax\n6.exit");
printf("\n enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: add(); break;
case 2: search(); break;
case 3: clrscr(); disp(root,40,7,16); break;
case 4: findmin(); break;
case 5: findmax(); break;
case 6: exit(0);
}
}
}
void add()
{
int x;
t=(nodeptr)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&x);
t->data=x;
t->left=NULL;
t->right=NULL;

if(root==NULL)
root=t;
else
{
p=q=root;
while(q!=NULL&&x!=p->data)
{
p=q;
if(x<p->data)
q=p->left;
else
q=p->right;
}
if(x==p->data)
printf("%d is duplicate number\n",x);
else if(x<p->data)
p->left=t;
else
p->right=t;
}
}
void search()
{
int x;
printf("enter the elt to search");
scanf("%d",&x);
p=q=root;
while(q!=NULL&&x!=p->data)
{
p=q;
if(x<p->data)
q=p->left;
else
q=p->right;
}
if(x==p->data)
printf("element is present");
else
printf("element is not present");
}
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 findmax()
{
t=root;
while(t->right!=NULL)
t=t->right;
printf("maximum %d",t->data);
}
void findmin()
{
t=root;
while(t->left!=NULL)
t=t->left;
printf("minimum %d",t->data);
}

OUTPUT:
1.add
2.search
3.display
4.find min
5.find max
6.exit
enter the choice:1
enter the data:1
1.add
2.search
3.display
4.find min
5.find max
6.exit
enter the choice:1
enter the data:3
1.add
2.search
3.display
4.find min
5.find max
6.exit
enter the choice:3
1
3
4
1.add
2.search
3.display
4.find min
5.find max
6.exit
enter the choice:6

#include<stdio.h>
#include<conio.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);
void main()
{
bool ht_inc;
int info;
int choice;
struct node *root=(struct node*)malloc(sizeof(struct node));
root=NULL;
while(1)
{
printf("1.insert\n");
printf("2.display\n");
printf("3.quit\n");
printf("enter the 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");
}
}
}
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);
}
struct node *insert(int info,struct node *pptr,int *ht_inc)
{
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:
pptr->balance=0;
*ht_inc=false;
break;
case 0:

pptr->balance=1;
break;
case 1:
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;
else
pptr->balance=0;
if(bptr->balance==-1)
aptr->balance=1;
else
aptr->balance=0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc=false;
}
}
}
if(info>pptr->info)
{
pptr->rchild=insert(info,pptr->rchild,ht_inc);
if(*ht_inc==true)
{
switch(pptr->balance)
{
case 1:
pptr->balance=0;
*ht_inc=false;

break;
case 0:
pptr->balance=-1;
break;
case -1:
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 totation\n");
bptr=aptr->lchild ;
aptr->lchild=bptr->rchild;
bptr->rchild=aptr;
pptr->rchild=bptr->lchild;
bptr->lchild=pptr;
if(bptr->balance==-1)
pptr->balance=1;
else
pptr->balance=0;
if(bptr->balance==1)
aptr->balance=-1 ;
else
aptr->balance=0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc=false;
}
}
}
return(pptr);
}
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);
}
return(0);
}
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d",ptr->info);
inorder(ptr->rchild);
}
return(0);
}

OUTPUT:
1.insert
2.diplay
3.quit
enter the choice:1
enter the value to be inserted:4
1.insert
2.display
3.quit
enter the choice is:2
tree is:
4
2
inorder travesal is:24
1.insert
2.display
3.quit
enter the choice:3

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void create();
void insert();
void delet();
void display();
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*last=NULL,*next,*prev,*cur;
void create()
{
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
printf("\nENTER THE POSITION: ");
scanf("%d",&pos);
if((pos==1) &&(first!=NULL))
{
cur->link = first;
first=cur;
}
else
{
next=first;
while(c<pos)
{
prev=next;
next=prev->link;
c++;
}
if(prev==NULL)

{
printf("\nINVALID POSITION\n");
}
else
{
cur->link=prev->link;
prev->link=cur;
}
}
}
void delet()
{
int pos,c=1;
printf("\nENTER THE POSITION : ");
scanf("%d",&pos);
if(first==NULL)
{
printf("\nLIST IS EMPTY\n");
}
else if(pos==1 && first->link==NULL)
{
printf("\n DELETED ELEMENT IS %d\n",first->data);
free(first);
first=NULL;
}
else if(pos==1 && first->link!=NULL)
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\n DELETED ELEMENT IS %d\n",cur->data);
free(cur);
}
else
{
next=first;
while(c<pos)
{
cur=next;
next=next->link;
c++;
}
cur->link=next->link;
next->link=NULL;
if(next==NULL)
{

printf("\nINVALID POSITION\n");
}
else
{
printf("\n DELETED ELEMENT IS %d\n",next->data);
free(next);
}
}
}
void display()
{
cur=first;
while(cur!=NULL)
{
printf("\n %d",cur->data);
cur=cur->link;
}
}
void main()
{
int ch;
clrscr();
printf("\n\nSINGLY LINKED LIST");
do
{
printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
insert();
display();
break;
case 3:
delet();
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice...");

}
}while(1);
}

OUTPUT:
1.creat
2.insert
3.delete
4.exit
enter ur choice:1
enter the data:2
1.create
2.insert
3.delete
4.exit
enter ur choice:3
enter ur position:1
deleted element is 2
1.create
2.insert
3.delete
4.exit
enter ur choice:2
enter the data:1
enter the position:3
invalid position 3
1.create
2.insert
3.delete
4.exit
enter ur choice:2
enterthe data:2
enter the position:2
1.create
2.insert
3.delete
4.exit

#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 number of vertices:");
scanf("%d",&n);
printf("enter 1000 for no path\n");
printf("enter weighted matrix\n");
for(i=1;i<=n;i++)
{
printf("cost between the edge\t%d,%d:\t");
scanf("%f",&a[i][j]);
}//inner for
}//outer for
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 opf minimum cost spanning tree:%d",c);
getch();
}
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 no.of vertices:3
enter 1000 for no path
enter weighted matrix
cost between the edges 1,1: 1000
cost between the edges 1,2: 6
cost betweeen the edges1,3: 2
cost between the edges 2'1: 6
cost between the edges 2,2: 1000
cost between the edges 2,3: 5
cost between the edges 3.1: 2
cost between the edges 3,2: 5

cost between the edges 3,3: 1000


#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);
}
}
}
void disp(nodeptr root,int col,int wid,int row)
{
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;
}
}

OUTPUT:
1.add
2.disp
3.preorder
4.inorder
5.postorder
enter ur choice:1
enter the data:1
1.add
2.disp
3.preorder
4.inorder
5.postorder
enter ur choice:1
enter the data:5
5 is duplicate
1.add
2.disp
3.preorder
4.inorder
5.postorder
enter ur choice:2
1.add
2.disp
3.preorder
4.inorder
5.postorder
1 5
8
2
enter ur choice:6

Potrebbero piacerti anche