c - 为什么我不能在 C 中使用连续实现将元素插入到列表中?
问题描述
我确实在 C 中实现了一个列表,如下所示。问题是当我尝试使用InsertList()
函数插入元素时,它返回“尝试插入到不在列表中的位置”消息。但相反,我希望列表在上述位置插入元素。为了实现它,我应该如何修改我的代码?
#include <stdlib.h>
#define MAX 20
#define EMPTY 0
#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=EMPTY;
}
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;
printf("The entered element is: %d\n",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\n", 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-1];
printf("Retrieved element is: %d\n", x);
}
}
void Traverse(List *l)
{
if(IsListEmpty(l))
printf("Try to traverse a empty list\n");
else
printf("The elements of this list are: ");
{
for(int i=0;i<ListSize(l);i++)
{
printf("%d ", l->entry[i]);
}
}
}
我的main()
功能如下:
{
List l;
CreateList(&l);
Traverse(&l);
DeleteList(2,&l);
//InsertLast(5,&l);
//InsertLast(6,&l);
InsertList(1,3,&l);
InsertList(2,2,&l);
InsertList(3,1,&l);
RetrieveList(1,&l);
DeleteList(2,&l);
int results = ListSize(&l);
printf("The size of the list%d\n",results);
Traverse(&l);
return 0;
}
解决方案
一开始。您的列表为空。因此,ListSize
返回 0。您尝试插入绝对在列表边界之外的位置 1。尝试在位置 0 插入。这是有道理的,因为列表是空的,位置 0 没有设置。为什么要在位置 1 插入?
您的代码中还有一些错误。比如插入时,要先移位内容,再设置新元素。您将其设置为 index (p-1)
。按照您的逻辑在位置 1 插入它可能会起作用,但会令人困惑。如果你在某个位置插入一些东西,它应该准确地插入那里。
您的代码可能如下所示
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]=x;
printf("The entered element is: %d\n",x);
l->count++;
}
}
我看到您有一个额外的功能InsertLast
,但InsertList
也可以成功附加一个元素。您可以将其保留原样或禁止在插入函数中附加。
所以,调试愉快!
推荐阅读
- wysiwyg - 所见即所得编辑器中的图像缩略图
- python - plt.figure 不调整图形大小
- azure - 将 --isolation 进程添加到 azure acr build 命令
- dns - 使用 $dig 获取为 DNS 区域提供服务的所有名称服务器
- nrf52 - OpenThread API:未定义对 otThreadGetMaxAllowedChildren() 的引用
- javascript - Javascript - 从对象数组中读取值的复选框
- sql-server - 我无法从 sql server 2014 连接到我的数据库或主机
- html - 有没有办法让 3 个表单元素排成一行?
- javascript - 带有分页的下一个和上一个按钮功能
- python - 如何在当前选项卡的文本小部件中插入文本?