Queue implementation in C Language

Source: Internet
Author: User

A queue is a data structure opposite to a stack. It is a first in, first out (FIFO) structure. It is also a structure with limited operations.

ABSTRACT Data Type of the queue:

ADT queue =

{

Initqueue (& Q)

// Create a queue

Destroyqueue (& Q)

// Destroy a queue

Clearqueue (& Q)

// Clear the queue

............

}

Queue implementation in C Language

1:

Ordered queue:

/*************************************** ****
* Filename: squeue. c
* Author: hufeng Date: 2007/11/21
* Version: V1.0
* Description: The program creat a queue and lots
* Of Operating
**************************************** ***/

/* Header file */
# Include <stdio. h>
# Define max 20

/* Creat a struct */
Typedef struct squeue
{
Int date [Max];/* declartion A array */
Int front, rear;/* declarion a front and a rear pointer */

} Sq;

/* Assign queue each ELEM */
Void assign_squeue (SQ * P, int count)
{
Int I;/* declaration a cycle variable */

If (p-> rear> MAX-1)
{
Printf ("overflow! /N ");
Return;
}

Printf ("Please enter each ELEM:/N ");
For (I = 0; I <count; I ++)
{
Scanf ("% d", & P-> date [++ p-> rear]);
}
}

/* Insert a new ELEM in the queue */
Void insert_squeue (SQ * P, int num)
{
If (p-> rear> MAX-1)
{
Printf ("over flow! /N ");
Return;
}

Else
{
P-> date [++ p-> rear] = num;
}

}

/* Delete a elem */
Void delete_squeue (SQ * P)
{
Int X;
If (p-> front = p-> rear)
{
Printf ("Empty queue! /N ");
Return;
}
Else
{
X = p-> date [++ p-> front];
Printf ("p-> front = % d/N", p-> front );
Printf ("the deleted number is: % d/N", X );
}
}

/* Main Program */
Int main ()
{
Sq * P = NULL;/* declaration a pointer */
Int num;/* decalration a number of queue */

P-> front =-1;
P-> rear = p-> front;

Printf ("Please enter the length of queue:/N ");
Scanf ("% d", & num );

Assign_squeue (p, num);/* assign array date */

/* Insert a number in the queue */
Printf ("Please enter which number you want to insert:/N ");
Scanf ("% d", & num );
Insert_squeue (p, num );

/* Delete a number */
Delete_squeue (P );

Getch ();
Return 0;

}

2:

Cyclic queue:

/*************************************** ****
* Filename: cycle_queue.c
* Author: hufeng Date: 2007/11/21
* Version: V1.0
* Description: The program is a cycle queue
**************************************** *****/

/* Header file */
# Include <stdio. h>
# Define max 20

/* Creat a struct */
Typedef struct cycle_queue
{
Int date [Max];
Int front, rear;
} CQ;

/* Assign queue */
Void assign_cyclequeue (CQ * P, int N)
{
Int I;
Int X;

If (n <1 | n> MAX-1)
{
Printf ("overflow! /N ");
Return;
}

Printf ("Please enter each ELEM:/N ");
For (I = 0; I <n; I ++)
{
Scanf ("% d", & X );
P-> date [p-> rear ++] = X;
}
}

/* Insert a number */
Void insert_cyclequeue (CQ * P, int num)
{
If (P-> rear + 1) % max = p-> front)
{
Printf ("Full queue! /N ");
Return;
}
Else
{
P-> date [++ p-> rear] = num;
}
}

/* Delete Number */
Void delete_cyclequeue (CQ * P)
{
Int X;
If (P-> front + 1) % max = p-> rear)
{
Printf ("Empty queue! /N ");
Return;
}

X = p-> date [p-> front];
Printf ("p-> front = % d/N", p-> front );
Printf ("the deleted number is: % d/N", X );
}

/* Main Program */
Int main ()
{

Int num;
CQ * P = NULL;
P-> front = 0;
P-> rear = p-> front;

Printf ("Please enter the length of queue:/N ");
Scanf ("% d", & num );

Assign_cyclequeue (p, num );

Printf ("Please enter the inserting number:/N ");
Scanf ("% d", & num );

Insert_cyclequeue (p, num );
Delete_cyclequeue (P );

Getch ();
Return 0;
}

3.

Chain Team:

/*************************************** ****
* Filename: cycle_queue.c
* Author: hufeng Date: 2007/11/21
* Version: V1.0
* Description: The program is a cycle queue
**************************************** *****/

/* Header file */
# Include <stdio. h>
# Define max 20

/* Creat a struct */
Typedef struct cycle_queue
{
Int date [Max];
Int front, rear;
} CQ;

/* Assign queue */
Void assign_cyclequeue (CQ * P, int N)
{
Int I;
Int X;

If (n <1 | n> MAX-1)
{
Printf ("overflow! /N ");
Return;
}

Printf ("Please enter each ELEM:/N ");
For (I = 0; I <n; I ++)
{
Scanf ("% d", & X );
P-> date [p-> rear ++] = X;
}
}

/* Insert a number */
Void insert_cyclequeue (CQ * P, int num)
{
If (P-> rear + 1) % max = p-> front)
{
Printf ("Full queue! /N ");
Return;
}
Else
{
P-> date [++ p-> rear] = num;
}
}

/* Delete Number */
Void delete_cyclequeue (CQ * P)
{
Int X;
If (P-> front + 1) % max = p-> rear)
{
Printf ("Empty queue! /N ");
Return;
}

X = p-> date [p-> front];
Printf ("p-> front = % d/N", p-> front );
Printf ("the deleted number is: % d/N", X );
}

/* Main Program */
Int main ()
{

Int num;
CQ * P = NULL;
P-> front = 0;
P-> rear = p-> front;

Printf ("Please enter the length of queue:/N ");
Scanf ("% d", & num );

Assign_cyclequeue (p, num );

Printf ("Please enter the inserting number:/N ");
Scanf ("% d", & num );

Insert_cyclequeue (p, num );
Delete_cyclequeue (P );

Getch ();
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.