静态链表

#include <iostream>
#include <algorithm>

using namespace std;
#define SIZE 6

struct comp {
    int data;
    int cur;
};

void reserveArr(comp *array);
int initArr(comp *array);
void Display(comp *array, int);
int mallocArr(comp *array);
void insertArr(comp*, int, int, int);
void deleteArr(comp*, int, int);
int findArr(comp*, int, int);
void alterArr(comp*, int, int, int);

int main()
{
    comp array[SIZE];
    int body = initArr(array);
    cout << "静态链表为:" << endl;
    Display(array, body);
    cout << "插入数据:" << endl;
    insertArr(array, body, 3, 4);
    Display(array, body);
    cout << "删除节点之后的链表:" << endl;
    deleteArr(array, body, 3);
    Display(array, body);
    cout << "寻找节点:" << endl;
    int pos = findArr(array, body, 2);
    if (pos != -1) {
        cout << "节点位置:" << pos << endl;
    }
    cout << "更改数据:" << endl;
    alterArr(array, body, 2, 10);
    Display(array, body);


    system("PAUSE");
    return 0;
}

void reserveArr(comp *array) {
    for (int i = 0; i < SIZE; ++i) {//游标指向下一节点
        array[i].cur = i + 1;
    }
    array[SIZE - 1].cur = 0;//最后一个节点游标为0
}

int mallocArr(comp *array) {
    int i = array[0].cur;
    if (array[0].cur) {
        array[0].cur = array[i].cur;
    }

    return i;
}

int initArr(comp* array) {
    reserveArr(array);
    int body = mallocArr(array);
    int tempbody = body;
    for (int i = 1; i < 4; i++) {
        array[tempbody].data = i;
        if (i + 1 < 4) {
            int j = mallocArr(array);
            tempbody = j;
        }
    }
    array[tempbody].cur = 0;
    return body;
}

void Display(comp *array, int body) {
    int tempbody = body;
    while (array[tempbody].cur) {
        cout << array[tempbody].data
            << "," << array[tempbody].cur << endl;
        tempbody = array[tempbody].cur;
    }

    cout << array[tempbody].data << "," << array[tempbody].cur << endl;
}

void insertArr(comp* array, int body, int pos, int elem) {
    int tempbody = body;//临时变量,准备遍历
    for (int i = 1; i < pos; ++i) {//循环到准备插入位置的前一节点位置
        tempbody = array[tempbody].cur;
    }
    int value = mallocArr(array);//申请一个节点,返回新节点位置
    array[value].data = elem;//将新元素插入新节点
    array[value].cur = array[tempbody].cur;//新节点游标等于上一节点游标
    array[tempbody].cur = value;//上一节点游标等于新节点位置
}

void free(comp* array, int k) {
    array[k].cur = array[0].cur;//将删除节点游标为备用链表表头游标(指向第一个未使用节点)
    array[0].cur = k;//备用..指向删除节点
}

void deleteArr(comp* array, int body, int pos) {
    int tempbody = body;
    while (tempbody != pos) {//遍历找到需要删除的节点位置,error point
        tempbody = array[tempbody].cur;
        if (tempbody == 0) {
            cerr << "Not Find Delete Point1!!!" << endl;
            return;
        }
    }
    int temppos = tempbody;
    tempbody = body;
    while (array[tempbody].cur != temppos) {//循环找到要删除节点的前一位置
        tempbody = array[tempbody].cur;
        if (tempbody == 0) {
            cerr << "Not Find Delete Point2!!!" << endl;
            return;
        }
    }
    array[tempbody].cur = array[temppos].cur;//前一节点游标变为将删除节点游标
    free(array, temppos);
}

int findArr(comp* array, int body, int data) {
    int tempbody = body;
    while (array[tempbody].data != data) {
        tempbody = array[tempbody].cur;
        if (tempbody == 0) {
            cerr << "Not Find!!!" << endl;
            return -1;
        }
    }

    return tempbody;
}

void alterArr(comp* array, int body, int oldelem, int newelem) {
    int tempbody = body;
    int flag = findArr(array, body, oldelem);
    if (flag == -1) {
        cerr << "No Data!!!" << endl;
        return;
    }
    array[flag].data = newelem;
}

 

posted @ 2018-12-21 13:33  Hk_Mayfly  阅读(140)  评论(0编辑  收藏  举报