首页 > 编程语言 >数据结构代码整理_队列Queue(C++)

数据结构代码整理_队列Queue(C++)

时间:2023-06-20 10:01:15浏览次数:32  
标签:Node code C++ Queue front entry 数据结构 rear


所谓队列,就是先进先出规则的实现。

基于链表实现

main.cpp

#include<iostream>
#include"Queue.h"
using namespace std;
int main()
{
	Queue q;
	q.append(1);
	q.append(2);
	Queue_entry a;
	q.retrieve(a);
	cout << a << "  " <<q.empty();
	return 0;
}

Node.h

#pragma once
typedef int Node_entry;
struct Node {
    //  data members
    Node_entry entry;
    Node* next;
    //  constructors
    Node();
    Node(Node_entry item, Node* add_on = nullptr);
};
Node::Node()
{
    next = nullptr;
}


Node::Node(Node_entry item, Node* add_on)
{
    entry = item;
    next = add_on;
}

Queue.h

#pragma once
#include"Node.h"
enum Error_code {
    success, fail, range_error, underflow,
    overflow, fatal, not_present, duplicate_error,
    entry_inserted, entry_found, internal_error
};
typedef int Queue_entry;
class Queue {
public:
    //  standard Queue methods
    Queue();
    bool empty() const { return front == nullptr; };
    Error_code append(const Queue_entry& item);
    Error_code serve();
    Error_code retrieve(Queue_entry& item) const;
    //  safety features for linked structures
    ~Queue() {};
    Queue(const Queue& original) {};
    void operator =(const Queue& original) {};
protected:
    Node* front, * rear;
};

Queue::Queue()
/*
Post: The Queue is initialized to be empty.
*/
{
    front = rear = nullptr;
}


Error_code Queue::append(const Queue_entry& item)
/*
Post: Add item to the rear of the Queue and return a code of success
      or return a code of overflow if dynamic memory is exhausted.
*/
{
    Node* new_rear = new Node(item);
    if (new_rear == nullptr)
        return overflow;
    if (rear == nullptr)
        front = rear = new_rear;
    else {
        rear->next = new_rear;
        rear = new_rear;
    }
    return success;
}
Error_code Queue::serve()//删除队头
/*
Post: The front of the Queue is removed.  If the Queue
      is empty, return an Error_code of underflow.
*/
{
    if (front == nullptr)
        return underflow;
    Node* old_front = front;
    front = old_front->next;
    if (front == nullptr)
        rear = nullptr;
    delete old_front;
    return success;
}
Error_code Queue::retrieve(Queue_entry& item) const {
    if (front)
    {
        item = front->entry;
        return success;
    }
    else
        return underflow;

}

基于数组实现

main.cpp

#include<iostream>
using namespace std;
#include"Queue.h"
int main()
{
	Queue q;
	q.append('a');
	q.serve();//删队头
	q.append('b');
	q.serve();
	q.append('c');
	q.append('d');
	q.serve();
	char temp;
	q.retrieve(temp);
	cout << temp;
	return 0;
}

Queue.h

#pragma once
enum Error_code {
    success, fail, range_error, underflow, overflow, fatal,
    not_present, duplicate_error, entry_inserted, entry_found,
    internal_error
};
const int maxqueue = 10; //  small value for testing
typedef char Queue_entry; // The program will use stacks with float entries. 
class Queue {
public:
    Queue();
    bool empty() const;
    Error_code serve();
    Error_code append(const Queue_entry& item);
    Error_code retrieve(Queue_entry& item) const;

protected:
    int count;
    int front, rear;
    Queue_entry entry[maxqueue];
};
class Extended_queue : public Queue {
public:
    bool full() const;
    int size() const;
    void clear();
    Error_code serve_and_retrieve(Queue_entry& item);
};

Queue::Queue()
/*
Post: The Queue is initialized to be empty.
*/
{
    count = 0;
    rear = maxqueue - 1;
    front = 0;
}


bool Queue::empty() const
/*
Post: Return true if the Queue is empty, otherwise return false.
*/
{
    return count == 0;
}


Error_code Queue::append(const Queue_entry& item)//尾插
/*
Post: item is added to the rear of the Queue. If the Queue is full
return an Error_code of overflow and leave the Queue unchanged.
*/

{
    if (count >= maxqueue) return overflow;
    count++;
    rear = ((rear + 1) == maxqueue) ? 0 : (rear + 1);
    entry[rear] = item;
    return success;
}


Error_code Queue::serve()//删掉队头
/*
Post: The front of the Queue is removed. If the Queue
is empty return an Error_code of underflow.
*/

{
    if (count <= 0) return underflow;
    count--;
    front = ((front + 1) == maxqueue) ? 0 : (front + 1);
    return success;
}


Error_code Queue::retrieve(Queue_entry& item) const//返回队头
/*
Post: The front of the Queue retrieved to the output
      parameter item. If the Queue is empty return an Error_code of underflow.
*/

{
    if (count <= 0) return underflow;
    item = entry[front];
    return success;
}


int Extended_queue::size() const
/*
Post:   Return the number of entries in the Extended_queue.
*/
{
    return count;
}


标签:Node,code,C++,Queue,front,entry,数据结构,rear
From: https://blog.51cto.com/u_16165815/6520516

相关文章

  • PTA_乙级_1001 害死人不偿命的(3n+1)猜想 (C++_数论)
    卡拉兹(Callatz)猜想:对任何一个正整数n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把(3n+1)砍掉一半。这样一直反复砍下去,最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹......
  • 数据结构代码整理_栈Stack(C++)
           所谓栈,就是先进后出规则的实现,这种数据结构在DFS等算法体现的较为明显,因为课程要求不得不进行各种数据结构实现代码整理,就发出来与大家分享。下面有两种实现方法一个是基于数组实现的,另一个是基于链表实现的。基于数组实现源码main.cpp//main.cpp:定义控制台应用程......
  • C++使用ranges库解析INI文件
    C++使用ranges库解析INI文件引言C++20引入了<ranges>头文件,C++23对其进行了完善,本文将使用该头文件提供的adaptor编写一个简单的ini解析器。ini文件格式介绍一般的ini文件由section和entry部分,比如[section]key=value;Thisisentry.section和entry均独占一行,其中sectio......
  • C++四种类型转换
    篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++四种类型转换相关的知识,希望对你有一定的参考价值。const_cast主要用于删除变量的const属性,便于赋值constinta=2;int*p=const_cast<int*>(&a);*p=3;reinterpret_cast仅仅是重新解释类型,没有二进制的......
  • C++ 关键字四种cast类型转换
    1.23四种cast类型转换作用:克服c中强制类型转化带来的风险,C++引入四种更加安全的强制类型转换运算符(明确转换的目的,偏于程序的维护和分析)const_cast://1.去除const属性,将只读变为只读写//2.针对常量指针、常量引用和常量对象constchar*p;char*p1=const_cast<char*>(p......
  • C++ 数据类型转换详解之终极无惑
    程序开发环境:VS2017+Win32+Debug文章目录1.隐式数据类型转换2.显示数据类型转换3.C++新式类型转换3.1const_cast3.2static_cast3.3dynamic_cast3.3.1向下转换3.3.2交叉转换3.4reinterpret_cast4.重载相关类型转换操作符4.1不同类对象的相互转换4.2基本数据类型与类对象......
  • C++面试八股文:什么是智能指针?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第19面:面试官:什么是智能指针?二师兄:智能指针是C++11引入的类模板,用于管理资源,行为类似于指针,但不需要手动申请、释放资源,所以称为智能指针。面试官:C++11引入了哪些智能指针?二师兄:三种,分别是shared_ptr、unique_ptr、和weak_ptr。......
  • C++面试八股文:什么是智能指针?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第19面:面试官:什么是智能指针?二师兄:智能指针是C++11引入的类模板,用于管理资源,行为类似于指针,但不需要手动申请、释放资源,所以称为智能指针。面试官:C++11引入了哪些智能指针?二师兄:三种,分别是shared_ptr、unique_ptr、和weak_ptr。......
  • Go 设计模式|组合,一个对数据结构算法和职场都有提升的设计模式
    Go设计模式|组合,一个对数据结构算法和职场都有提升的设计模式原创 KevinYan11 网管叨bi叨 2023-01-1608:45 发表于北京收录于合集#用Go学设计模式24个大家好,我是每周在这里陪你进步的网管~,这次我们继续设计模式的学习之旅。本次要学习的是组合模式,这个模式呢,平时要做......
  • C++继承和派生
    #继承和派生在C++中,继承和派生是面向对象编程的两个重要概念,用于实现类与类之间的关系。继承是指一个类可以从另一个类中继承属性和方法,并且可以在此基础上扩展出自己的属性和方法。被继承的类称为基类(父类),继承的类称为派生类(子类)。在C++中,可以通过以下方式定义一个派生类:```c++cl......