五种编程语言解释数据结构与算法—特殊链表
目录
1、双向链表介绍
1.1、为什么要引入双向链表?
解决单链表在插入时,需要找到当前结点的驱节点,于是乎需要一次遍历的操作。为进一步的简化时间复杂度,我们在这里引入了双向链表。
1.2、双向链表的逻辑结构示意图
1.3、插入操作图解
该算法的时间复杂度为O(1)。由于尾结点的结构与其他位置都不相同,因此,针对尾结点进行插入操作时,需要分类讨论。
1.4、删除操作图解
该算法的时间复杂度为O(1)。由于尾结点的结构与其他位置都不相同,因此,针对尾结点进行删除操作时,需要分类讨论。
2、循环链表介绍
2.1、循环单链表介绍
因为当辅助指针在尾部时,无法找到头指针。而头指针找到尾部时,也需要一次遍历。如果是循环链表,该项操作只需要向下寻找便可以很方便的找到头部。因此,仅设尾指针操作效率会更高。
2.2、循环双链表介绍
2.3、如何判断空表?
3、静态链表介绍
静态链表是由数组来实现的,本质上就是将单链表的指针域改变为了数据下标。并不常用。
4、顺序表 VS 链表
4.1、存放方式不同
- 顺序表可以实现顺序存取和随机存取
- 单链表只能实现顺序存取
4.2、逻辑结构和物理结构不同
- 顺序表逻辑相邻物理上也相邻,通过相邻表示逻辑关系
- 单链表逻辑相邻物理上不一定相邻,通过指针表示逻辑关系
4.3、基本操作的差异性
4.3.1、插入和删除操作的差异性
4.3.2、查找操作
4.4、内存空间上的差异性
4.5、怎样选择线性表的存储结构?
5、线性表的三个常用操作
5.1、求最值
5.1.1、顺序表中
5.1.2、单链表中
5.2、逆置线性表
5.2.1、顺序表中
5.2.2、单链表中
5.3、归并线性表
5.3.1、归并无序线性表
5.3.2、归并有序线性表
5.3.2.1、顺序表中
5.3.2.2、单链表表中
6、C语言实现循环链表
6.1、CircleLinkList.h文件中的内容
#include "CircleLinkList.h"
typedef struct PERSON {
CircleLinkNode node;
char name[64];
int age;
int score;
}Person;
void MyPrint(CircleLinkNode* data) {
Person* p = (Person*)data;
printf("Name:%s,Age:%d,Score:%d
",p->name,p->age,p->score);
}
int MyCompare(CircleLinkNode* data1,CircleLinkNode* data2) {
Person* p1 = (Person*)data1;
Person* p2 = (Person*)data2;
if(strcmp(p1->name,p2->name) == 0 && p1->age == p2->age && p1->score == p2->score) {
return MY_TRUE;
}
return MY_FALSE;
}
int main() {
//创建循环链表
CircleLinkList* list = Init_CircleLinkList();
//创建数据
Person p1,p2,p3,p4,p5;
strcpy(p1.name,"aaa");
strcpy(p2.name,"bbb");
strcpy(p3.name,"ccc");
strcpy(p4.name,"ddd");
strcpy(p5.name,"eee");
p1.age = 11;
p2.age = 12;
p3.age = 13;
p4.age = 14;
p5.age = 15;
p1.score = 30;
p2.score = 40;
p3.score = 50;
p4.score = 60;
p5.score = 70;
//数据入链表
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p1);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p2);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p3);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p4);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p5);
//打印
Print_CircleLinkList(list,MyPrint);
printf("=============================");
Person pDel;
strcpy(pDel.name,"ccc");
pDel.age = 13;
pDel.score = 50;
//删除
RemoveByValue_CircleLinkList(list,(CircleLinkNode*)&pDel,MyCompare);
//打印
Print_CircleLinkList(list,MyPrint);
//释放内存
FreeSpace_CircleLinkList(list);
return 0;
}
6.2、CircleLinkList.c文件中的内容
#include "CircleLinkList.h"
//初始化函数
CircleLinkList* Init_CircleLinkList() {
CircleLinkList* list = (CircleLinkList*)malloc(sizeof(CircleLinkList));
list->head.next = &(list->head);
list->size = 0;
return list;
}
//插入函数
void Insert_CircleLinkList(CircleLinkList* list,int pos,CircleLinkNode* data) {
if(list == NULL) return;
if(data == NULL) return;
if(pos < 1 || pos > list->size + 1) {
pos = list->size + 1;
}
//根据位置查找插入位置的前一个结点
//辅助指针变量
CircleLinkNode* pCurrent = &(list->head);
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent->next;
}
//新数据入链表
data->next = pCurrent->next;
pCurrent->next = data;
list->size++;
}
//获得第一个元素
CircleLinkNode* Front_CircleLinkList(CircleLinkList* list) {
if(list == NULL) return NULL;
return list->head.next;
}
//根据位置删除
void RemoveByPos_CircleLinkList(CircleLinkList* list,int pos) {
if(list == NULL) return;
if(pos < 1 || pos > list->size) return;
//根据pos找前一个结点
//辅助指针变量
CircleLinkNode* pCurrent = &(list->head);
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent->next;
}
//缓存当前结点的下一个结点
CircleLinkNode* pNext = pCurrent->next;
pCurrent->next = pNext->next;
pNext->next = NULL;
list->size--;
}
//根据值删除
void RemoveByValue_CircleLinkList(CircleLinkList* list,CircleLinkNode* data,COMPARENODE compare) {
if(list == NULL) return;
if(data == NULL) return;
CircleLinkNode* pPrev = &(list->head);
CircleLinkNode* pCurrent = pPrev->next;
for(int i = 1; i <= list->size;i++) {
if(compare(pCurrent,data) == MY_TRUE) {
pPrev->next = pCurrent->next;
pCurrent->next = NULL;
list->size--;
break;
}
pPrev= pCurrent;
pCurrent = pPrev->next;
}
}
//获得链表的长度
int Size_CircleLinkList(CircleLinkList* list) {
if(list == NULL) return -1;
return list->size;
}
//判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList* list) {
if(list->size == 0) {
return MY_TRUE;
}
return MY_FALSE;
}
//查找
int Find_CircleLinkList(CircleLinkList* list,CircleLinkNode* data,COMPARENODE compare) {
if(list == NULL) return -1;
if(data == NULL) return -1;
CircleLinkNode* pCurrent = list->head.next;
int flag = -1;
for(int i = 1;i <= list->size;i++) {
if(compare(pCurrent,data) == MY_TRUE) {
flag = i;
break;
}
pCurrent = pCurrent->next;
}
return flag;
}
//打印
void Print_CircleLinkList(CircleLinkList* list,PRINTNODE print) {
if(list == NULL) return;
CircleLinkNode* pCurrent = list->head.next;
int flag = -1;
for(int i = 1;i <= list->size * 3;i++) {
if(pCurrent == &(list->head)) {
pCurrent = pCurrent->next;
printf("------------------------------------------
");
}
print(pCurrent);
pCurrent = pCurrent->next;
}
}
//释放内存
void FreeSpace_CircleLinkList(CircleLinkList* list) {
if(list == NULL) return;
free(list);
}
6.3、main.c文件中的内容
#include "CircleLinkList.h"
typedef struct PERSON {
CircleLinkNode node;
char name[64];
int age;
int score;
}Person;
void MyPrint(CircleLinkNode* data) {
Person* p = (Person*)data;
printf("Name:%s,Age:%d,Score:%d
",p->name,p->age,p->score);
}
int MyCompare(CircleLinkNode* data1,CircleLinkNode* data2) {
Person* p1 = (Person*)data1;
Person* p2 = (Person*)data2;
if(strcmp(p1->name,p2->name) == 0 && p1->age == p2->age && p1->score == p2->score) {
return MY_TRUE;
}
return MY_FALSE;
}
int main() {
//创建循环链表
CircleLinkList* list = Init_CircleLinkList();
//创建数据
Person p1,p2,p3,p4,p5;
strcpy(p1.name,"aaa");
strcpy(p2.name,"bbb");
strcpy(p3.name,"ccc");
strcpy(p4.name,"ddd");
strcpy(p5.name,"eee");
p1.age = 11;
p2.age = 12;
p3.age = 13;
p4.age = 14;
p5.age = 15;
p1.score = 30;
p2.score = 40;
p3.score = 50;
p4.score = 60;
p5.score = 70;
//数据入链表
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p1);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p2);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p3);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p4);
Insert_CircleLinkList(list,100,(CircleLinkNode*)&p5);
//打印
Print_CircleLinkList(list,MyPrint);
printf("=============================
");
Person pDel;
strcpy(pDel.name,"ccc");
pDel.age = 13;
pDel.score = 50;
//删除
RemoveByValue_CircleLinkList(list,(CircleLinkNode*)&pDel,MyCompare);
//打印
Print_CircleLinkList(list,MyPrint);
//释放内存
FreeSpace_CircleLinkList(list);
return 0;
}
6.4、输出结果
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ccc,Age:13,Score:50
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
------------------------------------------
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ccc,Age:13,Score:50
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
------------------------------------------
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ccc,Age:13,Score:50
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
=============================
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
------------------------------------------
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
------------------------------------------
Name:aaa,Age:11,Score:30
Name:bbb,Age:12,Score:40
Name:ddd,Age:14,Score:60
Name:eee,Age:15,Score:70
Process finished with exit code 0
7、C++语言来实现循环链表
7.1、CircleLinkList.h文件中的内容
#ifndef XGP_STUDY_DEMO46_CIRCLELINKLIST_H
#define XGP_STUDY_DEMO46_CIRCLELINKLIST_H
#include <iostream>
using namespace std;
class CircleLinkNode {
public:
CircleLinkNode* next;
};
//比较回调
typedef bool(*COMPARENODE)(CircleLinkNode*,CircleLinkNode*);
//打印回调
typedef void(*PRINTNODE)(CircleLinkNode*);
class CircleLinkList {
public:
CircleLinkNode head;
int size;
//初始化函数
CircleLinkList* Init_CircleLinkList();
//插入函数
void Insert_CircleLinkList(int pos,CircleLinkNode* data);
//获得第一个元素
CircleLinkNode* Front_CircleLinkList();
//根据位置删除
void RemoveByPos_CircleLinkList(int pos);
//根据值删除
void RemoveByValue_CircleLinkList(CircleLinkNode* data,COMPARENODE compare);
//获得链表的长度
int Size_CircleLinkList();
//判断是否为空
int IsEmpty_CircleLinkList();
//查找
int Find_CircleLinkList(CircleLinkNode* data,COMPARENODE compare);
//打印
void Print_CircleLinkList(PRINTNODE print);
//释放内存
void FreeSpace_CircleLinkList();
};
#endif //XGP_STUDY_DEMO46_CIRCLELINKLIST_H
7.2、CircleLinkList.cpp文件中的内容
#include "CircleLinkList.h"
CircleLinkList *CircleLinkList::Init_CircleLinkList() {
this->head.next = &(this->head);
this->size = 0;
return this;
}
void CircleLinkList::Insert_CircleLinkList(int pos, CircleLinkNode* data) {
if(this == NULL) return;
if(data == NULL) return;
if(pos < 1 || pos > this->size + 1) {
pos = this->size + 1;
}
//根据位置查找插入位置的前一个结点
//辅助指针变量
CircleLinkNode* pCurrent = &(this->head);
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent->next;
}
//新数据入链表
data->next = pCurrent->next;
pCurrent->next = data;
this->size++;
}
CircleLinkNode *CircleLinkList::Front_CircleLinkList() {
if(this == NULL) return NULL;
return this->head.next;
}
void CircleLinkList::RemoveByPos_CircleLinkList(int pos) {
if(this == NULL) return;
if(pos < 1 || pos > this->size) return;
//根据pos找前一个结点
//辅助指针变量
CircleLinkNode* pCurrent = &(this->head);
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent->next;
}
//缓存当前结点的下一个结点
CircleLinkNode* pNext = pCurrent->next;
pCurrent->next = pNext->next;
pNext->next = NULL;
this->size--;
}
void CircleLinkList::RemoveByValue_CircleLinkList(CircleLinkNode *data, COMPARENODE compare) {
if(this == NULL) return;
if(data == NULL) return;
CircleLinkNode* pPrev = &(this->head);
CircleLinkNode* pCurrent = pPrev->next;
for(int i = 1; i <= this->size;i++) {
if(compare(pCurrent,data) == 0) {
pPrev->next = pCurrent->next;
pCurrent->next = NULL;
this->size--;
break;
}
pPrev= pCurrent;
pCurrent = pPrev->next;
}
}
int CircleLinkList::Size_CircleLinkList() {
if(this == NULL) return -1;
return this->size;
}
int CircleLinkList::IsEmpty_CircleLinkList() {
if(this->size == 0) {
return true;
}
return false;
}
int CircleLinkList::Find_CircleLinkList(CircleLinkNode *data, COMPARENODE compare) {
if(this == NULL) return -1;
if(data == NULL) return -1;
CircleLinkNode* pCurrent = this->head.next;
int flag = -1;
for(int i = 1;i <= this->size;i++) {
if(compare(pCurrent,data)) {
flag = i;
break;
}
pCurrent = pCurrent->next;
}
return flag;
}
void CircleLinkList::Print_CircleLinkList(PRINTNODE print) {
if(this == NULL) return;
CircleLinkNode* pCurrent = this->head.next;
int flag = -1;
for(int i = 1;i <= this->size * 3;i++) {
if(pCurrent == &(this->head)) {
pCurrent = pCurrent->next;
printf("------------------------------------------
");
}
print(pCurrent);
pCurrent = pCurrent->next;
}
}
void CircleLinkList::FreeSpace_CircleLinkList() {
if(this == NULL) return;
delete this;
}
7.3、main.cpp文件中的内容
#include <ostream>
#include "CircleLinkList.h"
class Person {
public:
CircleLinkNode node;
string name;
int age;
int score;
Person(string name, int age, int score) : name(name), age(age), score(score) {
}
friend std::ostream &operator<<(std::ostream &os, const Person &person) {
os << " name: " << person.name << " age: " << person.age << " score: "
<< person.score;
return os;
}
bool operator==(const Person &rhs) const {
return name == rhs.name &&
age == rhs.age &&
score == rhs.score;
}
bool operator!=(const Person &rhs) const {
return !(rhs == *this);
}
};
void MyPrint(CircleLinkNode* data) {
Person* p = (Person*)data;
cout<<*p<<endl;
}
bool MyCompare(CircleLinkNode* data1,CircleLinkNode* data2) {
return data1 == data2;
}
int main() {
//创建循环链表
CircleLinkList* list = new CircleLinkList();
list = list->Init_CircleLinkList();
//创建数据
Person p1("aaa",11,30);
Person p2("bbb",12,40);
Person p3("ccc",13,50);
Person p4("ddd",14,60);
Person p5("eee",15,70);
//数据入链表
list->Insert_CircleLinkList(100,(CircleLinkNode*)&p1);
list->Insert_CircleLinkList(100,(CircleLinkNode*)&p2);
list->Insert_CircleLinkList(100,(CircleLinkNode*)&p3);
list->Insert_CircleLinkList(100,(CircleLinkNode*)&p4);
list->Insert_CircleLinkList(100,(CircleLinkNode*)&p5);
//打印
list->Print_CircleLinkList(MyPrint);
cout<<"================================="<<endl;
Person pDel("ccc",13,50);
//删除
list->RemoveByValue_CircleLinkList((CircleLinkNode*)&pDel,MyCompare);
//打印
list->Print_CircleLinkList(MyPrint);
//释放内存
list->FreeSpace_CircleLinkList();
return 0;
}
7.4、输出结果
name: aaa age: 11 score: 30
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
------------------------------------------
name: aaa age: 11 score: 30
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
------------------------------------------
name: aaa age: 11 score: 30
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
=================================
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
------------------------------------------
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
------------------------------------------
name: bbb age: 12 score: 40
name: ccc age: 13 score: 50
name: ddd age: 14 score: 60
name: eee age: 15 score: 70
Process finished with exit code 0
8、Java语言来实现循环链表
8.1、CircleLinkList.java文件中的内容
package com.xgp.循环列表;
public interface CircleLinkList {
//初始化函数
CircleLinkList Init_CircleLinkList();
//插入函数
void Insert_CircleLinkList(int pos,CircleLinkNode data);
//获得第一个元素
CircleLinkNode Front_CircleLinkList();
//根据位置删除
void RemoveByPos_CircleLinkList(int pos);
//根据值删除
void RemoveByValue_CircleLinkList(CircleLinkNode data);
//获得链表的长度
int Size_CircleLinkList();
//判断是否为空
boolean IsEmpty_CircleLinkList();
//查找
int Find_CircleLinkList(CircleLinkNode data);
//打印
void Print_CircleLinkList();
//释放内存
void FreeSpace_CircleLinkList();
}
8.2、CircleLinkListImpl.java文件中的内容
package com.xgp.循环列表;
public class CircleLinkListImpl implements CircleLinkList {
public CircleLinkNode head;
public int size;
@Override
public CircleLinkList Init_CircleLinkList() {
CircleLinkNode node = new CircleLinkNode();
node.next = null;
this.head = node;
//构成个环
this.head.next = this.head;
this.size = 0;
return this;
}
@Override
public void Insert_CircleLinkList(int pos, CircleLinkNode data) {
if(data == null) return;
if(pos < 1 || pos > this.size + 1) {
pos = this.size + 1;
}
//根据位置查找插入位置的前一个结点
//辅助指针变量
CircleLinkNode pCurrent = this.head;
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent.next;
}
//新数据入链表
data.next = pCurrent.next;
pCurrent.next = data;
this.size++;
}
@Override
public CircleLinkNode Front_CircleLinkList() {
return this.head.next;
}
@Override
public void RemoveByPos_CircleLinkList(int pos) {
if(pos < 1 || pos > this.size) return;
//根据pos找前一个结点
//辅助指针变量
CircleLinkNode pCurrent = this.head;
for(int i = 1;i < pos;i++) {
pCurrent = pCurrent.next;
}
//缓存当前结点的下一个结点
CircleLinkNode pNext = pCurrent.next;
pCurrent.next = pNext.next;
pNext.next = null;
this.size--;
}
@Override
public void RemoveByValue_CircleLinkList(CircleLinkNode data) {
if(data == null) return;
CircleLinkNode pPrev = this.head;
CircleLinkNode pCurrent = pPrev.next;
for(int i = 1; i <= this.size;i++) {
// System.out.println(pCurrent.equals(data));
if(pCurrent.equals(data)) {
pPrev.next = pCurrent.next;
pCurrent.next = null;
this.size--;
break;
}
pPrev= pCurrent;
pCurrent = pPrev.next;
}
}
@Override
public int Size_CircleLinkList() {
return this.size;
}
@Override
public boolean IsEmpty_CircleLinkList() {
if(this.size == 0) {
return true;
}
return false;
}
@Override
public int Find_CircleLinkList(CircleLinkNode data) {
if(data == null) return -1;
CircleLinkNode pCurrent = this.head.next;
int flag = -1;
for(int i = 1;i <= this.size;i++) {
if(pCurrent.equals(data)) {
flag = i;
break;
}
pCurrent = pCurrent.next;
}
return flag;
}
@Override
public void Print_CircleLinkList() {
CircleLinkNode pCurrent = this.head.next;
int flag = -1;
for(int i = 1;i <= this.size * 3;i++) {
if(pCurrent.equals(this.head)) {
pCurrent = pCurrent.next;
System.out.println("------------------------------------------");
}
System.out.println(pCurrent);
pCurrent = pCurrent.next;
}
}
@Override
public void FreeSpace_CircleLinkList() {
this.head = null;
this.size = 0;
System.gc();
}
}
class CircleLinkNode {
public CircleLinkNode next;
@Override
public String toString() {
return "CircleLinkNode{" +
"next=" + next +
'}';
}
}
8.3、Main.java文件中的内容
package com.xgp.循环列表;
import java.util.Objects;
public class Main {
public static void main(String[] args) {
//创建循环链表
CircleLinkList list = new CircleLinkListImpl();
list = list.Init_CircleLinkList();
//创建数据
Person p1 = new Person("aaa",11,30);
Person p2 = new Person("bbb",12,40);
Person p3 = new Person("ccc",13,50);
Person p4 = new Person("ddd",14,60);
Person p5 = new Person("eee",15,70);
//数据入链表
list.Insert_CircleLinkList(100,p1);
list.Insert_CircleLinkList(100,p2);
list.Insert_CircleLinkList(100,p3);
list.Insert_CircleLinkList(100,p4);
list.Insert_CircleLinkList(100,p5);
//打印
list.Print_CircleLinkList();
System.out.println("=================================");
Person pDel = new Person("ccc",13,50);
//删除
list.RemoveByValue_CircleLinkList(pDel);
//打印
list.Print_CircleLinkList();
//释放内存
list.FreeSpace_CircleLinkList();
}
}
class Person extends CircleLinkNode {
public String name;
public int age;
public int score;
public Person(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "name:" + name + " age:" + age + " score:" + score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return age == person.age &&
score == person.score &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, score);
}
}
8.4、输出结果
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ccc age:13 score:50
name:ddd age:14 score:60
name:eee age:15 score:70
------------------------------------------
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ccc age:13 score:50
name:ddd age:14 score:60
name:eee age:15 score:70
------------------------------------------
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ccc age:13 score:50
name:ddd age:14 score:60
name:eee age:15 score:70
=================================
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ddd age:14 score:60
name:eee age:15 score:70
------------------------------------------
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ddd age:14 score:60
name:eee age:15 score:70
------------------------------------------
name:aaa age:11 score:30
name:bbb age:12 score:40
name:ddd age:14 score:60
name:eee age:15 score:70
进程完成,退出码 0
9、JavaScript语言来实现循环链表
9.1、CircleLinkList.js文件中的内容
class CircleLinkNode {
}
class CircleLinkList {
Init_CircleLinkList() {
var node = new CircleLinkNode();
node.next = null;
this.head = node;
//构成个环
this.head.next = this.head;
this.size = 0;
return this;
}
Insert_CircleLinkList(pos,data) {
if(data == null) return;
if(pos < 1 || pos > this.size + 1) {
pos = this.size + 1;
}
//根据位置查找插入位置的前一个结点
//辅助指针变量
var pCurrent = this.head;
for(var i = 1;i < pos;i++) {
pCurrent = pCurrent.next;
}
//新数据入链表
data.next = pCurrent.next;
pCurrent.next = data;
this.size++;
}
Front_CircleLinkList() {
return this.head.next;
}
RemoveByPos_CircleLinkList(pos) {
if(pos < 1 || pos > this.size) return;
//根据pos找前一个结点
//辅助指针变量
var pCurrent = this.head;
for(var i = 1;i < pos;i++) {
pCurrent = pCurrent.next;
}
//缓存当前结点的下一个结点
var pNext = pCurrent.next;
pCurrent.next = pNext.next;
pNext.next = null;
this.size--;
}
RemoveByValue_CircleLinkList(data) {
if(data == null) return;
var pPrev = this.head;
var pCurrent = pPrev.next;
for(var i = 1; i <= this.size;i++) {
if(pCurrent.name == data.name && pCurrent.age == data.age && pCurrent.score == data.score) {
pPrev.next = pCurrent.next;
pCurrent.next = null;
this.size--;
break;
}
pPrev= pCurrent;
pCurrent = pPrev.next;
}
}
Size_CircleLinkList() {
return this.size;
}
IsEmpty_CircleLinkList() {
if(this.size == 0) {
return true;
}
return false;
}
Find_CircleLinkList(data) {
if(data == null) return -1;
var pCurrent = this.head.next;
var flag = -1;
for(var i = 1;i <= this.size;i++) {
if(pCurrent == data) {
flag = i;
break;
}
pCurrent = pCurrent.next;
}
return flag;
}
Print_CircleLinkList() {
var pCurrent = this.head.next;
for(var i = 1;i <= this.size * 3;i++) {
if(pCurrent.next == this.head) {
console.log(pCurrent);
pCurrent = pCurrent.next.next;
if(i != this.size * 3) {
console.log("------------------------------------------");
}
}else {
console.log(pCurrent);
pCurrent = pCurrent.next;
}
}
}
FreeSpace_CircleLinkList() {
this.head = null;
this.size = 0;
}
}
9.2、CircleLinkList.html文件中的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="CircleLinkList.js"></script>
</head>
<body>
<script>
class Person {
constructor(name,age,score) {
this.name = name;
this.age = age;
this.score = score;
}
}
//创建循环链表
var list = new CircleLinkList();
list = list.Init_CircleLinkList();
//创建数据
var p1 = new Person("aaa",11,30);
var p2 = new Person("bbb",12,40);
var p3 = new Person("ccc",13,50);
var p4 = new Person("ddd",14,60);
var p5 = new Person("eee",15,70);
//数据入链表
list.Insert_CircleLinkList(100,p1);
list.Insert_CircleLinkList(100,p2);
list.Insert_CircleLinkList(100,p3);
list.Insert_CircleLinkList(100,p4);
list.Insert_CircleLinkList(100,p5);
//打印
list.Print_CircleLinkList();
console.log("=================================");
var pDel = new Person("ccc",13,50);
//删除
list.RemoveByValue_CircleLinkList(pDel);
//打印
list.Print_CircleLinkList();
//释放内存
list.FreeSpace_CircleLinkList();
</script>
</body>
</html>
9.3、输出结果
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ccc", age: 13, score: 50, next: null }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
------------------------------------------ CircleLinkList.js:111:29
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ccc", age: 13, score: 50, next: null }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
------------------------------------------ CircleLinkList.js:111:29
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ccc", age: 13, score: 50, next: null }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
================================= CircleLinkList.html:42:17
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
------------------------------------------ CircleLinkList.js:111:29
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
------------------------------------------ CircleLinkList.js:111:29
Object { name: "aaa", age: 11, score: 30, next: {…} }
CircleLinkList.js:114:25
Object { name: "bbb", age: 12, score: 40, next: {…} }
CircleLinkList.js:114:25
Object { name: "ddd", age: 14, score: 60, next: {…} }
CircleLinkList.js:114:25
Object { name: "eee", age: 15, score: 70, next: {…} }
CircleLinkList.js:108:25
10、Python语言来实现循环链表
10.1、CircleLinkList.py文件中的内容
class CircleLinkNode:
pass
class CircleLinkList:
# 初始化操作
def Init_CircleLinkList(self):
self.head = CircleLinkNode()
self.head.next = self.head
self.size = 0
return self
def Insert_CircleLinkList(self,pos,data):
if(data == None):
return
if(pos < 1 or pos > self.size + 1):
pos = self.size + 1
# //根据位置查找插入位置的前一个结点
# //辅助指针变量
pCurrent = self.head
for i in range(1,pos):
pCurrent = pCurrent.next
# //新数据入链表
data.next = pCurrent.next
pCurrent.next = data
self.size += 1
def Front_CircleLinkList(self):
return self.head.next
def RemoveByPos_CircleLinkList(self,pos):
if(pos < 1 or pos > self.size):
return
# //根据pos找前一个结点
# //辅助指针变量
pCurrent = self.head
for i in range(1,pos):
pCurrent = pCurrent.next
# //缓存当前结点的下一个结点
pNext = pCurrent.next
pCurrent.next = pNext.next
pNext.next = None
self.size -= 1
def RemoveByValue_CircleLinkList(self,data):
if (data == None):
return
pPrev = self.head
pCurrent = pPrev.next
for i in range(1,self.size + 1):
if(pCurrent.equals(data)):
pPrev.next = pCurrent.next
pCurrent.next = None
self.size -= 1
break
pPrev = pCurrent
pCurrent = pPrev.next
def Size_CircleLinkList(self):
return self.size
def IsEmpty_CircleLinkList(self):
return self.size == 0
def Find_CircleLinkList(self,data):
if(data == None):
return -1
pCurrent = self.head.next
flag = -1
for i in range(1,self.size + 1):
if(pCurrent.equals(data)):
flag = i
break
pCurrent = pCurrent.next
return flag
def Print_CircleLinkList(self):
pCurrent = self.head.next
for i in range(1,self.size*3 + 1):
if(pCurrent == self.head):
pCurrent = pCurrent.next
print("------------------------------")
print(pCurrent)
pCurrent = pCurrent.next
def FreeSpace_CircleLinkList(self):
self.head = None
self.size = 0
10.2、main.py文件中的内容
from CircleLinkList import *
# 定义一个Person类
class Person:
def __init__(self,name,age,score):
self.name = name
self.age = age
self.score = score
def __str__(self) -> str:
return "name: "+self.name+",age:"+str(self.age)+",score:"+str(self.score)
def equals(self,p) -> bool:
return self.name == p.name and self.age == p.age and self.score == p.score
list = CircleLinkList();
list = list.Init_CircleLinkList();
p1 = Person("aaa",11,30)
p2 = Person("bbb",12,40)
p3 = Person("ccc",13,50)
p4 = Person("ddd",14,60)
p5 = Person("eee",15,70)
list.Insert_CircleLinkList(100,p1)
list.Insert_CircleLinkList(100,p2)
list.Insert_CircleLinkList(100,p3)
list.Insert_CircleLinkList(100,p4)
list.Insert_CircleLinkList(100,p5)
list.Print_CircleLinkList()
print("==============================================")
pDel = Person("ccc",13,50)
list.RemoveByValue_CircleLinkList(pDel)
list.Print_CircleLinkList()
list.FreeSpace_CircleLinkList()
10.3、输出结果
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ccc,age:13,score:50
name: ddd,age:14,score:60
name: eee,age:15,score:70
------------------------------
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ccc,age:13,score:50
name: ddd,age:14,score:60
name: eee,age:15,score:70
------------------------------
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ccc,age:13,score:50
name: ddd,age:14,score:60
name: eee,age:15,score:70
==============================================
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ddd,age:14,score:60
name: eee,age:15,score:70
------------------------------
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ddd,age:14,score:60
name: eee,age:15,score:70
------------------------------
name: aaa,age:11,score:30
name: bbb,age:12,score:40
name: ddd,age:14,score:60
name: eee,age:15,score:70
Process finished with exit code 0