Experiment 2: Write a program and verify that linear table order represents all operations

This blog post is from the book "Data Structure" by Mr. Yan Weimin. I saw that there is an experiment 2 today, I wrote it immediately and shared it on the blog. The code is fully executable by the Clion compiler. The complete topic is as follows:

topic reproduction

Write a main program to design and verify all operations represented by linear table order
(at least algorithm 2.3, 2.4, 2.5), and design an algorithm to delete all elements whose value is greater than min and less than max.

topic analysis

  1. 2.3 is a linear table insertion
  2. 2.4 is a linear table deletion
  3. 2.5 is a linear table lookup
  4. What I designed myself is a delete function (two methods are given)

2.3 Linear table insertion

Linear table insertion is the process of moving the value backwards, we just need to write it according to the book.

Status ListInsert_Sq(SqList &L,int i,ElementType e){
    
    
    if(i<1 || i>L.length+1) {
    
    
        return ERROR;

    }

    if(L.length >= L.listsize){
    
    
        ElementType * newbase = (ElementType *) realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElementType));
        if(!newbase) exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    ElementType *q = &(L.elem[i-1]);
    for(ElementType *p = &(L.elem[L.length-1]);p>=q;--p){
    
    
        *(p+1) = *p;
    }
    *q = e;
    ++L.length;
    return OK;
}

Just traverse from the back to the front.

2.3 Linear table deletion

delete from front to back

Status ListDelete_Sq(SqList &L,int i,ElementType &e){
    
    
    if((i<1)|| (i>L.length)) return ERROR;
    int* p = &(L.elem[i-1]);
    e = *p;
    int* q = L.elem + L.length - 1;
    for(++p;p<=q;++p)*(p-1)=*p;
    --L.length;
    return OK;
}

2.5 Linear table lookup

Find this value directly. We just traverse from start to finish.

Status compare(ElementType a,ElementType b){
    
    
    return a==b?0:1;
}
int LocateElem_Sq(SqList L,ElementType e,Status (*compare)(ElementType,ElementType)){
    
    
    int i = 1;
    int* p  = L.elem;


    while(i<=L.length && !(*compare)(*p++,e)){
    
    
        ++i;

    }
    if(i<=L.length) return i;
    else return 0;
}

2.6 Design your own function to delete the range

For this question, I first used the ordinary O(n^2) complexity to calculate, and then we replaced it with the O(n) complexity of space-to-time, so these two algorithms are given one by one. I feel that the second one is easier to understand.

Status Delete_minToMax(SqList &L,ElementType min,ElementType max){
    
    
    while(1){
    
    
        int flag = true;
        int i;
        for(i =0;i<L.length;i++){
    
    
            if(L.elem[i]>min && L.elem[i]<max){
    
    
                flag = false;
                break;
            }
        }
        for(int j= i+1;j<L.length;j++){
    
    
            L.elem[j-1] = L.elem[j];
        }

        if(flag) break;
        else L.length--;
    }
    return TRUE;
}

Status Delete_minToMax2(SqList &L,ElementType min,ElementType max){
    
    
    SqList L_cp;
    InitList_Sq(L_cp);
    for(int i=0;i<L.length;i++){
    
    
        if(L.elem[i]>min && L.elem[i]<max) continue;
        L_cp.elem[L_cp.length++] = L.elem[i];
    }
    L.length = 0;
    for(int i=0;i<L_cp.length;i++){
    
    
        L.elem[L.length++] = L_cp.elem[i];
    }
    return TRUE;
}

2.7 Summary

The topic is not difficult as a whole, mainly because some small details need to be dealt with separately, such as when to delete and subtract L.length. These are all thought out in advance, so the question will be faster.

full code

#include<iostream>
#define ERROR -1
#define OVERFLOW -1
#define TRUE 1
#define OK 1
#define LISTINCREMENT 10
#define LIST_INIT_SIZE 100
typedef int Status;
typedef int ElementType;
using namespace std;
typedef struct{
    
    
    ElementType *elem;
    int listsize;
    int length = 0;
}SqList;

//2.3
Status ListInsert_Sq(SqList &L,int i,ElementType e){
    
    
    if(i<1 || i>L.length+1) {
    
    
        return ERROR;

    }

    if(L.length >= L.listsize){
    
    
        ElementType * newbase = (ElementType *) realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElementType));
        if(!newbase) exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    ElementType *q = &(L.elem[i-1]);
    for(ElementType *p = &(L.elem[L.length-1]);p>=q;--p){
    
    
        *(p+1) = *p;
    }
    *q = e;
    ++L.length;
    return OK;
}


Status ListDelete_Sq(SqList &L,int i,ElementType &e){
    
    
    if((i<1)|| (i>L.length)) return ERROR;
    int* p = &(L.elem[i-1]);
    e = *p;
    int* q = L.elem + L.length - 1;
    for(++p;p<=q;++p)*(p-1)=*p;
    --L.length;
    return OK;
}
Status compare(ElementType a,ElementType b){
    
    
    return a==b?0:1;
}
int LocateElem_Sq(SqList L,ElementType e,Status (*compare)(ElementType,ElementType)){
    
    
    int i = 1;
    int* p  = L.elem;


    while(i<=L.length && !(*compare)(*p++,e)){
    
    
        ++i;

    }
    if(i<=L.length) return i;
    else return 0;
}

Status InitList_Sq(SqList &L){
    
    
    L.elem = (ElementType *)malloc(LIST_INIT_SIZE*sizeof(ElementType));
    if(!L.elem) exit(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK;
}

//并设计一个算法删除所有值大于min而且小于max的元素。

/*Status Delete_minToMax(SqList &L,ElementType min,ElementType max){
    while(1){
        int flag = true;
        int i;
        for(i =0;i<L.length;i++){
            if(L.elem[i]>min && L.elem[i]<max){
                flag = false;
                break;
            }
        }
        for(int j= i+1;j<L.length;j++){
            L.elem[j-1] = L.elem[j];
        }

        if(flag) break;
        else L.length--;
    }
    return TRUE;
}*/

Status Delete_minToMax2(SqList &L,ElementType min,ElementType max){
    
    
    SqList L_cp;
    InitList_Sq(L_cp);
    for(int i=0;i<L.length;i++){
    
    
        if(L.elem[i]>min && L.elem[i]<max) continue;
        L_cp.elem[L_cp.length++] = L.elem[i];
    }
    L.length = 0;
    for(int i=0;i<L_cp.length;i++){
    
    
        L.elem[L.length++] = L_cp.elem[i];
    }
    return TRUE;
}
int main(){
    
    


    //test1:
    SqList  L;

    int flag  = InitList_Sq(L);
    flag = ListInsert_Sq(L,1,1);
    flag = ListInsert_Sq(L,1,2);
    flag = ListInsert_Sq(L,1,3);
    flag = ListInsert_Sq(L,1,4);
    flag = ListInsert_Sq(L,1,5);
    flag = ListInsert_Sq(L,1,6);
    cout << "test1:" << endl;
    for(int i=0;i<L.length;i++){
    
    
        cout << L.elem[i] << " ";
    }
    cout << endl;

    //test2:
    int x;

    flag = ListDelete_Sq(L,2,x);
    cout << "test2:";
    cout << endl << "Delete Element:" << x << endl;

    for(int i=0;i<L.length;i++){
    
    
        cout << L.elem[i] << " ";
    }

    //test3: find == 4
    cout << endl << "test3: find4:" << endl;
    int pos;
    pos = LocateElem_Sq(L,4,compare);
    cout << L.elem[pos] << endl;


    //test4:delete 2 - 5
    cout << "test4:" << endl <<"(2,5)Delete before:" << endl;
    for(int i=0;i<L.length;i++){
    
    
        cout << L.elem[i] << " ";
    }
    cout << endl << "after Deleted :" << endl;
    /*flag = Delete_minToMax(L,2,5);*/
    flag = Delete_minToMax2(L,2,5);
    for(int i=0;i<L.length;i++){
    
    
        cout << L.elem[i] << " ";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/123181480