我正在做我的家庭作業,而且這是我已經得到的。我現在需要知道如何將輸入到列表中的信息打印出來。我需要重新配置insertNode函數以將列表從最小到最大排序。已命令鏈接列表打印
#include <stdio.h>
#include <stdlib.h>
struct listNode{
int data; //ordered field
struct listNode *next;
};
//prototypes
void insertNode(struct listNode *Head, int x);
int printList(struct listNode *Head, int x);
int freeList(struct listNode *Header, int x);
//main
int main(){
struct listNode Head = {0, NULL};
int x = 1;
printf("This program will create an odered linked list of numbers greater"
" than 0 until the user inputs 0 or a negative number.\n");
while (x > 0){
printf("Please input a value to store into the list.\n");
scanf("%d", &x);
insertNode(&Head, x);
}
printf("Program terminated.\n");
system("PAUSE");
}
void insertNode(struct listNode * Head, int x){
struct listNode *newNode, *current;
newNode = malloc(sizeof(struct listNode));
newNode->data = x;
newNode->next = NULL;
current = Head;
while (current->next != NULL && current->data < x)
{
current = current->next;
}
if(current->next == NULL){
current->next = newNode;
}
else{
newNode->next = current->next;
current->next = newNode;
}
}
普羅蒂普:'的malloc(的sizeof(* listNode));' – Wug
[打印有序鏈表(HTTP的可能重複://計算器。com/questions/13240332/print-an-ordered-linked-list) – Joe