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

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/Makefile New set of files to be created Create 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_tab

How to Access MOODLE in Intranet and Internet

When Moodle is accessed either in Intranet or internet, there will not be any issue. But occasions when the MOODLE Site has to be accessed both in the intranet and in the Internet, here is a simple trick Server Used: IBM Blade Servers Operating system: Windows Server 2008 Moodle Version: 2.4 WAMP Server is used. Number of Users: 3000 (Students) + 200 (Faculty) Open the config.php from ~/moodle/config.php include these lines $CFG->wwwroot = 'http://'.$server_id.'/vitcc'; $CFG->dataroot  = 'C:\\wamp\\moodledata'; before the following line $CFG->directorypermissions = 07xx; Restart the WAMP Server and you can Check MOODLE Site both in Internet and Intranet. The above Image tells the intranet Link and the internet link can be opened outside the campus network

Electrical Machine Design (equations)

Factors DC Machine Transformers Induction Machines Synchronous Machines Output Equation P a =C o D 2 Ln, where Pa=P/h for generators, Pa=P for motors For Single Phase Q=2.22 f B m A i K w A w d 10 -3 For Three Phase Q=3.33 f B m A i K w A w d 10 -3 Q=C o D 2 L n s KVA Input Q= HP * 0.746 / Cos f * h Q=C o D 2 L n s KVA Input Q= HP * 0.746 / Cos f * h For Turbo alternators Q=1.11B av ac K ws V a 2 L 10 -3 /n s Output Coefficient C o =B av ac* 10 -3 where Bav-magnetic loading and ac - electric loading DNA C o =11 K ws B av ac 10 -3 C o =11 K ws B av ac 10 -3 Choice of Magnetic Loading Flux Density in Teeth Frequency of Flux Reversals Size of machine DNA Magnetizing current, Flux Density, Iron loss Iron loss, Stability, Voltage Rating, Parallel Operation, Transient ShortCircuit current