设为首页 加入收藏

TOP

数据结构C线性表现实(一)
2019-09-03 03:40:35 】 浏览:118
Tags:数据结构 线性 现实

linearList.h

#ifndef _INC_STDIO_8787
#define _INC_STDIO_8787
#include <stdio.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100    // 线性表存储空间的初始分配量
#define LIST_INCREMENT 10    // 线性表存储空间的分配增量

typedef int ElemType;        // 数据元素的类型

typedef struct {
    ElemType *elem;    // 存储空间的集地址
    int length;        // 当前线性表的长度
    int listsize;    // 当前分配的存储容量
} LinearList;

int init_list(LinearList *list);    //初始化线性表

void clear_list(LinearList *list);

void destroy_list(LinearList* list);

int list_empty(LinearList* list);

int list_length(LinearList* list);

void print_list(LinearList* list);

int locate_elem(LinearList* list, ElemType* x);

int prior_elem(LinearList* list, ElemType* cur_elem, ElemType* pre_elem);

int get_elem(LinearList* list, int index, ElemType* e);

int next_elem(LinearList* list, ElemType* cur_elem, ElemType* next_elem);

int insert_elem(LinearList* list, int index, ElemType* e);

int delete_elem(LinearList* list, int index, ElemType* e);

int append_elem(LinearList* list,ElemType* e);

int pop_elem(LinearList* list, ElemType* e);

void union_list(LinearList* list_a, LinearList* list_b, LinearList* list_c);

void intersect_list(LinearList* list_a, LinearList* list_b, LinearList* list_c);

void except_list(LinearList* list_a,LinearList* list_b, LinearList* list_c);
#endif

linearList.c

#include "linearList.h"

int init_list(LinearList *list)
{
    list->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if (!list->elem)
    {
        return -1;
    }
    list->length = 0;
    list->listsize = LIST_INIT_SIZE;
    return 0;
}

void clear_list(LinearList *list)
{
    list->length = 0;
}

void destroy_list(LinearList* list)
{
    free(list);
}

int list_empty(LinearList* list)
{
    return (list->length == 0);
}

int list_length(LinearList* list)
{
    return list->length;
}

int locate_elem(LinearList* list, ElemType* x)
{
    int pos = -1;
    int i;
    for (i = 0; i < list->length; i++)
    {
        if (list->elem[i] == *x)
        {
            pos = i;
        }
    }
    return pos;
}

int prior_elem(LinearList* list, ElemType* cur_elem, ElemType* pre_elem)
{
    int pos = -1;
    pos = locate_elem(list, cur_elem);
    if(pos <= 0) 
    {
        return -1;
    }
    *pre_elem = list->elem[pos-1];
    return 0;
}

int insert_elem(LinearList* list, int index, ElemType* e)
{
    ElemType *q, *p;
    if (index < 0 || index >= list->length)
    {
        return -1;
    }
    if (list->length >= list->listsize)
    {
        ElemType *newbase = (ElemType*)realloc(list->elem, (list->listsize + LIST_INCREMENT)*sizeof(ElemType));
        if (!newbase)
        {
            return -1;
        }
        list->elem = newbase;
        list->listsize += LIST_INCREMENT;
    }
    q = &(list->elem[index]);
    for (p = &(list->elem[list->length-1]); p >= q; p--)
    {
        *(p+1) = *p;
    }
    *q = *e;
    ++list->length;
    return 0;
}

int append_elem(LinearList* list,ElemType* e)
{
    if (list->length >= list->listsize)
    {
        ElemType *newbase = (ElemType*)realloc(list->elem, (list->listsize + LIST_INCREMENT)*sizeof(int));
        if (!newbase)
        {
            return -1;
        }
        list->elem = newbase;
        list->listsize += LIST_INCREMENT;
    }
    list->elem[list->length] = *e;
    ++list->length;
    return 0;
}

void print
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C_常用快捷键 下一篇STM32F4 阿波罗寄存器点亮LED灯

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目