c++链表,栈(有点)


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

我试着在列表中添加一个新元素,我如何像后进先出内存(堆栈)一样移动头部,所以最后一个元素是在最上面…

任何帮助将不胜感激!指针和节点最近搞得我脑子都乱了....

在do-while循环中试试

addNode(data, count, head);

代替

addNode( data, count );

同时,修改addNode的签名如下:

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

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

您可以使用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;
}

听起来你想学习一些关于链接列表的知识。太棒了!

无论如何,我不会给你确切的答案,但我会给你一些伪代码中的指针,特别是你的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

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

因此,要实现删除最后添加的元素的函数,只需要编写一个简单的pop操作:复制头的地址,使第二个元素成为新的头,并释放复制地址处的内存:
Node* oldHead = head;
head = head->next;
delete oldHead;
return head;

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

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

相关内容

  • 没有找到相关文章

最新更新