链表c++,问题自学

linked list C++ , question selflearning

本文关键字:问题 c++ 链表      更新时间:2023-10-16
        #include <iostream>
    using namespace std;

    struct Node
    {
        int item;   // storage for the node's item
        Node* next;   // pointer to the next node 
    };
  Node* 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;
    return q;
}

    int main()
    {
        int a, count=0;
        int data;
        bool repeat;
        Node *head= NULL;   
        //^^ 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  
          while(head != NULL)
        {
            cout << "output" << temp->item << endl;
            cout << temp->next << endl;
        }
        system("pause");
        return 0; 
    }

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

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

temp为未初始化指针。所以- - - - - -

temp-> item = a;  // temp is not initialized or pointing to a memory location
                  // that has Node object to use operator ->

首先,temp需要使用new分配内存位置。

temp = new Node;
temp -> item = a;

现在赋值给head。同样地,在while循环中也为子节点分配内存。并在程序终止前使用delete返回从child到head获得的所有资源。

你似乎有一些误解:

你的"头"是列表的开始。它总是开始。

通过将元素赋值给最后一个节点的next指针来添加追加元素到链表。

第三,你没有分配任何东西。

Node *head= new Node();   
Node *temp = new Node();
cout<<"enter something into data"<<endl;
cin >> a ;
temp->item = a;
head->next = temp;

现在…要添加下一个内容,您要么需要跟踪最后一个节点(tail),要么遍历列表以找到最后一个节点。

Node *nextNode = new Node();
nextNode->item = 0.0;
Node *i;
for (i = head; i->next != null; i = i->next);
i->next = nextNode;

这是O(n)执行时间。通过跟踪尾巴,你使它成为O(1):

Node *head= new Node();
Node *tail = head;   
Node *temp = new Node();
cout<<"enter something into data"<<endl;
cin >> a ;
temp->item = a;
tail->next = temp;
tail = temp;
Node *nextNode = new Node();
nextNode->item = 0.0;
tail->next = nextNode;
tail = nextNode;

EDIT:如前所述,如果您想在列表中添加前缀,您将:

temp->next = head;
head = temp;

因为我不确定每个答案都完全回答了这个问题,所以这里有一个链表实现(没有测试):

// your (correct) structure
struct Node
{
    float item;   // storage for the node's item
    Node* next;   // pointer to the next node 
};

现在我们需要两个指针来跟踪列表:

/* some pointers */
struct List
{
    Node* head;
    Node* tail;
};

现在我们需要创建一些元素。正如其他人所说,您可以使用new:

来完成此操作。
/* create some elements we want to link in */
Node* elem1 = new Node();
Node* elem2 = new Node();
Node* elem3 = new Node();
/* maybe even set their properties! */
elem1->item = 3.14;
elem2->item = 3.14;
elem3->item = 3.14;

注意到我还没有尝试将这些元素添加到列表中吗?这是因为我脑子里有这样一个函数:

void addtolist(List &list, Node* node)
{
    /* if no head, initialise the list */
    if ( list->head == NULL )
    {
        list->head = node;
        list->tail = node;
    }
    else if ( list->head != NULL && list->tail != NULL )
    {
        /* access the tail element and set its 
           next to this ptr. 
           Move tail to this node */
        list->tail->next = node;
        list->tail = node;
    }
    else
    {
        /* probably raise an exception! */
    }
}

你可以这样调用它:

List l;
addtolist(l, elem1); /* etc */

删除元素有点棘手,因为你必须去那个元素,记住它的前一个元素,抓住它的下一个元素,把它们连接起来,然后删除你所在的Node*。

现在遍历列表…你的术语HEAD|TAIL让我想起了Erlang和尾部递归,其中当前元素被称为头部,其余元素被称为尾部。如果我写:

Node* cur = l.head;
while ( cur != NULL )
{
    // do something with cur.item ? 
    cur = cur->next;
}

你可以看到这正在发生。这里用head替换cur是无害的,这要归功于List结构体。

最后,我在这里使用了一种非常类似于c的方法,但也有模板的作用域:

template<typename T>
struct node
{
    T item;   // storage for the node's item
    Node<T>* next;   // pointer to the next node 
};

并将List结构体封装为一个类:

template<typename T>
class List
{
protected:
    Node<T>* head;
    Node<T>* tail;
public:
    void addtolist(Node<T>* node);
    Node<T>* gethead();
    Node<T>* gettail();
}

让你更接近std::list

另外请注意,您正在对

执行从intfloat的隐式强制转换。
temp-> item = a;

作为aint,而temp->itemdouble

解决你的问题:你想在访问temp之前分配一个新的结构,因此

temp = new Node();