【问题标题】:Cycle through List and Ordering循环浏览列表和排序
【发布时间】:2015-02-14 22:08:17
【问题描述】:

我想创建一个链表。 用户添加数字,想法是数字按降序插入到列表中。

这是我所做的,但是在重新排列时,它只是订购第一个数字......

int addInputNumber(numberList **node){

  numberList *temp;
  int userInput;
  temp = (numberList*)malloc(sizeof(numberList));
  //Memory Check
  if ( temp == 0 )//out of memory, return 0
      return 0;
  //Get the users input
  printf("Give me a Number!\n");
  scanf("%d",&userInput);

 //Add it to the list.
  temp->numbero = userInput;
  ///Link to the list.
  temp->next = *node; 
  *node = temp; 

  //Lets cycle through the list.
  numberList *temp2;
  int helpNumber;
  temp2 = *node;

  //Rearrange the list.
  while(temp2 != 0){
    if(temp->numbero < temp2->numbero){

      //Switch position..
      helpNumber= temp2->numbero;
      temp2->numbero = temp->numbero;
      temp->numbero = helpNumber;
      temp2 = *node;// If we change number, we must cycle from the beginning AGAIN. 
    }//eof if
    temp2 = temp2->next;
  }//eof while

  return 0;
}//eof addNUmber function.

这是结构以防万一:

typedef struct dynamicNumberList {
  int numbero;
  struct dynamicNumberList *next;
}numberList;

我有 2 个简单的问题。

  1. 为什么只排第一个数字?

  2. 此列表向左侧添加了一个空格(视觉上),我怎样才能使它在右侧添加一个空格?

【问题讨论】:

    标签: c arraylist linked-list structure


    【解决方案1】:

    您需要养成为每个任务创建一个函数的习惯,而不是将所有内容都塞进一个函数中。它使代码更易于阅读和重用,并减少了出错的机会。

    正确的实现可能如下所示:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct s_List
    {
      int n;
      struct s_List *next;
    } List;
    
    void print_list (List *head)
    {
        List *ptr;
    
        for (ptr = head; ptr; ptr = ptr->next) {
            printf ("%d\t", ptr->n);
        }
        putchar ('\n');
    }
    
    List * make_node (int n, List *next)
    {
        List * node = malloc (sizeof(List));
        node->n = n;
        node->next = next;
        return node;
    }
    
    void insert_number_front (List **head, int n)
    {
        *head = make_node (n, *head);
    }
    
    void insert_number_after (List *prev, int n)
    {
        prev->next = make_node (n, prev->next);
    }
    
    // If HEAD is sorted, it will stay sorted after insertion
    void insert_number_sorted (List **head, int n)
    {
        List *ptr;
        List *ptr2;
    
        // search for the rightmost node whose number is smaller than n.
        ptr2 = NULL;
        for (ptr = *head; ptr; ptr = ptr->next) {
            if (ptr->n >= n)
                break;
            ptr2 = ptr;
        }
    
        // If such a node exists we insert the new node after it,
        // otherwise we insert it at the front of the list.
        if (ptr2) {
            insert_number_after (ptr2, n);
        }
        else {
            insert_number_front (head, n);
        }
    }   
    
    int input_number ()
    {
        int n;
    
        printf ("enter a number: ");
        scanf ("%d", &n);
        return n;
    }
    
    int main ()
    {
        List *head = NULL;
        int i;
    
        // By adding elements exclusively with insert_number_sorted()
        // we ensure the list is always sorted
        for (i = 0; i < 5; i++) {
            int n;
    
            n = input_number ();
            insert_number_sorted (&head, n);
        }
    
        print_list (head);
    
        return 0;
    }
    

    回答你的第二个问题,你这里有一个单链表,可以用指向第一个节点的指针来描述。如果您希望能够在后面插入节点,则需要维护一个指向最后一个节点的附加指针。但是,在这种情况下,这不是必需的。

    【讨论】:

    • 开头段落很准确!
    • @Antoine Mathys 感谢您的帮助!不过我有一个问题,除了 insert_node_sorted 之外,我几乎理解了所有内容。我不太了解 for(ptr=... 和 if(ptr2) 的用途。如果您能解释一下,我将不胜感激!我会将每个特定部分的功能作为一种习惯。谢谢您的建议和帮助!
    • @AntoineMathys 非常感谢。我刚刚看到您的编辑,它有所帮助,但我无法说出这句话,我没那么先进哈哈: tmp = ptr2 ? &(ptr2->next) : head;....我猜这和 if(ptr2) 做的一样.....
    • @AntoineMathys 你几乎给了我那里的代码,我真的很感谢你,但我看到了这个“ptr2 ? &(ptr2->next) : head;”我怎样才能查找更多信息?我没有得到什么'?','&(ptr2->next)'和':head;'方法。我假设 &ptr2->next 被给出了 head 的引用。但是我想知道怎么办?和:意思。对不起,我通过网络学习哈哈
    • @AntoineMathys 太棒了,非常感谢。我不知道这个运营商。我可以在此列表创建中添加以前的链接吗?如果是这样,我将在哪里修改此代码?(我假设我只需要在创建节点时添加 node-prev = NULL; 和链接时 node->prev = ptr2; 对吗??
    猜你喜欢
    • 2020-10-14
    • 2016-08-08
    • 2020-01-02
    • 2023-03-22
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多