【问题标题】:Staque, stack which can add int numbers to bottom and top alsoStaque,堆栈,也可以将 int 数字添加到底部和顶部
【发布时间】:2020-09-10 12:01:40
【问题描述】:

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

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

我试图使用只有数据和下一个(指向下一个节点对象的指针)的 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;
            }
        }
    }

【问题讨论】:

  • 这看起来像是出于教育目的,所以这不会立即提供帮助,但std::deque
  • 建议:1:别再想ins stack了。只要您可以在两端进行操作,您就没有堆栈。你需要不同的想法。一方面,您需要两种推送方法,一种用于顶部,一种用于底部。或者因为我们试图不以堆栈的方式思考,一个用于前端,一个用于后端。您还需要两个 pops.2:编写此类并在担心应用程序逻辑之前对其进行测试,一方面是偶数,另一方面是赔率。如果你构建你的 staque,让它只做你现在需要它做的事情,那么以后重用它会更加困难。
  • 3:进一步分解。编写一个并测试一个通用链表,它可以完成所有常见的链表工作并编写 staque 以便它使用链表。现在您有了一个链表、一个 staque 和一个在一端添加偶数并在另一端添加赔率的工具。
  • 一般建议:从计划开始。问问自己,“我需要做什么?”然后写下你的答案。将答案分解为步骤。将这些步骤分解为步骤。继续前进,直到您拥有一组可以轻松编写和组装到程序中的小构建块。如果你不知道怎么做,就把它变成一堆小问题,然后攻击小问题。如果你发现你必须在许多步骤中做同样的事情,那就概括它,让每个人都使用同样的东西。有人已经写过的小步骤,赔率真的很好。
  • 你有一个double-ended queue

标签: c++ stack queue


【解决方案1】:

我以某种方式做到了,该代码很糟糕,但是我没有时间使它变得更好,如果有人对此感兴趣,那就是: 标题:

#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;
        }
    }
}

【讨论】:

    【解决方案2】:

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

    #包括 #包括

    使用命名空间标准;

    struct node { //vqmnit 节点结构 诠释信息; 结构节点*下一个; // 下一个指针 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 轴节点

    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::deleteFromFront() {

    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::deleteFromLast() { //uknidanwashla

    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::displayList() { //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;
    

    }

    【讨论】:

    • SO 是一个只有英文的网站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多