斯塔克,堆栈,也可以在底部和顶部添加整数

Staque, stack which can add int numbers to bottom and top also

本文关键字:顶部 添加 整数 堆栈 也可以 斯塔克 底部      更新时间:2023-10-16

>我正在尝试制作类似队列或堆栈的结构,在其中我可以在底部和顶部添加和删除整数。

如果输入 int 是偶数(%2 = 0(,则将其添加到顶部,如果奇数(%2 = 1(,则添加到底部。

我试图使用只获取数据和 next(指向下一个 Node 对象的指针(的 Node 类来制作它,但由于这个原因,我无法在顶部添加或删除 int,只能添加到底部。

这是一个头文件:


#include <iostream>
using namespace std;
#ifndef myStaque
#define myStaque
class Staque
{
public:
Staque();
Staque(const Staque & a);
bool ifEmpty() const;
void push(const int& b);
void print() const;
int top() const;
int bottom() const;
void delKenti();
void delLuwi();
Staque& operator= (const Staque& a);
private:
class Node
{
public:
int data;
Node* next;
Node* previous;
Node(int a, Node* c = 0, Node* b = 0): data(a), next(c), previous(b){}
};
Node* myTop;
Node* myBottom;
};
#endif

还有我试图让它工作的 cpp(但它不起作用(:

#include "myStaque.h"
#include <new>
using namespace std;
Staque::Staque()
{
}
Staque::Staque(const Staque& a) {
*this = a;
}
Staque& Staque:: operator= (const Staque& a) {
Staque::Node* ptr;
for (ptr = a.myBottom; ptr != 0; ptr = ptr->next)
{
myTop = a.myTop;
myBottom = a.myBottom;
}
return *this;
}
bool Staque::ifEmpty() const
{
return (myBottom == 0);
}
void Staque::push(const int& b)
{
if (ifEmpty()) {
myBottom = new Staque::Node(b, 0);
myTop = myBottom;
}
if (b % 2) {
Staque::Node* tmp;
tmp = new Staque::Node(b, 0);
myTop->next = tmp;
myTop = tmp;
}
else {
myBottom = new Staque::Node(b, myBottom);
}
}
void Staque::print() const
{
Staque::Node* ptr;
for (ptr = myBottom; ptr != 0; ptr = ptr->next)
cout << ptr->data << ", ";
cout << endl;
}
void Staque::delLuwi() {
if (myBottom->data % 2 && myTop->data % 2) {
cout << "Bottom and top are kenti " << endl;
}
else {
if (!(myBottom->data % 2)) {
myBottom = myBottom->next;
}
else if (!(myTop->data % 2)) {
Staque::Node* tmp;
tmp = myBottom;
while ( !(tmp->next = 0) ) {
tmp = tmp->next;
}
myTop = tmp;
myTop->next = 0;
}
}
}
void Staque::delKenti() {
if (!(myBottom->data % 2) && !(myTop->data % 2)) {
cout << "Bottom and top are kenti " << endl;
}
else {
if (myBottom->data % 2) {
myBottom = myBottom->next;
}
else if (myTop->data % 2) {
Staque::Node* tmp;
tmp = myBottom;
while (!(tmp = nullptr)) {
tmp = tmp->next;
}
myTop = tmp;
myTop->next = 0;
}
}
}

我不知何故做到了,那段代码很糟糕,但我没有时间让它变得更好,如果有人感兴趣的话: 页眉:

#include <iostream>
using namespace std;
#ifndef myStaque
#define myStaque
class Staque
{
public:
Staque();
Staque(const Staque & a);
bool ifEmpty() const;
void push(const int& b);
void print() const;
int top() const;
int bottom() const;
void delKenti();
void delLuwi();
Staque& operator= (const Staque& a);
private:
class Node
{
public:
int data;
Node* next;
Node(int a, Node* c = 0): data(a), next(c){}
};
Node* myTop;
Node* myBottom;
};
#endif

和 CPP:

#include "myStaque.h"
#include <new>
using namespace std;
Staque::Staque()
{
}
Staque::Staque(const Staque& a) {
*this = a;
}
Staque& Staque:: operator= (const Staque& a) {
Staque::Node* ptr;
for (ptr = a.myBottom; ptr != 0; ptr = ptr->next)
{
myTop = a.myTop;
myBottom = a.myBottom;
}
return *this;
}
bool Staque::ifEmpty() const
{
return (myBottom == 0);
}
void Staque::push(const int& b)
{
if (ifEmpty()) {
myBottom = new Staque::Node(b, 0);
return;
}
if (myBottom->next == 0) {
if (b % 2) {
myTop = new Staque::Node(b, 0);
myBottom->next = myTop;
}
else {
myTop = myBottom;
myBottom = new Staque::Node(b, myTop);
}
}
else {
if (b % 2) {
Staque::Node* tmp;
tmp = new Staque::Node(b, 0);
myTop->next = tmp;
myTop = tmp;
}
else {
myBottom = new Staque::Node(b, myBottom);
}
}
}
void Staque::print() const
{
Staque::Node* ptr;
for (ptr = myBottom; ptr != 0; ptr = ptr->next)
cout << ptr->data << ", ";
cout << endl;
}
void Staque::delLuwi() {
if (ifEmpty()) {
cout << "Staque is empty" << endl;
return;
}
if (myBottom->data % 2 && myTop->data % 2) {
cout << "Cant delete Luwi number Bottom and top are kenti " << endl;
}
else {
if (!(myBottom->data % 2)) {
myBottom = myBottom->next;
}
else if (!(myTop->data % 2)) {
Staque::Node* tmp;
tmp = myBottom;
while ( tmp->next != myTop ) {
tmp = tmp->next;
}
myTop = tmp;
myTop->next = 0;
}
}
}
void Staque::delKenti() {
if (ifEmpty()) {
cout << "Staque is empty" << endl;
return;
}
if (!(myBottom->data % 2) && !(myTop->data % 2)) {
cout << "Cant delete Kenti number Bottom and top are Luwi " << endl;
}
else {
if (myBottom->data % 2) {
myBottom = myBottom->next;
}
else if (myTop->data % 2) {
Staque::Node* tmp;
tmp = myBottom;
while (tmp->next !=myTop) {
tmp = tmp->next;
}
myTop = tmp;
myTop->next = 0;
}
}
}

Tsu shi swavlob shen albat... Mgoni mushaobs es cade aba

#include #include

使用命名空间标准;

struct node {//vqmnit node struqturas int info; 结构节点* 下一个; Next pointeri miutitebs shemdeg elementze };

类堆栈队列 {

私人: 结构节点*头; 整数大小;

公共:

struct node* createNewNode(int);   //vqmnit xal nodes
void insertAtFront(int);   //winidan damateba
void insertAtLast(int);      // uknidan damateba
void deleteFromFront();        // winidan washla
void deleteFromLast();           //uknidan washla
void displayList();          //stack queues bechdva
StackQueue() {
head = NULL;
size = 0;
}

};

struct node* StackQueue::createNewNode(int value( {//vqmnit axal nodes

struct node* temp;
temp = new(nothrow) (struct node);
temp->info = value;
temp->next = NULL;
return temp;

}

void StackQueue::insertAtFront(int value( {//winidan vamatebt

struct node* temp, * p;
temp = createNewNode(value);
if (head == NULL) { // es aris mashin roca stack queue carielia pirveli elementi emanteba
head = temp;
head->next = NULL;
}
else {             //aq chveulebisamebr shegvaqvs
p = head;
head = temp;
head->next = p;
}
cout << "nElement inserted at front successfully.";
size++;

}

void StackQueue::insertAtLast(int value( {//uknidan damateba

struct node* temp, * s;
temp = createNewNode(value);
if (head == NULL) {          //pirveli elementis damateba
head = temp;
head->next = NULL;
}
else {               // danarcheni elementebis damateba
s = head;
while (s->next != NULL) {
s = s->next;
}
temp->next = NULL;
s->next = temp;
}
cout << "nElement inserted at end successfully.";
size++;

}

void StackQueue::d eleteFromFront(( {

if (size == 0) {
return;
}

struct node* s;
s = head;
if (head == NULL) {           //tu ukve carielia vegar amovshlit da vprintavt am mesijs
cout << "nThe staque is Empty";
return;
}
if (s->info % 2 == 0) {              // tu luwia ishleba
head = head->next;
free(s);
size--;
cout << "nEven element deleted.";
if (size == 0) {
head = NULL;
}
}

}

void StackQueue::d eleteFromLast(( {//uknidan washla

if (size == 0) {
return;
}
struct node* s, * temp;
s = head;
if (head == NULL) {              //carielis shemtxvevashi shemdegi mesiji
cout << "nThe staque is Empty";
return;
}
do {
temp = s;
s = s->next;
} while (s != nullptr);
if (s->info % 2 == 1)     {      // tu ar aris luwi anu kentia vshlit
temp->next = NULL;
free(s);
size--;
cout << "nOdd element deleted";
if (size == 0)
head = NULL;
}

}

void StackQueue::d isplayList(( {//vbechdavt sias am stekis

struct node* temp;
if (head == NULL) {
cout << "nThe staque is Empty";         //tu carielia amasac vbechdavt
return;
}
temp = head;
cout << "nElements of staque are: ";
while (temp != NULL) {
cout << temp->info << " ";
temp = temp->next;
}
cout << endl;

}