【问题标题】:How to make the linked list ordered in C++?如何使链表在 C++ 中排序?
【发布时间】:2016-06-20 13:31:17
【问题描述】:
#include <iostream>
#include <string>
using namespace std;
struct node
{
    int num;
    node*next;
};

bool isEmpty(node *head);
char menu();
void insertasfirstelement(node *&head, node *&last, int num);
void insert(node *&head, node *&last, int num);
void remove(node *&head, node *&last);
void showlist(node*c);

bool isEmpty(node*head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;

    cout << "\n\nMenu:\n";
    cout << "\n1. Add an item";
    cout << "\n2. Remove an item";
    cout << "\n3. Show the list";
    cout << "\n4. Exit" <<endl;

    cin >> choice;
    return choice;
}
void insertasfirstelement(node *&head, node*&last, int num)
{
    node * temp = new node;
    temp ->num=num;
    temp ->next=NULL;
    head = temp;
    last = temp;
}
void insert(node *&head, node *&last, int num)
{
    if(isEmpty(head))
        insertasfirstelement(head,last,num);
    else
    {
        node *temp = new node;
        temp ->num=num;
        temp ->next=NULL;
        last ->next= temp;
        last = temp;
    }
}
void remove(node *&head, node *&last)
{
    if(isEmpty(head))
        cout << "List is empty\n";
    else if(head ==  last)
    {
        delete head;
        head = NULL;
        last = NULL;
    }
    else
    {
        node *temp = head;
        head = head -> next;
        delete temp;

    }
}
void showlist(node*c)
{
    if(isEmpty(c))
        cout <<"The list is empty\n";
    else
    {
        cout << "The values are: \n";
        while(c !=NULL)
        {
            cout << c -> num << endl;
            c = c -> next;
        }
    }
}
int main()
{
    node *head=NULL;
    node *last=NULL;
    char choice;
    int num;

    do{
        choice = menu();

        switch(choice)
        {
            case '1':   cout << "Please enter a number: ";
                        cin >> num;
                        insert(head, last, num);
                        break;
            case '2':   remove(head,last);
                        break;
            case '3':   showlist(head);``
                        break;
            default:    cout << "System exit\n";
        }
    } while(choice != '4');
}

所以我已经能够得到一个工作链表。但我一直无法弄清楚如何把它整理好。我也不知道怎么做,所以我可以删除我插入的数字。我正在尝试了解链接列表如何更好地工作,因此如果您能提供一些帮助,将不胜感激。

【问题讨论】:

    标签: c++ list sorting linked-list


    【解决方案1】:

    以下两个函数都假设您想要一个按升序排序的链表。

    要将新节点插入到您的链表对象中,您可以尝试:

    void insert(node *&head, node *&last, int num)
    {
        if(isEmpty(head))         
            insertasfirstelement(head,last,num);
        else if(num > tail->num)    //if new number to be inserted is greater than tail, insert at end and avoid needless iteration through list
        {
            node* temp = new node;
            temp->num = num;
            temp-next = NULL;
            last->next = temp;
            last = temp;
        }
        else
        {
            node *temp = new node;
            temp ->num=num;
            temp ->next=NULL;
            node* ptr = head;    //to lead iteration through linked list
            node* prev = NULL;  //to trail ptr
            while(ptr && (ptr->num > temp->num))  //iterate through linked list while new node's value is less than ptr's value and ptr is not beyond end of list (assuming last->next == NULL)
            {
                prev = ptr;
                ptr = ptr->next;
            }
            if(!prev)            // if prev never iterated, the temp node is the new head node
           {
               head = temp;
               head->next = ptr;
           }
           else if(prev == last)  //if prev is tail, the temp node is the new tail node
           {
               prev->next = temp;
               tail = temp;
           }
           else   //otherwise, insert temp node in between prev and ptr
           {
                prev-next = temp;
                temp->next = ptr;
           }
        }
    }
    

    要从您的链表对象中删除一个节点,请尝试:

    void remove(node *&head, node *&last, int x) // added a int parameter to determine which node is to be removed
    {
        if(isEmpty(head))
            cout << "List is empty\n";
        else 
        {
            node* ptr = head;
            node* prev = NULL;
            while(ptr && (ptr->num != x)   //iterate through linked list while desired value to be deleted is not found and have not gone through entire list
            {
                prev = ptr;
                prt = ptr->next;
            } 
            if(!prev)      //if prev never iterated, the head is the value to be deleted
            {
                node* garbage = head;
                head = head->next;
                delete garbage;
            }
            else if(ptr==tail)   //if the value to be deleted is the tail's value, reassign tail to prev and delete ptr
            {
               tail = prev;
               tail->next = NULL;
               delete ptr;
            }
            else if(!ptr)   //if ptr == NULL, value to be deleted was not in list
            {
                cout << "Value " << x << " could not be found in list.\n";     // or throw exception 
            }
            else      //reassign prev's next to ptr's next and delete ptr;
            {
                prev-next = ptr->next;
                delete ptr;
            }
        }
    }
    

    【讨论】:

    • 我完全明白你在这里做什么。我试图将 q 指针与一个 int 值 num 进行比较,以尝试遍历列表,而不是让 num 指向某个东西。我一年没写代码了,所以我有点生疏了,我想弄清楚我能用这种语言做的所有事情。我打算尝试设计和编写游戏代码。
    【解决方案2】:

    您可以通过比较节点的数据字段来使链表排序

    试试这个方法:

    1 - 直到节点为 NULL 遍历链表。

    2 - 如果当前节点的数据小于下一个节点的数据,则交换两个数据。

    3 - 否则移动到下一个节点

    如果你想订购链表而不交换数据...... 即更改节点的下一个,然后您也可以这样做..

    用于删除节点

    试试这个方法

    1- 要求用户输入他/她要删除的节点的数据。

    2- 搜索具有该数据的节点。

    3- 如果找到该节点,则删除该节点。

    4- 否则在剩余链表中查找节点。

    注意:这就像怎么做...你必须建立逻辑,这样你才能保持链表的一致性:)

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 1970-01-01
      • 2013-10-01
      • 2023-04-04
      • 1970-01-01
      • 2016-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多