只是做了一些关于它的编辑,我想你说的什么,但它没有工作,所以我尝试的东西我有点更熟悉,但它似乎没有正常工作。 它古怪的打印信息,然后崩溃。对于〔实施例:当我开关输入9-8-7-6-5-4-3-2-1然后0打印,其打印还给我0-0-0-9- 1-2-3-4-5-6-7-8,然后崩溃? 当我称输入1-2-3-4-5-6-7-8-9然后0打印,其打印还给我0-0-0-1-2-3-4-5-6-7- 8-9,然后崩溃。
#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 freeList(struct listNode *Head, int x);
//main
int main(){
struct listNode Head = {0, NULL};
int x = 1;
int ret = 0;
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);
}
ret = printList(&Head);
}
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;
}
}
int printList(struct listNode * Head){
struct listNode *current = Head;
while (Head != NULL){
printf("%d \n", *current);
current = current->next;
}
}