C ++链表,堆栈(排序)

时间:2011-04-18 01:13:29

标签: c++ stack linked-list self

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

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

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

6 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以将std::stack用于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;
}

答案 2 :(得分:0)

在do-while循环中试试这个

addNode(data, count, head);

而不是

addNode( data, count );

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

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

答案 3 :(得分: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;
    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;
}

答案 4 :(得分: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

答案 5 :(得分:0)

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

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

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