ghostbody / 15-cpp-learning

15 c++ learning repository of SDCS SYSU
6 stars 3 forks source link

static linked list, memory limit exceeded #6

Open JohnTargaryen opened 8 years ago

JohnTargaryen commented 8 years ago

k8tm i6s tyg2orecuqpu2 hello +7, there are some random test cases in static linked list that occurs the [Memory limit for your program (32 MB) exceeded] problem, would you show me how to fix it ? thx.

ghostbody commented 8 years ago

@JohnTargaryen Make sure that the max storage is no more than MAX_SIZE which is 1000. Ignore the insert operation if the class can not keep this promise after that.

JohnTargaryen commented 8 years ago
void list::insert(int position, const int& data) {
    if (position > _size)  // 不能插入 直接返回
    return;

    if (_size == 1000) {
        empty_head = -1;
        return;
    }
    for (int i = 0; i < 1000; i++) {
        if (storage[i].data == 1111)
        empty_head = i;
        break;
    }
    storage[empty_head].data = data;  //  用一个空位存放数据
    int temp = head;
    int front, next, times = position - 1;

    if (empty_head == 0) {  //  如果是整个空表的第一次插入
        storage[empty_head].next = -1;
        empty_head++;
        _size++;
        return;
    }

    if (position == 0) {  //  如果插在零位 原来的头位为插入位的下一位 插入位为头位
    storage[empty_head].next = head;
    head = empty_head;
    empty_head++;
    _size++;
    return;
    }

    for (int i = 1; i <= times; i++)  //  找到插入位置的前一个
    temp = storage[temp].next;
    front = temp;  // 插入位置前一个
    next = storage[front].next;   //  插入位置后一个

    if (storage[front].next == -1) {  //  如果插入位为最后一位
        storage[front].next = empty_head;
        storage[empty_head].next = -1;
        empty_head++;
        _size++;
        return;
    }

    storage[empty_head].next = next;  //  置插入位的下一位
    storage[front].next = empty_head;
    empty_head++;
    _size++;
}

void list::erase(int position) {
    if (position + 1 > _size)
    return;
    if (position == 0) {
        int temp = head;
        storage[temp].data = 1111;
        head = storage[temp].next;
        storage[temp].next = -1;
        _size--;
        return;
    }
    int times = position - 1;
    int temp = head;
    for (int i = 1; i <= times; i++) {
        temp = storage[temp].next;
    }
    int deleteoneindex = storage[temp].next;
    int next = storage[deleteoneindex].next;
    storage[temp].next = next;
    storage[deleteoneindex] = 1111;
    storage[deleteoneindex] = -1;
    _size--;
}

here's my code, but there are cases that occurs memory limit exceeded still.. would you help me ..? please @ghostbody

=================================[Phase1: check_plagirism]============================================================= Pass.

=================================[Phase2: compile_submission]========================================================== Pass compilation. You got 10 points.

=================================[Phase3: check_style]================================================================= Pass google style check without any violation. Good job! You get 10 out of totally 10 points.

=================================[Phase4: execute_standard_test_cases]================================================= Pass all standard test cases. Good job! You get 10 out of totally 10 points.

=================================[Phase5: execute_random_test_cases]=================================================== 14 out of 100 test cases execution fail(s). You get 43 out of totally 50 points. One example of failed random test case

 [Test input]
+---------------------------------------------------------------------
|995
|0 246 1 844 0 497 0 493 1 140 1 750 0 525 1 716 1 377 2 901 1 616 1 344 0 40 2 202 1 154 1 930 0 844 0 703 1 740 0 529 2 220 1 630 1 441 1 445 1 471 0 339 0 4 1 838 1 779 0 600 1 621 2 512 1 679 2 412 2 727 1 166 2 493 2 792 0 935 0 584 1 72 2 71 2 913 1 917 1 337 1 210 1 245 2 431 2 409 2 810 1... (more input data)
+---------------------------------------------------------------------

 [Memory limit for your program (32 MB) exceeded]

=================================[Phase6: validate_memory_access]====================================================== Pass memory access checking. Good job! You get 20 out of totally 20 points.

ghostbody commented 8 years ago

@JohnTargaryen To be honest your code is so ugly.

For example:

    for (int i = 0; i < 1000; i++) {
        if (storage[i].data == 1111)
        empty_head = i;
        break;
    }

This for loop is meaningless.... And 1111 is meaningless. I think maybe you are lazy to maintain a empty list. So you set a flag for all empty nodes? Bad Style.

Also,

    if (empty_head == 0) {  //  如果是整个空表的第一次插入
        storage[empty_head].next = -1;
        empty_head++;
        _size++;
        return;
    }

    if (position == 0) {  //  如果插在零位 原来的头位为插入位的下一位 插入位为头位
    storage[empty_head].next = head;
    head = empty_head;
    empty_head++;
    _size++;
    return;
    }

What's the meaning of this two block of if condition, it's quite confusing.

I think your thoughts are in a mess. Please make your code clear, or you will meet a lot of problems.

JohnTargaryen commented 8 years ago

@ghostbody thx for you honesty, i'm indeed using flag-setting to determine whether a node is empty,because i don't know how to maintain a empty list, could you explain it to me ? thx.

ghostbody commented 8 years ago

@JohnTargaryen empty_head is the same as head. There are two list in the class static list. The head is the list where user use it to store list nodes. However, the empty_head is the list to maintain empty nodes in the array. When you need an allocation for a user node, get one from the empty list. If you deallocate a node, you should add it to the empty node list.