您的位置:首页 > 其它

数组&链表

2016-02-15 21:08 465 查看
线性表:在逻辑上一组具有前后对应关系的元素的集合

特征:

必存在唯一的“第一元素”

必存在唯一的“最后元素”

除最后元素外,其他元素均存在后驱

除第一元素外,其他元素均存在前驱

逻辑形式为 a0a1a2a3a4.....an

数组:计算机底层表达线性表的一种存储方式,是用一组地址连续的存储单元依次存储线性表的数据组织方式

链表:计算机底层表达线性表的另一种存储方式,是用地址非连续的存储单元存储线性表的数据,数据元素的逻辑顺序由链表中的指针连接次序实现。

特点:

链表由一系列结点组成,结点可以在运动时 动态生成,每个结点包含两个部分,一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

链表有多种变种,分别为单向链表,双向链表,循环链表,静态链表。

各结构简单的实现如下:

// Author: Waihui Zheng
// 线性表:有序线性表、无序线性表
// 实现方式:数组,链表
// 链表:单向链表、双向链表、循环链表、静态链表

#include <iostream>

// 建立元素为int类型的数组
void show_array() {
int int_array[10];
for (int i = 0; i < 10; ++i) {
int_array[i] = i;
}

std::cout << "数组: ";
for (int i = 0; i < 10; ++i) {
std::cout << int_array[i] << " ";
}

std::cout << std::endl;
}

// 建立单向链表
struct SinglyLinkedNode {
int value;
SinglyLinkedNode* next;

SinglyLinkedNode(int v = 0) : value(v), next(NULL) {}
};

void show_singly_list() {
SinglyLinkedNode* head = new SinglyLinkedNode(3);
SinglyLinkedNode* current = head;

current->next = new SinglyLinkedNode(1);
current = current->next;

current->next = new SinglyLinkedNode(5);
current = current->next;

std::cout << "单向链表: ";
for (current = head; current != NULL; current = current->next) {
std::cout << current->value << " ";
}
std::cout << std::endl;

current = head;
while (current != NULL) {
SinglyLinkedNode* p = current;
current = current->next;
delete p;
}
}

// 建立双向链表
struct DoubleLinkedNode {
int value;
DoubleLinkedNode* pre;
DoubleLinkedNode* next;

DoubleLinkedNode(int v = 0) : value(v), pre(NULL), next(NULL) {}
};

void show_double_linked_list() {
DoubleLinkedNode* head = new DoubleLinkedNode(7);
DoubleLinkedNode* current = head;

current->next = new DoubleLinkedNode(2);
current->next->pre = current;
current = current->next;

current->next = new DoubleLinkedNode(10);
current->next->pre = current;
current = current->next;

std::cout << "双向链表: ";
for (; current != NULL; current = current->pre) {
std::cout << current->value << " ";
}
std::cout << std::endl;

current = head;
while (current != NULL) {
DoubleLinkedNode* p = current;
current = current->next;
delete p;
}
}

// 单向循环链表
struct CycleSinglyLinkedNode {
int value;
CycleSinglyLinkedNode* next;

CycleSinglyLinkedNode(int v = 0) : value(v), next(NULL) {}
};

void show_cycle_singly_linked_list() {
CycleSinglyLinkedNode* head = new CycleSinglyLinkedNode(3);
CycleSinglyLinkedNode* last = new CycleSinglyLinkedNode(5);
head->next = last;
last->next = head;

last->next = new CycleSinglyLinkedNode(1);
last = last->next;
last->next = head;

std::cout << "单向循环链表: ";
CycleSinglyLinkedNode* cur = head;
do {
std::cout << cur->value << " ";
cur = cur->next;
} while (cur != head);
std::cout << std::endl;

cur = head;
do {
CycleSinglyLinkedNode* p = cur;
cur = cur->next;
delete p;
} while (cur != head);
}

// 静态链表
#define MAXN 100

struct StaticNode {
int value;
int next;
};

StaticNode s_static_linked_list[MAXN];

void show_static_linked_list() {
for (int i = 0; i < MAXN; ++i) {
s_static_linked_list[i].value = 0;
s_static_linked_list[i].next = -1;
}

s_static_linked_list[0].value = 7;
s_static_linked_list[0].next = 5;
s_static_linked_list[5].value = 3;
s_static_linked_list[5].next = 3;
s_static_linked_list[3].value = 10;

std::cout << "静态链表: ";
for (int i = 0; i != -1; i = s_static_linked_list[i].next) {
std::cout << s_static_linked_list[i].value << " ";
}
std::cout << std::endl;
}

int main() {
show_array();
show_singly_list();
show_double_linked_list();
show_cycle_singly_linked_list();
show_static_linked_list();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: