Prerequisite
There are some problems with the list structure of the c language that just don't work.
What do you want to do
There are no errors in the program itself, but the output is displayed in reverse order, and it seems to be inserted in reverse order of the order in which you want to insert.Please point out the parts that should be corrected so that the output is correct.
Affected Source Code
#include<stdio.h>
# include <stdlib.h>
US>structure node {
care element;
structure node*next;
};
structure node*initlist(){
structure node*n;
n = (struct node*) malloc (size of (struct node);
n - > next = NULL;
return n;
}
void insert(struct node*p, charx){
structure node*n;
n=(struct node*)malloc(sizeof(struct node)));
n->element=x;
n->next=p->next;
p->next=n;
}
void printlist(struct node*p){
if(p->next==NULL){
putchar('\n');
} else {
p=p->next;
putchar(p->element);
printlist(p);
putchar(p->element);
}
}
int main(intargc, char*argv[]){
structure node*list, *head;
char*p;
if(argc<2)
exit(-1);
list = initlist();
p = argv[1];
for (;*p;p++)
insert(list,*p);
printlist(list);
printf("\n");
for(;list;){
head=list;
list = list->next;
free(head);
}
return 0;
}
Correct Output
[ ]$ gcc-Wall-std = c99-o q5-1 q5-1.c
[ ] $./q5-1abcde
abcde
edcba
My output
[ ]$ gcc-Wall-std = c99-o q5-1 q5-1.c
[ ] $./q5-1abcde
edcba
abcde
I learned from another question site and changed the insert function as follows, and the output was successful.
void insert (struct node*p, char x) {
structure node*n;
n=(struct node*)malloc(sizeof(struct node)));
n->element=x;
n - > next = NULL;
while(p->next!=NULL)
p=p->next;
p->next=n;
}
540 Unable to install versioned in Google Colab
545 PHP ssh2_scp_send fails to send files as intended
683 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
525 Uncaught (inpromise) Error on Electron: An object could not be cloned
545 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.