静态链表

1. **初始化静态链表:**
   - `Initialize` 函数初始化静态链表,其中0号结点充当“头结点”,不存储具体数据。其他结点的 `next` 初始值为 -2,表示结点为空闲。

2. **插入操作:**
   - `Insert` 函数用于在指定位置插入新元素,找到一个空闲结点,然后将其插入到指定位置。

3. **按值查找操作:**
   - `FindByValue` 函数根据给定的值在链表中查找,返回第一个匹配值的位置。

4. **删除操作:**
   - `Delete` 函数删除指定位置的元素,将要删除的结点标记为空闲。

5. **输出静态链表:**
   - `PrintList` 函数输出静态链表的内容。

6. **主函数演示:**
   - 主函数演示了静态链表的初始化,插入元素,删除元素,以及输出链表内容的过程。

#include <stdio.h>
#define MAX_SIZE 7

typedef int ElemType;

// 定义静态链表结点
typedef struct {
  ElemType data; // 数据元素
  // 下一个结点的索引
  // -1 表示当前结点是链表的尾结点
  // -2,则表示当前结点是空闲结点
  int next;
} SLinkList[MAX_SIZE];

// 初始化静态链表
void Initialize(SLinkList list) {
  // 0号结点充当“头结点”,不具体存放数据
  list[0].next = -1;

  // 初始化其他结点的next为特殊值-2,表示结点空闲
  for (int i = 1; i < MAX_SIZE; i++) {
    list[i].next = -2;
  }
}

// 插入操作
int Insert(SLinkList list, int position, ElemType data) {
  // 找到一个空闲结点
  int freeNode = -1;
  for (int i = 1; i < MAX_SIZE; i++) {
    if (list[i].next == -2) {
      freeNode = i;
      break;
    }
  }

  // 如果没有空闲结点,插入失败
  if (freeNode == -1) {
    printf("插入失败,静态链表已满\n");
    return 0;
  }

  int current = 0; // 从头结点开始
  int count = 0;

  // 找到要插入位置的前一个结点
  // 直到找到前一个结点或者达到链表的末尾
  while (count < position && list[current].next != -1) {
    current = list[current].next;
    count++;
  }

  // 插入新结点
  list[freeNode].data = data; // 将数据存储在新结点
  list[freeNode].next = list[current].next; // 将新结点指向原位置的下一个结点
  list[current].next = freeNode; // 更新前一个结点的指向,使其指向新结点

  return 1;
}

// 按值查找操作
int FindByValue(SLinkList list, ElemType value) {
  int current = list[0].next; // 从第一个数据结点开始
  int position = 1; // 初始化位置

  while (current != -1) {
    // 比较当前结点的数据与目标值
    if (list[current].data == value) {
      // 找到匹配值,返回位置
      return position;
    }

    // 没有找到,继续遍历下一个结点
    current = list[current].next;
    position++;
  }

  // 没有找到匹配值,返回 -1 表示未找到
  return -1;
}

// 删除操作
int Delete(SLinkList list, int position) {
  int current = 0; // 从头结点开始
  int count = 0;

  // 找到要删除位置的前一个结点
  // 直到找到前一个结点或者达到链表的末尾
  while (count < position && list[current].next != -1) {
    current = list[current].next; // 移动到下一个结点
    count++;
  }

  // 判断是否找到要删除的位置
  if (list[current].next == -1 || count < position) {
    printf("删除失败,位置无效\n");
    return 0;
  }

  // 要删除的结点
  int toBeDeleted = list[current].next;

  // 更新前一个结点的指向,跳过要删除的结点
  list[current].next = list[toBeDeleted].next;

  // 标记要删除的结点为空闲结点
  list[toBeDeleted].data = NULL;
  list[toBeDeleted].next = -2;

  return 1;
}

// 输出静态链表
void PrintList(SLinkList list) {
  int current = list[0].next; // 从第一个数据结点开始
  while (current != -1) {
    printf("%d ", list[current].data);
    current = list[current].next;
  }
  printf("\n");
}

int main() {
  SLinkList list;
  Initialize(list);

  Insert(list, 0, 10);
  Insert(list, 1, 20);
  Insert(list, 1, 30);
  Insert(list, 3, 40);

  printf("静态链表内容:\n");
  PrintList(list);

  Delete(list, 1);
  printf("删除第2个元素后的静态链表内容:\n");
  PrintList(list);

  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值