You are on page 1of 18

//circular queue using linked list

#include<stdio.h>

struct node

int data;

struct node *next;

}*front,*rear;

main()

front=rear=NULL;

int ch,data;

while(1)

printf("1.Enqueue \n");

printf("2.Dequeue \n");

printf("3.Peek \n");

printf("4.Display \n");

printf("5.Exit \n");

printf("Enter your choice");

scanf("%d",&ch);

switch(ch)

case 1: printf("Enter element");

scanf("%d",&data);

enqueue(data);

break;

case 2: dequeue();

break;

case 3: peek();

break;
case 4: display();

break;

case 5: exit(1);

default: printf("Invalid option");

void enqueue(int data)

struct node *newnode;

newnode=(struct node*) malloc(sizeof(struct node));

newnode->data=data;

newnode->next=NULL;

if(rear==NULL)

front=rear=newnode;

rear->next=front;

else

rear->next=newnode;

rear=newnode;

rear->next=front;

void dequeue()

struct node *temp;

temp=front;

if(front==NULL && rear==NULL)


{

printf("Queue is empty");

else if(front==rear)

front=rear=NULL;

free(temp);

else

front=front->next;

rear->next=front;

free(temp);

void display()

struct node *temp;

temp=front;

if(front==NULL && rear==NULL)

printf("Queue is empty");

else

while(temp->next!=front)

printf("%d \t",temp->data);

temp=temp->next;

printf("%d \n",temp->data);
}

void peek()

if(front==-1 && rear==-1)

printf("Queue is empty");

else

printf("%d",front->data);

Output
1.Enqueue
2.Dequeue
3.Peek
4.Display
5.Exit
Enter your choice:-
//implement circular queue library.
# include <stdio.h>
# include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
main()
{
int choice,n,m,pos,i,key,count,leaf;
struct node *root=NULL,*root1,*addr;
while(1)
{
printf("1.Insert Element\n");
printf("2.Preorder\n");
printf("3.Inorder\n");
printf("4.Postorder\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0; i < n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
root=insertbst(root,m);
}
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: printf("Enter the key to be deleted");
scanf("%d",&key);
root=deletebst(root,key);
break;
case 6: count=countnodes(root);
printf("Number of nodes are %d",count);
break;
case 7: leaf=countleaf(root);
printf("Number of leaf nodes are %d",leaf);
break;
case 8: printf("Enter the key to be searched");
scanf("%d",&key);
addr=search(root,key);
printf("Address=%u",addr);
break;
case 9: root1=treecopy(root);
break;
case 10:mirror(root);
break;
default:
printf("Wrong choice\n");
}/End of switch/
}/End of while/
}/End of main()/

insertbst(struct node *root,int num)


{
struct node *temp,*newnode,*parent;
newnode= (struct node *)malloc(sizeof(struct
node));
newnode->data = num;
newnode->left =newnode->right= NULL;

if(root==NULL)
{
root= newnode;
}
else
{

temp=root;
while(temp!=NULL)
{
parent=temp;
if(num<temp->data)
temp=temp->left;
else
temp=temp->right;
}
if(num<parent->data)
parent->left=newnode;
else
parent->right=newnode;
}
return root;
}
void preorder(struct node *root)
{
struct node *temp=root;
if(temp!=NULL)
{
printf("%d",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void inorder(struct node *root)
{
struct node *temp=root;
if(temp!=NULL)
{
inorder(temp->left);
printf("%d",temp->data);
inorder(temp->right);
}
}
void postorder(struct node *root)
{
struct node *temp=root;
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%d",temp->data);

}
}

deletebst(struct node *root, int key)


{
struct node *temp;
if(root==NULL)
return root;

if(key < root->data)


root->left=deletebst(root->left,key); //left subtree
else if(key > root->data)
root->right=deletebst(root->right,key); // right
subtree
else //Node found
{
// Leaf node or node with one child
if (root->left==NULL)
{
temp=root->right;
free (root);
return temp;
}
else if (root->right == NULL)
{
temp=root->left;
free (root);
return temp;
}
//node with two children. Find the inorder successor
temp=root->right;
while (temp && temp->left != NULL)
temp=temp->left;
// Copy the inorder successor's content to node to
be deleted
root->data = temp->data;
// Delete the inorder successor
root->right=deletebst (root->right, temp->data);
}
return root;
}
int countnodes(struct node *root)
{
static int count=0;
struct node *temp=root;
if (temp!= NULL)
{
count++;
countnodes(temp->left);
countnodes(temp->right);
}
return count;
}
int countleaf(struct node *root)
{
static int leaf=0;
struct node *temp =root;
if (temp!=NULL)
{
if((temp->left==NULL)&&(temp->right==NULL))
leaf++;
countleaf(temp->left);
countleaf(temp->right);
}
return leaf;
}
search(struct node *root,int key)
{
struct node *temp=root;
while (temp!=NULL)
{
if(key==temp->data)
return temp;
if(key<temp->data)
temp=temp->left; //search left subtree
else
temp=temp->right; // search right subtree
}
return NULL;
}
treecopy(struct node *root)
{
struct node *newnode;
if (root!=NULL)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=root->data;
newnode->left=treecopy(root->left);
newnode->right=treecopy(root->right);
return (newnode);
}
else
return NULL;
}
void mirror(struct node *root)
{
struct node *temp,*temp1;
temp=root;
if(temp!=NULL)
{
if(temp->left!=NULL)
mirror(temp->left);
if (temp->right!=NULL)
mirror(temp->right);
// interchange the children nodes
temp1=temp->left;
temp->left=temp->right;
temp->right=temp1;
}
}
Output
1.Insert Element
2.Preorder
3.Inorder
4.Postorder
Enter your choice :

You might also like