单链表sLinkList类,模板类
队列(queue )是一种先进先出(first in first out,简称 FIFO表)的线性表。
它只允许在表的一端进行插入,而在另一端删 除元素。
在队列中,允许插入的一端叫做队尾(rear),允许删除的一端 称为队头(front)。
一、队列的顺序表示和实现,循环队列(数组)
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -1
- #define MAXQSIZE 100 //最大队列长度
- typedef int Status;
- typedef int QElemType;
- /* 0.队列结构体 */
- typedef struct {
- QElemType *base; // 指针(指向动态分配存储空间)
- int front; //头指针,若队列不空,
- //指向队列头元素
- int rear; //尾指针,若队列不空,
- //指向队列尾元素的下 一个位置
- }SqQueue; //定义循环队列类型
- /* 1.队列Q初始化(构造一个空队列Q) */
- Status InitQueue (SqQueue &Q) {
- Q.base = (QElemType *) malloc(MAXQSIZE *sizeof (QElemType));
- if (!Q.base) exit (OVERFLOW);// 存储分配失败
- Q.front = Q.rear = ; //队列为空(数组下标均为0)
- return OK;
- }
- /* 2.队列Q的队尾插入元素e*/
- Status EnQueue (SqQueue &Q, QElemType e) {
- if ((Q.rear+) % MAXQSIZE == Q.front)
- return ERROR; //队列满 rear+1 == front
- Q.base[Q.rear] = e; //插入队尾
- Q.rear = (Q.rear+) % MAXQSIZE; //队尾指针更新 rear+1
- return OK;
- }
- /* 3.删除队列Q的队头元素 */
- Status DeQueue (SqQueue &Q, QElemType &e) {
- if (Q.front == Q.rear) return ERROR; //空队列 front == rear
- e = Q.base[Q.front]; //返回删除的元素
- Q.front = (Q.front+) % MAXQSIZE; //删除元素(更新队头指针 front+1)
- return OK;
- }
- /* 测试代码 */
- int main()
- {
- int array[] = {};
- SqQueue Q; //创建一个队列变量
- InitQueue(Q);//队列初始化(分配空间,构造空队列)
- for(int i=; i<; ++i)
- EnQueue(Q,i+); //队列插入元素
- for(int j=; j<; ++j)
- DeQueue(Q,array[j]); //删除队列元素,到数组array
- for(int k=; k<; ++k) //输出数组元素
- printf("%3d",array[k]);
- printf("\n");
- return ;
- }
- //浙大数据结构 \ 顺序队列
- typedef int Position;
- struct QNode {
- ElementType *Data; /* 存储元素的数组 */
- Position Front, Rear; /* 队列的头、尾指针 */
- int MaxSize; /* 队列最大容量 */
- };
- typedef struct QNode *Queue;
- Queue CreateQueue( int MaxSize )
- {
- Queue Q = (Queue)malloc(sizeof(struct QNode));
- Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
- Q->Front = Q->Rear = ;
- Q->MaxSize = MaxSize;
- return Q;
- }
- bool IsFull( Queue Q )
- {
- return ((Q->Rear+)%Q->MaxSize == Q->Front);
- }
- bool AddQ( Queue Q, ElementType X )
- {
- if ( IsFull(Q) ) {
- printf("队列满");
- return false;
- }
- else {
- Q->Rear = (Q->Rear+)%Q->MaxSize;
- Q->Data[Q->Rear] = X;
- return true;
- }
- }
- bool IsEmpty( Queue Q )
- {
- return (Q->Front == Q->Rear);
- }
- ElementType DeleteQ( Queue Q )
- {
- if ( IsEmpty(Q) ) {
- printf("队列空");
- return ERROR;
- }
- else {
- Q->Front =(Q->Front+)%Q->MaxSize;
- return Q->Data[Q->Front];
- }
- }
二、队列的链式表示和实现,链队列(链表)
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -1
- #define MAXQSIZE 100 //最大队列长度
- typedef int Status;
- typedef int QElemType;
- typedef struct Node QNode; //结点类型
- typedef struct Node* QueuePtr;//结点指针
- struct Node { //定义结点
- QElemType data; //结点data
- QueuePtr next; //结点next指针
- };
- /* 0.队列结构体 */
- typedef struct {
- QueuePtr front; // 队头指针
- QueuePtr rear; // 队尾指针
- } LinkQueue; // 链队列类型
- /* 1.队列Q初始化(构造一个空队列Q) */
- Status InitQueue (LinkQueue &Q) {
- //队列的队头、队尾指向队头结点(空队列)
- Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
- if (!Q.front) exit (OVERFLOW);//存储分配失败
- Q.front->next = NULL;//头指针置空
- return OK;
- }
- /* 2.销毁队列Q */
- Status DestroyQueue (LinkQueue &Q) {
- Q.front = Q.front->next;//带头结点的队列
- while (Q.front)
- {
- Q.rear = Q.front->next;
- free (Q.front) ;
- Q.front = Q.rear;
- }
- return OK;
- }
- /* 3.队列Q的队尾插入元素e*/
- Status EnQueue (LinkQueue &Q, QElemType e) {
- QueuePtr p = (QueuePtr) malloc (sizeof (QNode));
- if(!p) exit (OVERFLOW); //存储分配失败
- p->data = e;
- p->next = NULL; //新结点p->next置为NULL
- Q.rear->next = p;//插入队列
- Q.rear = p; //更新队尾
- return OK;
- }
- /* 3.删除队列Q的队头元素 */
- Status DeQueue (LinkQueue &Q, QElemType &e) {
- if (Q.front == Q.rear) return ERROR; //队列为空
- QueuePtr p = Q.front->next;
- e = p->data;
- Q.front->next = p->next; //删除队头元素(其后继元素挂到队列头结点)
- if (Q.rear == NULL) Q.rear = Q.front; //队尾空,队列置空
- free (p); //释放删除的对头元素
- return OK;
- }
- /* 测试代码 */
- int main()
- {
- int array[] = {};
- LinkQueue Q; //创建一个队列变量
- InitQueue(Q);//队列初始化(分配空间,构造空队列)
- for(int i=; i<; ++i)
- EnQueue(Q,i+); //队列插入元素
- for(int j=; j<; ++j)
- DeQueue(Q,array[j]); //删除队列元素,到数组array
- for(int k=; k<; ++k) //输出数组元素
- printf("%3d",array[k]);
- printf("\n");
- return ;
- }
- /* 只有尾指针的循环队列 */
- #include <stdio.h>
- #include <malloc.h>
- #define OK 1
- #define ERROR 0
- typedef int QElemType, Status;
- typedef struct LinkQueueNode* LinkQueuePTR;
- /* 0.结点 */
- struct LinkQueueNode{
- QElemType e;
- LinkQueuePTR next;
- };
- /* 1. 初始化队列*/
- Status InitQueue(LinkQueuePTR *rear)
- {
- LinkQueuePTR p;
- p = (LinkQueuePTR)malloc(sizeof(struct LinkQueueNode));
- if(p == NULL)
- return ERROR;
- p->next = p;
- *rear = p;
- return OK;
- }
- /* 2. 队列是否为空*/
- Status isEmpty(LinkQueuePTR rear)
- {
- if(rear == NULL)
- return ERROR;
- return rear == rear->next ? OK : ERROR;
- }
- /* 3. 进队*/
- Status enqueue(LinkQueuePTR *rear,QElemType e)
- {
- LinkQueuePTR p;
- p = (LinkQueuePTR)malloc(sizeof(struct LinkQueueNode));
- if(p == NULL)
- return ERROR;
- p->e=e;
- p->next = (*rear)->next;
- (*rear)->next = p;
- *rear = p;
- return OK;
- }
- /* 4. 出队*/
- Status dequeue(LinkQueuePTR *rear,QElemType *e)
- {
- if(isEmpty(*rear))
- return ERROR;
- LinkQueuePTR front;
- front = (*rear)->next->next;
- if(front == *rear){/* 是否只一个结点 */
- *rear = front->next;
- (*rear)->next = *rear;
- }
- else{
- (*rear)->next->next = front->next;
- }
- *e = front->e;
- free(front);
- return OK;
- }
- /* 测试代码 */
- int main()
- {
- LinkQueuePTR rear = NULL;
- QElemType e;
- int n = ;
- /* 初始化只有尾指针的循环队列 */
- InitQueue(&rear);
- /* 队列输入数据(进队) */
- for(int i=; i<n; ++i){
- scanf("%d",&e);
- enqueue(&rear,e);
- }
- /* 出队 */
- while(!isEmpty(rear))
- {
- dequeue(&rear,&e);
- printf("%5d",e);
- }
- return ;
- }
- //浙大数据结构 \ 链队列
- typedef struct Node *PtrToNode;
- struct Node { /* 队列中的结点 */
- ElementType Data;
- PtrToNode Next;
- };
- typedef PtrToNode Position;
- struct QNode {
- Position Front, Rear; /* 队列的头、尾指针 */
- int MaxSize; /* 队列最大容量 */
- };
- typedef struct QNode *Queue;
- bool IsEmpty( Queue Q )
- {
- return ( Q->Front == NULL);
- }
- ElementType DeleteQ( Queue Q )
- {
- Position FrontCell;
- ElementType FrontElem;
- if ( IsEmpty(Q) ) {
- printf("队列空");
- return ERROR;
- }
- else {
- FrontCell = Q->Front;
- if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */
- Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */
- else
- Q->Front = Q->Front->Next;
- FrontElem = FrontCell->Data;
- free( FrontCell ); /* 释放被删除结点空间 */
- return FrontElem;
- }
- }
单链表sLinkList类,模板类的更多相关文章
- C++中的链表节点用模板类和用普通类来实现的区别
C++中的链表节点通常情况下类型都是一致的.因此我们可以用模板来实现. #include <iostream> using namespace std; template<typen ...
- 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- C++学习笔记36:类模板
类模板的目的 设计通用的类型式,以适应广泛的成员数据型式 类模板的定义格式 template<模板形式参数列表>class 类名称{...}; 原型:template<typenam ...
- C++解析(26):函数模板与类模板
0.目录 1.函数模板 1.1 函数模板与泛型编程 1.2 多参数函数模板 1.3 函数重载遇上函数模板 2.类模板 2.1 类模板 2.2 多参数类模板与特化 2.3 特化的深度分析 3.小结 1. ...
- 如何用boost::serialization去序列化派生模板类(续)
在 如何用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类, 在写測试用例时一直出现编译错误,调了非常久也没跳出来,今天偶然试了一下...竟然调了出来. ...
- C++_进阶之函数模板_类模板
C++_进阶之函数模板_类模板 第一部分 前言 c++提供了函数模板(function template.)所谓函数模板,实际上是建立一个通用函数,其函数类型和形参类型不具体制定,用一个虚拟的类型来 ...
- C++ 类模板与模板类详解
在C++的Template中很多地方都用到了typename与class这两个关键字,有时候这两者可以替换,那么这两个关键字是否完全一样呢? 事实上class用于定义类,在模板引入c++后,最初定义模 ...
- C++程序设计方法4:类模板
类模板 在定义类时也可以将一些类型抽象出来,用模板参数来替换,从而使类更具有通用性.这种类被称为模板类,例如: template <typename T> class A { T data ...
- C++ 类模板基础知识
类模板与模板类 为什么要引入类模板:类模板是对一批仅仅成员数据类型不同的类的抽象,程序员只要为这一批类所组成的整个类家族创建一个类模板,给出一套程序代码,就可以用来生成多种具体的类,(这类可以看作是类 ...
随机推荐
- Codeforces Round #422 (Div. 2)E. Liar sa+st表+dp
题意:给你两个串s,p,问你把s分开顺序不变,能不能用最多k段合成p. 题解:dp[i][j]表示s到了前i项,用了j段的最多能合成p的前缀是哪里,那么转移就是两种,\(dp[i+1][j]=dp[i ...
- spring reference
Spring框架概述 Spring可以轻松创建Java企业应用程序.它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并可根据应用程序的需要灵活地创 ...
- python3 doc2vec文本聚类实现
import sys #doc2vev import gensim import sklearn import numpy as np from gensim.models.doc2vec impor ...
- C#: int 与 byte[] 互转
public static int ToInt32(params byte[] v) { ; var len = v.Length; ) { len = ; } ; i < len; i++) ...
- css常用的属性
CSS------属性值篇 display: none | block | inline(默认值) | inline-block(css2新增) | inherit none :此元素不会再显示 {注 ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- 学号20175212 《Java程序设计》第7周学习总结
学号20175212 <Java程序设计>第7周学习总结 教材学习内容总结 8.1.String类 可以使用String类声明对象并创建对象,例如: String s = new Stri ...
- 【Monkey】Monkey稳定性测试常用命令
Monkey稳定性测试常用命令: 1.adb shell monkey n 2.adb shell monkey -p com.android.calculator2 1000 3.adb shel ...
- 阅读github上的项目源码
1.基础资料 函数手册,类库函数手册2.和程序相关的专业资料 高数,linux文件系统3.相关项目的文档资料4.留备份,构造可运行的环境,找开始的地方 main(),5.分层次阅读,写注解,编程思想, ...
- h5属性直接控制上传文件类型
和公司前端交互的时候发现我在选择上传文件的时候只能选择图片,其他类型,text,doc等等等等全部无法选择 仔细查看了下代码,发现归功于H5新增(??没查到资料,不确定是不是H5的)的input属性 ...