[英]Ascending order Linked List in C
I have been tasked with implementing an insert function which will be used in a project. 我的任务是实现将在项目中使用的插入功能。 If user input is 1 2 3 4, the desired output of the print statement would be 1, 2, 3, 4. Currently, my print statement is returning 4, 3, 2, 1 but I believe this to be correct.
如果用户输入为1 2 3 4,则打印语句的期望输出将为1、2、3、4。当前,我的打印语句返回4、3、2、1,但我认为这是正确的。 I think my issues lie within my input function (which is nested within a while loop to get user input).
我认为我的问题出在我的输入函数之内(该函数嵌套在while循环中以获取用户输入)。 This is using C
这是使用C
Any help would be appreciated. 任何帮助,将不胜感激。
struct set {
int data;
struct set* next_p;
};
struct set* getInput( struct set* head_p, int val ) {
struct set* temp;
temp->data = val;
temp->next_p = head_p;
return temp;
} /* getInput */
struct set* makeSet( struct set* head_p ) {
int val;
printf( "Please enter a positive integer, or a negative to stop: \n" );
scanf("%d", &val);
while ( 100 ) {
head_p = getInput( head_p, val );
scanf("%d", &val);
}
return head_p;
}
struct set* make_aSet(int val ) {
struct set* temp;
if((temp = malloc(sizeof(*temp))) != NULL){
temp->data = val;
temp->next_p = NULL;
} else {
fprintf(stderr, "can't make new a set\n");
}
return temp;
}
struct set* makeSet(void) {
struct set dummy_head, *current = &dummy_head;
int val;
printf( "Please enter a positive integer, or a negative to stop: \n" );
while (1==scanf("%d", &val) && val >= 0 ) {
current->next_p = make_aSet( val );
current = current->next_p;
}
return dummy_head.next_p;
}
Sorry mate was busy but hey i fixed your code.I already mentioned the problem was that you were appending the newNode before the head. 对不起,队友很忙,但是嘿,我修复了您的代码。我已经提到过问题是您在头之前附加了newNode。
#include <stdio.h>
#include <stdlib.h>
struct set {
int data;
struct set* next_p;
};
struct set* getInput( struct set* ptr, int val )//appends a newNode to ptr which is the last node of Linked list
{
struct set* temp=NULL;
temp = (struct set*)malloc(sizeof(struct set));
temp->data = val;
temp->next_p = NULL;
if(ptr!=NULL)//if Linked list has been created,i.e.,only if we have a head run this
ptr->next_p=temp;
return temp;
} /* getInput */
struct set* makeSet( struct set* head_p ) {
int val;
struct set* ptr=head_p;
printf( "Please enter a positive integer, or a negative to stop: \n" );
while (1) {
scanf("%d", &val);
if(val>=0)
ptr=getInput( ptr, val );//appends only value>=0 to linked list else breaks from the infinite loop
else
break;
if(head_p==NULL)//To set the head of the linked List! True only for the 1st node in the linked list
head_p=ptr;
}
return head_p;
}
void display(struct set* head_p)
{
if(head_p==NULL)
printf("\nno List in Memory\n"); //
else
{
struct set* ptr=head_p;
printf("\n the linked list is\nHead");
while(ptr!=NULL)
{
printf("-->%d",ptr->data);
ptr=ptr->next_p;
}
}
}
int main()
{
struct set* head_p=NULL;
head_p=makeSet((head_p));//returns the head of the linked list created
display(head_p);//displays the linked list
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.