Implementation of chain queue initialization, queuing, and team-out operation

Source: Internet
Author: User


The basic operation implementation code of chain queue initialization, Enqueue, and outbound is as follows:

#include <iostream>

using namespace Std;

#define TRUE 1

#define FALSE 0


Chain Queue definition

typedef struct NODE

{

int data;//data field

struct Node *next;//pointer field

}linkqueuenode;


typedef struct

{

Linkqueuenode *front;//Team head pointer front

Linkqueuenode *rear;//team head pointer rear

}linkqueue;


Initialize Q to an empty chain queue

int Initqueue (Linkqueue *q)

{

Q->front = (Linkqueuenode *) malloc (sizeof (Linkqueuenode));

if (q->front! = NULL)

{

Q->rear = q->front;

Q->front->next = NULL;

return TRUE;

}

Else

{

return false;//Overflow

}

}


Inserting a data element x into the queue Q

int Enterqueue (linkqueue *q,int x)

{

Linkqueuenode *newnode;

NewNode = (Linkqueuenode *) malloc (sizeof (Linkqueuenode));

if (NewNode! = NULL)

{

newnode->data=x;

Newnode->next = NULL;

Q->rear->next = NewNode;

Q->rear = NewNode;

return TRUE;

}

Else

{

return false;//Overflow

}

}


Queue Q's team head element out of the team and store it in the storage space referred to by x

int Deletequeue (linkqueue *q, int *x)

{

Linkqueuenode *p;

if (Q->front = = q->rear)

{

return FALSE;

}

p = q->front->next;

Q->front->next = p->next;//Team head element p out team

if (q->rear==p)//If there is only one element p in the team, then p becomes the empty team after the team

{

Q->rear= q->front;

}

*x = p->data;

Free (P);//freeing up storage space

return TRUE;

}


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1771880

Implementation of chain queue initialization, queuing, and team-out operation

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.