学习笔记 学习笔记
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)

爱做梦的奋斗青年

曾梦想仗剑走天涯,后来bug太多就没去
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)
  • 后端技术

  • web技术

  • 数据结构

    • 线性表-栈
    • 线性表-队列
    • 线性表-链表
      • 单向链表的实现
      • 链表的应用
    • Java实现链表
    • 树
    • 图
  • C语言

  • 编程技术
  • 数据结构
爱做梦的奋斗青年
2021-03-07
目录

线性表-链表

链表是物理存储单元上非连续的,非顺序的存储结构,由一系列节点组成。

# 单向链表的实现

实例代码
function LinkList() {
	//	定义节点
	var Node = function(data) {
			this.data = data;
			this.next = null;
		}
	var length = 0; //	⻓度
	var head = null; //	头节点
	var tail = null; //	尾节点
	//	添加⼀个新元素
	this.append = function(data) {
		//	创建新节点
		var node = new Node(data);
		//	如果是空链表
		if (head == null) {
			head = node;
			tail = head;
		} else {
			tail.next = node; //	尾节点指向新创建的节点
			tail = node; //	tail指向链表的最后⼀个节点
		}
		length += 1; //	⻓度加1
		return true;
	};
	//	返回链表⼤⼩
	this.length = function() {
		return length;
	};
	//	在指定位置插⼊新的元素
	this.insert = function(index, data) {
		if (index == length) {
			return this.append(data);
		} else if (index > length || index < 0) {
			return false;
		} else {
			var new_node = new Node(data);
			if (index == 0) {
				new_node.next = head;
				head = new_node;
			} else {
				var insert_index = 1;
				var curr_node = head;
				//	找到应该插⼊的位置
				while (insert_index < index) {
					curr_node = curr_node.next;
					insert_index += 1;
				}
				var next_node = curr_node.next;
				curr_node.next = new_node;
				new_node.next = next_node;
			}
			length += 1;
			return true;
		}
	};
	//	删除指定位置的节点
	this.remove = function(index) {
		if (index < 0 || index >= length) {
			return null;
		} else {
			var del_node = null;
			if (index == 0) {
				// head指向下⼀个节点
				del_node = head;
				head = head.next;
			} else {
				var del_index = 0;
				var pre_node = null;
				var curr_node = head;
				while (del_index < index) {
					del_index += 1;
					pre_node = curr_node;
					curr_node = curr_node.next;
				}
				del_node = curr_node;
				pre_node.next = curr_node.next;
				// 如果删除的是尾节点
				if (curr_node.next == null) {
					tail = pre_node;
				}
			}
			length -= 1;
			del_node.next = null;
			return del_node.data;
		}
	};
	// 删除尾节点
	this.remove_tail = function() {
		return this.remove(length - 1);
	};
	// 删除头节点
	this.remove_head = function() {
		return this.remove(0);
	};
	// 返回指定位置节点的值
	this.get = function(index) {
		if (index < 0 || index >= length) {
			return null;
		}
		var node_index = 0;
		var curr_node = head;
		while (node_index < index) {
			node_index += 1;
			curr_node = curr_node.next;
		}
		return curr_node.data;
	};
	// 返回链表头节点的值
	this.head = function() {
		return this.get(0);
	}
	// 回链表尾节点的值
	this.tail = function() {
		return this.get(length - 1);
	}
	// 返回指定元素的索引,如果没有,返回-1,有多个相同元素,返回第⼀个
	this.indexOf = function(data) {
		var index = -1;
		var curr_node = head;
		while (curr_node) {
			index += 1
			if (curr_node.data == data) {
				return index;
			} else {
				curr_node = curr_node.next;
			}
		}
		return -1;
	};
	//	输出链表
	this.print = function() {
		var curr_node = head;
		var str_link = ""
		while (curr_node) {
			str_link += curr_node.data.toString() + "	->";
			curr_node = curr_node.next;
		}
		str_link += "null";
		console.log(str_link);
		console.log("⻓度为" + length.toString());
	};
	//	isEmpty
	this.isEmpty = function() {
		return length == 0;
	};
	//	清空链表
	this.clear = function() {
		head = null;
		tail = null;
		length = 0;
	};
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

# 链表的应用

  1. 快乐数 - LeetCode连接 (opens new window)
    编写一个算法来判断一个数 n 是不是快乐数。
    - 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
    - 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
    - 如果 可以变为  1,那么这个数就是快乐数。
    - 如果 n 是快乐数就返回 true ;不是,则返回 false 。
    
    1
    2
    3
    4
    5
    实例代码
    class Solution {
    	public boolean isHappy(int n) {
    		int p = n;
    		int q = n;
    		do {
    			p = getNext(p);
    			q = getNext(getNext(q));
    		} while (p!=q && q != 1);
    		return q==1;
    	}
    	public int getNext (int x){
    		int z = 0;
    		while(x != 0){
    			z += (x % 10) * (x % 10);
    			x /= 10;
    		}
    		return z;
    	}
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
编辑此页 (opens new window)
上次更新: 2021/03/07 14:56:17
线性表-队列
Java实现链表

← 线性表-队列 Java实现链表→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式