显示一个排序的链表,其中包含 c 中的每个新元素
display a sorted linked list with every new element in c
所以我试图用 c 编写一个程序,让用户输入数字,直到输入数字 0。然后创建一个链表,将输入的整数从小到大排序。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *ptr;
} node;
node* insert(node* head, int num) {
node *temp, *prev, *next;
temp = (node*)malloc(sizeof(node));
temp->data = num;
temp->ptr = NULL;
if(!head){
head=temp;
} else{
prev = NULL;
next = head;
while(next && next->data<=num){
prev = next;
next = next->ptr;
}
if(!next){
prev->ptr = temp;
} else{
if(prev) {
temp->ptr = prev->ptr;
prev-> ptr = temp;
} else {
temp->ptr = head;
head = temp;
}
}
}
return head;
}
void free_list(node *head) {
node *prev = head;
node *cur = head;
while(cur) {
prev = cur;
cur = prev->ptr;
free(prev);
}
}
int main(){
int num;
node *head, *p;
head = NULL;
do {
printf("Enter a number: ");
scanf("%d",&num);
printf("%d->\n", num);
if(num) {
head = insert(head, num);
}
} while(num);
p = head;
printf("\nThe entered numbers are:\n");
while(p) {
printf("%d->", p->data);
p = p->ptr;
}
free_list(head);
printf("NULL");
return 0;
}
如您所见,输出将如下所示:
Enter number: 6
6->NULL
Enter number: 3
3->NULL
Enter number: 9
9->NULL
Enter number: 0
0->
The entered numbers are :
3->6->9->NULL
但是,我想要的是这些数字在输入和插入列表时进行排序(我还需要不显示 0)。所以输出应该是这样的:
Enter number: 6
6->NULL
Enter number: 3
3->6->NULL
Enter number: 9
3->6->9->NULL
Enter number: 0
The entered numbers are :
3->6->9->NULL
有人可以帮我解决这个问题吗???
您实际上是按排序顺序将元素插入到列表中。您只是在读入当前值而不是整个列表后打印当前值。
因此将打印代码放在一个函数中,然后在插入一个值并读取所有值后调用该函数。
我已经修改了一些你的代码,请检查:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *ptr;
} node;
node *
insert (node * head, int num)
{
node *temp, *prev, *next;
temp = (node *) malloc (sizeof (node));
temp->data = num;
temp->ptr = NULL;
if (!head)
{
head = temp;
}
else
{
prev = NULL;
next = head;
while (next && next->data <= num)
{
prev = next;
next = next->ptr;
}
if (!next)
{
prev->ptr = temp;
}
else
{
if (prev)
{
temp->ptr = prev->ptr;
prev->ptr = temp;
}
else
{
temp->ptr = head;
head = temp;
}
}
}
return head;
}
void
free_list (node * head)
{
node *prev = head;
node *cur = head;
while (cur)
{
prev = cur;
cur = prev->ptr;
free (prev);
}
}
int
main ()
{
int num;
node *head, *p;
head = NULL;
do
{
printf ("\nEnter a number: ");
scanf ("%d", &num);
if (num != 0)
{
printf ("%d->\n", num);
}
if (num)
{
head = insert (head, num);
p = head;
printf ("\nThe entered numbers are:\n");
while (p)
{
printf ("%d->", p->data);
p = p->ptr;
}
}
}
while (num);
free_list (head);
return 0;
}
output->
Enter a number: 8
8->
The entered numbers are:
8->
Enter a number: 2
2->
The entered numbers are:
2->8->
Enter a number: 4
4->
The entered numbers are:
2->4->8->
Enter a number: 1
1->
The entered numbers are:
1->2->4->8->
Enter a number: 0
我不明白你是如何得到这个输出的
Enter number: 6
6->NULL
Enter number: 3
3->NULL
Enter number: 9
9->NULL
Enter number: 0
0->
在此 do-while 循环中
do {
printf("Enter a number: ");
scanf("%d",&num);
printf("%d->\n", num);
if(num) {
head = insert(head, num);
}
} while(num);
唯一输出内容的语句是
printf("%d->\n", num);
但它不输出字符串 ->NULL
.
函数 insert 中没有输出语句。
要在 do while 语句中获得所需的输出,您需要编写
do {
printf("Enter a number: ");
scanf("%d",&num);
if(num) {
head = insert(head, num);
for ( p = head; p != NULL; p = p->ptr )
{
printf("%d->", p->data);
}
puts( "NULL " );
}
} while(num);
你的函数 insert
也太复杂了。 if 语句和局部变量太多。该函数可以写得更简单。例如
node * insert( node *head, int num )
{
node *temp = malloc( sizeof( node ) );
temp->data = num;
if ( head == NULL || head->data < num )
{
temp->ptr = head;
head = temp;
}
else
{
node *current = head;
while ( ( current->ptr != NULL ) &&
!( num < current->ptr->data ) )
{
current = current->ptr;
}
temp-ptr = current->ptr;
current->ptr = temp;
}
return head;
}
所以我试图用 c 编写一个程序,让用户输入数字,直到输入数字 0。然后创建一个链表,将输入的整数从小到大排序。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *ptr;
} node;
node* insert(node* head, int num) {
node *temp, *prev, *next;
temp = (node*)malloc(sizeof(node));
temp->data = num;
temp->ptr = NULL;
if(!head){
head=temp;
} else{
prev = NULL;
next = head;
while(next && next->data<=num){
prev = next;
next = next->ptr;
}
if(!next){
prev->ptr = temp;
} else{
if(prev) {
temp->ptr = prev->ptr;
prev-> ptr = temp;
} else {
temp->ptr = head;
head = temp;
}
}
}
return head;
}
void free_list(node *head) {
node *prev = head;
node *cur = head;
while(cur) {
prev = cur;
cur = prev->ptr;
free(prev);
}
}
int main(){
int num;
node *head, *p;
head = NULL;
do {
printf("Enter a number: ");
scanf("%d",&num);
printf("%d->\n", num);
if(num) {
head = insert(head, num);
}
} while(num);
p = head;
printf("\nThe entered numbers are:\n");
while(p) {
printf("%d->", p->data);
p = p->ptr;
}
free_list(head);
printf("NULL");
return 0;
}
如您所见,输出将如下所示:
Enter number: 6
6->NULL
Enter number: 3
3->NULL
Enter number: 9
9->NULL
Enter number: 0
0->
The entered numbers are :
3->6->9->NULL
但是,我想要的是这些数字在输入和插入列表时进行排序(我还需要不显示 0)。所以输出应该是这样的:
Enter number: 6
6->NULL
Enter number: 3
3->6->NULL
Enter number: 9
3->6->9->NULL
Enter number: 0
The entered numbers are :
3->6->9->NULL
有人可以帮我解决这个问题吗???
您实际上是按排序顺序将元素插入到列表中。您只是在读入当前值而不是整个列表后打印当前值。
因此将打印代码放在一个函数中,然后在插入一个值并读取所有值后调用该函数。
我已经修改了一些你的代码,请检查:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *ptr;
} node;
node *
insert (node * head, int num)
{
node *temp, *prev, *next;
temp = (node *) malloc (sizeof (node));
temp->data = num;
temp->ptr = NULL;
if (!head)
{
head = temp;
}
else
{
prev = NULL;
next = head;
while (next && next->data <= num)
{
prev = next;
next = next->ptr;
}
if (!next)
{
prev->ptr = temp;
}
else
{
if (prev)
{
temp->ptr = prev->ptr;
prev->ptr = temp;
}
else
{
temp->ptr = head;
head = temp;
}
}
}
return head;
}
void
free_list (node * head)
{
node *prev = head;
node *cur = head;
while (cur)
{
prev = cur;
cur = prev->ptr;
free (prev);
}
}
int
main ()
{
int num;
node *head, *p;
head = NULL;
do
{
printf ("\nEnter a number: ");
scanf ("%d", &num);
if (num != 0)
{
printf ("%d->\n", num);
}
if (num)
{
head = insert (head, num);
p = head;
printf ("\nThe entered numbers are:\n");
while (p)
{
printf ("%d->", p->data);
p = p->ptr;
}
}
}
while (num);
free_list (head);
return 0;
}
output->
Enter a number: 8
8->
The entered numbers are:
8->
Enter a number: 2
2->
The entered numbers are:
2->8->
Enter a number: 4
4->
The entered numbers are:
2->4->8->
Enter a number: 1
1->
The entered numbers are:
1->2->4->8->
Enter a number: 0
我不明白你是如何得到这个输出的
Enter number: 6
6->NULL
Enter number: 3
3->NULL
Enter number: 9
9->NULL
Enter number: 0
0->
在此 do-while 循环中
do {
printf("Enter a number: ");
scanf("%d",&num);
printf("%d->\n", num);
if(num) {
head = insert(head, num);
}
} while(num);
唯一输出内容的语句是
printf("%d->\n", num);
但它不输出字符串 ->NULL
.
函数 insert 中没有输出语句。
要在 do while 语句中获得所需的输出,您需要编写
do {
printf("Enter a number: ");
scanf("%d",&num);
if(num) {
head = insert(head, num);
for ( p = head; p != NULL; p = p->ptr )
{
printf("%d->", p->data);
}
puts( "NULL " );
}
} while(num);
你的函数 insert
也太复杂了。 if 语句和局部变量太多。该函数可以写得更简单。例如
node * insert( node *head, int num )
{
node *temp = malloc( sizeof( node ) );
temp->data = num;
if ( head == NULL || head->data < num )
{
temp->ptr = head;
head = temp;
}
else
{
node *current = head;
while ( ( current->ptr != NULL ) &&
!( num < current->ptr->data ) )
{
current = current->ptr;
}
temp-ptr = current->ptr;
current->ptr = temp;
}
return head;
}