/*Circular Queue implementation using linked list Written by Shivam Rana Date- 3-4-12*/ #include<stdio.h> #include<stdlib.h> #define que struct queue #define pf printf #define sf scanf struct queue{ int info; struct queue *link; }; que *front=NULL,*rear=NULL; int count=0; void push(int n) { que *newnode; newnode=(struct queue*)malloc(sizeof(struct queue)); newnode->info=n; newnode->link=NULL; if(count==0) front=newnode; else rear->link=newnode; rear=newnode; rear->link=front; count++; } int pop(void) { int n; que *temp; if(count==0) return (-1); count--; if(front==rear) { n=front->info; free(front); front=NULL; rear=NULL; }else { temp= front ; n = temp-> info ; front = front -> link ; rear -> link = front ; free ( temp ) ; } return n; } void display(void) { que *temp; int i; if(count==0) pf("Empty"); else { temp=front; for(i=0;i<count;i++) { pf("%d ",temp->info); temp=temp->link; } } pf("\n"); } int size(void) { return count; } int main() { int n,ch=10; while(ch!=0) { pf("\n What do you want to do??\n"); pf("1.Push\n"); pf("2.Pop\n"); pf("3.SizeOfQueue\n"); pf("4.Display\n"); pf("0.EXIT\n"); sf("%d",&ch); switch(ch) { case 1: { pf("What no. do you want to push in queue\n"); sf("%d",&n); push(n); break; } case 2: { n=pop(); if(n==-1) pf("Queue is empty\n"); else pf("Number poped from queue is %d\n",n); break; } case 3: { n=size(); pf("Size of queue is %d\n",n); break; } case 4: { pf("Queue is -->> "); display(); } case 0: break; default: pf("Wrong Choice\n"); break; } } }
Archive for the ‘Lab 8’ Category
0
/*Linear Queue implementation using linked list Written by Shivam Rana Date- 2-4-12*/ #include<stdio.h> #include<stdlib.h> #define que struct queue #define pf printf #define sf scanf struct queue{ int info; struct queue *link; }; que *front=NULL,*rear=NULL; int count=0; void push(int n) { que *temp,*newnode; temp=front; newnode=(struct queue*)malloc(sizeof(struct queue)); newnode->info=n; newnode->link=NULL; if(count==0) front=newnode; else { while(temp->link!=NULL) temp=temp->link; temp->link=newnode; } count++; } int pop(void) { int n; que *temp,*newnode; if(count==0) return NULL; count--; temp=front; n=temp->info; front=temp->link; free(temp); return n; } void display() { que *temp; int i; if(count==0) pf("Empty"); else { temp=front; for(i=0;i<count;i++) { pf("%d ",temp->info); temp=temp->link; } } pf("\n"); } int size(void) { return count; } int main() { int n,ch=10; while(ch!=0) { pf("\n What do you want to do??\n"); pf("1.Push\n"); pf("2.Pop\n"); pf("3.SizeOfQueue\n"); pf("4.Display\n"); pf("0.EXIT\n"); sf("%d",&ch); switch(ch) { case 1: { pf("What no. do you want to push in queue\n"); sf("%d",&n); push(n); break; } case 2: { n=pop(); if(n==NULL) pf("Queue is empty\n"); else pf("Number poped from queue is %d\n",n); break; } case 3: { n=size(); pf("Size of queue is %d\n",n); break; } case 4: { pf("Queue is -->> "); display(); } case 0: break; default: pf("Wrong Choice\n"); break; } } }
/*Linear Queue implementation using linked list Written by Shivam Rana Date- 2-4-12 */ #include<stdio.h> int queue[100],front=0,rear=-1,count=0; void push(int n) { queue[++rear]=n; count++; } void pop(void) { if(count==0) printf("Queue is empty\n"); else { count--; printf("Number poped from queue is %d\n",queue[front++]); } } void display() { int i; if(count==0) printf("Empty"); else for(i=front;i<=rear;i++) printf("%d ",queue[i]); printf("\n"); } int size(void) { return count; } int main() { int n,ch=10; while(ch!=0) { printf("\n What do you want to do??\n"); printf("1.Push\n"); printf("2.Pop\n"); printf("3.SizeOfQueue\n"); printf("4.Display\n"); printf("0.EXIT\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("What no. do you want to push in queue\n"); scanf("%d",&n); push(n); break; } case 2: { pop(); break; } case 3: { n=size(); printf("Size of queue is %d\n",n); break; } case 4: { printf("Queue is -->> "); display(); } case 0: break; } } }