Search code examples
clistdata-structurescontiguous

Why I couldn't insert elements into the list with contiguous implementation in C?


I did implement a list in C as follows. The problem is when I try to insert elements using InsertList() function it returns "Try to insert to a position not in list" message. But instead I wanted the list to insert the elements in the said positions. To make it happen how I should modify my code?.

#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]);
        }
    }
}

My main() function is as follows:

{
    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;
}

Solution

  • At the beginning. Your list is empty. So, ListSize returns 0. You tries to insert to the position 1 which is definitely outside of the list boundaries. Try to insert at position 0. It makes sense as the list is empty, position 0 is not set. Why to insert at position 1?

    There is some more bugs in your code. For example, when you insert, you want to shift the content first and then set the new element. You set it at index (p-1). Following your logic by inserting at position 1 it may work but it is confusing. If you insert something at certain position, it should be inserted exactly there.

    Your code may look like this

     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++;                                                                                                                                                                                                                                                                             
         }                                                                                                                                                                                                                                                                                       
    }  
    

    I saw that you have an extra function InsertLast but the InsertList can also successfully append an element. It is up to you to leave it as is or forbid appending in the insert function.

    So, happy debugging!