You are on page 1of 51

Name:S.

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++;
}

printf("\n 1.inorder 2.preorder 3.postoroder 4.display 5.exit");


do
{
printf("\n enter the choice.....");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n inorder traversal.....");
inorder(T);
break;
case 2:printf("\n preoder traversal.....");
preorder(T);
break;
case 3:printf("\n postorder traversal....");
postorder(T);
break;
case 4:
structure(T,0);
break;
default:printf("\n terminating....");
}
}
while(ch<4);
getch();
}
node insertion(int x,node T)
{
struct tree * newnode;
newnode=(struct tree *)malloc(sizeof(struct tree));
if(newnode==NULL)
printf("\n no memory...");
else
{
if(T==NULL)
{
newnode->ele=x;
newnode->llink=NULL;
newnode->rlink=NULL;
T=newnode;
}
else
{
if(x<T->ele)
T->llink=insertion(x,T->llink);
else
T->rlink=insertion(x,T->rlink);
}
}
return T;
}
void inorder(node T)
{
if(T!=NULL)
{
inorder(T->llink);
printf("%d\t",T->ele);
inorder(T->rlink);
}}
void preorder(node T)
{
if(T!=NULL)
{
printf("%d\t",T->ele);
preorder(T->llink);
preorder(T->rlink);
}}
void postorder(node T)
{
if(T!=NULL)
{
postorder(T->llink);
postorder(T->rlink);
printf("%d\t",T->ele);
}}

void padding ( char ch, int n )


{
int i;

for ( i = 0; i < n; i++ )


putchar ( ch );
}

void structure ( struct tree *T, int level )


{
int 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 );
}
}

SAMPLE INPUT AND OUTPUT::

enter the number of elements....7

enter the element.....13

enter the element.....8

enter the element.....15

enter the element.....6

enter the element.....9

enter the element.....14

enter the element.....16

1.inorder 2.preorder 3.postoroder 4.display 5.exit


enter the choice.....1

inorder traversal.....6 8 9 13 14 15 16

enter the choice.....2

preoder traversal.....13 8 6 9 15 14 16

enter the choice.....3

postorder traversal....6 9 8 14 16 15 13

enter the choice.....4


~
16
~
15
~
14
~
13
~
9
~
8
~
6
~
Name:S.VANDHANA
Reg no:31509104108
Title:binary search tree

//binary search
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left,*right;
};

struct tree * insert(int x,struct tree *T)


{

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;
}

struct tree * min(struct tree *T)


{
if(T==NULL)
return NULL;
else if(T->left==NULL)
return T;
else
return min(T->left);
}
struct tree * del(int x,struct tree *T)
{
if(x<T->data)
T->left=del(x,T->left);
else if(x>T->data)
T->right=del(x,T->right);
else if(T->left&&T->right)
{
struct tree *temp;
temp=(struct tree *)malloc(sizeof(struct tree *));
temp=min(T->right);
T->data=temp->data;
T->right=del(temp->data,T->right);
}
else
{
if(T->left==NULL)
T=T->right;
else if(T->right==NULL)
T=T->left;
}
return T;
}
void padding ( char ch, int n )
{
int i;

for ( i = 0; i < n; i++ )


putchar ( ch );
}

void structure ( struct tree *T, int level )


{
int i;

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();}

sample input and output::

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::

//Insertion In a AVL Tree


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item,height;
struct node* left,*right;
};
typedef struct node* Tree;
void newdisp(Tree T)
{ static int i=0,j;
if(T!=NULL)
{printf("%d\t",T->item);
i++;
newdisp(T->right);
printf("\n");
for(j=0;j<i;j++)
printf("\t");
newdisp(T->left);
i--;
}
else
printf("--\t");
}
int max(int a,int b)
{ if(a>b)
return a;
return b;
}
int findheight(Tree T)
{ if(T==NULL)
return -1;
return T->height;
}
Tree singlerightrotate(Tree Par)
{ Tree temp=Par->right;
Par->right=temp->left;
temp->left=Par;
Par->height=(max(findheight(Par->left),findheight(Par->right))+1);
temp->height=(max(findheight(temp->left),findheight(temp->right))+1);
return temp;
}
Tree singleleftrotate(Tree Par)
{ Tree temp;
temp=Par->left;
Par->left=temp->right;
temp->right=Par;
Par->height=max(findheight(Par->left),findheight(Par->right))+1;
temp->height=max(findheight(temp->left),findheight(temp->right))+1;
return temp;
}
Tree doublerightrotate(Tree Par)
{ Par->right=singleleftrotate(Par->right);
Tree temp=singlerightrotate(Par);
return temp;
}
Tree doubleleftrotate(Tree Par)
{ Par->left=singlerightrotate(Par->left);
Tree temp=singleleftrotate(Par);
return temp;
}
Tree insert(Tree T,int i)
{
if(T==NULL)
{T=(Tree)malloc(sizeof(struct node));
T->item=i;
T->left=NULL;
T->right=NULL;
T->height=0;
}
else if(T->item<i)
{T->right=insert(T->right,i);
if((findheight(T->right)-findheight(T->left))== 2)
if(i> T->right->item)
T=singlerightrotate(T);
else
T=doublerightrotate(T);
}
else if(T->item>i)
{T->left=insert(T->left,i);
if((findheight(T->left)-findheight(T->right))== 2)
if(i< T->left->item)
T=singleleftrotate(T);
else
T=doubleleftrotate(T);
}
else
printf("\nSorry!A duplicate key cannot be inserted!");
T->height=(max(findheight(T->left),findheight(T->right))+1);
return T;
}
void freetree(Tree t)
{ if(t!=NULL)
{freetree(t->left);
freetree(t->right);
free(t);
}
}
int main()
{ Tree root=NULL;
int n=1;
while(n!=0)
{printf("\nEnter element to be inserted(0 to exit):");
scanf("%d",&n);
if(n)
{root=insert(root,n);
printf("\nTree after balancing:\n");
newdisp(root);
}
}
freetree(root);
getch();
}

SAMPLE INPUT AND OUTPUT::

Enter element to be inserted(0 to exit):9

Tree after balancing:


9 --
--
Enter element to be inserted(0 to exit):5

Tree after balancing:


9 --
5 --
--
Enter element to be inserted(0 to exit):7

Tree after balancing:


7 9 --
--
5 --
--
Enter element to be inserted(0 to exit):3

Tree after balancing:


7 9 --
--
5 --
3 --
--
Enter element to be inserted(0 to exit):5

Sorry!A duplicate key cannot be inserted!


Tree after balancing:
7 9 --
--
5 --
3 --
--
Enter element to be inserted(0 to exit):6

Tree after balancing:


7 9 --
--
5 6 --
--
3 --
--
Enter element to be inserted(0 to exit):8

Tree after balancing:


7 9 --
8 --
--
5 6 --
--
3 --
--
Enter element to be inserted(0 to exit):10

Tree after balancing:


7 9 10 --
--
8 --
--
5 6 --
--
3 --
--
Enter element to be inserted(0 to exit):0
NAME::S.VANDHANA
REG NO:31509104108
TITLE:JOSEPHUS PROBLEM USING CIRCULAR LINKED LIST

//josephus problem using circular linked list


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item;
struct node* next;
};
typedef struct node* circlist;
struct node* insert(circlist head,int i)
{ circlist t,temp;
t=(circlist)malloc(sizeof(struct node));
if(!t)
return head;
t->item=i;
t->next=NULL;
if(head==NULL)
{head=t;
head->next=head;
}
else
{for(temp=head;temp->next!=head;temp=temp->next);
t->next=temp->next;
temp->next=t;
}
return head;
}
struct node* del(circlist head,int i)
{ circlist temp,t;
if(head==NULL)
return head;
for(temp=head;temp->next!=head;temp=temp->next)
if(temp->next->item==i)
{t=temp->next;
temp->next=t->next;
free(t);
return head;
}
if(head->item==i)
{t=head;
head=head->next;
temp->next=head;
free(t);
return head;
}
if(temp->next==head)
{printf("\nSorry!Item not found!!!");
return head;}

}
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();
}

SAMPLE INPUT AND OUTPUT::

Enter the number of players N:4

Enter the number of passes M:2

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);
}

SAMPLE INPUT AND OUTPUT::

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

Queue is not full!


Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:5

Queue is not empty!


Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:2

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

Item 8 found in position 2


Menu
1.Enqueue 2.Dequeue 3.Find 4.Isfull 5.Isempty 6.Display 7.Exit
Enter choice:6

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]);
}}

SAMPLE INPUT AND OUTPUT::

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

enter the element to be inserted..5


the element inserted is 5
menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..1

enter the element to be inserted..6

the element inserted is 6


menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..2

the element deleted is 7


menu
1.insertion
2.deletion
3.display
4.exit
enter your choice..3

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);
}

SAMPLE INPUT AND OUTPUT::

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...1

enter the element to be inserted...5

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...1

enter the element to be inserted...6

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...2

enter the element to be inserted...4

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...1

enter the element to be inserted...7

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...3

the deleted element is 7


1.insertion at beg 2.insertion at end 3.deletion at beg
4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...4

the deleted element is 4


1.insertion at beg 2.insertion at end 3.deletion at beg
4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...5

queue not empty...


1.insertion at beg 2.insertion at end 3.deletion at beg
4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...6

queue not full...


1.insertion at beg 2.insertion at end 3.deletion at beg
4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...7
6 5

1.insertion at beg 2.insertion at end 3.deletion at beg


4.deletion at end 5.isempty 6.isfull 7.display 8.exit...
enter your choice...8
NAME:S.VANDHANA
REG NO:31509104108
TITLE::QUEUE USING SINGLE LINKED LIST

#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++;
}

printf("\n the inserted element is %d ",newnode->data);


}
else
{
printf("\n insertion not possible....queue is full");
r=4;}
if(c==4)
r=4;
return;}
void isfull()
{
if(r==4)
printf("\n queue is full...");
else
printf("\n queue not full...");
return;}
void delete()
{
struct node *temp;
if(front==NULL)
{
printf("\n queue empty...deletion not possible...");
return;
}
temp=front;
if(front==rear)
front=rear=NULL;
else
front=front->link;
printf("\n the deleted element is %d ",temp->data);
free(temp);
r--;
return;}
void display()
{
struct node *temp;
temp=front;
printf("\n the contents of the queue are...");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}return;
}
void isempty()
{
if(front==NULL)
{
printf("\n queue is empty...");

}
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);
}

Sample input and output::

1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...


enter your choice...1

enter the value to be inserted...5

the inserted element is 5


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...1

enter the value to be inserted...6

the inserted element is 6


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...1

enter the value to be inserted...7

the inserted element is 7


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...2

the deleted element is 5


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...3

the contents of the queue are...6


7

1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...


enter your choice...4

queue not full...


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...5

queue not empty...


1.insertion 2.deletion 3.display 4.isfull 5.isempty 6.exit...
enter your choice...6
NAME:S.VANDHANA
REG NO:31509104108
TITLE:JOSEPHUS PROBLEM USING CIRCULAR QUEUES

#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);
}

SAMPLE INPUT AND OUTPUT::

1.insertion 2.display before playing 3.play the game 4.exit...


enter your choice...1

enter the number of players....4

the inserted player 1


the inserted player 2
the inserted player 3
the inserted player 4
1.insertion 2.display before playing 3.play the game 4.exit...
enter your choice...2

enter the elements to be displayed...5

the contents of the queue are...


1
2
3
4
1

1.insertion 2.display before playing 3.play the game 4.exit...


enter your choice...3

enter the interval...2

the eliminated person in the round 1 is 3


the eliminated person in the round 2 is 2
the eliminated person in the round 3 is 4
the winner is 1
1.insertion 2.display before playing 3.play the game 4.exit...
enter your choice...4
NAME:S.VANDHANA
REG NO:31509104108
TITLE:EXCEPTION HANDLING

STACKS AND QUEUES

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();
}
}

SAMPLE INPUT AND OUTPUT::

1.Stack
2.Queue
3.Exit
Enter your choice:1

1.Push
2.Pop
3.Display
4.Exit1

Enter the element to be inserted:4

The elements of the stack are: 4


1.Push
2.Pop
3.Display
4.Exit1

Enter the element to be inserted:6

The elements of the stack are: 6 4


1.Push
2.Pop
3.Display
4.Exit1

Enter the element to be inserted:7

The elements of the stack are: 7 6 4


1.Push
2.Pop
3.Display
4.Exit2

The popped element is :7


The elements of the stack are: 6 4
1.Push
2.Pop
3.Display
4.Exit3

The elements of the stack are: 6 4


1.Push
2.Pop
3.Display
4.Exit4

1.Stack
2.Queue
3.Exit
Enter your choice:2

1.Enqueue
2.Dequeue
3.Display
4.Exit1

Enter the number to be inserted:5

The elements in queue are: 5


1.Enqueue
2.Dequeue
3.Display
4.Exit1

Enter the number to be inserted:7

The elements in queue are: 5 7


1.Enqueue
2.Dequeue
3.Display
4.Exit1

Enter the number to be inserted:9

The elements in queue are: 5 7 9


1.Enqueue
2.Dequeue
3.Display
4.Exit2
The Deleted item is:5
The elements in queue are: 7 9
1.Enqueue
2.Dequeue
3.Display
4.Exit3

The elements in queue are: 7 9


1.Enqueue
2.Dequeue
3.Display
4.Exit4

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 your name..RAM

enter your age..34

are you a graduate(y/n)...Y

are you the citizen of INDIA(y/n)...Y

do u have any police record(y/n)....N


you are a valid candidate....
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit1

enter your name..DEV

enter your age..39

are you a graduate(y/n)...Y

are you the citizen of INDIA(y/n)...Y

do u have any police record(y/n)....N


you are a valid candidate....
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit2
...candidate info...
NAME CANDIDATE NO
RAM 1
DEV 2
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit3

enter your age...19

Are you a citizen of INDIA(y/n)...Y

you are a valid candidate..


enter your id no...1

enter the candidate no corresponding to whom you want to caste your vote1

your vote is casted


MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit3

enter your age...45

Are you a citizen of INDIA(y/n)...Y

you are a valid candidate..


enter your id no...2
1
enter the candidate no corresponding to whom you want to caste your vote1

your vote is casted


MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit4

the winner is RAM


MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit5
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit1

enter your name..RAVI

enter your age..34

are you a graduate(y/n)...Y

are you the citizen of INDIA(y/n)...Y

do u have any police record(y/n)....N

you are a valid candidate....


MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit3

enter your age...56

Are you a citizen of INDIA(y/n)...Y

you are a valid candidate..


enter your id no...1

enter the candidate no corresponding to whom you want to caste your vote1

your vote is casted


MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit3

enter your age...67

Are you a citizen of INDIA(y/n)...Y

you are a valid candidate..


enter your id no...1
1
you have already voted
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit5

MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit1

enter your name..ravi

enter your age..34

are you a graduate(y/n)...y

are you the citizen of INDIA(y/n)...n

invalid candidate
MENU
1.apply for candidate
2.candidate info
3.vote
4.Want to know the winner
5.exit

You might also like