Delete Node Method and Print Method

时间:2015-06-30 14:00:18

标签: c++ data-structures linked-list doubly-linked-list

I am trying to made simple doubly linked list.I made a Insert Method to add integer and a delete method to delete a number form the list.I aslo have two methods called ShowForword and ShowBackWord.Calling these methods all is going well.But when I delete a node from the list something goes wrong after deleting a node Print functions does not work anymore.Here is my Code so far.

List.h

        class List
    {
        public:
            List();
            void Insert(int data);
            void Delete(int data);
            void ShowForword();
            void ShowBackWord();
        private:
            typedef struct Node{
            int data;
            Node * next;
            Node * prev;
            }* NodePtr;

            NodePtr head;
            NodePtr tail;
            NodePtr current;
            NodePtr temp;
    };
 List.cpp

#include "List.h"
#include <iostream>
#include <cstddef>
using namespace std;

#define null NULL
List::List()
{
    head = null;
    temp = null;
    current = null;
     tail = null;
}

void List::Insert(int data)
{
    NodePtr n = new Node;
    n->next = null;
    n->data = data;

    if(head != null)
    {
        current = head;
        while(current->next != null)
        {
            current = current->next;
        }

        n->prev = tail;
        current->next = n;
        tail = n;
    }
    else
    {
        n->prev = null;
        head = n;
        tail = n;
    }

}
void List::Delete(int data)
{
  NodePtr del = null;
  current = head;
  temp = head;

  while(current !=null && current->data != data)
  {
      temp = current;
      current = current->next;
  }

  if(current == null)
  {
    cout<<"Not Found";
     delete del;
  }
  else
  {
      del = current;
      if(tail->next != null)
      current = current->next;

      temp->next = current;
      current->prev = temp;
      if(del == head)
      {

          head = head->next;
          head->prev = null;

          temp = null;
      }
      if(del == tail)
      {

          tail = tail->prev;

      }


     delete del;

  }
}
void List::ShowForword()
{
    current = head;
    if(current == null)
    {
        cout<<"Nothing\n";
    }
    else
    {
    while(current != null)
    {
        cout<<current->data<<endl;
        current = current->next;
    }
}
}
void List::ShowBackWord()
{

    current = tail;
    if(current == null)
    {
        cout<<"Nothing\n";
    }
    else
    {

    while(current != null)
    {


        cout<<current->data<<endl;
        current = current->prev;

    }
    }
}

Main.cpp

#include <iostream>
#include "List.h"
using namespace std;

int main()
{
   List lit;

   lit.Insert(67);
  lit.Insert(7);
   lit.Insert(15);
   lit.Insert(20);
   lit.Insert(50);
   lit.Insert(100);


   lit.Delete(100);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();
   cout<<"\n\n";

     lit.Delete(15);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();
    cout<<"\n\n";

    lit.Delete(50);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();

   cout<<"\n\n";

    lit.Delete(7);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();
   cout<<"\n\n";

    lit.Delete(67);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();

   cout<<"\n\n";

    lit.Delete(20);
   lit.ShowForword();
   cout<<"\n\n";
   lit.ShowBackWord();
   cout<<"\n\n";
   lit.ShowForword();
   cout<<"\n\n";
}

0 个答案:

没有答案