鱼C论坛

 找回密码
 立即注册
查看: 461|回复: 2

怎么写该线性表

[复制链接]
发表于 2023-6-19 10:05:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
A       0   1   2   3   4   5   6   7
data       60  50 78 90  34      40
next  3   5    7   2   0   4        1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-19 15:48:24 | 显示全部楼层
linear_table = {
    0: {'data': 60, 'next': 3},
    1: {'data': 50, 'next': 5},
    2: {'data': 78, 'next': 7},
    3: {'data': 90, 'next': 2},
    4: {'data': 34, 'next': 0},
    5: {'data': None, 'next': 4},
    6: {'data': 40, 'next': None},
    7: {'data': None, 'next': 1}
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 13:45:58 | 显示全部楼层
参考:

  1. typedef struct Node {
  2.     int data;
  3.     struct Node *next;
  4. } Node;

  5. typedef struct LinkedList {
  6.     Node *head;  // 头结点
  7. } LinkedList;

  8. // 初始化链表
  9. LinkedList* init() {
  10.     // 申请头结点空间
  11.     Node* head = (Node*)malloc(sizeof(Node));
  12.     head->next = head;  // 头结点的next指向自己,构成循环链表
  13.     LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
  14.     list->head = head;
  15.     return list;
  16. }

  17. // 插入节点
  18. void insert(LinkedList* list, int pos, int data) {
  19.     // 找到pos的前一个节点
  20.     Node* pre = list->head;
  21.     for (int i = 1; i <= pos - 1; i++) {
  22.         pre = pre->next;
  23.     }
  24.    
  25.     // 申请新节点空间
  26.     Node* node = (Node*)malloc(sizeof(Node));
  27.     node->data = data;
  28.    
  29.     // 将新节点next指向pos节点
  30.     node->next = pre->next;
  31.     pre->next = node;
  32. }

  33. // 打印链表
  34. void print(LinkedList* list) {
  35.     Node* cur = list->head->next;
  36.     while (cur != list->head) {
  37.         printf("%d ", cur->data);
  38.         cur = cur->next;
  39.     }
  40. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 16:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表