【问题标题】:Remove function for Doubly Linked List c++删除双向链表c ++的函数
【发布时间】:2016-06-11 17:47:11
【问题描述】:

我正在尝试为我的双向链表类实现删除功能。

#include <iostream>

#include <string>
 #include <vector>
 #include <stdio.h>
#include <stdlib.h>

 using namespace std;



 class DblLinkedBag
  {
    private:
       struct node{


       string data;
       node* next;
       node* prev; 
    }*start=NULL;

      int itemCount;  
      string item; 
      node *head;
      node *tail; 



      public:
      DblLinkedBag();
      ~DblLinkedBag();                       
      int getCurrentSize();
      bool isEmpty();
     bool add(string value);
      bool remove(string item);
     void clear();
      bool contains(string target);
     int getFrequencyOf();
    string retStart();


    string getItem();



 }; 

这就是目前的移除功能。

  bool DblLinkedBag::remove(string value)

   {
   node* to_remove = head;
   while(to_remove && to_remove->data != item)
   to_remove = to_remove->next;

  // Do the removal if we found it
  if(to_remove)
  {
  // If it was at the head, advance the head to the next item
    if(to_remove == head)
   head = head->next;


   // Remove from the list
   if(to_remove->next)
   to_remove->next->prev = to_remove->prev;
   if(to_remove->prev)
   to_remove->prev->next = to_remove->next;

 // Free the removed node
 delete to_remove;
 itemCount--;
 return true;
}

return false; 

当我尝试运行它时,什么也没有发生,它甚至不返回 false。可能是因为我实现添加功能的方式吗?这是我的添加功能。

   bool DblLinkedBag::add(string value)
   {

    node* n;
   bool add=false;
   cout<<itemCount<<endl;
   if(itemCount==0)
    {
    n=new node;
    n->data=value;
    n->prev=NULL;
    head=n;
    tail=n;
    add=true;


   }

  if(itemCount>0 && itemCount<7)
   {
    n= new node;
    n->data=value;
    n->prev=tail;
    tail->next=n;
    tail=n;
    add=true;


 }

   itemCount++;
   return add;
  }

任何帮助将不胜感激。

这就是我调用函数的方式。

  void displayBag(int size)
  {  DblLinkedBag bag;

    cout << "The bag contains " <<size
    << " items:" << endl;


  int numberOfEntries = size;
  //for (int i = 0; i < numberOfEntries; i++)
  // {

    //  cout <<bagItems[i]  << " ";
    // }  // end for
  //    cout << endl << endl;

  }  // end displayBag



void copyConstructorTester()
{

      DblLinkedBag bag;
      string items[6] = {"zero", "one", "two", "three", "four", "five"};
      for (int i = 0; i < 6; i++)
      {
      cout << "Adding " << items[i] << endl;
      bag.add(items[i]);

     //    bool success = bag.add(items[i]);
       //if (!success)
      //     cout << "Failed to add " << items[i] << " to the bag." << endl;
     }


     void bagTester()
     {
     DblLinkedBag bag;
      cout << "Testing the Link-Based Bag:" << endl;
      cout << "isEmpty: returns " << bag.isEmpty()
     << "; should be 1 (true)" << endl;
    cout<<"*BAG TESTER*"<<endl;
    displayBag(bag.getCurrentSize());

    string items[] = {"one", "two", "three", "four", "five", "one"};
    cout << "Add 6 items to the bag: " << endl;
    for (int i = 0; i < 6; i++)
       {
       bag.add(items[i]);
      }  // end for



     displayBag(bag.getCurrentSize());
     bag.display();
     cout << "isEmpty: returns " << bag.isEmpty()
     << "; should be 0 (false)" << endl;


     cout << "getCurrentSize: returns " << bag.getCurrentSize()
     << "; should be 6" << endl;

    cout << "Try to add another entry: add(\"extra\") returns "
    << bag.add("extra") << endl;

    cout << "contains(\"three\"): returns " << bag.contains("three")
  << "; should be 1 (true)" << endl;
  // cout << "contains(\"ten\"): returns " << bag.contains("ten")
      // << "; should be 0 (false)" << endl;
   // cout << "getFrequencyOf(\"one\"): returns "
   // << bag.getFrequencyOf("one") << " should be 2" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
   << "; should be 1 (true)" << endl;
  // cout << "getFrequencyOf(\"one\"): returns "
   // << bag.getFrequencyOf("one") << " should be 1" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
    << "; should be 1 (true)" << endl;
   cout << "remove(\"one\"): returns " << bag.remove("one")
     << "; should be 0 (false)" << endl;
   // cout << endl;

    displayBag(bag.getCurrentSize());

    cout << "After clearing the bag, ";
    bag.clear();

   cout << "isEmpty: returns " << bag.isEmpty()
   << "; should be 1 (true)" << endl;
  }  // end bagTester





    int main()
   { 


   copyConstructorTester();
   bagTester();
   return 0;
 }  // end main

【问题讨论】:

  • 您是否使用调试器单步调试您的代码?
  • 你调用删除了吗?
  • @Anedar 不,我没有
  • 在你的双链表及其链接中画出 3 个元素的图片。拿一支红铅笔,画出当你移除中间的项目时会改变的链接。检查您的代码是否符合预期。迭代第一个元素,假设它是列表的开头,最后一个元素,如果它是列表的结尾。
  • 那么你应该这样做 - 它会给你很多关于正在发生的事情和原因的信息。

标签: c++ linked-list doubly-linked-list dev-c++


【解决方案1】:

这就是你如何绘制一个双向链表来计算节点的删除。

  Node 1  
+----------+------+------+  
| Previous | Data | Next |   
|  -0-     |  ?   |      |  
+----------+------+------+  
    ^                 |  
    |     Node 2      V  
+----------+------+------+  
| Previous | Data | Next |   
+----------+------+------+  
    ^                 |  
    |     Node 3      V  
+----------+------+------+  
| Previous | Data | Next |   
|          |  ?   |  -0- |  
+----------+------+------+  

让我们假设以下指针:

Node * node1 = head;
Node * node2(node1->next);
Node * node3(node2->next);

一种可能是从 previous 链接开始:
让节点 3 之前的链接指向节点 1:

  node3->previous = node1;  

  Node 1  
+----------+------+------+  
| Previous | Data | Next |   
|  -0-     |  ?   |      |  
+----------+------+------+  
   ^   ^              |  
   |   |              |
+--+   |              |    
|      |     Node 2   V  
|   +------+------+------+  
|   | Prev | Data | Next |   
|   +------+------+------+  
|                     |  
+---+                 |  
    |     Node 3      V  
+----------+------+------+  
| Previous | Data | Next |   
|          |  ?   |  -0- |  
+----------+------+------+  

下一步是使Node 1Next链接指向节点3:
node1-&gt;next = node3;

 Node 1  
+----------+------+------+  
| Previous | Data | Next |   
|  -0-     |  ?   |      |  
+----------+------+------+  
   ^   ^              |  
   |   |              |
+--+   |              +------+   
|      |     Node 2          |
|   +------+------+------+   |  
|   | Prev | Data | Next |   |   
|   +------+------+------+   |  
|                   |        |  
|                   |  +-----+  
+---+               |  |   
    |     Node 3    V  V  
+----------+------+------+  
| Previous | Data | Next |   
|          |  ?   |  -0- |  
+----------+------+------+  

在上图中,当您跟踪从Node 1Node 3Node 3Node 1 的链接时,您会注意到Node 2 未被访问。

因此Node 2 可以安全地删除。

最后,删除节点 2

delete node2;

总结:

Node * current = head;
Node * previous = head;

// Find the node
while (current->data != key)
{
    previous = current;
    current = current->next;
}

// Assume node was found.
Node after = current->next;

// Make previous point to the node after the current.
previous->next = after;

// Make the node after the current node point to the previous.
if (after != NULL)
{
  after->previous = previous;
}

// Delete the current node.
delete current;

【讨论】:

    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 2020-12-18
    • 2014-03-04
    • 2021-04-01
    • 1970-01-01
    • 2011-03-12
    • 2021-11-22
    相关资源
    最近更新 更多