链队列小程序
来源:互联网 发布:macair怎么卸载软件 编辑:程序博客网 时间:2024/06/05 20:32
中午学习的队列程序。我的博客好水~~~ 水水水~~~
q->front->next是头指针 q->front不保存数据
获得第一个数据:
qnode* p;
p=q->front->next
p->data
添加数据:
q->rear->next=(qnode*)malloc(sizeof(qnode));
q->rear=q->rear->next
q->real->data=e;
q->real->next=NULL:
销毁队列:利用q->rear保存了队列下一个node
while(q->front)
{
q->rear=q->front->next;
free(q->front);
q->front=q->rear;
}
q->front->next是头指针 q->front不保存数据
获得第一个数据:
qnode* p;
p=q->front->next
p->data
添加数据:
q->rear->next=(qnode*)malloc(sizeof(qnode));
q->rear=q->rear->next
q->real->data=e;
q->real->next=NULL:
销毁队列:利用q->rear保存了队列下一个node
while(q->front)
{
q->rear=q->front->next;
free(q->front);
q->front=q->rear;
}
程序:
// linkqueue.cpp : 定义控制台应用程序的入口点。//#include <stdio.h>#include "stdafx.h"#include <stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef struct Qnode{int data;struct Qnode* next;}qnode;typedef struct{qnode* front;qnode* rear;}linkqueue;void init(linkqueue* q){//q->front 不存dataq->front = (qnode*)malloc(sizeof(qnode));q->rear = q->front;(q->front)->next = NULL;}int enqueue(linkqueue* q,int e){q->rear->next = (qnode*)malloc(sizeof(qnode));q->rear = q->rear->next;if (!q->rear)return ERROR;q->rear->data = e;q->rear->next = NULL;return OK;}int dequeue(linkqueue* q,int& e){if (q->front == q->rear){printf("linkqueue is empty . can't dequeue\n");return ERROR;}qnode* p;p = q->front->next;(q->front)->next = p->next;e = p->data;free(p);return OK;}int gethead(linkqueue* q){if (q->front == q->rear)return -1;elsereturn q->front->next->data;}void visit(linkqueue* q){qnode* p;p = q->front->next;if (q->front == q->rear)printf("linkqueue is empty");else{while (p){printf("%d-> ", p->data);p = p->next;}printf("\n");}}void destroy(linkqueue* q){while (q->front){q->rear = q->front->next;free(q->front);q->front = q->rear;}}int _tmain(int argc, _TCHAR* argv[]){int n,i;linkqueue* queue=(linkqueue*)malloc(sizeof(linkqueue));int a,select;int e;printf("create a empty linkqueue\n");init(queue);printf("please input linkqueue length\n");scanf("%d", &n);printf("please input linkqueue calue\n");for (i = 0; i < n; i++){scanf("%d", &a);enqueue(queue, a);}visit(queue);printf("select 1---destroy()\n");printf("select 2---visit()\n");printf("select 3---gethead()\n");printf("select 4---enqueue()\n");printf("select 5---dequeue()\n");printf("select 6---quit()\n");while (1){printf("please select (1--6):\n");scanf("%d", &select);switch (select){case 1:destroy(queue);break;case 2:visit(queue);break;case 3:printf("queue->front->%d\n", gethead(queue));break;case 4:printf("please inter the value:\n");scanf("%d", &e);enqueue(queue, e);visit(queue);break;case 5:dequeue(queue, e);printf("delete head %d\n", e);visit(queue);break;case 6:exit(0);default:break;}}return 0;}
- 链队列小程序
- 消息队列的小程序
- 队列程序
- 《数据结构》链队列同学程序调试修改
- 小程序(4)——优先队列(基于静态数组的最小堆实现)
- 每天一个小程序(8)——顺序循环队列
- Linux下进程之间通过消息队列通信小程序示例
- Java小程序之自定义数组队列的实现(高级画板重绘基础)
- python小程序:以后台服务方式 定时 监视redis队列 (anaconda3.5.2)
- 队列小述
- 双向队列 小练
- 消息队列小实例
- 队列小练习
- 小程序
- 小程序
- 小程序
- 小程序
- 小程序
- Python 专题一 python 使用 MySQL
- QAction
- 编程忠告——给自己
- 黑马程序员-自学笔记-HTML基础
- C#Windows窗体界面设计_01_绘制三角函数_五点作图法
- 链队列小程序
- 黑马程序员-自学笔记-CSS基础
- c中指针的声明识别
- c/c++ 的内存机制
- bank05-2——银行计算利息
- [Android开发常见问题-20] Android 网络通信框架Volley简介(Google IO 2013)
- HTTP协议中Unity3D获取网络图文方式
- TCP与UDP的不同接包处理方式
- 简要介绍Java中的枚举类