菜单

一个顺序列表的实现

2019年8月2日 - 4166am金沙下载
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 20

#define SUCCESS 1
#define FAILED 0
#define ERROR -1

typedef int ElementType;

typedef struct {
    ElementType data[MAXSIZE];
    int length;
} SqList;

int initSqList(SqList * list);
int getElement(const SqList * const list, int position,  ElementType * element_to_be_store);
int addElementToList(SqList * list, ElementType element);
ElementType insertInToList(SqList * list, int position, ElementType element);
void showAll(const SqList * const list);
void empty(SqList * list);
int deleteByPosition(SqList * list, int position, ElementType *save_deleted_element);
int getLength(const SqList * list);
int contains(const SqList * list, ElementType element);

int main() {
    ElementType deleteElement;
    ElementType tmp = 0;
    ElementType element;
    int i = 0;
    SqList list;
    SqList * list_pt;
    list_pt = &list;

    //初始化列表
    initSqList(list_pt);

    //添加元素至尾部

    for (; i < MAXSIZE + 1; i++) {
        addElementToList(list_pt, i * 10);
    }

    //显示所有列表元素
    showAll(list_pt);

    deleteByPosition(list_pt, 1, &deleteElement);
    showAll(list_pt);

    contains(list_pt, 66);
    showAll(list_pt);

    getElement(list_pt, 2, &element);

    empty(list_pt);
    for (i = 0; i < MAXSIZE + 1; i++) {
        insertInToList(list_pt, i, i * 2);
    }

    insertInToList(list_pt, 1, 222);

    showAll(list_pt);
    while( list_pt->length > 0 )
    {
        deleteByPosition(list_pt, 1, &tmp);
        //printf("=====你删除的元素值为:%d, 位置为%d=======\\n", tmp, i);
    }

    showAll(list_pt);
    contains(list_pt, 90);
    system("pause");
    return 0;
}

//初始化列表,即把该列表的数据清0
int initSqList(SqList * list) {
    memset(list, 0, sizeof(SqList));
    list->length = 0;
    printf("初始化列表......\\n");
    return SUCCESS;
}

//添加元素至列表的尾部
int addElementToList(SqList * list, ElementType element) {
    if (list == NULL || list->length < 0 || list->length >= MAXSIZE) {
        printf("添加元素至列表尾部失败!添加的元素为:%d\\n", element);
        return ERROR;
    }
    list->data[list->length] = element;
    list->length++;
    printf("添加元素至列表尾部成功!添加的元素为:%d\\n", element);
    return SUCCESS;
}

//插入元素到某位置,成功则返回该元素。
ElementType insertInToList(SqList * list, int position, ElementType element) {
    int i = 0;
    if (list == NULL || list->length >= MAXSIZE
        || position < 1|| position > list->length+1 ||position > MAXSIZE) {printf
        ("插入元素失败! 插入的位置为:%d, 插入的元素为:%d;当前列表的长度为:  %d\\n", position, element,
        list->length);
    return FAILED;
    }
    printf("插入前,列表的长度为:%d\\n", list->length);

    for (i = list->length; i >= position - 1; i--) {
        list->data[i] = list->data[i - 1];
    }
    list->data[position - 1] = element;
    list->length++;
    printf("插入元素成功! 插入的位置为:%d, 插入的元素为:%d\\n", position, element);
    printf("插入后,列表的长度为:%d\\n", list->length);
    return element;
}

//删除某元素
int deleteByPosition(SqList * list, int position, ElementType * save_deleted_element) {
                         int i = 0;
                         if (list == NULL || list->length < 1 || position < 1 || position > list->length) {
                                 printf("删除失败!列表为空或你要删除的位置:%d无效!\\n", position);
                                 return FAILED;
                         }

                         *save_deleted_element = list->data[position - 1];
                         for (i = position; i <=list->length; i++) {
                             list->data[i - 1] = list->data[i];
                         }
                         list->length--;
                         printf("删除成功!你要删除的位置为%d, 该位置的元素为%d\\n", position, *save_deleted_element);
                         return SUCCESS;
}

//获取第position个位置的元素
int getElement(const SqList * const list, int position,
               ElementType * element_to_be_store) {
                   if (list->length < 1 || position < 1 || position > list->length) {
                       *element_to_be_store = -1;
                       printf("获取元素失败!获取的位置为:%d, 该位置的元素值为:%d\\n", position,
                           *element_to_be_store);
                       return ERROR;
                   }
                   *element_to_be_store = list->data[position - 1];
                   printf("获取元素成功!获取的位置为:%d, 该位置的元素值为:%d\\n", position, *element_to_be_store);
                   return SUCCESS;
}

//清空列表
void empty(SqList * list) {
    memset(list, 0, sizeof(SqList));
    list->length = 0;
    printf("清空列表......\\n");
}

//显示列表所有元素
void showAll(const SqList * const list) {
    int i = 0;
    if (list == NULL || list->length < 0 || list->length > MAXSIZE) {
        printf("列表没有数据或列表为NULL或或发生溢出");
        exit(ERROR);
    }

    printf("当前列表的长度为:  %d\\n", list->length);
    for (i = 0; i < list->length; i++) {
        printf("%d: = %d\\n", (i + 1), list->data[i]);
    }
}

//获取列表的长度
int getLength(const SqList * list) {
    return list->length;
}

//是否包含某个值,如果是,则返回该值在列表中第一次出现的位置;否则返回0;如果列表为NULL,则返回-1;
int contains(const SqList * list, ElementType element) {
    int i = 0;
    if (list == NULL || list->length < 1) {
        printf("查找失败!列表为空!\\n");
        return ERROR;
    }

    for (; i < list->length; i++) {
        if (element == list->data[i]) {
            printf("查找成功!你要查找的元素为:%d, 该元素在列表中第一次出现的位置为:%d\\n", element, i + 1);
            return (i + 1);
        }
    }
    return FAILED;
}
//该片段来自于http://outofmemory.cn

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图