Skip to main content

Posts

Queue

Queue:                 Queue is a linear structure which follows a particular order in which the operations are performed. Operations on Queue:  Mainly the following four basic operations are performed on queue: Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.  Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.  Front: Get the front item from queue.  Rear: Get the last item from queue.    Applications of Queue:  Queue is used when things don’t have to be processed immediately, but have to be processed in First InFirst Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios. 1) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.  2) When data is transferred asynchronously (data not necessarily received at sa
Recent posts

Binary Search Tree Operations

Binary Search Tree Operations   //Main File #include<stdio.h> #include<stdlib.h> #include "tree.h" NODE * create(NODE *root); NODE * insert(NODE *root,int n); NODE *search(NODE * root, int n); void display(); void preorder(NODE * root); void inorder(NODE * root); void postorder(NODE * root); NODE * deletebst(NODE *root, int n); int Node_count(NODE *root); int Leaf_count(NODE *root); NODE *treecopy(NODE *root); int comparebst(NODE *root, NODE *root1); int sumOdd(NODE *node); int sumEven(NODE *node1); void mirror(NODE *root); int smallest(NODE * root); int largest(NODE * root); void main() { NODE *root=NULL, *root1=NULL;     int n, num, osum, esum, choice; printf("\t\t>>Operations of Binary Search Tree<<\n\n");     printf("\t\t\t>>First Create BST<<\n");             //CREATE     root = create(root);     root1 = create(root1); do { printf("\n\n1.INSERT"); printf("\n2.SEARCH"); prin

Implementation of Priority Queue

Implementation of Priority Queue  CODE 👇 #include<stdio.h> #define MAX 50 typedef struct { int data[MAX]; int front,rear; }QUEUE; void InitQ(QUEUE *pq) {   pq->front = pq->rear = -1; } void AddQ(QUEUE *pq, int num) { int i; for(i=pq->rear;i>pq->front;i--)     if(num>pq->data[i])   pq->data[i+1]=pq->data[i];//shift element         else         break;        pq->data[i+1]=num;        pq->rear++; } int DelQ(QUEUE *pq) { int num;   pq->front++;   num=pq->data[pq->front];     return(num); } int isempty(QUEUE *pq) { return(pq->front == pq->rear); } int isfull(QUEUE *pq) {   return(pq->rear==MAX-1); } void main() {   int num,choice;   QUEUE q1;   InitQ(&q1);   printf(">>Implementation of Priority Queue<<");   do   {     printf("\n1: Add \n");     printf("2: Delete \n");     printf("3: Exit \n");     printf("Enter Your Choic

Dynamic Implementation of Circular Queue

Dynamic Implementation of Circular Queue  CODE 👇 #include<stdio.h> #include<stdlib.h> typedef struct node { int info;   struct node *next; }NODE; NODE *front,*rear; void InitQ() {   rear = NULL; } void AddQ(int num) {   NODE *newnode; newnode=(NODE*)malloc(sizeof(NODE));   newnode->info=num;   if(rear==NULL)   {     rear=newnode;     rear->next=rear;   }   else   {     newnode->next=rear->next;     rear->next=newnode;     rear=newnode;   } } int DelQ() { NODE *front=rear->next;   int num=front->info;   if(rear->next==rear)   free(rear);   else   {     rear->next=front->next;     free(front);   }   return(num); } int isempty() {   return(rear == NULL); } void main() {   int choice,num;   InitQ();   printf(">>Dynamic Implementation of Circular Queue<<\n");   do   {     printf("\n1: Add \n");     printf("2: Delete \n");    

Static Implementation of Circular Queue

Static Implementation of Circular Queue CODE 👇 #include<stdio.h> #define MAX 50 typedef struct {   int data[MAX];   int front,rear; }QUEUE; void InitQ(QUEUE *pq) {   pq->front = pq->rear = MAX-1; } void AddQ(QUEUE *pq, int num) {   pq->rear=(pq->rear+1)%MAX; pq->data[pq->rear]=num; } int DelQ(QUEUE *pq) {   pq->front=(pq->front+1)%MAX;   return(pq->data[pq->front]); } int isempty(QUEUE *pq) {   return(pq->front == pq->rear); } int isfull(QUEUE *pq) {   return((pq->rear+1)%MAX==pq->front); } void main() {   int num,choice;   QUEUE q1;   InitQ(&q1);   printf(">>Static Implementation of Circular Queue<<\n");   do   {     printf("\n1: Add \n");     printf("2: Delete \n");     printf("3: Exit \n");     printf("Enter your choice: \t");     scanf("%d",&choice);     switch(choice)     {       case 1: printf("E

Dynamic Implemenatation Of Linear Queue

Dynamic Implemenatation Of Linear Queue CODE 👇 #include <stdio.h> typedef struct node { int info; struct node *next; }NODE; NODE *front, *rear; void initq() { front=rear=NULL; } int isempty() { return (front==NULL); } void addq(int num) { NODE *nn; nn = (NODE*)malloc(sizeof(NODE)); nn->info=num; nn->next=NULL; if(front == NULL) rear=front=nn; else { rear->next=nn; rear=nn; }   } int removeq() { int num; NODE *temp=front; num = front->info; front = front->next; free (temp); if(front==NULL) rear=NULL; return (num); } void main() { int choice, num; initq(); printf(">>Dynamic Implementation Of Linear Queue<<"); do { printf("\n1.ADD \n2.DELETE \n3.EXIT\n"); printf("Enter Your Choice:\t"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter The Element:\t"); scanf("%d",&num); addq(nu

Static Implementation of Linear Queue

  Static Implementation of Linear Queue CODE 👇 #include <stdio.h> #define MAX 50 typedef struct { int data[MAX]; int front, rear; }QUEUE; void initQ(QUEUE *pq) { pq->front = pq->rear = -1; } void addQ(QUEUE *pq, int num) { pq->rear++; pq->data[pq->rear] = num; } int delQ(QUEUE *pq) { int num; pq->front++; num=pq->data[pq->front]; return (num); } int isempty(QUEUE *pq) { return(pq->front == pq->rear); } int isfull(QUEUE *pq) { return(pq->rear == MAX-1); } void main() { int n, choice; QUEUE q1; initQ(&q1); printf(">>Static Implementation Of Queue<<"); do { printf("\n1.ADD \n"); printf("2.DELETE \n"); printf("3.EXIT \n"); printf("Enter Your Choice: \t"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter Element To Be Added:"); scanf("%d", &n); if(isfull(&