Я работаю над визуальной студией.Мой проект: написать драйвер для стека и очереди.Тестовый драйвер - это программа, созданная для тестирования функций, которые должны быть помещены в библиотеку.Его основная цель - полностью протестировать функции, поэтому он не имеет применения.В программе вы будете использовать два стека и две очереди, как описано ниже.
a.inS - стек ввода: используется для хранения всего пользовательского ввода
b.inQ - очередь ввода: используется для хранения всего пользовательского ввода
c.outS - стек вывода: используется для хранения данных, удаленных из inQ
d.outQ - очередь вывода: используется для хранения данных, удаленных из inS
И мне нужно сделать в основном следующее: 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. stackADT.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
QueueADT.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";
}