如何插入一个分类的单链接列表中

How to insert into a sorted singly linked list?

本文关键字:单链接 链接 列表 分类 何插入 插入 一个      更新时间:2023-10-16

我正在制作一个排序的链接列表,其中用户输入一个数字,并且该数字插入到排序位置。我目前在排序功能方面有问题。

  class Node
    {
    public:
        int data;
        class Node *next;
        Node(int info, Node *ptr = 0) 
        {
            data = info; 
            next = ptr;
        }
    };
    class Node *head = NULL;

    class stack
    {
    private:
        Node *temp;
    public: 
        stack()
        {
            temp = 0;
        }
        bool isEmpty()
        {
        return temp == NULL;
    }

我决定使用插入功能(它称为Sort,但实际上它只是将一个节点插入链接列表中),这就是为什么我有当前指针和以前的指针。我想我的问题是我走正确的道路吗?我是新手,所以我只想一些指导来解决这个问题。任何技巧都将不胜感激。

 void sort(int data)
    {
        class Node *born = new Node(data);
        Node* current = head;
        Node* previous = NULL;
        //the list is empty case
        if (isEmpty())
            temp = born;
        else
        {
            while (current != NULL)
            {
                if (current->data >= temp->data)
                {
                    born->next = current;
                    temp = born;
                    break;
                }
                else
                {
                    previous = current;
                    current = temp->next;
                }
            }
            if (current == head)
            {
                born->next = head;
                head = born;
            }
            else
            {
                born->next = current;
                previous->next = born;
            }
            /*else
            {
                born->next = temp;
                temp = born;
            }*/
        }
    }
    void print()
    {
        cout<<"stack from the top"<<endl;
            for(Node *top = temp; top != 0; top=top->next)
                cout << top->data << " ";
            cout << endl;
        /*while(temp != NULL)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }
        */
    }
    int pop()
    {
        if (isEmpty())
                return -999;        
            int intReturn = temp->data;
            Node *top;
            top = temp;
            temp = temp->next;
            delete top;

            return intReturn;
    }
    };
    int main(void)
    {
        int num=0;
        stack boss;
        while(num!=-1)
        {
            cout<<"Please Enter a Number"<<endl;
            cin >> num;
            if (num == -1)
                boss.print();
            else
                boss.sort(num);
        }
        cout<<endl<<endl;

        system("PAUSE");
        return 0;
    }

我对您对事物的逻辑步骤和/或事物的命名有担忧。我不能只是编辑您的代码来修复它,因此,如果您同意我的意见,我已经根据此理解重写您的代码:

  • a head指向第一个节点,而 tail指向最后一个节点。
  • 检查列表是否为空,然后headtail指向新节点。
  • 否则,检查是否要将新节点放在开始时,然后进行
  • 否则,检查一下是否适合中间,然后进行
  • 否则,将其放在最后一个地方

此外,我的其他更改包括:

a。对于类stack

  • 我将其重命名为linkedlist
  • 我将两个成员变量headtail参考第一个和最后一个节点

b。对于sort()方法

  • 我将其重命名为insert
  • 我明确地清楚地写了每个重要情况(空列表,第一个节点,中间节点,最后一个节点)

我还删除了using namespace std并明确编写了std::,因为这是一个更好的做法。这里和那里也有一些小变化。

我的最终代码:

class Node
{
public:
  int data;
  class Node* next;
  Node(int info, Node* ptr = 0) 
  {
    data = info; 
    next = ptr;
  }
};
class LinkedList
{
private:
  Node* head;
  Node* tail;
public: 
  LinkedList()
  {
    head = 0;
    tail = 0;
  }
  bool isEmpty()
  {
    return head == NULL;
  }
  void insert(int data)
  {
    Node* newNode = new Node(data);
    if (isEmpty())
    {
      head = newNode;
      tail = newNode;
    }
    // if node to be placed at the top
    else if (data > head->data)
    {
      newNode->next = head;
      head = newNode;
    }
    else
    {
      Node* temp = head;
      Node* previousTemp = head;
      while (temp != NULL)
      {
        // if node to be placed in the middle
        if (temp->data < data)
        {
          previousTemp->next = newNode;
          newNode->next = temp;
          break;
        }
        previousTemp = temp;
        temp = temp->next;
      }
      // if node to be placed at the end
      if (temp == NULL)
      {
        tail->next = newNode;
        tail = tail->next;
      }
    }
  }
  void print()
  {
    std::cout << "Stack from the top" << std::endl;
    Node* temp = head;
    while(temp != NULL)
    {
      std::cout << temp->data << " ";
      temp = temp->next;
    }
  }
  int pop()
  {
    if (isEmpty())
    {
      return -999;
    }
    int intReturn = head->data;
    Node *top;
    top = head;
    head = head->next;
     delete top;

     return intReturn;
  }
};
int main(void)
{
  int num = 0;
  LinkedList linkedList;
  while (num != -1)
  {
    std::cout << "Please enter a number: ";
    std::cin >> num;
    if (num == -1)
    {
      linkedList.print();
    }
    else
    {
      linkedList.insert(num);
    }
  }
  std::cout << std::endl << std::endl;
  system("PAUSE");
  return 0;
}

这有帮助吗?