Skip to main content

Queue implementation using C

The readers are requested to check for any errors in the program (as the program is typed and copied from MS Word)

QUEUE IMPLEMENTATION USING ARRAYS

#include<stdio.h>

#include<conio.h>

void create(void);

void display(void);

void queue_in(void);

void queue_out(void);

void front_rear(void);

int front,rear;

int q[25];

void create()

{

int n;

printf("Enter the number of elements in the queue \n");

scanf("%d",&n);

printf("Enter the elements \n");

for(rear=0;rear<n;rear++)

{

scanf("%d",&q[rear]);

}

front=0;

}

void display()

{

if(rear!=0)

{

printf("The elements in queue \n");

for(front=0;front<rear;front++)

{

printf("%d",q[front]);

}

front=0;

}

else if(rear==0)

printf("The queue is empty \n");

else if(rear==25)

printf("The queue is full \n");

}

void front_rear()

{

if(rear!=0)

{

printf("the front element is : %d \n",q[front]);

printf("the rear element is : %d \n",q[rear-1]);

}

else

printf("Queue is empty \n");

}

void queue_in()

{

if(rear<25)

{

printf("Enter the elements \n");

scanf("%d",&q[rear]);

++rear;

}

else

printf("Queue is full \n");

}

void queue_out()

{

if(rear!=0)

{

printf("The deleted element is : %d \n ",q[front]);

for(front=0;front<rear-1;++front)

q[front]=q[front+1];

rear--;

front=0;

}

}

int main()

{

int ch=0;

clrscr();

printf("QUEUE MANIPULATION \n");

printf("creation \n");

create();

display();

getch();

do

{

clrscr();

printf("Queue operations \n");

printf("---------------- \n");

printf("1. Inserting in to the Queue \n");

printf("2. Deletion from queue \n");

printf("3. Front and rear element \n");

printf("4. Displaying the queue \n");

printf("5. Exit \n");

printf("Enter your choice \n");

scanf("%d\n",&ch);

switch(ch)

{

case 1 :queue_in();

display();

break;

case 2 :queue_out();

display();

break;

case 3 :front_rear();

break;

case 4 :display();

break;

case 5 :printf("END \n");

break;

default: printf("Invalid Entry \n");

}

getch();

}

while(ch!=5);

return (0);

}

Sample output:

QUEUE MANIPULATION

Creation

Enter the number of elements in the queue

4

Enter the elements

1

2

3

4

The elements in queue

1234

Queue operations

----------------

1. Inserting in to the Queue

2. Deletion from queue

3. Front and rear element

4. Displaying the queue

5. Exit

Enter your choice

1

5

Enter the elements

The elements in queue

12345

Enter your choice

3

the front element is : 1

the rear element is : 5

Enter your choice

4

The elements in queue

12345

Enter your choice

2

The deleted element is: 1

The elements in queue

2345

Enter your choice

5

END

 

QUEUE USING LINKED LIST

#include<stdio.h>

typedef struct node

{

int data;

struct node*link;

}queue;

queue*getnode();

void releasenode(queue*p);

int isfull();

int isempty(queue*front);

void enqueue(queue**frontptr,queue**rearptr,int value);

void dequeue(queue**frontptr,queue**rearptr,int*value);

void peek(queue*front,int*value);

void view(queue*front);

int size(queue*front);

void displaymenu(void);

void main()

{

queue*front=NULL,*rear=NULL;

int choice,item;

displaymenu();

while(1)

{

printf("\n?");

scanf("%d",&choice);

switch(choice)

{

case 1:

if(isfull())

printf("\nQueue overflow on ENQUEUE");

else

{

printf("\nEnter the element:");

fflush(stdin);

scanf("%d",&item);

enqueue(&front,&rear,item);

}

break;

case 2:

if(isempty(front))

printf("\n Queue underflow on DEQUEUE");

else

{

dequeue(&front,&rear,&item);

printf("\nThe dequeued value is %d",item);

}

break;

case 3:

if(!isempty(front))

{

peek(front,&item);

printf("\nThe front value is %d",item);

}

else

printf("\nQueue is empty");

break;

case 4:

printf("\nCount of queue elements=%d",size(front));

break;

case 5:

view(front);

break;

default:

printf("\nEnd of run of your program..");

exit(0);

}

}

}

void displaymenu()

{

printf("\nRepresentation of queue using linked list...");

printf("\n\t1.enqueue");

printf("\n\t2.dequeue");

printf("\n\t3.peek");

printf("\n\t4.size");

printf("\n\t5.view");

printf("\n\t6.exit");

}

void releasenode(queue*p)

{

free(p);

}

queue*getnode()

{

int size;

queue*newnode;

size=sizeof(queue);

newnode=(queue*)malloc(size);

return(newnode);

}

int isempty(queue*front)

{

if(front==NULL)

return 1;

else

return 0;

}

int isfull()

{

queue*newnode;

newnode=getnode();

if(newnode==NULL)

return 1;

releasenode(newnode);

return 0;

}

void enqueue(queue**frontptr,queue**rearptr,int value)

{

queue*newnode;

if(isfull())

{

printf("\nMemory not available");

return;

}

newnode=getnode();

newnode->data=value;

newnode->link=NULL;

if(*frontptr==NULL)

*frontptr=newnode;

else

(*rearptr)->link=newnode;

*rearptr=newnode;

}

void dequeue(queue**frontptr,queue**rearptr,int*value)

{

queue*tempnode;

if(isempty(*frontptr))

return;

tempnode=*frontptr;

*frontptr=(*frontptr)->link;

if(*frontptr==NULL)

*rearptr=NULL;

*value=tempnode->data;

releasenode(tempnode);

}

void peek(queue*front,int*value)

{

if(isempty(front))

{

printf("\nThe queue is empty!!!");

return;

}

*value=front->data;

}

void view(queue*front)

{

if(isempty(front))

{

printf("\nThe queue is empty!!!");

return;

}

printf("\nQueue contains...front->");

while(front!=NULL)

{

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

front=front->link;

}

printf("rear/n");

}

int size(queue*front)

{

int count=0;

if(front==NULL)

return count;

for(;front!=NULL;)

{

count++;

front=front->link;

}

return count;

}

Sample output:

Representation of queue using linked list..

1. enqueue

2. dequeue

3. peek

4. size

5. view

6. exit

?2

Queue underflow on DEQUEUE

?3

Queue is empty

?4

Count of queue elements=0

?5

The queue is empty!!!

?1

Enter the element:11

?1

Enter the element:22

?1

Enter the element:33

?5

Queue contains...front->11-->22-->33-->rear/n

?4

Count of queue elements=3

?2

The dequeued value is 11

?6

Comments

Popular posts from this blog

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of vanet-routing-compare.cc (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./build.py --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from nsnam.org website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q

Installing NS-3.32 in Ubuntu 20.04

This is about installing ns version 3.32 in Ubuntu 20.04 LTS. #ns3 #ns3 .32 #networksimulation The commands used in the video are given here. $] sudo apt update $] sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-qmake qt5-default gnuplot-x11 wireshark Download the ns-allinone-3.32.tar.bz2 package from nsnam.org and copy it to /home/ folder See the full video for detailed instructions Extract it either in GUI or using command $] tar jxvf ns-allinone-3.32.tar.bz2 $] cd ns-allinone-3.32/ $] ./build.py --enable-examples --enable-tests The above command will take some time to install all the packages  You can see the output as shown below ns3 To check whether ns3 installed successfully, use the following commands. $] cd ns-3.32/ $] ./waf --run hello-simulator You should get the output as Hello Simulator $] ./waf --run first This is the example from the ns-3.32/exa