鱼C论坛

 找回密码
 立即注册
查看: 2249|回复: 2

链表中得段错误 找了很久找不出错误

[复制链接]
发表于 2012-11-11 19:11:57 | 显示全部楼层 |阅读模式
10鱼币
//main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"

void showmovies(Item item);

int main()
{
    List movies;
    Item temp;

    InitializeList(&movies);

    if(ListIsFull(movies))
    {
        fprintf(stderr, "No memory available! Bye!\n");
        exit(EXIT_SUCCESS);
    }

    puts("Enter first movie title: ");
    while(gets(temp.title) != NULL && temp.title[0] != '\0')
    {
        puts("Enter your rating <0 - 10>: ");
        scanf("%d", &temp.rating);
        while(getchar() != '\n')
            continue;
        if(!AddItem(temp, &movies))
        {
            fprintf(stderr, "Problem allocating memory.\n");
            break;
        }
        if(ListIsFull(movies))
        {
            puts("The list is now full");
            break;
        }
        puts("Enter next movie title (empty title to stop): ");
    }
    if(ListIsEmpty(movies))
        printf("No data entered");
    else
    {
        printf("Here is the movie list: \n");
        Traverse(movies, showmovies);
    }
    printf("You enter %d movies.\n", ListItemCount(movies));
    EmptyTheList(&movies);
    puts("Bye!");

    return 0;
}

void showmovies(Item item)
{
    printf("Movies: %s Rating :%d\n", item.title, item.rating);
}

//list.h
#ifndef LIST_H_
#define LIST_H_

#include <stdbool.h>

#define TSIZE 45
typedef struct film
{
    char title[TSIZE];
    int rating;
}Item;

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

typedef Node *List;

void InitializeList(List *plist);
bool ListIsEmpty(const List *plist);
bool ListIsFull(const List *plist);
unsigned ListItemCount(const List *plist);
bool AddItem(Item item, List *plist);
void Traverse(const List *plist, void (*pfun)(Item item));
void EmptyTheList(List *plist);

#endif

list.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"

static void CopyToNode(Item item, Node *pnode);

void InitializeList(List *plist)
{
    *plist = NULL;
}

bool ListIsEmpty(const List *plist)
{
    if(*plist == NULL)
        return true;
    else
        return false;
}

bool ListIsFull(const List *plist)
{
    Node *pt;
    bool full;

    pt = (Node *)malloc(sizeof(Node));
    if(pt == NULL)
        full = true;
    else
        full = false;
    free(pt);
    return full;
}

unsigned ListItemCount(const List *plist)
{
    unsigned count = 0;
    Node *pnode = *plist;

    while(pnode != NULL)
    {
        ++count;
        pnode = pnode->next;
    }

    return count;
}

bool AddItem(Item item, List *plist)
{
    Node *pnew;
    Node *scan = *plist;

    pnew = (Node *)malloc(sizeof(Node));
    if(pnew == NULL)
        return false;

    CopyToNode(item, pnew);
    pnew->next = NULL;
    if(scan == NULL)
        *plist = pnew;//无头指针!第一个Node就存有效数据了
    else
    {
        while(scan->next != NULL)
            scan = scan->next;
        scan->next = pnew;
    }
    return true;
}

void Traverse(const List *plist, void (*pfun)(Item item))
{
    Node *pnode = *plist;
    while(pnode != NULL)
    {
        pfun(pnode->item);//经过调试  这里使程序崩掉!  但是一直找不出错误在哪儿啊  是段错误 segmentation fault
        pnode = pnode->next;
    }
}

void EmptyTheList(List *plist)
{
    Node *psave;
    while(*plist != NULL)
    {
        psave = (*plist)->next;
        free(*plist);
        *plist = psave;
    }
}

static void CopyToNode(Item item, Node *pnode)
{
    pnode->item = item;
}
希望大神指点一二  这是数据结构的开端。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-11-11 19:41:21 | 显示全部楼层
段错误一般是指针越界了,好好检查下指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-27 16:05:52 | 显示全部楼层
帮顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-26 09:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表