c++ - 我得到“_free_dbg(块,_UNKNOWN_BLOCK);” 我正在尝试处理堆栈和队列
问题描述
我在视觉工作室工作。我的项目是:编写一个堆栈和队列测试驱动程序。测试驱动程序是为测试要放置在库中的函数而创建的程序。它的主要目的是完全测试功能,因此它没有应用程序使用。您将在程序中使用两个堆栈和两个队列,如下所述。
一个。inS – 输入堆栈:用于存储所有用户输入
湾。inQ - 输入队列:用于存储所有用户输入
C。outS – 输出栈:用于存储从 inQ 中删除的数据
d。outQ – 输出队列:用于存储从 inS 中删除的数据
而我需要在main中做如下操作:I——插入,D——删除,C——显示两个栈和两个队列的元素个数,T——显示两个栈顶的元素,F——显示两个队列前面的元素,R——显示两个队列末尾的元素
像这样,我正在尝试处理队列和堆栈。但如下所示。
//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>
////////////////////////////////////////////////////////////////
// delete() Fallback Ordering
//
// +-------------+
// |delete_scalar<----+-----------------------+
// +--^----------+ | |
// | | |
// +--+---------+ +--+---------------+ +----+----------------+
// |delete_array| |delete_scalar_size| |delete_scalar_nothrow|
// +--^----^----+ +------------------+ +---------------------+
// | |
// | +-------------------+
// | |
// +--+--------------+ +------+-------------+
// |delete_array_size| |delete_array_nothrow|
// +-----------------+ +--------------------+
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK);
#else
free(block);
#endif
}
以下是整个代码(两个 .h 文件和一个 .cpp 文件)。1.堆栈ADT.h
/**~*~*
Stack template
*~**/
#ifndef STACK_ADT_H
#define STACK_ADT_H
#include <iostream>
using namespace std;
template <class T>
class Stack
{
private:
// Structure for the stach nodes
struct StackNode
{
T value; // Value in the node
StackNode *next; // Pointer to next node
};
StackNode *top; // Pointer to the stack top
int count;
public:
//Constructor
Stack() { top = NULL; count = 0; }
// Destructor
~Stack();
// Stack operations
bool push(T);
bool pop(T &);
bool isEmpty();
int getCount();
bool getTop(T &);
};
/**~*~*
Destructor
*~**/
template <class T>
Stack<T>::~Stack()
{
StackNode *currNode, *nextNode;
// Position nodePtr at the top of the stack.
currNode = top;
// Traverse the list deleting each node.
while (currNode) //while (currNode != NULL)
{
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}
/**~*~*
Member function push pushes the argument onto
the stack.
*~**/
template <class T>
bool Stack<T>::push(T item)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = top;
top = newNode;
count++;
return true;
}
/**~*~*
Member function pop pops the value at the top
of the stack off, and copies it into the variable
passed as an argument.
*~**/
template <class T>
bool Stack<T>::pop(T &item)
{
StackNode *temp; // Temporary pointer
// empty stack
if (count == 0)
return false;
// pop value off top of stack
item = top->value;
temp = top->next;
delete top;
top = temp;
count--;
return true;
}
/**~*~*
Member function isEmpty returns true if the stack
is empty, or false otherwise.
*~**/
template <class T>
bool Stack<T>::isEmpty()
{
return count == 0;
}
/**~*~*
Member function getCount returns
the number of elements in the stack
*~**/
template <class T>
int Stack<T>::getCount()
{
return count;
}
/**~*~*
Member function getTop copies the value at the top
of the stack into the variable passed as an argument.
*~**/
template <class T>
bool Stack<T>::getTop(T &item)
{
if (top == NULL)
return false;
item = top->value;
return true;
}
#endif
队列ADT.h
#ifndef DYNAMICQUEUE_H #define DYNAMICQUEUE_H #include <iostream> using namespace std; template <class T> class Queue { private: // Structure for the queue nodes struct QueueNode { T value; // Value in the node QueueNode *next; // Pointer to next node }; QueueNode *front; // Pointer to the queue front QueueNode *rear; // Pointer to the queue rear int count; public: //Constructor Queue() { front = rear = NULL; count = 0; } // Destructor ~Queue(); // Queue operations bool enqueue(T); bool dequeue(T &); bool isEmpty(); int getCount(); bool queueFront(T &); bool queueRear(T &); }; /**~*~* Destructor *~**/ template <class T> Queue<T>::~Queue() { QueueNode *currNode, *nextNode; // Position nodePtr at the top of the stack. currNode = front; // Traverse the list deleting each node. while (currNode) //while (currNode != NULL) { nextNode = currNode->next; delete currNode; currNode = nextNode; } } /**~*~* Member function getCount returns the number of elements in the queue *~**/ template <class T> int Queue<T>::getCount() { return count; } /**~*~* Member function isEmpty returns true if the stack is empty, or false otherwise. *~**/ template <class T> bool Queue<T>::isEmpty() { return count == 0; } /**~*~* Member function enqueue inserts the argument into the queue. *~**/ template <class T> bool Queue<T>::enqueue(T item) { QueueNode *newNode; // Pointer to a new node // Allocate a new node and store num there. newNode = new QueueNode; if (!newNode) return false; newNode->value = item; // Update links and counter newNode->next = NULL; if (front == NULL) // insert to an empty queue front = newNode; else rear->next = newNode; count++; rear = newNode; return true; } /**~*~* Member function dequeue deletes the value at the front of the queue, and copies it into the variable passed as an argument. *~**/ template <class T> bool Queue<T>::dequeue(T &item) { QueueNode *pDel; // Temporary pointer // empty queue if (count == 0) return false; // delete the value at the front of the queue item = front->value; pDel = front; if (count == 1) rear = NULL; front = front->next; count--; delete pDel; return true; } /**~*~* Member function queueFront copies the value at the front of the queue into the variable passed as an argument. *~**/ template <class T> bool Queue<T>::queueFront(T &item) { if (front == NULL) return false; item = front->value; return true; } /**~*~* Member function queueRear copies the value at the rear of the queue into the variable passed as an argument. *~**/ template <class T> bool Queue<T>::queueRear(T &item) { if (rear == NULL) return false; item = rear->value; return false; } #endif
3.main.cpp
/**
CIS 22C: Homework 3
Build and process a sorted circularly doubly-linked list of Toy objects.
The list is sorted in ascending order by toy ID.
The toy ID is a unique key.
IDE: Visual Studio
Written By: Younseo Ryu
Changed By:
**/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype> // toupper function
#include "StackADT.h"
#include "QueueADT.h"
using namespace std;
void printWelcome();
void insertSQ(Stack<double> &, Queue<double> &);
void deleteSQ(Stack<double> &, Stack<double> &, Queue<double> &, Queue<double> &);
void displayCountSQ(Stack<double>, Stack<double>, Queue<double>, Queue<double>);
void displayTopS(Stack<double>, Stack<double>);
void displayEndQ(Queue<double>, Queue<double>);
int main()
{
Stack<double> inS; Stack<double> outS;
Queue<double> inQ; Queue<double> outQ;
printWelcome();
insertSQ(inS, inQ);
insertSQ(inS, inQ);
insertSQ(inS, inQ);
deleteSQ(inS, outS, inQ, outQ);
displayCountSQ(inS, outS, inQ, outQ);
displayTopS(inS, outS);
displayEndQ(inQ, outQ);
system("pause");
return 0;
}
/**********************
This function prints a welcome message and a description of the program
*********************/
void printWelcome()
{
cout << "\n\n\t\t *~~*~~* WELCOME *~~*~~*\n\n"
<< "\tTo the Stack and Queue Program!\n\n"
<< "\t\tThis program will: \n"
<< "\t\t -I: Insert \n"
<< "\t\t -D: Delete \n"
<< "\t\t -C: Display the number of elements in the two stacks and two queues \n"
<< "\t\t -T: Display the elements at the top of the two stakcs \n"
<< "\t\t -F: disdisplay the elements at the front of the two queues \n"
<< "\t\t -R: display the elements at the end of the two queues \n"
<< "\t\t -Q: to quit the program. \n\n\n";
cout << "\t\t \n\n";
}
void insertSQ(Stack<double> &stack, Queue<double> &queue)
{
double item;
cout << "enter a number to insert it in inS and inQ: ";
cin >> item;
stack.push(item);
queue.enqueue(item);
}
void deleteSQ(Stack<double> &inS, Stack<double> &outS, Queue<double> &inQ, Queue<double> &outQ)
{
double item;
cout << "the top stack element is popped.\n";
inS.pop(item);
outS.push(item);
inQ.dequeue(item);
outQ.enqueue(item);
}
void displayCountSQ(Stack<double> inS, Stack<double> outS, Queue<double> inQ, Queue<double> outQ)
{
cout << "there are " << inS.getCount() << " elements in the stack inS\n"
<< "there are " << outS.getCount() << " elements in the stack outS\n"
<< "there are " << inQ.getCount() << " elements in the queue inQ\n"
<< "there are " << outQ.getCount() << " elements in the queue outQ\n";
}
void displayTopS(Stack<double> inS, Stack<double> outS)
{
double item;
if (inS.getTop(item))
cout << "the top element in the stack inS is the following: " << item << "\n";
else
cout << "the stack inS is empty. so there's no top element in inS.\n";
if(outS.getTop(item))
cout << "the top element in the stack outS is the following: " << item << "\n";
else
cout << "the stack outS is empty. so there's no top element in inS.\n";
}
void displayEndQ(Queue<double> inQ, Queue<double> outQ)
{
double item;
if (inQ.queueFront(item))
cout << "the front element of the queue inQ is: " << item <<"\n";
else cout << "no front element found in inQ\n";
if (inQ.queueRear(item))
cout << "the rear element of the queue inQ is: " << item << "\n";
else cout << "no rear element found in inQ\n";
if (outQ.queueFront(item))
cout << "the front element of the queue inQ is: " << item << "\n";
else cout << "no front element found in inQ\n";
if (outQ.queueRear(item))
cout << "the rear element of the queue inQ is: " << item << "\n";
else cout << "no rear element found in inQ\n";
}
解决方案
推荐阅读
- spring - JPA,Hibernate:在现有的工作 DTO 中添加新变量时“无法在类上找到适当的构造函数”
- sql - 使用 python3 执行 .sql 脚本
- python - 如何将 N 个二维数组堆叠在一起作为图像
- c# - AWS S3:通过 Content-Disposition 标头而不是对象键前缀使用一组覆盖文件下载名称
- c# - 在 C# 库中使用 Serilog 和 appsettings.json - .NET Core Standard
- apache-flink - Flink AvroDeserializationSchema for specificRecord -- 使用泛型类类型
- java - 与外键约束作斗争
- visual-studio - 基础知识:在 groovy 中导入类
- javascript - Webpack 抱怨 typescript 定义文件导入 - 除非我添加 `d.ts` 结尾
- go - go-grpc 返回 TLS 错误“无法识别的名称”