C++数据结构链队出队入队

链队相关操作

  1. 初始化队列
  2. 入队
  3. 出队
#include "stdafx.h"
#include <iostream>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ELEMTYPE;
typedef int Status;

typedef struct LNode  //结点结构 单链表
{
	ELEMTYPE data;
	struct LNode *next;
}LNode,*Queueptr;

typedef struct     //队列的结构
{
	Queueptr front; //队头指针
	Queueptr rear;  //队尾指针
}LinkQueue;

Status initLinkQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new LNode;  //带头结点 初始化都指向头结点
	if(!Q.front) return OVERFLOW;
	Q.front->next = NULL;
}

bool isEmpty(LinkQueue &Q) //判队空
{
	if (Q.front->next == NULL)
		return true;
	else
		return false;
}
void EnQueue(LinkQueue &Q,ELEMTYPE e) //入队
{
	LNode *p = (LNode *)malloc(sizeof(LNode));
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}

void ExQueue(LinkQueue &Q,ELEMTYPE &e) //出队
{
	if(isEmpty(Q))
		return;
	LNode *p = Q.front->next;
	e = p->data;
	Q.front->next = Q.front->next->next;
	delete (p);
	if (isEmpty(Q)) //若空表 尾指针置初位
		Q.rear = Q.front;
}

void Visit(LinkQueue &Q)
{
	LNode *p = Q.front->next;
	while(p)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
发布了16 篇原创文章 · 获赞 25 · 访问量 1140

猜你喜欢

转载自blog.csdn.net/weixin_41546300/article/details/104650256