C Language Array Add Delete Navigation Question.

Asked 2 years ago, Updated 2 years ago, 67 views

Let's set the size of the array at 100,000 Use the insert function in the main window The error window appears without entering the number. Please catch the wrong part of the insert function that I defined.

#include <stdio.h>
#include <stdlib.h>
#define MAX_LIST_SIZE 100000

typedef int Element;
Element data[MAX_LIST_SIZE];
int length = 0;

void error(char *str)
{
    fprintf(stderr, "%s\n", str);
    exit(1);
};

void init_list()                 {length = 0; }
void clear_list()                {length = 0; }
int is_empty()                   {return length == 0; }
int is_full()                    {return length == MAX_LIST_SIZE; }
int get_entry(int num)           {return data[num]; }
void replace(int num, Element e) {data[num] = e; }
int size() {return length; }

void insert(int pos, int e ){
    if(is_full==0 && pos >= 0 && pos<=length){
        for(int i = length ; i>pos ; i--){
            data[i] = data[i-1];
        }
        data[pos] = e;
        length++;
    }
    else error ("Saturation & Error || Insertion Position Error".");
}

void delete(int pos)
{
    if(is_empty()==0 && 0 <= pos && pos < length){
        for(int i=pos+1 ; i < length ; i++ ){
            data[i-1] = data[i];
            length--;
        }
    }
    else error ("Blank Status Error || Delete Location Error");
}

int find (Element e)
{
    for(int i = 0 ; i < length ; i++){
        if(data[i] == e ){
            return i;
        }
    }
    return -1;
}

void print_list(){
    for(int i = 0 ; i < length ; i++){
        printf("%2d", data[i]);
    }
    printf("\n");
}


void main()
{
    init_list();
    insert(0, 10);
    print_list();
}

c array insert

2022-09-22 16:18

1 Answers

void insert(int pos, int){
    if(is_full==0 && pos >= 0 && pos<=length){
        for(int i = length ; i>pos ; i--){
            data[i] = data[i-1];
        }
        data[pos] = e;
        length++;
    }
    else error ("Saturation & Error || Insertion Position Error".");
}

In the second line, you defined is_full as a function, but you omitted ()

if(is_full()==0&&pos>=0&&pos<=length)


2022-09-22 16:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.