#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.... - Charles E.... - Ronald L.... - Clifford Stein:哥伦比亚大学工业工程与运筹学副教授。...
- **书名**:《算法导论》第三版(Introduction to Algorithms, Third Edition) - **作者**:Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein - **出版社**:MIT Press - **出版日期**...
《算法导论》第三版是一本广受欢迎的计算机科学教科书,由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 ...
Chapter 5: Probabilistic Analysis and Randomized Algorithms Lecture Notes 5-1 Solutions 5-9 Chapter 6: Heapsort Lecture Notes 6-1 Solutions 6-10 Chapter 7: Quicksort Lecture Notes 7-1 Solutions 7-9 ...
《算法导论》第二版(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 the Design and Analysis of Algorithms 算法分析与设计基础 官方答案 适用于本书第二版 不是有些复制,粘贴到word里的答案,包含题目tips和详细分析 仅限1-10章
Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition
Fulkerson style “augmenting paths.” We benchmark these algorithms on a number of typical graphs in the contexts of image restoration, stereo, and segmentation. In many cases, our new algorithm works...
《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