'Sequential storage structure of linear tables and their basic operations with C

I'm new to C, and in order to practice I found a task on the Internet:

Write C code that has functions that can: (1) Create a linear table; (2) Input data elements 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; 3) Delete the data element 5; (4) Display the data elements in the current linear table in turn. Assume that the number of data elements of the linear table does not exceed 100 in the worst case.

i.e. entering numbers from 1-10, I must lead them into a linear table while deleting 5 from the table.

I stopped there, when deleting 5 from the table, I have a problem that 5 is simply not present and vice versa is detected several times, and the remaining digits after 5 are not used.

123455555

I have a problem that 5 simply does not go away and vice versa is displayed several times. I have some guesses about where I may have made a mistake, but at the moment I am in a stupor and nothing new comes to my mind. I would like to ask you to take a look at my code and give your opinion on how to fix this error. I am not asking for a complete solution to my problem, but I need an idea that would help me and guide me, thanks! (I have indicated places where an error may occur)

#include <stdio.h>
#define MaxSize  100

typedef int DataType;

typedef struct
{
    DataType list[MaxSize];
    int size;
} SeqList;

void ListInitiate(SeqList *L)       /* Initialization of the linear table  */   
{
    L->size = 0;                    
}

int ListLength(SeqList L)    
{
    return L.size;
}

int ListInsert(SeqList *L, int i, DataType x) 
/* Inserting the data element value x before the position i */ 
{
    int j;
    if(L->size >= MaxSize)
    {
        printf("can not be inserted! \n"); 
        return 0;
    }
    else if(i < 0 || i > L->size )
    {
        printf("Parameter i is illegal! \n");
        return 0;
    }
    else
    {    
        for(j = L->size; j > i; j--) L->list[j+1] = L->list[j];    /* Preparing for insertion */   
        L->list[i] = x;             
        L->size ++;       
        return 1;
    }     /* NOT sure about this piece of code */

}

int ListDelete(SeqList *L, int i, DataType *x)
/* Deleting the data element value of position i */
{
    int j;
    if(L->size <= 0)
    {
        printf("The linear table is empty and no data elements can be deleted!\n");
        return 0;
    }
    else if(i < 0 || i > L->size-1)
    {
        printf("Parameter i is illegal!");
        return 0;
    }
    else
    { 
        *x = L->list[i];      /* Saving the deleted element to parameter x */
        for(j = i +1; j <= L->size-1; j++) L->list[j] = L->list[j-1];    
        L->size--;             
        return 1;
    }      /* NOT sure about this piece of code */

}

int ListGet(SeqList L, int i, DataType *x)
{
    if(i < 0 || i > L.size-1)
    {
        printf("Parameter i is illegal! \n");
        return 0;
    }
    else
    {
        *x = L.list[i];
        return 1;
    }
}

int main(void)
{
    SeqList myList;
    int i , x;
    ListInitiate(&myList);
    for(i = 0; i < 10; i++) 
        ListInsert(&myList, i, i+1); 
    ListDelete(&myList, 4, &x);
    for(i = 0; i < ListLength(myList); i++)
    {
        ListGet(myList,i,&x);      
        printf("%d", x);
    }
    return 0;
}
c


Solution 1:[1]

Your deletion loop is wrong. You have:

for(j = i +1; j <= L->size-1; j++) L->list[j] = L->list[j-1];

That should be split over two lines:

for (j = i + 1; j <= L->size-1; j++)
    L->list[j] = L->list[j-1];

This carefully copies the deleted element (L->list[j-1]) up the array. What you need to do is copy the undeleted elements down the array:

for (j = i; j < L->size-1; j++)
    L->list[j] = L->list[j+1];

This code works. You are allowed not to like the static in front of each function (but I need them to avoid warnings from my default GCC compilation options (-O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common). I also extracted the printing loop from main() into a function — you can then use it multiple times easily, as shown:

#include <stdio.h>
#define MaxSize  100

typedef int DataType;

typedef struct
{
    DataType list[MaxSize];
    int size;
} SeqList;

static void ListInitiate(SeqList *L)
{
    L->size = 0;
}

static int ListLength(SeqList L)
{
    return L.size;
}

static int ListInsert(SeqList *L, int i, DataType x)
{
    int j;
    if (L->size >= MaxSize)
    {
        printf("can not be inserted!\n");
        return 0;
    }
    else if (i < 0 || i > L->size)
    {
        printf("Parameter i is illegal!\n");
        return 0;
    }
    else
    {
        for (j = L->size; j > i; j--)
            L->list[j + 1] = L->list[j];
        L->list[i] = x;
        L->size++;
        return 1;
    }
}

static int ListDelete(SeqList *L, int i, DataType *x)
{
    int j;
    if (L->size <= 0)
    {
        printf("The linear table is empty and no data elements can be deleted!\n");
        return 0;
    }
    else if (i < 0 || i > L->size - 1)
    {
        printf("Parameter i is illegal!\n");
        return 0;
    }
    else
    {
        *x = L->list[i];
        //for (j = i + 1; j <= L->size - 1; j++)
        //    L->list[j] = L->list[j - 1];
        for (j = i; j < L->size - 1; j++)
            L->list[j] = L->list[j + 1];
        L->size--;
        return 1;
    }
}

static int ListGet(SeqList L, int i, DataType *x)
{
    if (i < 0 || i > L.size - 1)
    {
        printf("Parameter i is illegal!\n");
        return 0;
    }
    else
    {
        *x = L.list[i];
        return 1;
    }
}

static void ListPrint(const char *tag, SeqList L)
{
    int length = ListLength(L);
    printf("%s (%d):", tag, length);
    for (int i = 0; i < length; i++)
    {
        int x;
        ListGet(L, i, &x);
        printf(" %d", x);
    }
    putchar('\n');
}

int main(void)
{
    SeqList myList;

    ListInitiate(&myList);
    for (int i = 0; i < 10; i++)
        ListInsert(&myList, i, i + 1);

    ListPrint("Insertion complete", myList);

    int x;
    if (ListDelete(&myList, 4, &x))
        printf("Deleted %d\n", x);
    else
        printf("Failed to delete anything!\n");

    ListPrint("Deletion complete", myList);

    return 0;
}

Output:

Insertion complete (10): 1 2 3 4 5 6 7 8 9 10
Deleted 5
Deletion complete (9): 1 2 3 4 6 7 8 9 10

The code should check that the insertions succeed. I added the test on the deletion primarily because GCC 11.2.0 complained about x 'maybe used uninitialized', and my compiler options don't allow complaints from the compiler.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1