线性表的C++实现

/*************************************************************************
	> 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值