从链接列表的末尾删除

时间:2016-07-30 20:00:33

标签: c++ linked-list nodes

我正在编写一个生成6个随机数的程序,并将它们放在链表中并输出。然后在它获得所有6个数字后,它删除第一个节点并输出剩余的5个,然后它将删除最后一个节点并输出剩余的4个来回,直到没有剩下的节点。无论如何,我能够在其中创建链接列表和存储节点并输出它们,我能够每次删除第一个节点,但我无法弄清楚如何删除最后一个节点。任何有关如何删除最后一个节点的帮助将不胜感激。我使用pop_back函数删除最后一个节点...

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "SortedLinkedList.h"

using namespace std;

struct node
{
    int data;
    node *next;
};

node *head = NULL;

void push_sorted(int value)
{
    node *newNode = new node;
    newNode->data = value;
    newNode->next = NULL; 
    if(head == NULL)
    {
        head = newNode;
    }
    else
    {
        node *newNode_2 = head;
        while(newNode_2->next != NULL)
        {
            newNode_2 = newNode_2-> next;
        }
        newNode_2->next = newNode;
    }
}

void pop_front()
{
    node *temp;
    temp = head;
    head = head->next;
    free(temp);
}

void pop_back()
{
}

bool isEmpty(int count)
{
    if(count == 0)
    {
        return false;
    }

    else
    {
        return true;
    }

}

void print()
{
    node* current = head;
    while(current != NULL)
    {
        cout << current-> data << " ";
        current = current->next;
    }
    cout << endl;
}
int main()
{
    int count  = 6;
    const int NUMS = 6;     //insert elements into the sorted linked list in an ascending order
    const int RANGE = 21;   //each element is in the range [-10, 10]
    /*SortedLinkedList mylist;*/
    srand(time(0));
    for (int i = 0; i < NUMS; i++)
    {
        int data = (rand() % RANGE) - 10;
        cout << "Adding " << data << " to the sorted linked list: " << endl;
        push_sorted(data);
        print();
    }

    while ((isEmpty(count) == true))
    {
        cout << "Removing from front..." << endl;
        pop_front();
        print();
        count --;
        cout << "Removing from back..." << endl;
        pop_back();
        print();
        count --;
    }
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您已完成剩余的MCVE(输入,输出,插入等)。 (但是,我建议你在结构中移动方法。)

我决定提供以下递归解决方案。只有1个测试,它似乎工作。

如果您不理解递归,我建议您解决循环迭代方法。如果您确实理解了重新定义,那么您仍然应该解决循环迭代方法,无论如何,许多人都认为这种方法更容易。

void pop_back()
{
   bool isLast = false;
   if(nullptr != head)    // does list have any elements?
   {
      // use the recursive form to find last, and 
      // return a bool to find the next to last
      isLast = head->pop_backR();  // recurse to last element

      if (isLast) // i.e. only 1 element in list, the head
      {
         delete (head);  // remove it
         head = nullptr; 
      }
   }

   // note:  on std::list, calling pop_back when container empty is undefined.
  //    tbd - throw underflow?   your choice here
}


bool pop_backR()
{
   if(nullptr == next)
      return true; // last node found, return feedback to previous node

   // last node not found, continue search
   bool nxtIsLast = next->pop_backR();

   // check - did we find the last element yet?
   if (nxtIsLast)
   {
      // at this point we know next is last
      delete (next);   // so delete last
      next = nullptr;  // remove it from list
      return false;    // we are done, now decurse with no more deletes
   }
   else  // previous recursion did not find it
      return false; 
}

这似乎有效,但尚未经过充分测试。

我的系统输出 -

Adding -9 to the sorted linked list: 
-9 
Adding 0 to the sorted linked list: 
-9 0 
Adding -6 to the sorted linked list: 
-9 0 -6 
Adding 10 to the sorted linked list: 
-9 0 -6 10 
Adding -8 to the sorted linked list: 
-9 0 -6 10 -8 
Adding -3 to the sorted linked list: 
-9 0 -6 10 -8 -3 
Removing from front...
0 -6 10 -8 -3 
Removing from back...
0 -6 10 -8 
Removing from front...
-6 10 -8 
Removing from back...
-6 10 
Removing from front...
10 
Removing from back...