读书人

这个链表小程序用子函数表述为什么不成

发布时间: 2013-08-13 16:43:28 作者: rapoo

这个链表小程序用子函数表达为什么不成功?
我先在main函数里建立了链表,然后尝试用子函数来编写,总是不成功。一定在错误,不知怎么改?请指点。
下面是在main中建立链表的程序:

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "stdlib.h"

#define SIZE 40

struct film
{
char title[SIZE];
int rating;
};

typedef struct film Item;

typedef struct node
{
Item item;
struct node * next;
}Node;

int main(void)
{
Node *head = NULL, *current, *prev;
Item item;

puts("Enter first movie");
while(gets(item.title)!=NULL && item.title[0]!='\0')
{

puts("Enter the rating <0-10>:");
scanf("%d", &item.rating);
while(getchar() != '\n')
{
continue;
}

current = (Node *)malloc(sizeof(Node));
if(head == NULL)
{
head = current;
}
else
{
prev->next = current;
}
current->item = item;
current->next = NULL;
prev = current;
puts("Enter another movie:");
}

current = head;
while(current != NULL)
{
printf("%s %d\n", current->item.title, current->item.rating);
current = current->next;
}
putchar('\n');
return 0;
}


下面是我想在子函数中编写一部分,但是不成功

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "stdlib.h"

#define SIZE 40

struct film
{
char title[SIZE];
int rating;
};

typedef struct film Item;

typedef struct node
{
Item item;
struct node * next;
}Node;

Node * CreateList(Item item);

int main(void)
{
Node *head = NULL, *current;
Item item;

puts("Enter first movie");
while(gets(item.title)!=NULL && item.title[0]!='\0')
{

puts("Enter the rating <0-10>:");
scanf("%d", &item.rating);
while(getchar() != '\n')
{
continue;
}
head = CreateList(item);
puts("Enter another movie:");
}

current = head;
while(current != NULL)
{
printf("%s %d\n", current->item.title, current->item.rating);


current = current->next;
}
putchar('\n');
return 0;
}

Node * CreateList(Item item)
{
Node *head, *current, *prev;
current = (Node *)malloc(sizeof(Node));
current->item = item;
current->next = NULL;
if(head == NULL)
{
head = current;
}
else
{
prev->next = current;
}
prev = current;

return head;
}


[解决办法]

Node * CreateList(Item item)
{
// Node *head, *current, *prev; //这个时候是野指针哦!不是为NULL的
Node *head = NULL;
Node *current = NULL;
Node *prev = NULL;
current = (Node *)malloc(sizeof(Node));
current->item = item;
current->next = NULL;
if(head == NULL)
{
head = current;
}
else
{
prev->next = current;
}
prev = current;

return head;
}


[解决办法]


Node * CreateList(Item item)
{
static Node *head = NULL;
static Node *current = NULL;
static Node *prev = NULL;
current = (Node *)malloc(sizeof(Node));
current->item = item;
current->next = NULL;
if(head == NULL)
{


head = current;
}
else
{
prev->next = current;
}
prev = current;

return head;
}


[解决办法]
Node * CreateList(Item item)
{
// Node *head, *current, *prev;
Node *head = NULL;
Node *current = NULL;
Node *prev = NULL;
current = (Node *)malloc(sizeof(Node));
current->item = item;
current->next = NULL;
if(head == NULL)
{
head = current;
}
else
{
prev->next = current;
}
prev = current;
current = (Node *)malloc(sizeof(Node));
return head;
}

[解决办法]
引用:
我先在main函数里建立了链表,然后尝试用子函数来编写,总是不成功。一定在错误,不知怎么改?请指点。
下面是在main中建立链表的程序:

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "stdlib.h"

#define SIZE 40

struct film
{
char title[SIZE];
int rating;
};

typedef struct film Item;

typedef struct node
{
Item item;
struct node * next;
}Node;

int main(void)
{
Node *head = NULL, *current, *prev;
Item item;

puts("Enter first movie");
while(gets(item.title)!=NULL && item.title[0]!='\0')
{

puts("Enter the rating <0-10>:");
scanf("%d", &item.rating);
while(getchar() != '\n')
{
continue;
}

current = (Node *)malloc(sizeof(Node));
if(head == NULL)


{
head = current;
}
else
{
prev->next = current;
}
current->item = item;
current->next = NULL;
prev = current;
puts("Enter another movie:");
}

current = head;
while(current != NULL)
{
printf("%s %d\n", current->item.title, current->item.rating);
current = current->next;
}
putchar('\n');
return 0;
}



下面是我想在子函数中编写一部分,但是不成功

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "stdlib.h"

#define SIZE 40

struct film
{
char title[SIZE];
int rating;
};

typedef struct film Item;

typedef struct node
{
Item item;
struct node * next;
}Node;

Node * CreateList(Item item);

int main(void)
{
Node *head = NULL, *current;
Item item;

puts("Enter first movie");
while(gets(item.title)!=NULL && item.title[0]!='\0')
{

puts("Enter the rating <0-10>:");
scanf("%d", &item.rating);
while(getchar() != '\n')
{
continue;
}
head = CreateList(item);
puts("Enter another movie:");
}

current = head;
while(current != NULL)
{
printf("%s %d\n", current->item.title, current->item.rating);
current = current->next;
}
putchar('\n');
return 0;
}

Node * CreateList(Item item)
{
Node *head, *current, *prev;
current = (Node *)malloc(sizeof(Node));
current->item = item;
current->next = NULL;
if(head == NULL)
{
head = current;
}
else
{
prev->next = current;
}
prev = current;

return head;
}


在createlist函数中,每次都会将head设置为NULL,所以只会执行if判断中的 head = current;
然后返回head,所以每次你的程序只会记录最后一个节点的值。
在这种情况下,最好是将head变量设置成全局的,还有就是你的prev变量需要设置成静态的,否则,
在每次执行createlist函数的时候,首先会将其设置为NULL,然后执行else中的 prev->next = current;
这也是不对的,因为当前的prev的值为NULL,这样做的话会发生段错误。
骚年,建议你重新考虑下你的代码设计,或者去看看别人是怎么实现的

读书人网 >C语言

热点推荐