【数据结构】链表

数组:
数组中元素储存在连续的内存位置,声明数组时可以指定数组的大小,限制数组可以存储的元素数量。
但如果事先不知道元素的大小,可以申请一个足够大的数组,但是内存中可能没有足够大连续内存空间,如何合理地利用内存中的非连续空间呢
链表:一种非常灵活的动态数据结构,不将其元素存储在连续的内存位置中,所以可以任意添加链表元素的数量
链表与数组的不同:链表中的数据在内存中并不是顺序存储的,而是通过在链表的每个元素中,保存指向下一个元素的引用,来找到下一个元素。

struct LNode{
   
	int data;
	struct LNode * next;
};

struct LNode *head, *middle,*last;

head=(LNode *)malloc(sizeof(struct LNode));
middle=(LNode *)malloc(sizeof(struct LNode));
last=(LNode *)malloc(sizeof(struct LNode));

//数据域赋值
head->data=10;
middle->data=20;
last->data=30;

//指针域
head->next=middle;
middle->next=last;
last->next=NULL;

struct LNode *temp=head;

while(temp!=NULL){
   
	print("%d ",temp->data);
	temp=temp->next;
}

data数据域:可以存放该节点对应的任何数据类型,如int、char、float;next指针域:指向链表中下一个节点的引用指针

单向链表

单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。

  • 表元素域elem用来存放具体的数据
  • 链接域next用来存放下一个节点的位置(python中的标识)
  • 变量p指向链表的头节点head的位置,从p出发能找到链表中的任意节点

节点实现

class SingleNode(object):
	"""单链表的节点"""
	def __init__(self,item):
		# _item存放数据元素
		self.item=item
		# _next是下一个节点的标识
		self.next=None

用一个类来表示一个节点

class Node{
   
	constructor(value,next=null){
   
		this.value=value;
		this.next=next;
	}
}

单链表的实现

定义链表class

class LinkedList{
   
	constructor(value){
   
		this.head=new Node(value);
	}
}

单链表的操作

  • is_empty() 链表是否为空
  • length() 链表长度
  • travel() 遍历整个链表
  • add(item) 链表头部添加元素
  • append(item) 链表尾部添加元素
  • insert(pos,item) 指定位置添加元素
  • remove(item) 删除节点
  • search(item) 查找节点是否存在

给定一个值,查找对应的节点

findNode(value){
   
	let currentNode=this.head;
	while(currentNode.value!=value){
   
		currentNode=currentNode.next;
	}
	if(!currentNode){
   
		return null;
	}
	return currentNode;
}

在指定节点后面插入新节点

insertAfter(value,newValue){
   
	const newNode=new Node(newValue);
	const currentNode=this.findNode(value);

	newNode.next=currentNode.next;
	currentNode.next=newNode;
}

在尾部添加节点

append(value){
   
	const newNode=new Node(value);
	let currentNode=this.head;
	while(currentNode.next){
   
		currentNode=currentNode.next;
	}
	currentNode.next=newNode;
}

在头部插入节点

prepend(value){
   
	const newNode=new Node(value);
	newNode.next=this.head;
	this.head=newNode;
}

删除指定节点

remove(value){
   
	let currentNode=this.head;
	let previousNode=null;

	while(currentNode.value!=value){
   
		previousNode=currentNode;
		currentNode=currentNode.next;
	}
	if(currentNode==this.head){
   
		this.head=currentNode.next;
	}else{
   
		previousNode.next=currentNode.next;
	}
}	

删除头部节点

removeHead(){
   
	this.head=this.head.next;
}

删除尾部节点

removeTail(){
   
	let currentNode=this.head;
	let previousNode=null;
	while(currentNode.next){
   
		previousNode=currentNode;
		currentNode=currentNode.next;
	}
	previousNode.next=null;
}

遍历链表

traverse(){
   
	let currentNode=this.head;
	while(currentNode){
   
		console.log(currentNode.value);
		currentNode=currentNode.next;
	}
}

双链表

class Node{
   
		constructor(value,next=null,pre=null){
   
		this.value=value;
		this.next=next;
		this.pre=pre;
	}
}

循环链表

class LinkedList{
   
	constructor(value){
   
		this.head=new Node(value);
		//初始tail就是head
		//添加节点后,tail.next=head
		this.tail=this.head;
	}
}

相关推荐

  1. 数据结构

    2024-01-18 05:10:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 05:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 05:10:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 05:10:02       82 阅读
  4. Python语言-面向对象

    2024-01-18 05:10:02       91 阅读

热门阅读

  1. Qt中的线程池

    2024-01-18 05:10:02       51 阅读
  2. 三子棋/井字棋(C语言)

    2024-01-18 05:10:02       41 阅读
  3. 深入探讨 Go 语言中的 Map 类型(续)

    2024-01-18 05:10:02       57 阅读
  4. Linux 文件搜索大师:掌握 find 命令的艺术与示例

    2024-01-18 05:10:02       44 阅读
  5. Qt:信号

    2024-01-18 05:10:02       50 阅读
  6. Matlab中常见的数据平滑方式

    2024-01-18 05:10:02       45 阅读
  7. Lumerical ------ 直波导仿真及技巧

    2024-01-18 05:10:02       56 阅读
  8. HTTP API 认证技术详解(一):Basic Authentication

    2024-01-18 05:10:02       52 阅读
  9. uniapp——自定义导航栏的封装

    2024-01-18 05:10:02       61 阅读