线性表的C++实现

来源:互联网 发布:php插件机制 编辑:程序博客网 时间:2024/04/30 02:38
/*************************************************************************> File Name: List.cpp> Author: Shaojie Kang> Mail: kangshaojie@ict.ac.cn > Created Time: 2015年09月15日 星期二 09时43分55秒    > Problem:        线性表的实现 ************************************************************************/#include<iostream>using namespace std;const int MaxSize = 20;const int IncrementSize = 2;struct List {    int *head;    int length;    int size;};void InitList(List *list){    list->head = new int[MaxSize];    list->length = 0;    list->size = MaxSize;}void DestoryList(List *list){    delete [](list->head);    list->head = NULL;    list->length = 0;    list->size = 0;}void ClearList(List *list){    list->length = 0;}int GetListLength(List *list){    return list->length;}bool GetElement(List *list, int i, int &element){    if(i < 1 || i > list->length) return false;    element = *(list->head + i - 1);    return true;}int LocateElement(List *list, int element){    int position = 0;    while(position < list->length)    {        if(element == *(list->head + position))            break;        position++;    }    return position < list->length ? position+1 : 0;}bool PriorElement(List *list, int key, int &priorElement){    int cur = 1;    while(cur < list->length)    {        if(key == *(list->head + cur))            break;        cur++;    }    if(cur < list->length)    {        priorElement = *(list->head + cur - 1);        return true;    }    else return false;}bool ListInsert(List *list, int i, int key){    if(i < 1 || i > (list->length + 1))          return false;    if(list->size == list->length)    {        list->size += IncrementSize;        int *newHead = new int[list->size];        for(int j = 0; j < i-1; ++j)            *(newHead + j) = *(list->head + j);        for(int j = i-1; j < list->length; ++j)            *(newHead + j + 1) = *(list->head + j);        *(newHead + i - 1) = key;        delete [](list->head);        list->head = newHead;    }    else     {        for(int j = list->length-1; j >= i-1; --j)            *(list->head + j + 1) = *(list->head + j);        *(list->head + i - 1) = key;    }    list->length++;    return true;}bool ListDelete(List *list, int i, int &element){    if(i < 1 || i > list->length) return false;    element = *(list->head + i - 1);    for(int j = i; j < list->length; ++j)        *(list->head + j - 1) = *(list->head + j);    list->length--;    return true;}bool ListTraverse(List *list, bool(*visit)(int *)){    int *element = list->head;    for(int i = 0; i < list->length; ++i)    {        if(!visit(element++))            return false;     }    cout<<endl;    return true;}bool traverse(int *element){    cout<<++(*element)<<" ";    return true;}int main(){    List list;    InitList(&list);    int arr[] = {        6, 2, 1, 7, 8, 4, 9    };    for(int i = 0; i < sizeof(arr)/sizeof(int); ++i)        ListInsert(&list, i+1, arr[i]);    ListTraverse(&list, traverse);        DestoryList(&list);    return 0;}

0 0
原创粉丝点击