Implementation of sequential queues and chained queues

Source: Internet
Author: User

A queue is a commonly used data structure that, like a stack, is restricted in that the queue only allows data to be entered from one end and the other end. Unlike stacks, a stack is a "LIFO" mode, and a queue is a "FIFO" mode of operation. Just like the daily Line, the first line first out, the back of the rear team. For example, the order in which the queues are entered is 1,2,3,4,5, and the order of the Out queues is 1,2,3,4,5 (only one-time dequeue is considered).

Queues are also divided into sequential queues and chain queues, and sequential queues are also based on array implementations, like sequential stacks and linked lists, and chained queues are based on linked list implementations.

Sequential queues:

Sequential queue

`#include  <stdio.h> #define  size 10typedef struct queue{int data[size];int  front;int tail;} queue;//Initialize Queue Void init_queue (queue* qu) {qu->front=0;qu->tail=-1;} Determines whether the queue is full Int is_queue_empty (queue* qu) {if (qu->tail==-1) {return 1;} return 0;} Determines whether the queue is full Int is_queue_full (queue* qu) {if (qu->tail==size-1) {return 1;} return 0;} Gets the team first value Int get_topvalue (queue* qu) {if (Is_queue_empty (qu)) {printf ("queue is empty!\n"); Return -1;} Return qu->data[qu->front];} Into queue Void in_queue (queue* qu,int _data) {if (Is_queue_full (qu)) {printf ("queue is full!\ n "); return ;} Qu->tail++;qu->data[qu->tail]=_data;} Int out_queue (Queue* qu) {if (Is_queue_empty (qu)) {printf ("queue is empty!\n"); return -1 ;} Int ret_value=qu->data[qu->front];int i;for (i=0;i<qu->tail;i++) {qu->data[i]=qu-> DATA[I+1];} Qu->tail--;rEturn ret_value;} Element Void print_queue (queue* qu) {int i;for (i=0;i<qu->tail+1;i++) {printf ("%d\t") in the print queue, qu- >data[i]);} printf ("\ n");} Int main (int argc, char const *argv[]) {queue qu;init_queue (&qu); In_queue ( &qu,1); In_queue (&qu,23); In_queue (&qu,5); In_queue (&qu,3) in_queue (&qu,6);p rintf ("get_ Topvalue:%d\n ", Get_topvalue (&qu));//out_queue (&qu);//print_queue (&qu); Int i;for (i=0;i<7;i+ +) {printf ("%d\t", Out_queue (&qu));} printf ("\ n"); return 0;}`

Chained queues:

`Chain-Queue #include <stdio.h> #include  <stdlib.h>typedef struct Node{int data; Struct node* next;} node;typedef struct queue{node* front; Node* tail;} queue;//Initialize Queue Void init_queue (queue* qu) {qu->front=null;qu->tail=null;} Determines whether the queue is empty int is_queue_empty (queue* qu) {if (qu->front==null) {return 1;} return 0;} Gets the team first value Int get_front_value (queue* qu) {if (Is_queue_empty (qu)) {printf ("queue is empty!\n"); Return -1;} Return qu->front->data;} Into row Void in_queue (queue* qu,int _data) {node* newnode= (node*) malloc (1*sizeof (Node)); Newnode->data=_data;newnode->next=null;if (qu->front==null && qu->tail==null) {qu- >front=newnode;qu->tail=newnode;} Else{qu->tail->next=newnode;qu->tail=newnode;}} Dequeue Void out_queue (queue* qu) {if (Is_queue_empty (qu)) {printf ("queue is empty!\n"); return;} Node* temp=qu->froNt;qu->front=qu->front->next;if (qu->front==null) {qu->tail=null;} Free (temp); temp=null;} The value in the print queue Void print_queue (queue* qu) {node* curr=qu->front;while (Curr) {printf ("%d\t", curr- >data); curr=curr->next;} printf ("\ n");} Int main (int argc, char const *argv[]) {queue qu;init_queue (&qu);int i; for (i=0;i<10;i++) {in_queue (&qu,i*2);} Print_queue (&qu);p rintf ("get_front_value:%d\n", Get_front_value (&qu)), Out_queue (&qu);p Rint_queue ( &qu);p rintf ("get_front_value:%d\n", Get_front_value (&qu)); return 0;}`

Implementation of sequential queues and chained queues

Related Keywords:

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.