使用C ++中的链接列表堆栈 [英] Stack Using Linked List in C++

查看:202
本文介绍了使用C ++中的链接列表堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c ++中的链表创建一个堆栈。但是显示功能我只写了堆叠的顶部。我不明白为什么会发生这种情况。任何帮助或澄清非常感激。谢谢

I'm trying to create a stack using linked lists in c++. But the display function i have written prints only the top of the stack. I can't really understand why this is happening. Any help or clarification is much appreciated. Thanks

#include<iostream.h>
#include<conio.h>

class Node
{
protected:

    Node* next;
    int data;

public:

    Node(int d){data=d;}
    friend class Stack;
};

class Stack
{
public:
    Stack(){top->next='\0';length=0;}

void push(int d)
{
    Node *n=new Node(top->data);
    n->next='\0';
    top->next=n;
    top->data=d;
    length++;
}

int pop()
{
    top=top->next;
    length--;
    return top->data;
}

void displaystack()
{
    while(top->next!='\0')
    {
        cout<<top->data<<endl;
    top=top->next;
    }
}

int getlength()
{
    return length;
}

private:
    Node *top;
    int length;

};

void main()
{
    clrscr();
    Stack s;
    s.push(9);
    s.push(8);
    s.push(7);
    s.push(6);
    s.push(5);
    s.push(3);
    s.displaystack();
    int len=s.getlength();
    cout<<"length of stack is "<<len<<endl;
    getch();
}

它只打印以下内容:
3
堆栈长度为6

It only prints the following: 3 the length of stack is 6

-------- xxxxxxx ------- xxxxxxxx -------- xxxxxxx ---- ------- xxxxxxxxxxxxx --------------

--------xxxxxxx-------xxxxxxxx--------xxxxxxx-----------xxxxxxxxxxxxx--------------

编辑代码如下所示:
和工作也! (感谢@Kaathe):P

After editing the code looks like this: and works too! (thanks to @Kaathe) :P

#include<iostream.h>
#include<conio.h>

class Node
{
protected:
Node* next;
int data;

public:

Node(int d){data=d;}
friend class Stack;
};

class Stack
{
public:
Stack(){top->next=NULL;length=0;}
~Stack()
{
     while(top!=NULL)
 {
   Node* toDelete=top;
   top=top->next;
   delete toDelete;
 }

}

void push(int d)
{
Node *n=new Node(d);
n->next=top;
top=n;
length++;
}

int pop()
{
 Node* oldtop=top;
 top=top->next;
 int oldtopdata=oldtop->data;
 delete(oldtop);
 --length;
 return oldtopdata;
}

void displaystack()
{
Node* current=top;
while(current->next!=NULL)
    {
    cout<<current->data<<endl;
    current=current->next;
    }
}

int getlength()
{
return length;
}

private:
Node *top;
int length;

};


推荐答案

当您 push ,您要创建一个全新的 Node ,将其数据设置为值 d ,以及将其指向堆栈的旧顶部。然后,您可以将堆栈的顶部设置为此新节点。您实际上根本不需要修改旧的顶级节点。

When you push, you want to create an entirely new Node, set its data to the value d, and point it at the old top of the stack. You can then set the top of the stack to this new node. You actually don't need to modify the old top node at all.

所以push可以看到:

So push could read:

void push(int d)
{
    Node* newTop = new Node(d);
    newTop->next = top;
    top = newTop;
    ++length;
}

我刚刚注意到 pop 也不会按预期行为,因为您扔掉顶级节点,并返回下面的节点中的数据。也许你可以写:

I just noticed that pop also won't behave as expected, as you throw away the top node, and return the data in the node below it. Maybe you could write:

int pop() 
{
    Node* oldTop = top;
    top = top->next;
    int oldTopData = oldTop->data;
    delete(oldTop);
    --length;
    return oldTopData;
}

这可能是阻止人们弹出空堆栈的好主意(例如通过在 pop()开始时检查 length> = 1

It would probably also be a good idea to stop people from popping empty stacks (for example by checking that length >= 1 at the start of pop().

最后, displaystack 将会销毁 Stack 可能这样会更好:

Finally, displaystack will kind of destroy the Stack object if called, by losing the pointer to the top node. Maybe this would be better:

void displayStack()
{
    Node* currNode = top;
    while(currNode->next != nullptr)
    {
        std::cout << currNode->data << std::endl;
        currNode = currNode->next;
    }
}

也可以使用 nullptr 结束链表更有意义。

It makes more sense to me to end the linked list with a nullptr too.

另外,堆栈应该有一个析构函数,其中删除所有的节点 s - 我会让你写那个;)

Also, the stack should have a destructor which deletes all its Nodes - I'll let you write that one ;)

这篇关于使用C ++中的链接列表堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆