线性表-链表-顺序结构-SequentialList.c

本文介绍了一种使用动态数组的数据结构实现方法,并提供了关键函数的代码示例,包括初始化、插入、删除等操作。
#include "../include.h"

#define LIST_INIT_SIZE    3
#define LISTINCREMENT    2

typedef 
struct
{
    ElemType 
*base;
    
int length;
    
int listsize;
}
List;

Status InitList(List 
*l)
{
    l
->base = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));

    
if(!l->base)
    
{
    exit(OVERFLOW);
    }


    l
->length = 0;
    l
->listsize = LIST_INIT_SIZE;
}


Status DestoryList(List 
*l)
{
    
if(!l->base)
    
{
    free(l
->base);
    }


    l
->length = 0;
    l
->listsize = 0;
}


Status ListInsert(List 
*l,ElemType e,int i)
{
    
if(i < 0 || i > l->length)
    
{
    
return ERROR;
    }


    
if(l->length >= l->listsize)
    
{
    ElemType 
*newbase = (ElemType *)realloc(l->base,(l->listsize + LISTINCREMENT) * sizeof(ElemType));

    
if(!newbase)
    
{
        exit(OVERFLOW);
    }


    l
->base = newbase;
    l
->listsize += LISTINCREMENT;
    }


    ElemType 
*pei = l->base + i;
    ElemType 
*pej = l->base + l->length - 1;

    
while(pej >= pei)
    
{
    
*(pej + 1= *pej;

    pej
--;
    }


    
*pei = e;

    l
->length++;

    
return OK;
}


Status ListDelete(List 
*l,ElemType *e,int i)
{
    
if(i < 0 || i >= l->length)
    
{
    
return ERROR;
    }


    ElemType 
*pei = l->base + i;
    ElemType 
*pej = l->base + l->length - 1;

    
*= *pei;

    
while(pei < pej)
    
{
    
*pei = *(pei + 1);

    pei
++;
    }


    l
->length--;

    
return OK;
}


int Locate(List l,ElemType e)
{
    
int i;

    
for(i = 0; i < l.length; i++)
    
{
    
if(*(l.base + i) == e)
    
{
        
return i;
    }

    }


    
return INFEASIBLE;
}


void Print(List l)
{
    
int i;

    
for(i = 0; i < l.length; i++)
    
{
    printf(
"%d ",*(l.base + i));
    }


    printf(
" ");
}


int main()
{
    List l;

    printf(
"Init list...");

    InitList(
&l);

    printf(
"initialed. ");

    printf(
"Insert elements: 6 3 2 19 0 9 38 42 85 99 34 63 ");

    ListInsert(
&l,6,0);
    ListInsert(
&l,3,1);
    ListInsert(
&l,2,2);
    ListInsert(
&l,19,3);
    ListInsert(
&l,63,4);
    ListInsert(
&l,34,4);
    ListInsert(
&l,99,4);
    ListInsert(
&l,85,4);
    ListInsert(
&l,42,4);
    ListInsert(
&l,38,4);
    ListInsert(
&l,9,4);
    ListInsert(
&l,0,4);
    
    printf(
"Result: ");

    Print(l);

    printf(
"Locate elements: 42 63 6 ");

    printf(
"Result: ");

    printf(
"%d ",Locate(l,42));
    printf(
"%d ",Locate(l,63));
    printf(
"%d ",Locate(l,6));

    printf(
"Delete elements: 0 42 63 6 ");

    ElemType e;

    ListDelete(
&l,&e,11);
    ListDelete(
&l,&e,7);
    ListDelete(
&l,&e,4);
    ListDelete(
&l,&e,0);

    printf(
"Result: ");

    Print(l);

    printf(
"Destory list...");

    DestoryList(
&l);

    printf(
"destoried. ");
}

 
线性表链表、顺序栈、循环队列中,线性表和顺序栈、循环队列属于逻辑结构,链表属于物理结构(存储结构)。 线性表是一种逻辑结构,它是具有相同数据类型的 n(n≥0)个数据元素的有限序列,这里强调的是元素之间的逻辑关系,即一对一的线性关系,而不涉及这些元素在计算机内存中的具体存储方式 [^1]。 顺序栈是栈这种逻辑结构的一种顺序存储实现。栈是一种操作受限的线性表,限定仅在表尾进行插入和删除操作,元素具有后进先出的特征,这是它的逻辑特性;而顺序栈是用顺序存储结构来实现栈的功能,使用数组等方式存储元素,但栈的逻辑定义是核心 [^3]。 循环队列是队列这种逻辑结构的一种实现。队列是限定只能在表的一端作插入运算,在另一端作删除运算的线性表,具有先进先出的特征,这是其逻辑定义;循环队列是为了解决普通队列在使用数组存储时的空间浪费问题而设计的一种存储方式,本质上还是基于队列的逻辑特性 [^2]。 链表是一种物理结构(存储结构),它是通过节点和指针来实现元素的存储和连接,用于实现线性表等逻辑结构,每个节点除了存放数据元素外,还要存储指向下一个节点的指针,体现了元素在内存中的具体存储和连接方式 [^1]。 ```c // 简单的线性表顺序存储示例(用数组模拟) #include <stdio.h> #define MAX_SIZE 100 typedef struct { int data[MAX_SIZE]; int length; } SeqList; // 简单的顺序栈示例 typedef struct { int data[MAX_SIZE]; int top; } SeqStack; // 简单的循环队列示例 typedef struct { int data[MAX_SIZE]; int front; int rear; } CircularQueue; int main() { SeqList list; SeqStack stack; CircularQueue queue; // 初始化操作 list.length = 0; stack.top = -1; queue.front = 0; queue.rear = 0; return 0; } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值