#include<stdio.h>
#include<conio.h>
struct Node
{
int num;
struct Node*next;
};
struct Node*start=NULL;
void addToQueue(int num)
{
struct Node *t,*j;
t=(struct Node*)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
j=start;
while(j->next!=NULL)
{
j=j->next;
}
j->next=t;
}
}
int removeFromQueue()
{
struct Node *t;
int num;
if(start==NULL)return 0;//queue is empty
num=start->num;
t=start;
start=start->next;
free(t);
return num;
}
int isQueueEmpty()
{
return start==NULL;
}
void main()
{
int ch,num;
while(1)
{
printf("1 Add to queue\n");
printf("2 Remove from queue\n");
printf("3 Enter your choice");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter number to add to queue ");
scanf("%d",&num);
addToQueue(num);
printf("%d added to queue\n",num);
}
if(ch==2)
{
if(isQueueEmpty())
{
printf("Queue is empty\n");
}
else
{
num=removeFromQueue();
printf("%d removed from queue\n",num);
}
}
if(ch==3)
{
break;
}
}
}
~``~ Data Structures Using C......... Here you can know how the data structures are represented in the computer. You can learn about Stacks, Queues, Trees, Graphs, and many more which are related with the data structures. Here we have used c programming language to demonstrate some examples.
check it
Search This Blog
Showing posts with label queue. Show all posts
Showing posts with label queue. Show all posts
15 July, 2011
Queue using Linklist
27 April, 2011
An example of queue using Array
#include<stdio.h>
int queue[10];
int lowerBound=0;
int upperBound=9;
int front=-1;
int rear=-1;
void addToQueue(int num)
{
if(rear==upperBound) return; // queue Full
rear++;
queue[rear]=num;
if(front==-1)front=0;
}
int removeFromQueue()
{
int num,i;
if(rear==-1)return 0; //queue Empty
num=queue[front];
i=0;
while(i<rear)
{
queue[i]=queue[i+1];
i++;
}
rear--;
if(rear==-1)front=-1;
return num;
}
int isQueueFull()
{
return rear==upperBound;
}
int isQueueEmpty()
{
return rear==-1;
}
void main()
{
int ch,num;
while(1)
{
printf("1. Add To Queue\n");
printf("2. Remove From Queue\n");
printf("3. Enter your choice");
scanf("%d",&ch);
if(ch==1)
{
if(isQueueFull())
{
printf("Queue is full\n");
}
else
{
printf("Enter number to add to queue");
scanf("%d",&num);
if(num==0)
{
printf("Cannot add zero to queue\n");
}
else
{
addToQueue(num);
printf("%d added to queue \n",num);
}
}
}
if(ch==2)
{
if(isQueueEmpty())
{
printf("Queue is empty\n");
}
else
{
num=removeFromQueue();
printf("%d removed from queue\n",num);
}
}
if(ch==3)
{
break;
}
}
}
-----------------------------------------------------------------------------------------------------
Labels:
array,
queue,
Queue implementation through Array
Subscribe to:
Posts (Atom)