1. 程式人生 > >C/C++知識回顧 佇列的出隊和入隊

C/C++知識回顧 佇列的出隊和入隊

#include<iostream>
using namespace std;
typedef struct student
{
	int data;
	struct student *next;
}node;
typedef struct linkqueue
{
	node *first, *rear;
}queue;
//入隊
queue * push(queue * Q, int num)
{
	node *s = (node *)malloc(sizeof(node));
	s->data = num;
	s->next = NULL;
	if (Q->first==NULL)
	{
		Q->first = s;
		Q->rear = s;
	}
	else
	{
		Q->rear -> next = s;
		Q->rear = s;
	}
	return Q;
}
//出隊
int pop(queue * Q)
{
	if (Q->first==NULL)
	{
		cout << "佇列為空" << endl;
		return -1;
	}
	else
	{
		int x = Q->first->data;
		node *p = Q->first;
		if (p==Q->rear)
		{
			Q->first = NULL;
			Q->rear = NULL;
		}
		else
		{
			Q->first = Q->first->next;
			free(p);
			return x;
		}
	}


}
int main()
{


}