鱼C论坛

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

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

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

  4. void showmovies(Item item);

  5. int main()
  6. {
  7.     List movies;
  8.     Item temp;

  9.     InitializeList(&movies);

  10.     if(ListIsFull(movies))
  11.     {
  12.         fprintf(stderr, "No memory available! Bye!\n");
  13.         exit(EXIT_SUCCESS);
  14.     }

  15.     puts("Enter first movie title: ");
  16.     while(gets(temp.title) != NULL && temp.title[0] != '\0')
  17.     {
  18.         puts("Enter your rating <0 - 10>: ");
  19.         scanf("%d", &temp.rating);
  20.         while(getchar() != '\n')
  21.             continue;
  22.         if(!AddItem(temp, &movies))
  23.         {
  24.             fprintf(stderr, "Problem allocating memory.\n");
  25.             break;
  26.         }
  27.         if(ListIsFull(movies))
  28.         {
  29.             puts("The list is now full");
  30.             break;
  31.         }
  32.         puts("Enter next movie title (empty title to stop): ");
  33.     }
  34.     if(ListIsEmpty(movies))
  35.         printf("No data entered");
  36.     else
  37.     {
  38.         printf("Here is the movie list: \n");
  39.         Traverse(movies, showmovies);
  40.     }
  41.     printf("You enter %d movies.\n", ListItemCount(movies));
  42.     EmptyTheList(&movies);
  43.     puts("Bye!");

  44.     return 0;
  45. }

  46. void showmovies(Item item)
  47. {
  48.     printf("Movies: %s Rating :%d\n", item.title, item.rating);
  49. }
复制代码


//list.h
  1. #ifndef LIST_H_
  2. #define LIST_H_

  3. #include <stdbool.h>

  4. #define TSIZE 45
  5. typedef struct film
  6. {
  7.     char title[TSIZE];
  8.     int rating;
  9. }Item;

  10. typedef struct node
  11. {
  12.     Item item;
  13.     struct node *next;
  14. } Node;

  15. typedef Node *List;

  16. void InitializeList(List *plist);
  17. bool ListIsEmpty(const List *plist);
  18. bool ListIsFull(const List *plist);
  19. unsigned ListItemCount(const List *plist);
  20. bool AddItem(Item item, List *plist);
  21. void Traverse(const List *plist, void (*pfun)(Item item));
  22. void EmptyTheList(List *plist);

  23. #endif
复制代码


list.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

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

  5. void InitializeList(List *plist)
  6. {
  7.     *plist = NULL;
  8. }

  9. bool ListIsEmpty(const List *plist)
  10. {
  11.     if(*plist == NULL)
  12.         return true;
  13.     else
  14.         return false;
  15. }

  16. bool ListIsFull(const List *plist)
  17. {
  18.     Node *pt;
  19.     bool full;

  20.     pt = (Node *)malloc(sizeof(Node));
  21.     if(pt == NULL)
  22.         full = true;
  23.     else
  24.         full = false;
  25.     free(pt);
  26.     return full;
  27. }

  28. unsigned ListItemCount(const List *plist)
  29. {
  30.     unsigned count = 0;
  31.     Node *pnode = *plist;

  32.     while(pnode != NULL)
  33.     {
  34.         ++count;
  35.         pnode = pnode->next;
  36.     }

  37.     return count;
  38. }

  39. bool AddItem(Item item, List *plist)
  40. {
  41.     Node *pnew;
  42.     Node *scan = *plist;

  43.     pnew = (Node *)malloc(sizeof(Node));
  44.     if(pnew == NULL)
  45.         return false;

  46.     CopyToNode(item, pnew);
  47.     pnew->next = NULL;
  48.     if(scan == NULL)
  49.         *plist = pnew;//无头指针!第一个Node就存有效数据了
  50.     else
  51.     {
  52.         while(scan->next != NULL)
  53.             scan = scan->next;
  54.         scan->next = pnew;
  55.     }
  56.     return true;
  57. }

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

  67. void EmptyTheList(List *plist)
  68. {
  69.     Node *psave;
  70.     while(*plist != NULL)
  71.     {
  72.         psave = (*plist)->next;
  73.         free(*plist);
  74.         *plist = psave;
  75.     }
  76. }

  77. static void CopyToNode(Item item, Node *pnode)
  78. {
  79.     pnode->item = item;
  80. }
复制代码
希望大神指点一二  这是数据结构的开端。

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-11-11 19:41:21 | 显示全部楼层
段错误一般是指针越界了,好好检查下指针
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-8-27 16:05:52 | 显示全部楼层
帮顶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 01:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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