您的位置:首页 > 编程语言 > C语言/C++

C/C++知识回顾 队列的出队和入队

2017-03-06 21:53 239 查看
#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()
{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言
相关文章推荐