Когда я попытался реализовать список следующим образом в C. Моя программа терпит крах в середине пути. Я думаю, что есть проблема при передаче значений в функцию InsertList в Main (). Может кто-нибудь объяснить, в чем проблема в моей основной функции. Верны ли мои функции 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;
}