其他分享
首页 > 其他分享> > 线性表(C语言实现)

线性表(C语言实现)

作者:互联网

线性表

顺序存储结构

# include "stdlib.h"

# define LIST_INIT_SIZE 100
# define LIST_INCREMENT 10

typedef int elemtype;
typedef struct
{
    elemtype *elem;
    int length;
    int list_size;
} SqList;

// Create a linear table
int createList(SqList *l)
{
    l->elem = (elemtype *) malloc(LIST_INIT_SIZE * sizeof(elemtype));
    if(!l->elem)
    {
        return -1;
    }
    l->length = 0;
    l->list_size = LIST_INIT_SIZE;

    return 0;
}

// Locate an element
int locateElem(SqList *l, elemtype e)
{
    int index = 0;
    for (int i = 0; i < l->length; ++i)
    {
        if (l->elem[i] == e)
        {
            index = i;
        }
    }

    return index;
}

// Insert a element
int insertElem(SqList *l, int index, elemtype e)
{
    if (index < 1 || index > l->length + 1)
    {
        return -1;
    }
    if (l->length > l->list_size)
    {
        elemtype *new = (elemtype *) realloc(l->elem, (l->list_size + LIST_INCREMENT) * sizeof(elemtype));
        l->elem = new;
        l->list_size += LIST_INCREMENT;
    }

    elemtype *p;
    for (p = &(l->elem[l->length - 1]); p >= &(l->elem[index - 1]); --p)
    {
        *(p + 1) = *p;
    }
    l->elem[index - 1] = e;
    ++l->length;

    return 0;
}

// Delete an element
elemtype deleteElem(SqList *l, int index)
{
    if (index < 1 || index > l->length)
    {
        return -1;
    }

    elemtype e = l->elem[index - 1];
    for (elemtype *p = &l->elem[index - 1]; p < l->elem + l->length; ++p)
    {
        *(p) = *(p + 1);
    }
    --l->length;

    return e;
}


链式存储结构

# include "stdio.h"
# include "stdlib.h"

typedef int elemtype;

typedef struct ListNode
{
    elemtype data;
    struct ListNode *next;
} ListNode;

// The head interpolation
ListNode *createListHead(int length)
{
    ListNode *head = (ListNode *) malloc(sizeof(ListNode));
    head->next = NULL;
    for (int i = 0; i < length; ++i)
    {
        ListNode *curr = (ListNode *) malloc(sizeof(ListNode));
        scanf("%d", &curr->data);
        curr->next = head->next;
        head->next = curr;
    }

    return head;
}

// The tail interpolation
ListNode *createListTail(int length)
{
    ListNode *head = (ListNode *) malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *pointer = head;
    for (int i = 0; i < length; ++i)
    {
        ListNode *curr = (ListNode *) malloc(sizeof(ListNode));
        scanf("%d", &curr->data);
        pointer->next = curr;
        pointer = curr;
    }

    return head;
}

// fine an element
elemtype findElem(ListNode *l, int i)
{
    ListNode *head = l;
    ListNode *pointer = head;
    int index = 0;
    while (pointer->next != NULL && index != i)
    {
        pointer = pointer->next;
        ++index;
    }
    return pointer->data;
}

// Insert an element
int insertElem(ListNode *l, int i, elemtype e)
{
    ListNode *pointer = l;
    int index = 0;
    while (pointer != NULL && index != i - 1)
    {
        pointer = pointer->next;
        ++index;
    }
    ListNode *new = (ListNode *) malloc(sizeof(ListNode));
    new->data = e;
    new->next = pointer->next;
    pointer->next = new;

    return 0;
}

// Delete an element
elemtype deleteElem(ListNode *l, int i)
{
    ListNode *pointer = l;
    int index = 0;
    while (pointer != NULL && index != i - 1)
    {
        pointer = pointer->next;
        ++index;
    }
    elemtype e = pointer->next->data;
    pointer->next = pointer->next->next;

    return e;
}

标签:index,ListNode,线性表,实现,elemtype,C语言,int,next,pointer
来源: https://www.cnblogs.com/euler0525/p/16697862.html