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 TexLive 2019 in Ubuntu 18.04

Installation of TexLive 2019 in Linux (Ubuntu 18.04 LTS)
TeX (Tech)

Installation of TexLive 2019

Please watch the video for full installation



I used .iso file to download, the Total size is 3.3GB for Linux,

and i used the torrent file to download, it took me just 20 min to download the entire .iso file

Extract the .iso file to a folder and open a terminal

$] sudo ./install-tl
(it goes into a terminal mode, which is faster compared to the GUI Mode)

$] sudo ./install-tl -gui
after the installation, set the PATH, MANPATH and INFOPATH as suggested by LATEX

export PATH=$PATH:/usr/local/texlive/2019/bin/x86_64-linux
export MANPATH=/usr/local/texlive/2019/texmf-dist/doc/man
export INFOPATH=/usr/local/texlive/2019/texmf-dist/doc/info

put these lines in to the /home/pradeepkumar/.bashrc

$] gedit /home/pradeepkumar/.bashrc
We have installed TexLive 2019 and texstudio.

To install texstudio

$] sudo apt install texstudio
The look and feel of TexStudio looks like this image.


texlive, it install everyt…

Implementing a new system call in Kernel version 2.6.32

A system call is used by application or user programs to request service from the operating systems. Since the user programs does not have direct access to the kernel whereas the OS has the direct access. OS can access the hardware through system calls only.The following files has to be modified for implementing a system call/usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S/usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h/usr/src/linux-2.6.32.5/include/linux/syscalls.h/usr/src/linux-2.6.32.5/MakefileNew set of files to be createdCreate a new directory newcall/ inside the path “/usr/src/linux-2.6.32.5/” Create new files Makefile, newcall.c and put them in the /usr/src/linux-2.6.32.5/newcall/ folder Create new user files (in any folder of Linux) to test the system call
testnewcall.c, testnewcall.h (created in /home/pradeepkumar) syscall_table_32.S Find the file /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S and add the following line at the end
"…

Electrical Machine Design (equations)

FactorsDC Machine Transformers Induction Machines Synchronous MachinesOutput EquationPa=CoD2Ln, where Pa=P/h for generators, Pa=P for motorsFor Single Phase
Q=2.22 f Bm Ai Kw Aw d10-3
For Three Phase
Q=3.33 f Bm Ai Kw Aw d 10-3Q=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * hQ=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * h
For Turbo alternators
Q=1.11Bavac KwsVa2 L 10-3/nsOutput CoefficientCo=Bav ac* 10-3where Bav-magnetic loading and ac - electric loadingDNACo=11 Kws Bav ac 10-3Co=11 Kws Bav ac 10-3 Choice of Magnetic LoadingFlux Density in Teeth Frequency of Flux Reversals Size of machineDNAMagnetizing current, Flux Density, Iron lossIron loss, Stability, Voltage Rating, Parallel Operation, Transient ShortCircuit current Choice of Electric LoadingTemperature rise,
speed of machine, Voltage, Armature reaction, CommutationDNAOverload Capacity, Copper losses, Temperature rise, Leakage ReactanceCopper loss, Synchronous reactance, Temperature rise, Stray Load losses,
Voltage rating Flux …