数据结构——数组
本文最后更新于 151 天前,其中的信息可能已经有所发展或是发生改变。

数据结构——数组

1.文件注释内容

/**
 * @filename:    seqlist.c
 * @brief:       seqlist
 * @author:      kun
 * @date:        2026/1/30
 * @version:     ver1.0
 * @note         none
 * CopyRight (c) 2026   name  All Right Reseverd
 */

2.头文件

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

3.定义管理结构体和初始化队列

//定义管理结构体
typedef int ElementType;
typedef struct SequenceList
{
    ElementType *addr;
    unsigned int size;
    int Last;
}Seqlist_t;

//队列初始化
Seqlist_t * seqlist_init(unsigned int size)
{
    Seqlist_t *list = (Seqlist_t *)calloc(1,sizeof(Seqlist_t));
    if (list == NULL)
    {
        exit(-1);
    }
    list->addr = (ElementType *)calloc(size,sizeof(ElementType));
    if (list->addr == NULL)
    {
        free(list);
        exit(-1);
    }
    list->size = size;
    list->Last = -1;
    return list;
}

4.头插法

//头插法
bool addhead(Seqlist_t *list, ElementType element)
{
  //队列已满
  if(list->size == list->Last + 1)
    {   
        printf("队列已满,添加失败!\n");
        return false;
    }

  //队列未满
  for(int i = list->Last; i >= 0; i--)
  {
    list->addr[i+1] = list->addr[i];
  }
  list->addr[0] = element;
  list->Last++;
  return true;  
}

5.尾插法

//尾插法
bool addlast(Seqlist_t *list, ElementType element)
{   
    //队列已满
    if(list->size == list->Last + 1)//队列下标从0开始
    {   
        printf("队列已满,添加失败!\n");
        return false;
    }

    //队列未满
    list->addr[++list->Last] = element;
    return true;
}

6.删除元素

//删除元素
bool seqlistdel(Seqlist_t *list, ElementType element)
{ 
  //队列为空
  if(list->Last == -1)
  {
    printf("队列为空,删除失败\n");
    return false;
  }

  //目标值在尾部
  if(list->addr[list->Last] == element)
  {
    list->Last--;
    return true;
  }

  //目标值在头部或中间
  for(int i = 0; i <= list->Last; i++)
  {
    if(list->addr[i] == element)
    {
      for(int j = i; j < list->Last; j++)
      {
        list->addr[j] = list->addr[j+1];
      }
      list->Last--;
      return true;
    }
  }
  printf("目标值不存在!\n");
  return false;
}

7.内存释放

//内存释放
void seqlist_destroy(Seqlist_t *list)
{
    if (list == NULL) {
        return; // 如果指针为空,无需操作
    }

    // 释放动态数组内存
    if (list->addr != NULL) {
        free(list->addr);
        list->addr = NULL; // 避免野指针
    }

    // 释放结构体内存
    free(list);
    // 注意:无法将传入的 list 指针置为 NULL,需调用者自行置空
}

8.队列遍历

//遍历队列
void seqlist_printfelement(Seqlist_t *list)
{
  for(int i = 0; i <= list->Last; i++)
  {
    printf("%d \n", list->addr[i]);
  }

}

9.元素个数查询

//查询元素个数
int seqlist_length(Seqlist_t *list)
{
  return printf("当前元素个数:%d\n",list->Last+1);
}

9.编写main函数进行测试

int main(void) {
  Seqlist_t *seqlist =  seqlist_init(128);
  addhead(seqlist,10);
  addlast(seqlist,110);
  seqlist_length(seqlist);
  seqlist_printfelement(seqlist);
  seqlist_destroy(seqlist);
  seqlist = NULL; // 手动置空,防止悬空指针
  return 0;
  /*当前元素个数:2
    10 
    110 */
}

评论

  1. jkdjf
    Windows Chrome 145.0.0.0
    7 月前
    2026-3-02 15:54:48

    114514

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇