2012-11-05 51 views
1

我正在做我的家庭作业,而且这是我已经得到的。我现在需要知道如何将输入到列表中的信息打印出来。我需要重新配置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; 
     } 
} 
+0

普罗蒂普:'的malloc(的sizeof(* listNode));' – Wug

+0

[打印有序链表(HTTP的可能重复://计算器。com/questions/13240332/print-an-ordered-linked-list) – Joe

回答

3

Header->next为NULL,当你开始,当你添加一个元素你改变电流为Header

current = Header; 
write(current->next !=NULL); 
// set current to be Header->next (which is NULL) 
current = current->next; 
// dereference null 
current->next = newNode; 

相反,新的元素添加到末尾:

current = Header; 
while (current->next != NULL) 
    current = current->next; 
current->next = newNode; 
0
current = Header; 
write(current->next !=NULL); 
current = current->next; 
current->next = newNode; 

您正尝试访问current-> next,但没有分配它。而且你实际上并不是在寻找正确的位置来将它插入链表(从你的问题来看,它听起来像是应该排序的)。

试着这么做:

current = Header; 
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; 
} 
+0

我试过了,它似乎有效!我用我的新问题编辑了原文。 – user1801067

+0

我为你的其他问题添加了一个答案,以向你展示你在做什么错误:http://stackoverflow.com/questions/13240332/print-an-ordered-linked-list/13242728#13242728 –

0

,我们在您IMPL三个问题:

  1. 你应该在最后追加新的号码,所以你应该有两个指针到列表中:一个指向头部,另一个尾部(你也可以添加在列表顶部,那么你只需要一个指针,但是然后列表是反向排序的)

  2. poi中断链代码是错误的,我想你想尝试在你的头节点后插入新的元素,所以你的代码应该写成:

    current = Header-> next; //节点Header-> next当前指向

    Header-> next = newNode; //让标题 - >下一个指向新电子元件

    newNode-> next = current; //让新ele的下一个点指向老top ele

  3. header-node在某种程度上是特殊和无用的,你应该处理一个空列表作为特殊情况,并且标题应该初始为0,并且insertnode会像这样工作:

    如果(头== 0)

    header=newNode 
    

    其他

    //做这样的东西写在2

所有这一切都可以肯定地以不同的方式进行,但是这是一个常见的形式给出该列表的问题... (嗯,不知何故代码的4个前导空格不工作?)

0

得到它的工作,只需要弄清楚如何使的printList和空闲列表功能

#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 *Head, 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; 
     } 
}