【数据结构】队列的出队和入队操作
发布时间:2020-12-15 06:07:41 所属栏目:安全 来源:网络整理
导读:队列的操作规则是先进先出,要注意一下, 1.队列为空 2.队列只有一个元素,即头尾指针都指向空 3.初始化队列时,分配空间后不要忘记将头为指针置空 // 13_4.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include strin
|
队列的操作规则是先进先出,要注意一下, 1.队列为空 2.队列只有一个元素,即头尾指针都指向空 3.初始化队列时,分配空间后不要忘记将头为指针置空 // 13_4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
typedef struct linkqueue
{
node *first,*rear;
}queue;
queue *insert(queue *HQ,int x)
{
node *p = (node *)malloc(sizeof(node));
p->data = x;
p->next = NULL;
if (NULL == HQ->rear)
{
HQ->first = p;
HQ->rear = p;
}
else
{
HQ->rear->next = p;
HQ->rear = p;
}
cout << HQ->rear->data << endl;
return HQ;
}
queue *del(queue *HQ)
{
node *p;
int x;
if (NULL == HQ)
{
cout << "Queue is null!" << endl;
}
else
{
p = HQ->first;
x = HQ->first->data;
if (HQ->first == HQ->rear)
{
HQ->rear = NULL;
HQ->rear = NULL;
free(p);
}
else
{
HQ->first = HQ->first->next;
free(p);
}
cout << x << endl;
}
return HQ;
}
int _tmain(int argc,_TCHAR* argv[])
{
linkqueue *seq = (linkqueue *)malloc(sizeof(linkqueue));
seq->rear = NULL;
seq->first = NULL;
cout << "The inserted seq is: n" << endl;
for (int i = 0; i < 10; i++)
{
seq = insert(seq,i);
}
cout << "The deleted seq is: n" << endl;
for (int i = 0; i < 10; i++)
{
seq = del(seq);
}
return 0;
}
输出: The inserted seq is: 0 1 2 3 4 5 6 7 8 9 The deleted seq is: 0 1 2 3 4 5 6 7 8 9 请按任意键继续. . . (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
