链表简介
 链表是一种物理存储单元上不连续的存储结构,数据元素之间是通过链表中的指针
进行链接。链表是由一系列的节点(链表中每一个元素称为节点)组成,节点可以在运
行时动态生成。
每一个节点都包含两个部分:一个是存储数据的数据域,另一个是存储下一个节点
地址的指针域。
一般链表在一些需要快速插入/删除,而不太关心或者不需要随机访问的情况下使用。
链表相对于数组:链表允许在任意位置插入或删除节点,但是链表不支持随机访 问节点,只能从头节点逐个访问(遍历)每个节点。
链表分三种:单向链表、双向链表以及循环链表,分别如下图所示:
1、单向链表
 单向链表是链表中最简单的链表,它包含两个域:一个数据域和一个指针域,它
只可以向一个方向遍历。
2、双向链表
双向链表中每个节点中有数据域和两个指针域,前面的指针指向前一个节点,后一
个指针指向下一个节点。
3、循环链表
循环链表中,首节点和末节点被连接在一起,它也可以被视为“无头无尾”。这种 链表比较利于数据存储缓存。
【示例1-1】创建链表并输出链表
#include <iostream>
using namespace std;
// typedef struct node{
char name[20];
int age;
struct node *next;//
}Student;
//
#pragma mark Student* createList(int n)
{
Student *head = new Student; //
Student *pre = head; // pre for (int i = 0; i<n; i++)
{
Student *p = new Student;
printf("%d:",i+1); cin >> p->name;
cin >> p->age;
pre->next = p; //next
pre = p; //pre
p->next = NULL; //nextNULL }
return head; // }
void display(Student* head)
{
Student* p = head->next;
while (p) {
cout<<p->name<<endl;
p = p->next; }
}
int main(int argc, const char * argv[]) {
Student *head = createList(5);
display(head);
return 0;
}
【示例1-2】插入节点
 插入节点就是让一个特定的节点的指针链接我们新创建的节点,并且让新创建的节
点的指针链接原特定节点的下一个节点。
int length(Student *head)
{
int len = 0;
Student *p = head->next;
while (p != NULL) {
len++;
p = p->next; }
return len; }
16年8月19日 星期五
void insertElement(Student *head,int index)
{
if (index < 0 || index > length(head)) {
throw "out of range!";
}
Student *pre = head; //
for (int i = 0; i<index; i++) {
pre = pre->next;
}
Student *newNode = new Student;
cout<<":"; cin>>newNode->name>>newNode->age;
newNode->next = pre->next;
pre->next = newNode;
}
int main(int argc, const char * argv[]) {
Student *head = createList(5);
insertElement(head,1);
display(head);
return 0; }
【示例1-3】删除节点
 删除节点就是让删除节点的上一个节点指向删除节点的下一个节点,再把当前节点
空间释放掉。

void removeElement(Student *head,int index)
{
if (index < 0 || index > length(head)) {
throw "out of range!";
}
Student *pre = head; // for (int i = 0; i<index; i++) {
pre = pre->next;
}
Student *p = pre->next; //p pre->next = pre->next->next;
delete p; }
int main(int argc, const char * argv[]) {
Student *head = createList(5);
removeElement(head,1);
display(head);
return 0; }
 链表是一种物理存储单元上不连续的存储结构,数据元素之间是通过链表中的指针
进行链接。链表是由一系列的节点(链表中每一个元素称为节点)组成,节点可以在运
行时动态生成。
每一个节点都包含两个部分:一个是存储数据的数据域,另一个是存储下一个节点
地址的指针域。
一般链表在一些需要快速插入/删除,而不太关心或者不需要随机访问的情况下使用。
链表相对于数组:链表允许在任意位置插入或删除节点,但是链表不支持随机访 问节点,只能从头节点逐个访问(遍历)每个节点。
链表分三种:单向链表、双向链表以及循环链表,分别如下图所示:
1、单向链表
 单向链表是链表中最简单的链表,它包含两个域:一个数据域和一个指针域,它
只可以向一个方向遍历。
2、双向链表
双向链表中每个节点中有数据域和两个指针域,前面的指针指向前一个节点,后一
个指针指向下一个节点。
3、循环链表
循环链表中,首节点和末节点被连接在一起,它也可以被视为“无头无尾”。这种 链表比较利于数据存储缓存。
【示例1-1】创建链表并输出链表
#include <iostream>
using namespace std;
// typedef struct node{
char name[20];
int age;
struct node *next;//
}Student;
//
#pragma mark Student* createList(int n)
{
Student *head = new Student; //
Student *pre = head; // pre for (int i = 0; i<n; i++)
{
Student *p = new Student;
printf("%d:",i+1); cin >> p->name;
cin >> p->age;
pre->next = p; //next
pre = p; //pre
p->next = NULL; //nextNULL }
return head; // }
void display(Student* head)
{
Student* p = head->next;
while (p) {
cout<<p->name<<endl;
p = p->next; }
}
int main(int argc, const char * argv[]) {
Student *head = createList(5);
display(head);
return 0;
}
【示例1-2】插入节点
 插入节点就是让一个特定的节点的指针链接我们新创建的节点,并且让新创建的节
点的指针链接原特定节点的下一个节点。
int length(Student *head)
{
int len = 0;
Student *p = head->next;
while (p != NULL) {
len++;
p = p->next; }
return len; }
16年8月19日 星期五
void insertElement(Student *head,int index)
{
if (index < 0 || index > length(head)) {
throw "out of range!";
}
Student *pre = head; //
for (int i = 0; i<index; i++) {
pre = pre->next;
}
Student *newNode = new Student;
cout<<":"; cin>>newNode->name>>newNode->age;
newNode->next = pre->next;
pre->next = newNode;
}
int main(int argc, const char * argv[]) {
Student *head = createList(5);
insertElement(head,1);
display(head);
return 0; }
【示例1-3】删除节点
 删除节点就是让删除节点的上一个节点指向删除节点的下一个节点,再把当前节点
空间释放掉。

void removeElement(Student *head,int index)
{
if (index < 0 || index > length(head)) {
throw "out of range!";
}
Student *pre = head; // for (int i = 0; i<index; i++) {
pre = pre->next;
}
Student *p = pre->next; //p pre->next = pre->next->next;
delete p; }
int main(int argc, const char * argv[]) {
Student *head = createList(5);
removeElement(head,1);
display(head);
return 0; }