问题描述
到底为什么我们需要一个循环链表(单或双)数据结构?
Why exactly do we need a "Circular Linked List" (singly or doubly) data structure?
什么问题它解决是简单的链表(单或双)明显?
What problem does it solve that is evident with simple Linked Lists (singly or doubly)?
推荐答案
一个简单的例子是跟踪的该轮到谁了一个多玩家棋盘游戏。放在一个循环链表的所有球员。当一个玩家需要轮到他,提前到列表中的下一个玩家。这将导致无限期玩家之间的程序循环。
A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players.
要遍历一个循环链表,存储指向你看到的第一要素。当你再次看到该元素,你走过整个列表。
To traverse a circular linked list, store a pointer to the first element you see. When you see that element again, you have traversed the entire list.
void traverse(CircularList *c) {
CircularList start = c;
do {
operateOnNode(c);
c = c->next;
} while(c != start);
}
这篇关于到底为什么我们需要一个"循环链表" (单或双)数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!