I would like to sort the random strings of the alphabet you entered in ascending order of ASCII code and print them out.
example
Original string: bfGageGaheifhalenbcuafuhneixlDbfuhflfi0
After sorting: DGGaaaaaabbbceeeffffffffghhhhhhiiilllnnuux
Ends and prints when a number is entered.
I wrote the following myself, but only the last line feed is displayed in the output.
Please correct the code.
Additional
We have corrected the obvious mistake at this time.
You said the initialization of head.next=&tail; would not work, but how exactly should I fix it?
#include<stdio.h>
# include <stdlib.h>
structure ascii {
int num;
structure ascii* next;
};
int main() {
inti;
charc;
structure ascii head, tail;
head.next=&tail;
tail.num = 10000;
for(;(i=getchar())<48||i>57;){
if(i==10){
continue;
} else {
structure ascii*p=(struct ascii*) malloc(size of(struct ascii));
p->num=i;
structure ascii*w;
for(w=&head;w->next!=&tail;){
if(w->next->num<p->num){
w = w - > next;
if(w->next->num>=p->num){
p->next=w->next;
w->next=p;
break;
}
} else {
p->next=w->next;
w->next=p;
break;
}
}
}
}
structure ascii*q;
for(q=&head;q->next!=&tail;q=q->next){
c=q->num;
printf("%c",c);
}
printf("\n");
return 0;
}
if(i=10)
is infinitely wrong. In , one equal sign =
is substituted and two equal signs ==
if you want a comparison.
The following is a summary of the comments and answers so far.
I wrote comments about the points and changes.
#include<stdio.h>
# include <stdlib.h>
structure ascii {
int num;
structure ascii* next;
};
int main() {
/* All of the following structural data are initialized @metropolis-related */
structure ascii tail = {INT_MAX, NULL}, head = {-1, & tail};
/* i is only used in for, so move the definition*/
for(inti=0;(i=getchar())<48||i>57;){/*@metropolis pointed out &->||*/
If(i==10){/*@774RR pointed out=->==*/
continue;
}
else{
structure ascii*p=(struct ascii*) malloc(size of(struct ascii));
p->num=i;
/*
Since w is used only in for, move the definition.
Loop termination determination is NULL pointer, pointed out by @metropolis w->next!=&tail
Moving to the next structure moves to the termination processing of the loop control part.w = w - > next
*/
for(struct ascii*w=&head;w->next!=NULL;w=w->next){
/* In-loop processing is sufficient*/
if(w->next->num>=p->num){
p->next=w->next;
w->next=p;
break;
}
}
}
}
/*
Since q is used only in for, move the definition.
Initialize with head.next as the first printf will be invalid data if & head is first.
Loop termination determination is a NULL pointer, similar to @metropolis pointed out related w->next!=&tail
*/
for(struct ascii*q=head.next;q->next!=NULL;q=q->next){
/* c is used only in for, so move definition*/
charc=q->num;
printf("%c",c);
}
/* It would be even better if there is consideration for displaying the guide when there is no valid input data*/
printf("\n");
return 0;
}
Your code is
head
: dummy elements, not included in the actual list
tail
: watchdog element, simplifying termination checks by holding a value greater than the actual value (1000
)
Create a one-way link list under these conditions, and keep the new elements in ascending order by always inserting them in ascending order.
Initial State
head tail
+-----+-----+ +-----+-----+
| ???| & tail | - > | 1000 | ???|
+-----+-----+ +-----+-----+
After loading the first bfG
head[0][1][2]tail
+-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
| ???|&[0]|->|'G'|&[1]|->|'b'|&[2]|->|'f'|&tail|->|1000|???|
+-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
(This structure is common for linked lists.Therefore, head.next=&tail;
initialization does not work.)
If you read 'a' in this state, you must add it as a new element before [1], that is, after [0].
What's wrong with your code is that when the list is empty, that is, immediately after head.next=&tail;
, the for statement for (w=&head;w->next!=&tail;){...}
is never executed, the first code is the same.
That's what happened because I forced two types of processing into the for statement.It's better to separate the two.
//Find where to insert the new element
// "The location of the new element" is slightly different from the output of all elements in the second half because one element in front of the link is required.
structure ascii*w;
for(w=&head;w->next!=&tail;w=w->next){
if(w->next->num>=p->num){
break;
}
}
// Add a new element to that location
p->next=w->next;
w->next=p;
This means that at the end of the for statement, w
points to where you want to add the new element ([0] in the example above).Also, if the list is empty, w
points to the first dummy element, so you can add the first element in exactly the same way.
Also, there is a problem with the part where the list contents are printed in your code.As I wrote earlier, head
is a dummy element in your data structure, and if next
is a loop (q->next!=&tail
), the last element in the list is not output.
You have to write that part like this.
for(q=head.next;q!=&tail;q=q->next){
c=q->num;
printf("%c",c);
}
Just to be sure, I'll show you the entire code after both modifications have been taken in.
#include<stdio.h>
# include <stdlib.h>
structure ascii {
int num;
structure ascii* next;
};
int main() {
inti;
charc;
structure ascii head, tail;
head.next=&tail;
tail.num = 10000;
for(;(i=getchar())<48||i>57;){
if(i==10){
continue;
} else{
structure ascii*p=(struct ascii*) malloc(size of(struct ascii));
p->num=i;
// Find the location to insert the new element
// "The location of the new element" is slightly different from the output of all elements in the second half because one element in front of the link is required.
structure ascii*w;
for(w=&head;w->next!=&tail;w=w->next){
if(w->next->num>=p->num){
break;
}
}
// Add a new element to that location
p->next=w->next;
w->next=p;
}
}
structure ascii*q;
// All elements output
// Since it starts with a dummy element (`q=head.next`), it is part of the list until the guard dog element, so `q!=&tail`
for(q=head.next;q!=&tail;q=q->next){
c=q->num;
printf("%c",c);
}
printf("\n");
return 0;
}
(By the way, I rarely see 2 spindents in C.)C, which places spaces on both sides of the operator, does not follow the very popular space placement rules, making the code very difficult to read.Maybe the teacher(?) of the class(?) will do such coding, but it would be better to adjust it to the normal C community style as much as possible.)
In order to show where the modifications are essentially necessary, we left variable declarations in the original code, but as Kunif replied, the modern C coding style is to declare control variables in the for statement as much as possible.
In addition, link lists often require special processing, such as empty, leading, or trailing.Even in an environment where you can't use a debugger that can perform step execution traces, I think you can trace the movement in the brain, so I think it would be good if you could get into the habit of checking what happens in such a special case.
© 2024 OneMinuteCode. All rights reserved.