【发布时间】: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