将节点添加到链接列表的末尾

时间:2014-02-24 00:27:46

标签: c++ insert linked-list traversal

我一直在尝试将一个项目添加到链接列表的末尾。我想我已经掌握了这个概念,但是我在实现代码时遇到了困难。特别是,能够遍历链表并找到尾部。这是我到目前为止所拥有的。我一直在尝试不同的事情。任何帮助将不胜感激。

##include <iostream>
using namespace std;


class node
{
public:
    int data;
    node *next;
};


class linkedList
{
private:
    node* ptrHead;
    node* ptrTail;

    int size;

public:
    linkedList();  //default constructor
    void display();
    void addFront(int);
    void removeFront();
    void addBack(int);
    void removeBack();
};

//default constructor
linkedList::linkedList(){
    size = 0;
    ptrHead = ptrTail = NULL;
}
//display linked list
void linkedList::display(){
    node* current = ptrHead;

    while (current != NULL) {
        cout << current->data << " "; //display current item
        current = current->next; //move to next item
    }
    cout << size;
}
//add item to front of linked list
void linkedList::addFront(int addData){

    node* n = new node;
    n->next = ptrHead;
    n->data = addData;
    ptrHead = n;

    size++;
}
//remove item from front of linked list
void linkedList::removeFront(){

    node* n = ptrHead;
    ptrHead = ptrHead->next;
    delete n;

    size--;
}

       void linkedList::addBack(int addData){   ////work in progress


        node* n = new node; //create new node
        n->data = addData;  //input data
        n->next = NULL;     //set node to point to NULL

        if ( ptrTail == NULL )  // or if ( ptrTail == nullptr )
        {
            ptrHead = n;
            ptrTail = n;
        }
        else
        {
            ptrTail->next = n;
            ptrTail = n;
        }

        size++;
    }



    //this is the test code from my main function
              int main()
        {
            //test code
            linkedList list;

            list.addFront(40);
            list.addFront(30);
            list.addFront(20);
            list.addFront(10);
            list.addFront(0);
            list.addBack(50);
            list.addBack(60);

            list.display(); //50 60 7  (the 7 is the count/size of the linked list)
            cout << endl;
        }

3 个答案:

答案 0 :(得分:2)

for(int i=1; i<size; i++)
   pCurrent = pCurrent->next;

pCurrent = n;

这会奏效。但是你必须将size变量保持为Linked List的实际大小。

或者如果您想在最后添加元素,可以按照以下步骤操作。 保留一个额外的节点尾部并将元素添加到该节点。

if(head == NULL)
{
   head = n;
   tail = n;
}
else
{
   tail->next = n;
   tail = tail->next;
}

答案 1 :(得分:1)

试试这个

node* pCurrent = ptrHead;
if( pCurrent != NULL ){
    //find tail
    while (pCurrent->next != NULL)
        pCurrent = pCurrent->next;

    // add new node at end of tail
    pCurrent->next = n;
    } else {
        pCurrent = n;
    }
}

答案 2 :(得分:1)

您没有显示您的linkedList的定义。

所以我只能假设它有数据成员ptrTail和ptrHead。在这种情况下,该函数将按以下方式显示

void linkedList::addBack(int addData)
{
   node* n = new node; //create new node
   n->data = addData;  //input data
   n->next = NULL;     //set node to point to NULL

   if ( ptrTail == NULL )  // or if ( ptrTail == nullptr )
   {
      ptrHead = n;
   }
   else
   {
      ptrTail->next = n;
   }
   ptrTail = n;

   size++;
}

函数addFront可以用类似的方式定义

void linkedList::addFront(int addData)
{
   node* n = new node; //create new node
   n->data = addData;  //input data

   if ( ptrHead == NULL )  // or if ( ptrHead == nullptr )
   {
      ptrTail = n;
   }

   n->next = ptrHead;
   ptrHead = n;

   size++;
}

又一个功能

void linkedList::removeFront()
{
    if ( ptrHead != NULL )
    {
        if ( ptrTail == ptrHead ) ptrTail = NULL;
        node* n = ptrHead;
        ptrHead = ptrHead->next;
        delete n;
        size--;
    }
}