【问题标题】:Enter a node in middle to make a sorted linked lists在中间输入一个节点,组成一个排序的链表
【发布时间】:2014-07-14 05:31:41
【问题描述】:

以下是我自己尝试制作的程序。我想在中间输入一个节点,这样一个排序的链接列表作为输出。但是它不起作用。所以请帮助我得到一个完美的输出。让我明白这段代码出了什么问题?

#include<stdio.h>
#include<stdlib.h>
typedef struct node_type
{
    int roll_no;
    char name[10];
    float marks;
    struct node_type *next;
} node;
typedef node *list;
void show_list (list);
main () 
{
    list head, temp, tail;
    char ch;
    int n;
    head = NULL;
    printf ("Do you want to add?(y/n)");
    scanf ("%c", &ch);
    if (ch == 'y' || ch == 'Y')
    {
        tail = (list) malloc (sizeof (node));
        printf ("Enter the value for roll number:-\n");
        scanf ("%d", &(tail->roll_no));
        printf ("Enter name:-\n");
        scanf ("%s", tail->name);
        printf ("Enter marks:-\n");
        scanf ("%f", &(tail->marks));
        tail->next = head;
        head = tail;
        printf ("Enter more data?(y/n)\n");
        scanf ("\n%c", &ch);
    }
    while (ch == 'y' || ch == 'Y')
    {
        tail->next = (list) malloc (sizeof (node));
        printf ("Enter the value for roll number:-\n");
        scanf ("%d", &(tail->next->roll_no));
        printf ("Enter name:-\n");
        scanf ("%s", tail->next->name);
        printf ("Enter marks:-\n");
        scanf ("%f", &(tail->next->marks));
        temp = tail;
        printf ("Enter more data?(y/n)\n");
        scanf ("\n%c", &ch);
    }
    while (temp->roll_no < tail->roll_no)
    {
        head = tail;
        tail = tail->next;
        temp->next = tail;
        head->next = temp;
    }
    show_list (head);
}

void
show_list (list start) 
{
    while (start != NULL)  
    {
        printf ("%d \t %s \t %f \n", start->roll_no, start->name,
        start->marks);
        start = start->next;
    }
}

【问题讨论】:

  • 你有什么问题?
  • 我没有以排序方式获得链接列表。

标签: c singly-linked-list sortedlist


【解决方案1】:

您的while (ch == 'y' || ch == 'Y') 语句不断在列表末尾添加数据。

永远不会评估while (temp-&gt;roll_no &lt; tail-&gt;roll_no) 语句条件,因为当您退出前一个while 循环时,temp = tail。想想你的算法,然后再试一次。

提示:

  1. 分段您的代码
  2. 创建一个函数来创建一个节点node *createNode() 并返回一个指向它的指针。
  3. 制作另一个函数void insertNode( node * start, node *newNode)

然后,您可以将函数分隔为

while( 1 ) {
      /* <
          code that checks if you want to continue
          and breaks out of the loop otherwise
       >*/

     insertNode(head, createNode())
}

你就完成了。

您已经完成了createNode() 的工作。只需在另一个上多做一点。

【讨论】:

    【解决方案2】:

    您的插入机制错误。它总是在第二个位置添加/覆盖。 在第一个 while 循环的末尾添加以下两个语句来纠正这个问题:

    tail=tail->下一个;

    tail->next=NULL;

    然后丢弃完全没有意义的第二个 while 循环。

    然后尝试遍历并排序列表。


    否则,您可以在添加节点时对列表进行排序。 为此,请将程序中的两个 while 循环替换为以下代码:

    while (ch == 'y' || ch == 'Y')
        {
            list T;
            temp = (list) malloc (sizeof (node));
            printf ("Enter the value for roll number:-\n");
            scanf ("%d", &(temp->roll_no));
            printf ("Enter name:-\n");
            scanf ("%s", temp->name);
            printf ("Enter marks:-\n");
            scanf ("%f", &(temp->marks));
            temp->next=NULL;
            if(temp->roll_no<head->roll_no)
            {
                temp->next=head;
                head=temp;
            }
            else{
            for(T=head;;T=T->next)
            {
                if(T->next==NULL)
                {
                    T->next=temp;
                    break;
                }
                if(temp->roll_no<T->next->roll_no)
                {
                    temp->next=T->next;
                    T->next=temp;
                    break;
                }
            }
            }
            printf ("Enter more data?(y/n)\n");
            scanf ("\n%c", &ch);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多