2011-12-01 84 views
1

我正在编写的代码,从用户输入的整数,并创建一个链表,然后打印出列表。整数应该放在列表的前面。从C键盘输入链接列表

我需要一点帮助我不知道如何让打印出的数字。
这里是我的代码:

Node.h

#include <stdio.h> 
#include <stdlib.h> 

#ifndef _nodes 
#define _nodes 

typedef struct node_s { 
     int d; 
     struct node_s *next; 
} node1; 

node1 list(int d); 
node1 addbegin(int d, node1 *head); 
node1 print(node1 *head); 

#endif 

node.c

#include "node.h" 
#include <stdio.h> 

node1 *list_nodes(int d){ 
    node1 *node; 
    node=(node1*)malloc(sizeof(node1)); 
    node->d=d; 
    node->next=NULL; 
    return(node); 
} 

node1 init(node1 *head){ 
    head->next=NULL; 
} 

node1 addbegin_nodes(node1 *head, int d){ 
    node1 *newnode; 
    newnode=(node1*)malloc(sizeof(node1)); 
    newnode=list_nodes(d); 
    head->next=newnode; 
    return(newnode); 
} 

node1 print_nodes(node1 *head){ 
    node1 *temp; 
    temp=(node1*)malloc(sizeof(node1)); 
    for(temp=head->next; 
     temp; 
    temp=temp->next) 
      printf("%d", temp->d); 
    return (d); 
} 

MAIN.C

#include <stdlib.h> 
#include <stdio.h> 
#include "node.h" 

int main(int argc, char* argv[]) { 
    node1 *head; 
    node1 *start; 
    int num; 

    printf("Please enter integers: "); 
    while(scanf("%d", &num)!=EOF){ 
      head=(node1*)malloc(sizeof(node1)); 
      list(head); 
      int i=0; 
      for(i=0;i>=0;i--){ 
        addbegin(head, num); 
        print(head); 
      } 
      printf("\n"); 
      print(head); 
    } 
    return 0; 
} 
+0

作业问题 – Almo

+0

EOF是一个特殊字符。你是否退出循环? – BlackBear

+0

是的,这是一个重要的问题,我一直在为它工作了5小时,自杀!所以我最终决定寻求帮助。 EOF是文件的结尾。 – Cka91405

回答

0

更改print_nodes到

//recursive version 
void print_nodes(node1 *head) 
{ 
    if (head != NULL) 
    { 
     printf("%d\n", head->d); 
     print_nodes(head->next); 
    } 
} 

或者

// normal version 
void print_nodes(node1 *head) 
{ 
    node1 *temp = head; 

    while (temp != 0) 
    { 
     printf("%d\n", temp->d); 
     temp = temp->next; 
    } 
} 

我不知道现在为什么这些方法应该返回任何东西,请澄清意见。

+0

当我试着说它说node.c:24:警告:控制达到非无效函数的结尾 – Cka91405

+0

第24行是在你的代码中,我不知道在我的函数上的点。这也是警告,不是一个错误。代码是否工作 ?你试过两个版本吗? –

0
node1 init(node1 *head){ 
    head->next=NULL; 
} 

被定义为返回node1,但似乎没有返回语句。这可能是你的问题的一部分?