链式队列的概念及其相关基本运算

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//main.cpp
#include"linkqueue.h"
int main(void)
{

	linkqueue l=NULL;
	int i = 0, k = 0;

	l = initseqQueue();//创建一个空链式队列
	for (i = 0; i <= 5; i++)
	{

		inseqQueue(l, i * 2);//进队

	}

	show_seqQueue(l);//打印进队的元素
	printf("\n");
	
    k=outseqQueue(l);//出队
	printf("%d\n", k);

	k = outseqQueue(l);//出队
	printf("%d\n", k);

	printf("\n");

	show_seqQueue(l);//打印剩余的元素
	system("pause");
	return 0;
}

//linkqueue.cpp
#include"linkqueue.h"
/*
 * @file      linkqueue.cpp
 * @function  创建一个空链队列函数
 * @author    酸菜。
 * @date      2019-11-5
*/
linkqueue initseqQueue()
{
	linkqueue q = NULL;
	linklist p = NULL;

	q = (linkqueue)malloc(sizeof(linkqueue_node));
	p = (linklist)malloc(sizeof(linknode));

	if (q == NULL || p == NULL)
	{
		return NULL;
	}

	q->front= p;
	q->rear= p;

	p->data = 0;
	p->next = NULL;

	return q;

}
/*
 * @file      linkqueue.cpp
 * @function  创建一个进链式队列函数
 * @author    酸菜。
 * @date      2019-11-5
*/
void inseqQueue(linkqueue top, int value)
{
	int i = 0;
	linklist t = NULL;
	t = (linklist)malloc(sizeof(linknode));

	t->data = value;
	//i = t->data;
	t->next = NULL;

	top->rear->next = t;
	top->rear = t;

}
/*
 * @file      linkqueue.cpp
 * @function  创建一个出队列函数
 * @author    酸菜。
 * @date      2019-11-5
*/
int outseqQueue(linkqueue top)
{
	linklist t = NULL;
	int value = 0;

	t = top->front;
	top->front = top->front->next;
	value = top->front->data;

	free(t);

	return value;

}
/*
 * @file      linkqueue.cpp
 * @function  判断链式队列是否为空函数
 * @author    酸菜。
 * @date      2019-11-5
*/
int seqQueue_is_empty(linkqueue top)
{

	return (top->front == top->rear) ? 1 : 0;

}
/*
 * @file      linkqueue.cpp
 * @function  打印链式队列函数
 * @author    酸菜。
 * @date      2019-11-5
*/
void show_seqQueue(linkqueue top)
{
	linklist p = NULL;
	

	for (p = top->front->next; p != NULL; p = p->next)
	{

		printf("%d\n", p->data);

	}

}
#ifndef  _linkqueue_h
#define  _linkqueue_h

#include<stdio.h>
#include<iostream>

//节点数据类型
typedef struct node
{
	int data;

	struct node* next;

}*linklist,linknode;
//链式队列数据类型
typedef struct queue
{

	linklist front, rear;

}*linkqueue,linkqueue_node;

//相关函数外部声明
extern linkqueue initseqQueue();
extern void inseqQueue(linkqueue top, int value);
extern int  outseqQueue(linkqueue top);
extern int  seqQueue_is_empty(linkqueue top);
extern void show_seqQueue(linkqueue top);

#endif  //linkqueue.h

//栈和队列对比
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值