动态分配存储的顺序表

/*
 * 动态分配存储的顺序表
 */

#include <stdio.h>
#include <stdlib.h>

#define INIT_SIZE 100
#define EXPAND_SIZE 50

typedef int ElemType;
typedef struct {
    ElemType *head;
    int len; // 顺序表当前元素个数
    int size; // 顺序表当前最大容量
} Sqlist;

int init_sqlist(Sqlist *list)
{
    if( (list->head = malloc(INIT_SIZE*sizeof(ElemType))) == NULL ) // 分配初始空间
    {
        printf("init error.\n");
        return -1;
    }
    list->len = 0;
    list->size = INIT_SIZE;
    return 0;
}

int insert_sqlist(Sqlist *list, int n, ElemType item)
{
    ElemType *t;
    if( n > list->len+1 || n < 1 ) // 插入位置非法
    {
        printf("insert error, location illegal.\n");
        return -1;
    }
    if( list->len == list->size ) // 顺序表当前容量已经全部用完, 扩容
    {
        t = realloc( list->head, (list->size + EXPAND_SIZE)*sizeof(ElemType) );
        if( t == NULL)
        {
            printf("realloc error.\n");
            return -1;
        }
        list->head = t;
        list->size += EXPAND_SIZE;
    }

    for( t = list->head + list->len ; t > (list->head + (n-1)) ; t-- ) // 原第n位置元素及其后的元素依次后移
        *t = *(t-1);
    *t = item;
    list->len ++;
    return 0;
}

int delete_sqlist(Sqlist *list, int n)
{
    ElemType *t;
    if( n > list->len || n < 1 ) // 删除位置非法
    {
        printf("delete error, location illegal.\n");
        return -1;
    }

    for( t= list->head + (n-1) ; t < list->head + (list->len-1) ; t++ ) // 第n+1位置元素及其后的元素依次前移
        *t = *(t+1);
    list->len --;
    return 0;
}

int free_sqlist(Sqlist *list)
{
    free(list->head);
    return 0;
}

void print_sqlist(Sqlist list)
{
    int i;
    for(i=0; i<list.len; i++)
        printf("%d ", list.head[i]);
    printf("\n");
}

int main(void)
{
    Sqlist list;
    init_sqlist(&list);

    int i;
    for( i=1 ; i<=117; i++)
        insert_sqlist(&list,i,i);
    print_sqlist(list);

    delete_sqlist(&list, 5);
    delete_sqlist(&list, 115);
    print_sqlist(list);

    free_sqlist(&list);
    return 0;
}

转载于:https://www.cnblogs.com/DayByDay/p/3864382.html

在C语言中,动态内存分配通常使用`malloc()`和`free()`函数来创建和释放顺序表(也称为数组)。下面是一个简单的示例,演示了如何动态地创建、插入元素以及释放顺序表: ```c #include <stdio.h> #include <stdlib.h> // 定义顺序表结构体 typedef struct Node { int data; // 存储数据 struct Node* next; // 指向下一个节点的指针 } Node; // 动态创建一个新的顺序表节点 Node* create_node(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); // 分配空间 if (newNode != NULL) { newNode->data = value; newNode->next = NULL; } return newNode; } // 在顺序表末尾添加新节点 void insert_at_end(Node** head, int value) { Node* newNode = create_node(value); if (*head == NULL) { // 列表为空,直接将新节点设为首节点 *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // 删除并释放列表 void delete_list(Node** head) { Node* temp; while (*head != NULL) { temp = *head; *head = (*head)->next; free(temp); // 释放当前节点的空间 } } // 打印顺序表 void print_list(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* head = NULL; int n, value; printf("Enter number of elements: "); scanf("%d", &n); for (int i = 0; i < n; i++) { printf("Enter element %d: ", i + 1); scanf("%d", &value); insert_at_end(&head, value); // 插入元素到末尾 } printf("Sequence List: "); print_list(head); delete_list(&head); // 删除并释放所有节点 return 0; } ``` 在这个例子中,我们首先定义了一个顺序表的节点结构体`Node`,然后创建用于存储数据的新节点并将其加入到链表的末尾。删除节点时遍历链表并将每个节点逐个释放。当不再需要顺序表时,通过调用`delete_list`函数清除所有的节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值