【问题标题】:display output in `ascending` or `descending` order after storing value using linked list in c使用c中的链表存储值后以“升序”或“降序”顺序显示输出
【发布时间】:2019-07-29 18:52:27
【问题描述】:

我使用用户输入存储五个不同的值。并在从命令提示符插入时显示,现在我想在显示期间将output 组织为ascendingdescending 顺序。我搜索了这个,但我得到了更复杂的例子。我的代码很简单。我在学习。到目前为止,我已经做了什么,如果我可以在我的代码中实现ascendingdescending 顺序,那么它将对我更容易理解有帮助。如果可能的话,有人可以在我现有的代码中帮助我这样做。

#include <stdio.h>

struct list{
    int value;
    struct list *next;
};

int main(){

    struct list list1[5], *c;
    int i = 0;
    for(i = 0; i < 5; i++){
        printf("Enter value of list %d = ", i);
        scanf("%d", &list1[i].value);
    }
    for(i = 0; i < 4; i++){
        list1[i].next = &list1[i+1];
    }
    list1[4].next = NULL;

    c = &list1[0];
    while(c != NULL){
        printf("List value: %d\n", c->value);
        c = c->next;
    }
    return 0;
}

【问题讨论】:

  • 可能是因为没有给list1数组的成员分配内存。修改循环并使用更传统的方式插入到链表中。
  • 您希望以哪种方式组织这些值 - a) 根据顺序将它们移动到数组位置,或 b) 通过相应地更改 next 指针?
  • b) 通过相应地更改next 指针
  • 嗨,你还在问这个问题吗??那我可以为你写代码..
  • 我经历了不同的过程,但这是否可能像@Armali 在 b) 中所说的那样。

标签: c data-structures linked-list


【解决方案1】:

…在我的代码中实现ascendingdescending order…通过相应地更改下一个指针

您可以使用一种插入排序来执行此操作,方法是遍历未排序的列表并构建一个新列表,以排序顺序重新连接 next 指针:

    struct list *head = NULL, *nc;  // new (initially empty) list starts at head
    for (c = list1; c; c = nc)      // go through original list
    {
        struct list **pred = &head, *d;
        while ((d=*pred) && c->value > d->value) pred = &d->next;   // find pos.
        nc = c->next;               // get next element (before overwritten)
        c->next = d, *pred = c;     // insert element into sorted list
    }

这是升序;对于降序,将&gt; 更改为&lt;

【讨论】:

    【解决方案2】:

    您可以改为按顺序存储数据。可以按照此代码按排序顺序存储数据:=

    #include<stdio.h>
    #include<stdlib.h>
    
    struct List
    {
     struct List * next;
     int node;
    };
    
    struct List * add(struct List * head, int data)
    {
      struct List * newnode = (struct List *)malloc(sizeof(struct List));
      newnode->next = NULL;
      newnode->node = data;
    
     /* if list is empty (initially), put value  */
      if(head == NULL)
      {
         head = newnode;
         return head;
     }    
      /* if head itself smaller than the node to be inserted 
         the sign(<) has to be changed(>) for ascending order.
      */
     else if(head->node > data)
    {
       newnode->next = head;
       head = newnode;
    
       return head;
     }
     struct List * temp = head;
     struct List * current = NULL;
    
     while(temp->next)
     {
         current = temp;
        /*if node is fitted between current node and next of current node..
            the sign(>=) has to be changed(<=) and the sign (<=) has to be changed                     (>=)for ascending order .
         */
        if(current->node <= data && current->next->node >= data)
        {
            newnode->next = current->next;
            current->next = newnode;
            return head;
        }
        temp = temp->next;
    }
    temp->next = newnode;
    
    return head;
    }
    
    void display(struct List * head)
    {
     if(head == NULL)
         return;
    
      printf("%i ", head->node);
    
      display(head->next);
     }
     int main(void){
       int arr[] = {3, 0, 1, 4, 2};
       int n = sizeof(arr)/sizeof(1);
       struct List * head = NULL;
    
      for(int i=0; i<n; i++){
        head = add(head, arr[i]);
      }
    
      display(head);
      printf("\n");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 2018-06-03
      • 2017-01-13
      • 2011-10-03
      相关资源
      最近更新 更多