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 ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools 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 gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb http://in.archive.ubuntu.com/ubuntu bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/Makefile.in Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/Makefile.in ~xgraph-12.2/Makefile.in ~otcl-1.14/Makefile.in Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes ...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...