#include <stdio.h>
int next[9];
int key[9];
int prev[9];
int L;
int F;
void print_array();
void print_list(int);
void print_lists();
void create();
void exchange(int, int);
void compactify_list();
void swap_next(int, int);
void swap_key(int, int);
void swap_prev(int, int);
void swap(int, int);
void create() {
next[0] = 0;
key[0] = 0;
prev[0] = 0;
L = 7;
next[7] = 5;
key[7] = 9;
prev[7] = 0;
next[5] = 2;
key[5] = 16;
prev[5] = 7;
next[2] = 3;
key[2] = 4;
prev[2] = 5;
next[3] = 0;
key[3] = 1;
prev[3] = 2;
F = 6;
next[6] = 4;
key[6] = -1;
prev[6] = 0;
next[4] = 8;
key[4] = -1;
prev[4] = 6;
next[8] = 1;
key[8] = -1;
prev[8] = 4;
next[1] = 0;
key[1] = -1;
prev[1] = 8;
}
void swap_next(int a, int b) {
int temp;
temp = next[a];
next[a] = next[b];
next[b] = temp;
}
void swap_key(int a, int b) {
int temp;
temp = key[a];
key[a] = key[b];
key[b] = temp;
}
void swap_prev(int a, int b) {
int temp;
temp = prev[a];
prev[a] = prev[b];
prev[b] = temp;
}
void swap(int a, int b) {
swap_next(a, b);
swap_key(a, b);
swap_prev(a, b);
}
void exchange(int h, int x) {
if (next[x] == h) {
if (prev[x] != 0)
next[prev[x]]= h;
if (next[h] != 0)
prev[next[h]] = x;
swap_key(h, x);
next[x] = next[h];
prev[h] = prev[x];
prev[x] = h;
next[h] = x;
} else {
if (prev[h] != 0)
next[prev[h]] = x;
if (next[h] != 0)
prev[next[h]] = x;
if (prev[x] != 0)
next[prev[x]] = h;
if (next[x] != 0)
prev[next[x]] = h;
swap(h, x);
}
}
void compactify_list() {
int h = 1;
int x = L;
while (x != 0) {
if (x != h) {
if (h == F)
F = x;
exchange(h, x);
}
x = next[h];
h++;
}
L = 1;
}
void print_list(int i) {
while (i != 0) {
printf("index: %d, next: %d, key: %d, prev: %d\n", i, next[i], key[i], prev[i]);
i = next[i];
}
}
void print_lists() {
printf("------- doubly linked list ----------------\n");
print_list(L);
printf("------- free list ----------------\n");
print_list(F);
}
void print_array() {
int i;
printf("------- array ----------------\n");
for (i = 1; i < 9; i++) {
printf("index: %d, next: %d, key: %d, prev: %d\n", i, next[i], key[i], prev[i]);
}
}
int main(int argc, const char *argv[]) {
create();
print_array();
compactify_list();
print_lists();
return 0;
}
分享到:
相关推荐
In this, the third edition, we have once again updated the entire book. The changes cover a broad spectrum, including new chapters, revised pseudocode, and a more active writing style.
Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms...
Algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The book contains 244 figures—many with multiple parts—illustrating how ...
《算法导论》第三版是一本广受欢迎的计算机科学教科书,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein共同撰写。本书详细介绍了算法的概念、设计、分析、以及应用,并且被视为计算机...
"Introduction to Algorithms, the 'bible' of the field, is a comprehensive textbook covering the full spectrum of modern algorithms: from the fastest algorithms and data structures to polynomial-time ...
《算法导论》第二版(Introduction_to_Algorithms_2nd_Edition)是计算机科学领域的一本经典教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein四位作者合作撰写,由麻省理工学院...
Mit Press - Introduction To Algorithms 2Nd Edition Incl Exercises Edition.part1
Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition
《Introduction to algorithms THIRD EDITION》(《算法导论 第三版》)是计算机科学领域中一本极具权威性的教科书,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein四位作者共同撰写...
Introduction to Algorithms, Second Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein ISBN:0262032937 The MIT Press © 2001 (1180 pages) A course in computer ...
Introduction to Algorithms, Third Edition [精装] ~ Thomas H. Cormen (作者), Charles E. Leiserson (作者), Ronald L. Rivest (作者), Clifford Stein (作者) 出版社: The MIT Press; 3 (2009年9月30日) 精装...
Algorithms are described in English and in a "pseudocode" designed to be readable by anyone who has done a little programming. The book contains over 230 figures illustrating how the algorithms work. ...
Mit Press - Introduction To Algorithms 2Nd Edition Incl Exercises Edition.part2.rar
Algorithm-Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore.zip,Robert Lafore第二版的数据结构与算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
《Introduction to Algorithms, Third Edition》是托马斯·H·科曼、查尔斯·E·莱瑟森、罗纳德·L·里维斯特和克莱福德·斯坦共同撰写的一本经典算法教材。这本书自出版以来,就被广泛应用于大学计算机科学专业的...
Introduction to Algorithms, Third Edition 作者: Thomas H.Cormen / Charles E.Leiserson / Ronald L.Rivest / Clifford Stein
Introduction to Genetic Algorithms