//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
//栈和队列对比