Professional Documents
Culture Documents
Ds Prinouts
Ds Prinouts
VANDHANA
Reg no:31509104108
Title:TREE TRAVERSAL
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct tree *node;
node insertion(int,node T);
void inorder(node T);
void preorder(node T);
void postorder(node T);
void padding ( char ch, int n );
void structure ( struct tree *T, int level );
struct tree
{
int ele;
struct tree *rlink,*llink;
}*T=NULL;
main()
{
node T=NULL;
int data,ch,i=0,n;
printf("\n enter the number of elements....");
scanf("%d",&n);
while(i<n)
{printf("\n enter the element.....");
scanf("%d",&data);
T=insertion(data,T);
i++;
}
if ( T == NULL )
{
padding ( '\t', level );
puts ( "~" );
}
else
{
structure ( T->rlink, level + 1 );
padding ( '\t', level );
printf ( "%d\n", T->ele );
structure ( T->llink, level + 1 );
}
}
inorder traversal.....6 8 9 13 14 15 16
preoder traversal.....13 8 6 9 15 14 16
postorder traversal....6 9 8 14 16 15 13
//binary search
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left,*right;
};
if(T==NULL)
{
T=(struct tree*)malloc(sizeof(struct tree *));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else if(x<T->data)
{
T->left=insert(x,T->left);
}
else if(x>T->data)
{
T->right=insert(x,T->right);
}
return T;
}
if ( T == NULL )
{
padding ( '\t', level );
puts ( "-" );
}
else
{
structure ( T->right, level + 1 );
padding ( '\t', level );
printf ( "%d\n", T->data );
structure ( T->left, level + 1 );
}
}
void sort(struct tree * T)
{
if(T!=NULL)
{
sort(T->left);
printf("%d\t",T->data);
sort(T->right);
}}
main()
{
int a,i,ch,n;
struct tree *root=NULL;
menu :
printf("\nMENU\n1.Create tree\n2.Insert element\n3.Delete element\n4.Display
tree\n5.sorting the elements\n6.Exit\nEnter your choice : ");
scanf("%d",&ch);
loop:
switch(ch)
{
case 1:
printf("enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements(root first):\n ");
for(i=0;i<n;i++)
{
scanf("%d",&a);
root=insert(a,root);
}
structure(root,0);
break;
case 2 :
printf("Enter the element to be inserted : ");
scanf("%d",&a);
root=insert(a,root);
structure(root,0);
break;
case 3:
printf("enter the element to be deleted : ");
scanf("%d",&a);
root=del(a,root);
structure(root,0);
break;
case 4 :
structure(root,0);
break;
case 5 :
sort(root);
break;
case 6 :
exit (0);
break;
default :
printf("Enter a valid choice : ");
scanf("%d",&ch);
goto loop;
}
goto menu;
getch();}
MENU
1.Create tree
2.Insert element
3.Delete element
4.Display tree
5.Exit
Enter your choice : 1
enter the number of elements : 5
Enter the elements(root first):
8
9
6
4
7
~
9
~
8
~
7
~
6
~
4
~
MENU
1.Create tree
2.Insert element
3.Delete element
4.Display tree
5.Exit
Enter your choice : 2
Enter the element to be inserted : 3
~
9
~
8
~
7
~
6
~
4
~
3
~
MENU
1.Create tree
2.Insert element
3.Delete element
4.Display tree
5.Exit
Enter your choice : 3
enter the element to be deleted : 3
~
9
~
8
~
7
~
6
~
4
~
MENU
1.Create tree
2.Insert element
3.Delete element
4.Display tree
5.Exit
Enter your choice : 4
~
9
~
8
~
7
~
6
~
4
~
MENU
1.Create tree
2.Insert element
3.Delete element
4.Display tree
5.Exit
Enter your choice : 5
NAME::S.VANDHANA
REG NO:31509104108
TITLE:AVL TREE INSERTION
SOURCE CODE::
}
void disp(circlist head)
{ circlist temp;
printf("\nStatus:\n");
if(!head)
{printf("\nNULL");return;}
temp=head;
while(temp->next!=head)
{printf("%d-->",temp->item);
temp=temp->next;
}
printf("%d-->NULL",temp->item);
}
void delall(circlist head)
{ circlist t=head;
head=NULL;
free(t);
}
void josephus(int m,int n)
{ circlist head=NULL,temp;
int i,j;
for(i=1;i<=n;i++)
head=insert(head,i);
disp(head);
for(temp=head,i=0;head->next!=head;i++)
if(i==m)
{j=temp->item;
temp=temp->next;
head=del(head,j);
i=-1;
disp(head);
}
else
{temp=temp->next;}
printf("\nThe winner is:%d",head->item);
delall(head);
}
int main()
{ int m,n,i;
printf("\nEnter the number of players N:");
scanf("%d",&n);
printf("\nEnter the number of passes M:");
scanf("%d",&m);
josephus(m,n);
getch();
}
Status:
1-->2-->3-->4-->NULL
Status:
1-->2-->4-->NULL
Status:
1-->4-->NULL
Status:
1-->NULL
The winner is:1
NAME:S.VANDHANA
REG NO:31509104108
TITLE:CIRCULAR QUEUES
//Queue in arrays
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef int* queue;
void enqueue(int* front,int* rear,queue q,int i)
{ if(*rear==MAX-1&&*front==0||*rear==*front-1)
{printf("\nSorry!Queue is full!");
return;
}
else if(*rear==MAX-1)
*rear=0;
else if(*rear==-1)
*front=*rear=0;
else
(*rear)++;
q[*rear]=i;
}
int dequeue(int* front,int* rear,queue q)
{ int temp;
if(*front==-1)
{printf("\nSorry!Queue is empty!");
return -999;
}
temp=q[*front];
if(*front==*rear)
*front=*rear=-1;
else if(*front==MAX-1)
*front=0;
else
(*front)++;
return temp;
}
void display(int front,int rear,queue q)
{ int temp;
printf("\nQUEUE ELEMENTS......:\n");
if(front<=rear)
for(temp=front;temp!=-1&&temp<=rear;temp++)
printf("%d-->",q[temp]);
else
{for(temp=front;temp!=-1&&temp<MAX;temp++)
printf("%d-->",q[temp]);
for(temp=0;temp<=rear;temp++)
printf("%d-->",q[temp]);
}
printf("%d",q[front]);
}
void find(int front,int rear,queue q,int i)
{ int temp=1,t;
if(front<=rear)
{for(t=front;t!=-1&&t<=rear;t++,temp++)
if(q[t]==i)
{printf("\nItem %d found in position %d",i,temp);
return;
}}
else
{for(t=front;t!=-1&&t<MAX;t++,temp++)
if(q[t]==i)
{printf("\nItem %d found in position %d",i,temp);
return;
}
for(t=0;t<=rear;t++,temp++)
if(q[t]==i)
{printf("\nItem %d found in position %d",i,temp);
return;
}
}
printf("\nItem not found in queue!");
}
int isfull(int rear,int front)
{ if(rear==MAX-1&&front==0||rear==front-1)
return 1;
return 0;
}
int isempty(int front)
{ if(front==-1)
return 1;
return 0;
}
int main()
{ int ch,d,front,rear,q[MAX];
front=rear=-1;
do
{ printf("\nMenu\n1.Enqueue 2.Dequeue 3.Find 4.Isfull");
printf(" 5.Isempty 6.Display 7.Exit\nEnter choice:");
scanf("%d",&ch);
if(ch==1)
{printf("Enter the item to be inserted:");
scanf("%d",&d);
enqueue(&front,&rear,q,d);
display(front,rear,q);
}
else if(ch==2)
{d=dequeue(&front,&rear,q);
if(d!=-999)
printf("\nDequeued element=%d",d);
display(front,rear,q);
}
else if(ch==3)
{printf("Enter the item to be found:");
scanf("%d",&d);
find(front,rear,q,d);
}
else if(ch==4)
if(isfull(rear,front))
printf("\nQueue is full!");
else
printf("\nQueue is not full!");
else if(ch==5)
if(isempty(front))
printf("\nQueue is empty!");
else
printf("\nQueue is not empty!");
else if(ch==6)
display(front,rear,q);
else if(ch==7)
printf("Terminating!");
}while(ch!=7);
}
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:1
Enter the item to be inserted:6
QUEUE ELEMENTS......:
6-->6
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:1
Enter the item to be inserted:7
QUEUE ELEMENTS......:
6-->7-->6
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:1
Enter the item to be inserted:8
QUEUE ELEMENTS......:
6-->7-->8-->6
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:4
Dequeued element=6
QUEUE ELEMENTS......:
7-->8-->7
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:3
Enter the item to be found:8
QUEUE ELEMENTS......:
7-->8-->7
Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:7
NAME:S.VANDHANA
REG NO:31509104108
TITLE::ARRAY IMPLEMENTATION OF QUEUES
#include<stdio.h>
#include<conio.h>
int q[20],n=5,f=0,r=0;
main()
{
void insert();
void deleted();
void display();
int a;
do
{
printf("\n menu \n1.insertion \n2.deletion \n3.display \n4.exit");
printf("\n enter your choice..");
scanf("%d",&a);
switch(a)
{
case 1:insert();
break;
case 2:deleted();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\n invalid choice...");
break;
}
}
while(a<4);
getch();
}
void insert()
{
int b;
if(r>=n)
{
printf("\n overflow..");
return;
}
else
{
printf("\n enter the element to be inserted..");
scanf("%d",&b);
r++;
q[r]=b;
printf("\n the element inserted is %d",q[r]);
f=1;
return;
}}
void deleted()
{
int b;
if(f==0)
{
printf("\n queue overflow...");
return;
}
else
{
b=q[f];
printf("\n the element deleted is %d",b);
if(f==r)
f=r=0;
else
f++;
return;
}}
void display()
{
int i;
if(r==0)
{
printf("\n queue is empty..");
return;
}
else
{
for(i=1;i<=r;i++)
printf("\n %d\t",q[i]);
}}
menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..1
enter the element to be inserted..7
the element inserted is 7
menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..1
7
5
6
menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..4
NAME:S.VANDHANA
REG NO:31509104108
TITLE:: DOUBLE ENDED QUEUE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
const int count=5; int r;
static int c=0;
struct node
{
int data;
struct node *next;
struct node *prev;
}*front=NULL,*rear=NULL;
void addatend()
{int x;
struct node *q;
q=(struct node *)malloc(sizeof(struct node));
if(c<count)
{if(front==NULL)
{
printf("\n enter the element to be inserted...");
scanf("%d",&x);
q->data=x;
q->prev=NULL;
q->next=NULL;
front=q;
rear=q;c++;
}
else
{
printf("\n enter the element to be inserted...");
scanf("%d",&x);
q->data=x;
q->prev=rear;
q->next=NULL;
rear->next=q;
rear=q;c++;
}}
else{
printf("\n insertion not possible...queue is full..");
r=5;}
if(c==4)
r=5;
}
void addatbeg()
{
int x;
struct node *q;
q=(struct node *)malloc(sizeof(struct node));
if(c<count)
{if(front==NULL)
{
printf("\n enter the element to be inserted...");
scanf("%d",&x);
q->data=x;
q->prev=NULL;
q->next=NULL;
front=q;c++;rear=q;
}
else
{
printf("\n enter the element to be inserted...");
scanf("%d",&x);
q->data=x;
q->next=front;
q->prev=NULL;
front=q;
c++;
}}
else{
printf("\n insertion not possible...queue is full..");
r=4;}
if(c==4)
r=4;
}
void delatbeg()
{int temp;
struct node *q;
if(front==NULL)
printf("\n queue empty...deletion not possible...");
else
{
q=front;
temp=q->data;
front=q->next;
q->next->prev=NULL;
free(q);
if(front==NULL)
rear=NULL;r--;
printf("\n the deleted element is %d ",temp);
}
}
void delatend()
{
struct node *q,*left,*temp;
int item;
temp=front;
if(front==NULL)
printf("\n queue is empty....deletion not possible...");
else
{
while(temp!=rear)
{
left=temp;
temp=temp->next;
}
q=rear;
item=q->data;
free(q);
rear=left;
rear->next=NULL;
if(rear==NULL)
front=NULL;
printf("\n the deleted element is %d ",item);
r--;}
}
void isfull()
{
if(r==5)
printf("\n queue is full...");
else
printf("\n queue not full...");
}
void isempty()
{
if(rear==NULL)
printf("\n queue is empty...");
else
printf("\n queue not empty...");
}
void display()
{struct node *q;
q=front;
//printf("\n front->");
while(q!=NULL)
{
//if(q->next==NULL)
//printf("<-rear");
printf("%d\t",q->data);
q=q->next;
}
printf("\n");
}
main()
{int ch;
do
{
printf("\n1.insertion at beg 2.insertion at end 3.deletion at beg \n4.deletion at end 5.isempty
6.isfull 7.display 8.exit...");
printf("\n enter your choice...");
scanf("%2d",&ch);
switch(ch)
{
case 1:addatbeg();
break;
case 2:addatend();
break;
case 3:delatbeg();
break;
case 4:delatend();
break;
case 5:isempty();
break;
case 6:isfull();
break;
case 7:display();
break;
}
}while(ch<8);
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*front=NULL,*rear=NULL;
const int count=5;int r;
void insert()
{static int c=0;
int x;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
if(c<count)
{if(rear==NULL)
{printf("\n enter the value to be inserted...");
scanf("%d",&x);
newnode->data=x;
newnode->link=NULL;
front=newnode;
rear=newnode;
c++;
}
else
{printf("\n enter the value to be inserted...");
scanf("%d",&x);
newnode->data=x;
newnode->link=NULL;
rear->link=newnode;
rear=newnode;
c++;
}
}
else
{
printf("\n queue not empty...");
}return;}
main()
{
int ch;
do
{
printf("\n1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...");
printf("\n enter your choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:isfull();
break;
case 5:isempty();
break;
}
}while(ch<6);
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*front=NULL,*rear=NULL;
void insert()
{static int ele=1;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
{if(rear==NULL)
{newnode->data=ele;ele++;
newnode->link=front;
front=newnode;
rear=newnode;
}
else
{
newnode->data=ele;
ele++;
newnode->link=front;
rear->link=newnode;
rear=newnode;
}
printf("\n the inserted player %d ",newnode->data);
}
return;}
void display()
{int i,j;
struct node *temp;
temp=front;
printf("\n enter the elements to be displayed...");
scanf("%d",&i);
printf("\n the contents of the queue are...\n");
for(j=0;j<i;j++)
{
printf("%d\n",temp->data);
temp=temp->link;
}return;
}
void games(int players)
{int r=0;int da,inter,i;
printf("\n enter the interval...");
scanf("%d",&inter);
struct node *newnode,*another;
newnode=front;another=front;
while(r<players)
{if(r==players-1)
{
printf("\n the winner is %d ",newnode->data);
break;
}
else
{
for(i=0;i<inter;i++)
another=another->link;
for(i=0;i<inter-1;i++)
newnode=newnode->link;
newnode->link=another->link;
da=another->data;
newnode=another->link;
another=another->link;
printf("\n the eliminated person in the round %d is %d ",r+1,da);
r++;
}}}
main()
{
int i,ch;int r;
do
{
printf("\n1.insertion 2.display before playing 3.play the game 4.exit...");
printf("\n enter your choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter the number of players....");
scanf("%d",&r);
for(i=0;i<r;i++)
{insert();}
break;
case 2:display();
break;
case 3:games(r);
break;
}
}while(ch<4);
}
SOURCE CODE::
#include<iostream.h>
#include<conio.h>
class queue;
class stack
{
int a[3];
public:
int top;
stack()
{
top=-1;
}
void push()
{
int item;
try
{
if(top==2)
throw 0;
else
{
cout<<"\nEnter the element to be inserted:";
cin>>a[++top];
}
}
catch(int b)
{
if(b==0)
cout<<"\n STACK IS FULL...OVERFLOW!!!";
}
}
void pop()
{
try
{
if(top==-1)
throw 1;
else
cout<<"\nThe popped element is :"<<a[top--];
}
catch(int b)
{
if(b==1)
cout<<"\nSTACK IS EMPTY!!UNDERFLOW!!!";
}
}
void disp()
{
int i;
try
{
if(top==-1)
throw 3;
else
{
cout<<"\nThe elements of the stack are:";
for(i=top;i>=0;i--)
cout<<"\t"<<a[i];
}
}
catch(int b)
{
if(b==3)
cout<<"\n Stack is empty!!!\n";
}
}
};
class queue
{
int q[3];
public:
int front,rear;
queue()
{
front=0;
rear=front-1;
}
void enqueue()
{
try
{
if(rear==2)
throw 1;
else
{
cout<<"\n Enter the number to be inserted:";
cin>>q[++rear];
}
}
catch(int x)
{
cout<<"\nQUEUE IS FULL!!!";
}
}
void dequeue()
{
try
{
if(rear==front-1)
throw 2;
else
{
cout<<"The Deleted item is:"<<q[front];
front++;
}
}
catch(int x)
{
cout<<"QUEUE IS EMPTY!!!";
}
}
void dispq()
{
int i;
try
{
if(rear==front-1)
throw 3;
else
{
cout<<"\nThe elements in queue are:";
for(i=front;i<=rear;i++)
cout<<"\t"<<q[i];
}
}
catch(int x)
{
cout<<"\nQueue is empty";
}
}
};
main()
{
int c=1,ch=1,ch1=1;
stack s;
queue q;
while(c!=3)
{
cout<<"\n1.Stack\n2.Queue\n3.Exit\nEnter your choice:";
cin>>c;
if(c==1)
{
while(ch!=4)
{
cout<<"\n1.Push\n2.Pop\n3.Display\n4.Exit";
cin>>ch;
switch(ch)
{
case 1:s.push();
s.disp();break;
case 2:s.pop();
s.disp();break;
case 3:s.disp();
}
}
}
else if(c==2)
{
while(ch1!=4)
{
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit";
cin>>ch1;
switch(ch1)
{
case 1:q.enqueue();
q.dispq();break;
case 2:q.dequeue();
q.dispq();break;
case 3:q.dispq();
}
}
}
getch();
}
}
1.Stack
2.Queue
3.Exit
Enter your choice:1
1.Push
2.Pop
3.Display
4.Exit1
1.Stack
2.Queue
3.Exit
Enter your choice:2
1.Enqueue
2.Dequeue
3.Display
4.Exit1
1.Stack
2.Queue
3.Exit
Enter your choice:3
ELECTRONIC VOTING SYSTEM::
SOURCE CODE::
#include<iostream.h>
#include<conio.h>
struct canditate
{
char name[10];
int vno,cno;
}c[10];
class election
{
public:
int sc,age,sv,check[20];
char grad,citi,pol;
election()
{
sc=0;
sv=0;
}
void candinfo()
{
cout<<"enter your name";
cin>>c[sc].name;
cout<<"enter your age";
cin>>age;
if(age>40)
throw sc;
else
{
cout<<"are you a graduate(y/n)";
cin>>grad;
if(grad=='y')
throw sc;
else
{
cout<<"are u de citizen of INDIA(y/n)";
cin>>citi;
if(citi=='n')
throw sc;
else
{
cout<<"do u hae any police record(y/n)";
cin>>pol;
if(pol=='y')
throw sc;
else
{
cout<<"you are a valid candidate";
c[sc].cno=sc+1;
c[sc].vno=0;
sc++;
}
}
}
}
}
void display()
{
cout<<"candidate info";
int i;
cout<<"\n NAME \t CANDIDATE NO";
for(i=0;i<sc;i++)
cout<<"\n"<<c[i].name<<"\t"<<c[i].cno;
}
void vote()
{
int i,k=0,id,ag,no;
char cit;
cout<<"\nenter your age";
cin>>ag;
if(ag<18)
throw ag;
else
{
cout<<"\nAre you a citizen of INDIA(y/n)";
cin>>cit;
if(cit=='n')
throw sv;
else
{
cout<<"\nyou are a valid candidate";
cout<<"\nenter your id no";
cin>>id;
check[sv]=id;
}
}
if(sv==0)
{
cout<<"\nenter the candidate no corresponding to whom you want to caste
your vote";
cin>>no;
for(i=0;i<sc;i++)
if(no==c[i].cno)
{
k=1;
c[i].vno+=1;
cout<<"\nyour vote is casted";
}
if(k==0)
cout<<"\ninvalid candidate number";
sv++;
}
else
{
for(i=0;i<sv;i++)
{
cout<<check[i];
if(id==check[i])
throw 'y';
}
cout<<"\nenter the candidate no corresponding to whom you want to caste
your vote";
cin>>no;
for(i=0;i<sc;i++)
if(no==c[i].cno)
{
k=1;
c[i].vno+=1;
cout<<"\nyour vote is casted";
}
if(k==0)
cout<<" \ninvalid candidate number";
sv++;
}
}
void calculate()
{
int i,a[10],j,temp;
for(i=0;i<sc;i++)
a[i]=c[i].vno;
for(i=0;i<sc-1;i++)
for(j=i+1;j<sc;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<sc;i++)
if(a[0]==c[i].vno)
cout<<"\n the winner is "<<c[i].name;
}
};
main()
{
election e1;
int ch=0;
while(ch!=5)
{ cout<<"\n MENU";
cout<<"\n1.apply for candidate\n 2.candidate info\n 3.vote\n4.Want to know the
winner\n 5.exit";
cin>>ch;
switch(ch)
{
case 1:
try
{
e1.candinfo();
}
catch(int i)
{
cout<<"\ninvalid candidate";
}
break;
case 2:
e1.display();
break;
case 3:
try
{
e1.vote();
}
catch(int i)
{
cout<<"\ninvalid voter";
}
catch(char y)
{
cout<<"\n you have already voted";
}
break;
case 4:
e1.calculate();
break;
}
}
getch();
}
SAMPLE INPUT AND OUTPUT::
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit1
enter the candidate no corresponding to whom you want to caste your vote1
enter the candidate no corresponding to whom you want to caste your vote1
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit1
invalid candidate
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit