【问题标题】:Why my program is getting crashed while implementing the list in C?为什么我的程序在 C 中实现列表时会崩溃?
【发布时间】:2020-01-08 17:14:41
【问题描述】:

当我尝试在 C 中实现如下列表时。我的程序在中途崩溃了。我认为将值传递给 Main() 中的 InsertList 函数时存在问题。有人可以解释我的主要功能有什么问题吗?我的 DeleteList、RetrieveList 函数是否正确?将参数传递给这些函数时是否有任何错误?

#include <stdlib.h>
#define MAX 20
#define EMPTY -1
#define FULL MAX-1

typedef enum {FALSE, TRUE} Boolean;
typedef char ListEntry;
typedef int Position;
typedef struct list
{
    int count;
    ListEntry entry[MAX];
}List;

void CreateList(List *l)
{
    l->count=-1;
}

Boolean IsListEmpty(List *l)
{
    return (l->count==EMPTY);
}

Boolean IsListFull(List *l)
{
    return (l->count==FULL);
}

int ListSize(List *l)
{
    return (l->count);
}

void InsertLast(ListEntry x,List *l)
{
    if(IsListFull(l))
        printf("Try to insert to full list\n");
    else
    {
        l->entry[l->count]=x;
        l->count++;
        printf("The entered element at last is %d\n", x);
    }
}

void InsertList(Position p,ListEntry x,List *l)
{
    if(IsListFull(l))
        printf("Try to insert to full list\n");
    else if(p<0 || p>ListSize(l))
        printf("Try to insert to a position not in list\n");
    else
    {
        int i;
        for(i=ListSize(l)-1;i>=p;i--)
            l->entry[i+1]=l->entry[i];
        //l->entry[p-1]=x;
        l->count++;
    }
}

void ReplaceList(Position p,ListEntry x,List *l)
{
    if(IsListFull(l))
        printf("Try to replace to full list\n");
    else if(p<0 || p>ListSize(l))
        printf("Try to replace a position not in list\n");
    else
        l->entry[p-1]=x;
}

void DeleteList(Position p,List *l)
{
    int i;
    if(IsListEmpty(l))
        printf("Try to delete from a empty list\n");
    else if(p<0 || p>ListSize(l))
        printf("Try to delete a position not in list\n");
    else
    {
        ListEntry x=l->entry[p-1];
        for(i=p-1;i<ListSize(l);i++)
            l->entry[i]=l->entry[i+1];
        l->count--;
        printf("Deleted element is %d", x);
    }
}

void RetrieveList(Position p,List *l)
{
    if(IsListEmpty(l))
        printf("Try to retrieve from a empty list\n");
    else if(p<0 || p>ListSize(l))
        printf("Try to retrieve a position not in list\n");
    else{
        ListEntry x=l->entry[p];
    printf("Retrieved element is: %d", x);
    }
}

我的main()函数如下:

int main()
{
    List l;
    CreateList(&l);
    DeleteList(2,&l);
    InsertLast(5,&l);
    InsertLast(6,&l);
    InsertList(1,3,&l);
    InsertList(2,2,&l);
    InsertList(3,1,&l);
    RetrieveList(3,&l);
    DeleteList(2,&l);

    return 0;
}

【问题讨论】:

  • 您使用count 成员作为访问entry 数组的索引。确保索引在 0..(MAX-1) 范围内。我建议将EMPTY 设置为零。并使用这个 EMPTY 作为CreateList 中的初始化值。
  • 尝试在CreateList(不是-1)中设置l-&gt;count = 0;。还将EMPTY 更改为0 并将FULL 更改为MAX
  • 对于空列表,条目数为零,因此count应该为零。
  • @harper 我的 ListSize() 函数正确吗?
  • 我不能告诉你ListSize() 是否正确。这取决于您的期望。 ListSize() 如果列表中没有项目,则返回 0,每个项目返回 1。这看起来很有道理。为了认真使用这组函数,我会为每个函数添加注释(但不是如何),以及调用者应该期待什么。这就是所谓的接口文档。如果实现满足接口文档中描述的行为,您可以说函数是正确的。如果ListSizeshall 提供有关最大值的信息。列表中可能的项目,不正确。

标签: c list data-structures


【解决方案1】:

当您调用CreateList 时,您设置了l-&gt;count = -1,然后当您调用InsertLast 时,您尝试访问l-&gt;entry[-1],因此您遇到了分段错误

#define EMPTY 0

void CreateList(List *l)
{
    l->count=EMPTY;
}

这将解决您的问题。

【讨论】:

  • 谢谢@MickaelB。我的 RetrieveList 函数是否正确?
  • @Nithur 是的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多