【发布时间】:2012-11-05 19:31:45
【问题描述】:
我正在做我的家庭作业,这就是我所得到的。我现在需要知道如何打印出已经输入到列表中的信息。并且我需要重新配置 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));
标签: c