双向循环链表
#include <iostream> using namespace std; struct Link { Link* prior; int data; Link* next; }; Link* InitLink(Link* head) { head = new Link; head->data = 1; head->next = NULL; head->prior = NULL; Link* temp = head; for (int i = 2; i <= 3; ++i) { Link* list = new Link; list->data = i; list->next = NULL; list->prior = NULL; temp->next = list; list->prior = temp; temp = temp->next; } //形成循环链表 temp->next = head; head->prior = temp; return head; } void Display(Link* head) { Link* temp = head; if (temp) { while (temp->next != head) { cout << temp->data << "<->"; temp = temp->next; } cout << temp->data; } } int main() { Link* head = NULL; head = InitLink(head); Display(head); system("PAUSE"); return 0; }
在约瑟夫环问题中如果需要循环进行,这一轮顺时针,下一轮逆时针,在下一局顺时针...这时就能用双向循环链表