0% found this document useful (0 votes)
19 views

Queue

The document contains code for implementing queue data structure using array and dynamic array in C. It also contains code for tree traversal and level order traversal of a binary tree.

Uploaded by

yvanishree123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Queue

The document contains code for implementing queue data structure using array and dynamic array in C. It also contains code for tree traversal and level order traversal of a binary tree.

Uploaded by

yvanishree123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>
#include<limits.h>
#define size 5
int queue[size];
int front=-1;
int rear=-1;
int isFull()
{
if(rear==size-1)return 1;
return 0;
}
int isEmpty()
{
if(front==-1)return 1;
return 0;
}
void enqueue(int data)
{
if(isFull())
{
printf("QUEUE IS FULL");
return ;
}
if(isEmpty())
{
front++;
rear++;
queue[rear]=data;
return;
}
queue[++rear]=data;
}
void dequeue()
{
if(isEmpty())
{
printf("queue is empty");
return ;
}
front++;
if(front>rear)
{
front=-1;
rear=-1;
}
}
int Front()
{
if(isEmpty())
{
return INT_MIN;
}
return queue[front];
}
int back()
{
if(isEmpty())
{
return INT_MIN;
}
return queue[rear];
}
void printQueue()
{
printf("\n");
for(int i=front;i<=rear;i++)
{
printf("%d ",queue[i]);
}
printf("\n");
}

int main()
{
enqueue(20);
enqueue(20);
enqueue(20);
enqueue(20);
enqueue(20);
dequeue();dequeue();dequeue();
dequeue();
dequeue();dequeue();
printQueue();

------------------------dynamic array

#include <stdio.h>
#include<stdlib.h>
#include <limits.h>
struct Queue
{
int front ,rear;
int size;
int *arr;
};
struct Queue* createQueue(unsigned capacity)
{
struct Queue* q= (struct Queue*)malloc(sizeof(struct Queue));
q->front=q->rear=-1;
q->size=capacity;
q->arr=(int*)malloc(capacity*sizeof(int));
return q;
}
int isEmpty(struct Queue *queue)
{
if(queue->front==-1)return 1;
return 0;
}
int isFull(struct Queue *queue)
{
if(queue->rear==queue->size-1)return 1;
return 0;
}
void enqueue(struct Queue *queue,int value)
{
if(isFull(queue))
{
printf("queue is full");
return ;
}
if(isEmpty(queue))
{
queue->front++;
queue->rear++;
queue->arr[queue->rear]=value;
return ;
}
queue->rear++;
queue->arr[queue->rear]=value;
}
void dequeue(struct Queue* queue)
{
if(isEmpty(queue))
{
printf("queue is empty can't dequeue");
return ;
}
queue->front++;
if(queue->front>queue->rear)
{
queue->front=-1;
queue->rear=-1;
}
}
int front(struct Queue *queue)
{
if(isEmpty(queue))
{
printf("queue is empty");
return INT_MAX;
}
return queue->arr[queue->front];
}
int rear(struct Queue *queue)
{
if(isEmpty(queue))
{
printf("queue is empty");
return INT_MAX;
}
return queue->arr[queue->rear];
}
void printQueue(struct Queue* queue)
{
printf("\n");
for(int i=queue->front;i<=queue->rear;i++)
{
printf("%d ",queue->arr[i]);
}
printf("\n");
}
int main()
{
struct Queue *queue=createQueue(5);
enqueue( queue,19);
enqueue( queue,11);
enqueue( queue,1998);
printf("\nfront=%d",front(queue));
printf("\nrear=%d",rear(queue));
printQueue(queue);

return 0;
}

--------treee code

#include <iostream>
#include <queue>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node *right;
struct node *left;
};
struct node* createNode(int data)
{
struct node *n= (struct node*)malloc(sizeof(struct node));
n->data=data;
n->right=NULL;
n->left=NULL;
return n;
}
void levelorder(struct node *root)
{
printf("level order--");
if(root ==NULL)return ;
queue<struct node *>q;
q.push(root);
while(!q.empty())
{
struct node *temp=q.front();
q.pop();
printf("%d",temp->data);
if(temp->left!=NULL)q.push(temp->left);
if(temp->right!=NULL)q.push(temp->right);
}
}
void preorder(struct node *root)
{
if(root==NULL)return ;
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct node *root)
{
if(root==NULL)return ;
preorder(root->left);
printf("%d ",root->data);
preorder(root->right);
}
void postorder(struct node *root)
{
if(root==NULL)return ;
preorder(root->left);
preorder(root->right);
printf("%d ",root->data);
}
void printTree(struct node *root)
{
printf("\npre-order -");
preorder(root);
printf("\nin-order -");
inorder(root);
printf("\npost-order -");
postorder(root);
printf("\n");
}
int main()
{
struct node *root=createNode(10);
struct node *n1=createNode(20);
struct node *n2=createNode(30);
struct node *n3=createNode(40);
struct node *n4=createNode(50);
struct node *n5=createNode(60);
struct node *n6=createNode(70);
root->left=n1;root->right=n2;
n1->left=n3;n1->right=n4;
n2->left=n5;n2->right=n6;
printTree(root);
levelorder(root);
return 0;
}

You might also like