struct use case - linked lists

在说linked list数据结构前, 来想象一个场景。

假设要设计一个旅游线路,依次游览几个岛屿。如图,


 

第一种设计思路如下 : 

typedef struct {
  char * name;
  char * opens;
  char * closes;
}
// 使用数组存储岛屿信息, 行程按照数组下标顺序进行.
island tour[4];

但是这里有两个明显的问题 : 

1. 扩展性问题, 如果临时改变行程增加游览的岛屿怎么办? 比如现在是游览4个岛屿, 要变成5个.

2. 灵活性问题, 如果游览的顺序改变怎么办? 那就要做数组的位置交换. 如图 : 

那么有没有好的数据结构来解决这两个问题呢?

当然是有的, 就是这里要谈到的linked list. 也是一种递归结构. 就是结构体中存储了指向当前结构体的指针.

typedef struct island {
  char *name;
  char *opens;
  char *closes;
  struct island *next;
} island;

如图 : 


 

代码如下 : 

[root@db-172-16-3-150 zzz]# cat g.c
#include <stdio.h>

typedef struct island {
  char *name;
  char *opens;
  char *closes;
  struct island *next;
} island;

void display(island *start)
{
  island *i = start;
  for (; i != NULL; i = i->next) {
    fprintf(stdout, "Name: %s open: %s - %s\n", i->name, i->opens, i->closes);
  }
}

int main() {
  island amity = {"Amity", "09:00", "17:00", NULL};
  island craggy = {"Craggy", "09:00", "17:00", NULL};
  island isla_nublar = {"Isla Nublar", "09:00", "17:00", NULL};
  island shutter = {"Shutter", "09:00", "17:00", NULL};

  amity.next = &craggy;
  craggy.next = &isla_nublar;
  isla_nublar.next = &shutter;

  // 改变行程, 新增一个浏览岛屿, 放在isia nublar岛屿与shutter之间
  island skull = {"Skull", "09:00", "17:00", NULL};
  isla_nublar.next = &skull;
  skull.next = &shutter;

  // 显示行程
  display(&amity);
  return 0;
}

执行结果 :
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./g.c -o g && ./g
Name: Amity open: 09:00 - 17:00
Name: Craggy open: 09:00 - 17:00
Name: Isla Nublar open: 09:00 - 17:00
Name: Skull open: 09:00 - 17:00
Name: Shutter open: 09:00 - 17:00

【其他数据结构】

时间: 2024-12-01 07:15:17

struct use case - linked lists的相关文章

[LeetCode]160.Intersection of Two Linked Lists

[题目] Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have

[LeetCode] Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. 解题思路 首先把两个链表的所有非空结点入栈,然后比较栈顶元素并出栈,直到一个栈为空或者栈顶元素不相等,此时上一次比较的结点即为相交结点. 实现代码 /*****

【LeetCode从零单排】No.160 Intersection of Two Linked Lists

题目 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have n

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译 合并K个已排序的链表,并且将其排序并返回. 分析和描述其复杂性. 原文 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 代码 我们采用分治的方法来解决这个问题,其有K个链表,不断将其划分(partition),再将其归并(merge). 划分的部分并不难,将其不断分成两部分,但是需要注意的是可能出现start和end相等的情况,这时候就直接r

[LeetCode]21.Merge Two Sorted Lists

[题目] Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 21.Merge Two Sorted L

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的数组)

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode

[LeetCode]23.Merge k Sorted Lists

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 23.Merge k Sorted Lists * 来源:https://oj.leetcode.com/problems/me

Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:开始做过两两合并的链表,此时做k路合并,即将k个链表进行合并,可以先将这k个链表进行两两合并,知道合并为一个链表.直接调用前面写过的两两合并的例子.   C++代码实现: #include<iostream> #include<new> #include<vector>

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. C++代码如下: #include<iostream> #include<new> using namespace std; //Definition for singly-linked list. s