C++链表,堆栈(某种)

发布于 2024-11-02 12:51:53 字数 1348 浏览 4 评论 0原文

#include <iostream>
using namespace std;

struct Node
{
    int item;   // storage for the node's item
    Node* next;   // pointer to the next node
};
/**************
use reference 
**************/
void addNode(Node*& head, int data , int& count) 
{
    Node * q;     // new node
    q = new Node;  // allocate memory for the new mode
    q->item = data;  // inserting data for the new node
    q->next = head;   // point to previous node ?? how would i do that? ( am i doing it correctly?)
    count++; // keep track of number of node
    head = q;
}

int main()
{
    int a, count = 0;
    int data;
    char callen;
    Node *head = NULL;

    do
    {
        cout << "please enter the data for the next node" << endl;
        cin >> data;
        addNode(head, data, count);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> callen;
    }while( callen != 'n' );

    // assuming this is the print function
    while(head != NULL)
    {
        cout << "output" << head->item << endl;
        head = head->next;                      //next element
    }

    system("pause");
    return 0;
}

我尝试在列表中添加一个新元素,如何像后进先出内存(堆栈)一样移动头部,以便最后一个元素位于最顶部..

任何帮助将不胜感激!指针和节点最近弄乱了我的大脑......

#include <iostream>
using namespace std;

struct Node
{
    int item;   // storage for the node's item
    Node* next;   // pointer to the next node
};
/**************
use reference 
**************/
void addNode(Node*& head, int data , int& count) 
{
    Node * q;     // new node
    q = new Node;  // allocate memory for the new mode
    q->item = data;  // inserting data for the new node
    q->next = head;   // point to previous node ?? how would i do that? ( am i doing it correctly?)
    count++; // keep track of number of node
    head = q;
}

int main()
{
    int a, count = 0;
    int data;
    char callen;
    Node *head = NULL;

    do
    {
        cout << "please enter the data for the next node" << endl;
        cin >> data;
        addNode(head, data, count);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> callen;
    }while( callen != 'n' );

    // assuming this is the print function
    while(head != NULL)
    {
        cout << "output" << head->item << endl;
        head = head->next;                      //next element
    }

    system("pause");
    return 0;
}

I tried adding a new element in the list how would i move the head around like a LIFO memory (stack) so the last element is on the very top..

Any help would be appreciated ! The pointers and the nodes are messing with my brain lately ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦太阳 2024-11-09 12:51:54

在 do-while 循环中尝试这个

addNode(data, count, head);

而不是

addNode( data, count );

另外,更改 addNode 的签名,如下所示:

void addNode( int data , int& count , Node*& head)

In the do-while loop try this

addNode(data, count, head);

instead of

addNode( data, count );

Also, change the signature of addNode as follows:

void addNode( int data , int& count , Node*& head)
一抹苦笑 2024-11-09 12:51:54

该代码不应编译,因为您在 addNode 中使用变量 head,但 headmain 的本地变量。

The code shouldn't compile because you are using the variable head in addNode but head is local to main.

久而酒知 2024-11-09 12:51:54

您可以使用 std::stack 进行后进先出。

int main()
{
    int a, count=0;
    int data;
    bool repeat;
    stl::stack<int> lifo;

    // assuming it is an empty list at the beginning  and crating a new node below
    cout << "enter some data" << endl;
    cin >> a ;
    lifo.push(a);
    do
    {
        cout << "please enter the data for the next node" <<endl;
        cin >> data;
        lifo.push(data);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> repeat;
    }
    while (repeat == true);


    // assuming this is the print function
    while(!lifo.empty()) {
        cout << lifo.pop() << endl;
    }

    system("pause");
    return 0;
}

You could use std::stack for LIFO.

int main()
{
    int a, count=0;
    int data;
    bool repeat;
    stl::stack<int> lifo;

    // assuming it is an empty list at the beginning  and crating a new node below
    cout << "enter some data" << endl;
    cin >> a ;
    lifo.push(a);
    do
    {
        cout << "please enter the data for the next node" <<endl;
        cin >> data;
        lifo.push(data);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> repeat;
    }
    while (repeat == true);


    // assuming this is the print function
    while(!lifo.empty()) {
        cout << lifo.pop() << endl;
    }

    system("pause");
    return 0;
}
遗心遗梦遗幸福 2024-11-09 12:51:54

听起来您正在尝试了解一些有关链接列表的知识。惊人的!

无论如何,我不会给你确切的答案,但我会用伪代码给你一些指示,特别是对于你的 addNode 成员函数:

Node* addNode(Node* head, int data, int& count)
{
    create a new node
    let it point to head
    return the pointer to the new node for it to become the new head node
}

int main()
{
    // code...
    head = addNode(head, data, count);
    // more code...
}

作为视觉:

head
 \/
node A->node B->node C

new node->?
new node
     \/
    node A->node B->node C

Sounds like you're trying to learn a bit about link lists. Awesome!

Anyway, I'm not going to give you the exact answer, but I'll give you some pointers in pseudo code, in particular for your addNode member function:

Node* addNode(Node* head, int data, int& count)
{
    create a new node
    let it point to head
    return the pointer to the new node for it to become the new head node
}

int main()
{
    // code...
    head = addNode(head, data, count);
    // more code...
}

As a visual:

head
 \/
node A->node B->node C

new node->?
new node
     \/
    node A->node B->node C
他夏了夏天 2024-11-09 12:51:54

您这样做的方式,通过将 addNode 函数实现为推送操作,已经移动了头部,因此头部将始终指向您添加的最后一个元素。

因此,要实现删除最后添加的元素的功能,只需要编写一个简单的pop操作:复制头部地址,将第二个元素作为新的头部,并释放复制地址处的内存:

Node* oldHead = head;
head = head->next;
delete oldHead;
return head;

The way you're doing it, by implementing your addNode function as a push operation, already moves the head around, so the head will always point to the last element you added.

Therefore, to implement a function to delete the last element added, you just need to write a simple pop operation: copy the address of the head, make the second element the new head, and release the memory at the copied address:

Node* oldHead = head;
head = head->next;
delete oldHead;
return head;
硪扪都還晓 2024-11-09 12:51:54

您可以尝试以下修改后的代码。

#include <iostream>
using namespace std;

struct Node
{
    int item;   // storage for the node's item
    Node* next;   // pointer to the next node
};
/**************
use reference 
**************/
void addNode(Node*& head, int data , int& count) 
{
    Node * q;     // new node
    q = new Node;  // allocate memory for the new mode
    q->item = data;  // inserting data for the new node
    q->next = head;   // point to previous node ?? how would i do that? ( am i doing it correctly?)
    count++; // keep track of number of node
    head = q;
}

int main()
{
    int a, count = 0;
    int data;
    bool repeat;
    Node *head = NULL;
    // assuming it is an empty list at the beginning  and crating a new node below
    Node *temp;
    temp = new Node ;
    cout << "enter some data" << endl;
    cin >> a ;
    temp->item = a;
    temp->next = head;
    head = temp;
    //^^ assuming thats creating the first node ^^
    do
    {
        cout << "please enter the data for the next node" << endl;
        cin >> data;
        addNode(head, data, count);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> repeat;
    }
    while (repeat == true);


    // assuming this is the print function
    temp = head;
    while(temp != NULL)
    {
        cout << "output" << temp->item << endl;
        temp = temp->next;                      //next element
    }


    return 0;
}

You could try the following modified code.

#include <iostream>
using namespace std;

struct Node
{
    int item;   // storage for the node's item
    Node* next;   // pointer to the next node
};
/**************
use reference 
**************/
void addNode(Node*& head, int data , int& count) 
{
    Node * q;     // new node
    q = new Node;  // allocate memory for the new mode
    q->item = data;  // inserting data for the new node
    q->next = head;   // point to previous node ?? how would i do that? ( am i doing it correctly?)
    count++; // keep track of number of node
    head = q;
}

int main()
{
    int a, count = 0;
    int data;
    bool repeat;
    Node *head = NULL;
    // assuming it is an empty list at the beginning  and crating a new node below
    Node *temp;
    temp = new Node ;
    cout << "enter some data" << endl;
    cin >> a ;
    temp->item = a;
    temp->next = head;
    head = temp;
    //^^ assuming thats creating the first node ^^
    do
    {
        cout << "please enter the data for the next node" << endl;
        cin >> data;
        addNode(head, data, count);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >> repeat;
    }
    while (repeat == true);


    // assuming this is the print function
    temp = head;
    while(temp != NULL)
    {
        cout << "output" << temp->item << endl;
        temp = temp->next;                      //next element
    }


    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文