C语言数据结构_链队列
链队列
一、定义结构体
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct Qnode {
QElemType data;
struct Qnode* next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
LinkQueue Q;
二、初始化
链队的初始化
Status InitQueue(LinkQueue& Q) {
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
三、链队销毁
Status DestoryQueue(LinkQueue & Q) {
QueuePtr p;
while (Q.front) {
p = Q.front->next;
free(Q.front);
Q.front = p;
}
return OK;
}
四、链队列入队 e元素入队
Status EnQueue(LinkQueue& Q, QElemType e) {
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
//QueuePtr p = new QNode;
p->data = e;
if (!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
五、链队出队
Status DeQueue(LinkQueue& Q, QElemType& e) {
QueuePtr p;
if (Q.front == Q.rear) return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p) Q.rear = Q.front;
free(p);
return OK;
}
六、取元素
Status GetHead(LinkQueue Q, QElemType& e) {
if (Q.front == Q.rear) return ERROR;
e = Q.front->next->data;
}
七、main
int main() {
InitQueue(Q);
QElemType e;
printf("输入入队元素e,输入-1停止\n");
scanf("%d", &e);
while (e != -1) {
EnQueue(Q, e);
scanf("%d", &e);
}
GetHead(Q, e);
printf("头元素%d\n", e);
printf("出对\n");
while (Q.front != Q.rear)
{
DeQueue(Q, e);
printf("%d",e);
}
}
八、完整代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct Qnode {
QElemType data;
struct Qnode* next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
LinkQueue Q;
//链队的初始化
Status InitQueue(LinkQueue& Q) {
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
//链队销毁
Status DestoryQueue(LinkQueue & Q) {
QueuePtr p;
while (Q.front) {
p = Q.front->next;
free(Q.front);
Q.front = p;
}
return OK;
}
//链队列入队 e元素入队
Status EnQueue(LinkQueue& Q, QElemType e) {
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
//QueuePtr p = new QNode;
p->data = e;
if (!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
//链队出队
Status DeQueue(LinkQueue& Q, QElemType& e) {
QueuePtr p;
if (Q.front == Q.rear) return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p) Q.rear = Q.front;
free(p);
return OK;
}
//对头元素
Status GetHead(LinkQueue Q, QElemType& e) {
if (Q.front == Q.rear) return ERROR;
e = Q.front->next->data;
}
int main() {
InitQueue(Q);
QElemType e;
printf("输入入队元素e,输入-1停止\n");
scanf("%d", &e);
while (e != -1) {
EnQueue(Q, e);
scanf("%d", &e);
}
GetHead(Q, e);
printf("头元素%d\n", e);
printf("出对\n");
while (Q.front != Q.rear)
{
DeQueue(Q, e);
printf("%d",e);
}
}
运行结果:
输入入队元素e,输入-1停止
1
2
3
4
5
-1
头元素1
出对
12345
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 咕噜
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果