C language arrangement question

Asked 1 years ago, Updated 1 years ago, 73 views

C language array_list Please tell me how to get the result value in the find statement~ When you add a value to an array and navigate, The price of garbage is coming out

And while studying, I'm measuring the execution time for the first time From where to where do you measure the execution time?

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

typedef int Element;
Element data[MAX_LIST_SIZE];
int length = 0;
    int pic = 0;
    int pic2 = 0;
    int add_num = 0;
    int place = 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 ){
            printf("%d",data[i]);
        }
        else error ("input error");
    }
}

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

void prompt_2(){
    printf("Select where to add or delete nodes"\n");
    printf("1. Front 2. Back 3. Select Location\n");
    printf ("Select menu: ");
    printf("\n");
}


void main(){   
    /*
    do{
        prompt_1();
        scanf("%d",&pic);
        if(pic == 1){
           for 
        }
        /*if(pic == 2){
            prompt_2();
            delete();
        }
        if(pic == 3){
            find();
        }
        if(pic == 4){
            printf("Current Array Status: ");
            print_list();
        }
    }while(pic != 5);
    */
    while (1) {
        printf ("Select the action to execute\n");
        printf("1. Add nodes 2. Delete nodes 3. Search nodes 4. Output nodes 5. Select the End \n menu: ");
        scanf("%d", &pic);

        if (pic >= 1 && pic != 5) {
            switch (pic) {
                case 1 :

                    printf("Set insertable :");
                    scanf("%d", &add_num);
                    printf ("Select a location to insert\n");
                    printf("1. Front, 2. Back, 3. User location setting : ");"
                    scanf("%d", &pic2);
                    if(pic2 == 1){
                        insert(0,add_num);
                    }
                    else if(pic2 == 2){
                        insert(length,add_num);
                    }
                    else if(pic2 == 3){
                        printf("Set the location to insert (0~):");
                        scanf("%d", &place);
                        insert(place,add_num);
                    }

                    printf ("Inserted".\n\n");

                    break;
                case 2 :
                    printf("Set the location to be removed (0~): ");"
                    scanf("%d", &place);
                    delete(place);

                    printf ("Removed".\n\n");
                    break;
                case 3 :
                    printf("Set search location:");"
                    scanf("%d",&place);
                    find(place);
                    break;
                 case 4 :
                    printf("\n");
                    print_list();
                    break;
                default :
                    printf("Invalid value". Please re-enter.\n\n");
                    break;
            }
        }
        else if(pic == 5)
        break;
    }


}










array search

2022-09-22 18:34

1 Answers

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");
}

void find (Element e)
{
    for(int i = 0 ; i < length ; i++){
        if(data[i] == e ){
            printf("index : %d\n", i);
        }
    }
}

Measure the time by function The main part is an infinite loop anyway, so it won't be meaningful.


2022-09-22 18:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.