zoukankan      html  css  js  c++  java
  • 链式队列

    RationQueue.h

    #pragma once
    
    class RationQueue
    {
    public:
    	RationQueue();
    	~RationQueue();
    public:
    	bool push(double d);		//入栈
    	bool pop(double &data);		//出栈
    	bool empty();				//判断栈是否为空
    private:
    	typedef struct Node
    	{
    		double data;
    		Node *next;
    	}Node, *pNode;
    	pNode head;
    	pNode tail;					//带有头尾结点的链表
    };
    RationQueue.cpp

    #include <iostream>
    #include "RationQueue.h"
    
    RationQueue::RationQueue()
    {
    	head = new Node;
    	head->next = NULL;
    	tail = head;
    }
    
    RationQueue::~RationQueue()
    {
    	//删除所有结点(包括头结点)
    	while (head != NULL)
    	{
    		Node *s = head->next;
    		delete head;
    		head = s;
    	}
    	
    }
    
    //入栈
    bool RationQueue::push(double d)
    {
    	Node *p = new Node;
    	p->data = d;
    	p->next = NULL;
    
    	tail->next = p;
    	tail = p;
    	return true;
    	
    }
    
    //出栈
    bool RationQueue::pop(double &data)
    {
    	Node *p = head->next;
    	data = p->data;
    	head->next = p->next;
    	if (head->next == NULL)//此时只存在头结点
    	{
    		tail = head;
    	}
    	delete p;
    	p = NULL;
    	return true;
    }
    
    //判断栈是否为空
    bool RationQueue::empty()
    {
    	/*
    	if (head == tail)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    	*/
    	return head == tail;
    }
    测试.cpp

    #include <iostream>
    #include "RationQueue.h"
    #include <vld.h>
    using namespace std;
    
    void logo()
    {
    	cout << "+------------------------------------------------+" << endl;
    	cout << "+-------------------++1.入栈++-------------------+" << endl;
    	cout << "+-------------------++2.出栈++-------------------+" << endl;
    	cout << "+------------------------------------------------+" << endl;
    	cout << "请选择:";
    }
    
    int main()
    {
    	RationQueue Q;
    	bool quit = false;
    	int n;
    	double data;
    	while (!quit)
    	{
    		logo();
    		cin >> n;
    		switch (n)
    		{
    		case 1:
    			cout << "入栈:";
    			cin >> data;
    			Q.push(data);
    			break;
    		case 2:
    			if (!Q.empty())
    			{
    				Q.pop(data);
    				cout << data << " 出栈!" << endl;
    			}
    			else
    			{
    				cout << "栈内无数据!" << endl;
    			}
    			break;
    		default:
    			quit = true;
    			break;
    		}
    	}
    	return 0;
    }

    vld.h为检测内存溢出的工具

    Visual Leak Detector read settings from: G:VLDvld.ini
    Visual Leak Detector Version 2.5 installed.
    No memory leaks detected.
    Visual Leak Detector is now exiting.




    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    软件工程第1次阅读作业
    centOS 7设置静态IP,使用Xshell远程连接
    jmeter响应结果乱码问题
    Jmeter环境搭建详细介绍
    redis常用操作命令集合
    cocoapod 快速更新,加载
    获取验证码倒计时
    支持向量机SVM(Support Vector Machine)
    Ensemble Learning: Bootstrap aggregating (Bagging) & Boosting & Stacked generalization (Stacking)
    Eureka安全下线服务
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616348.html
Copyright © 2011-2022 走看看