添加/删除尾部,反向显示列表。链表c ++

时间:2013-11-29 07:14:38

标签: c++ singly-linked-list

我正在制作一个链接列表,我制作了一个菜单(作为一个菜鸟,这让我印象深刻:) :)并获得了添加前端并删除前端工作。虽然我希望能够在最后添加一个节点,然后将其删除并反向显示。我认为我的removelast功能看起来会起作用,但事实并非如此。不知道如何添加到尾部,并且不知道如何反向显示。我查了一些指南,但我不擅长从文本中理解,我喜欢看到与我的问题有关的例子。

看一看!

#include <iostream>
using namespace std;

struct node //node structure
{
    int number;
    node *next;

};

bool isempty(node *head);
char menu();
void first(node *&head, node *&last, int number );
void insert(node *&head, node *&last, int number);
void removefirst(node *&head, node *&last);
void removelast(node *&head, node *&last);
void shownode(node *current);
void showreverse(node *&last);


bool isempty(node *head)
{
    if(head == NULL)
        return true;
    else 
        return false;

}
char menu()
// I added a menu because in an actual application this is what you would have
//And it was fun :D
{
   char choice;
   cout<<"choose an option:"<<endl;
   cout<<"1. Add Node."<<endl;
   cout<<"2. Add Node to Last"<<endl
   cout<<"3. Remove First Node."<<endl;
   cout<<"4. Remove last node"<<endl;
   cout<<"5. Show Node List."<<endl;
   cout<<"6. Reverse Node List."<<endl;
   cout<<endl;
cin>>choice;
cout<<endl;

return choice;


}
void first(node *&head, node *&last, int number ) //adding first
{
    node *temp =new node;
    temp->number = number;
    temp->next = NULL;
    head = temp;
    last = temp;

}
void insert(node *&head, node *&last, int number)//adding more
{
    if(isempty(head))
        first(head, last, number);
    else 
    {
    node *temp =new node;
    temp->number = number;
    temp->next = NULL;  
    last->next = temp;
    last = temp;
    }
}

void addlast (node *&head, node *&last, int number)
{


    node *temp =new node;
    temp->number = number;
    temp->last = NULL;  
    last->next = temp;
    last = temp;



}
void removefirst(node *&head, node *&last)//destructor
{
    if(isempty(head))
        cout<<"List is empty."<<endl;
    else if (head == last)
    {
        delete head;
        head == NULL;
        last == NULL;

    }
    else 
    {
        node *temp = head;
        head = head->next;
        delete temp;


    }

}
void removelast(node *&head, node *&last)
{
    if(isempty(head))
        cout<<"List is empty."<<endl;
    else if (head == last)
    {
        delete last;
        head == NULL;
        last == NULL;

    }
    else 
    {
        node *temp = last;
        last = last->next;
        delete temp;


    }



}
void shownode(node *current)
{
    if (isempty(current))
        cout<<"list is empty"<<endl;

    else 
    {
        cout<<"Nodes in list:"<<endl;
        while(current != NULL)
        {

            cout<<current->number<<endl;
            current = current->next;

        }

    }

}

void showreverse(*&last)
{





}
int main()
{
  node *head = NULL;  
  node *last = NULL;
  char choice;
  int number;

  do{
      choice = menu();

      switch(choice)
      {
          case '1': cout<<"inert number:"<<endl;
                  cin>>number;
          insert(head, last, number);
          break;
          case '2': addlast(head, last, number);
          break;
          case '3': removefirst(head, last);
          break;
          case '4': removelast(head, last);
          break;
          case '5': shownode(head);
          break;
          case '6': showreverse(last);
          break;
          default: cout<<"Exit";


      }
  }while(choice != '4');


    return 0;
}

1 个答案:

答案 0 :(得分:0)

删除最后一个元素时,在removelast(node *&head, node *&last)函数中,最后一个节点的下一个成员仍将指向您删除的最后一个元素。

node *temp = last;
last = last->next;
delete temp;

您正在创建指向同一节点的指针,更改其下一个成员而不是删除它,因此前两个指令是无用的。问题出现在最后一个但是下一个成员,但是如果不迭代整个列表,你就无法改变它。我认为最好的方法是使用doubly linked list。使用双向链表只需将最后一个指针与头指针交换,或从最后一个解析为头。