Skip to content

Commit 3666b67

Browse files
author
tianqing.liang
committed
链表
1 parent 2e907e2 commit 3666b67

File tree

3 files changed

+76
-134
lines changed

3 files changed

+76
-134
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
<h3>使用Python、Flutter的Dart语言重写数据结构与算法</h3>
44

5-
65
1. **[Dart,Python]线性搜索**
76
2. **[Dart,Python]选择排序**
87
3. **[Dart,Python]插入排序**
98
4. **[Dart,Python]栈,队列,循环队列**
10-
5. 链表,链表实现栈,链表实现队列
9+
5. **[Dart,Python]链表,链表实现栈,链表实现队列**
1110
6. 递归
1211
7. **[Dart,Python]归并排序**
1312
8. **[Dart,Python]快速排序**

python/linkedlist.py

+72-132
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,89 @@
1-
class __Node(object):
1+
class Node(object):
22

3-
def __init__(self,value=None,next=None):
3+
def __init__(self,value):
44
self.value = value;
5-
self.next = next;
5+
self.next = None;
66

77
def __str__(self):
88
return self.value;
99

10+
1011
class LinkedList(object):
1112

1213
def __init__(self):
13-
self.__head = None
14-
self.__size = 0
14+
self.head = None
15+
self.size = 0
1516

1617
def getSize(self):
17-
return self.__size
18+
return self.size
1819

1920
def isEmpty(self):
20-
return self.__size == 0;
21+
return self.size == 0;
22+
23+
#头部添加元素
24+
def add(self, value):
25+
node = Node(value)
26+
node.next = self.head
27+
self.head = node
28+
self.size +=1
29+
30+
# 尾部添加元素
31+
def append(self, value):
32+
node = Node(value)
33+
if self.isEmpty():
34+
self.head = node
35+
else:
36+
cur = self.head
37+
while cur.next != None:
38+
cur = cur.next
39+
cur.next = node
40+
self.size +=1
41+
42+
# 指定位置添加元素
43+
def insert(self, pos, value):
44+
if pos <= 0:
45+
self.add(value)
46+
elif pos > self.size:
47+
self.append(value)
48+
else:
49+
node = Node(value)
50+
count = 0
51+
pre = self.head
52+
while count < (pos-1):
53+
count += 1
54+
pre = pre.next
55+
node.next = pre.next
56+
pre.next = node
57+
self.size +=1
58+
59+
# 删除节点
60+
def remove(self,value):
61+
cur = self.head
62+
pre = None
63+
isFind = False
64+
while cur != None:
65+
if cur.value == value:
66+
if not pre:
67+
self.head = cur.next
68+
else:
69+
pre.next = cur.next
70+
self.size -=1
71+
isFind = True
72+
break
73+
else:
74+
pre = cur
75+
cur = cur.next
76+
return isFind
77+
78+
# 查找节点是否存在
79+
def search(self,value):
80+
cur = self.head
81+
while cur != None:
82+
if cur.value == value:
83+
return True
84+
cur = cur.next
85+
return False
2186

22-
def remove(self,index):
23-
if index<0 or index> self.__size:
24-
raise Exception("Remove Failed,Illegal index")
25-
prev = self.__head
26-
for i in range(0,index):
27-
prev = prev.next
2887

2988

3089

@@ -34,122 +93,3 @@ def remove(self,index):
3493

3594

3695

37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
# class LinkedList<T> {
50-
# remove(int index){
51-
#
52-
# _Node? prev = _dummyHead;
53-
# for(var i =0;i<index ;i++){
54-
# prev = prev?.next;
55-
# }
56-
# _Node? retNode = prev!.next;
57-
# prev.next = retNode!.next;
58-
# retNode.next = null;
59-
# _size --;
60-
# return retNode.t;
61-
# }
62-
63-
# //从链表中删除第一个元素,返回删除元素
64-
# T? removeFirst(){
65-
# remove(0);
66-
# }
67-
# //从链表中删除第一个元素,返回删除元素
68-
# T? removeLast(){
69-
# remove(_size -1);
70-
# }
71-
72-
73-
# //在链表的index位置添加新的元素e
74-
# add(int index, T t) {
75-
# if (index < 0 || index > _size) {
76-
# throw Exception("Add Failed,Illegal index");
77-
# }
78-
# // if(index == 0){
79-
# // addFirst(t);
80-
# // }else{
81-
# _Node? prev = _dummyHead;
82-
# for (var i = 0; i < index; i++) {
83-
# prev = prev?.next;
84-
# }
85-
# _Node _node = new _Node.wihtHead(t);
86-
# _node.next = prev?.next;
87-
# prev?.next = _node;
88-
# _size++;
89-
# // }
90-
# }
91-
92-
# addLast(T t) {
93-
# add(_size, t);
94-
# }
95-
96-
# addFirst(T t) {
97-
# // _Node _node =new _Node.head(t);
98-
# // _node.next = _head;
99-
# // _head = _node;
100-
# add(0, t);
101-
# }
102-
103-
# //获取链表第index位置的元素
104-
# T get(int index){
105-
# if (index < 0 || index > _size) {
106-
# throw Exception("Get Failed,Illegal index");
107-
# }
108-
# _Node? cur = _dummyHead!.next;
109-
# for(var i=0;i<index;i++){
110-
# cur = cur!.next;
111-
# }
112-
# return cur!.t;
113-
# }
114-
115-
# T getFirst(){
116-
# return get(0);
117-
# }
118-
# T getLast(){
119-
# return get(_size-1);
120-
# }
121-
# set(int index,T t){
122-
# if (index < 0 || index > _size) {
123-
# throw Exception("Set Failed,Illegal index");
124-
# }
125-
# _Node? cur = _dummyHead!.next;
126-
# for(var i =0;i<index;i++){
127-
# cur = cur!.next;
128-
# }
129-
# cur!.t= t;
130-
# }
131-
132-
# bool contains(T t){
133-
# _Node? cur = _dummyHead!.next;
134-
# for(var i =0;i<_size-1;i++){
135-
# if(cur!.t.compareTo(t)){
136-
# return true;
137-
# }
138-
# cur = cur.next;
139-
# }
140-
# return false;
141-
# }
142-
143-
# @override
144-
# String toString() {
145-
# StringBuffer res =new StringBuffer();
146-
# for(_Node? cur = _dummyHead!.next ; cur != null ; cur = cur.next){
147-
# res.write(cur);
148-
# res.write("->");
149-
# }
150-
# res.write("NULL");
151-
# return res.toString();
152-
# }
153-
# }
154-
155-

python/multiSort.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
希尔排序
77
快速排序
88
归并排序
9+
二分搜索
910
'''
1011
import random
1112
import time
@@ -158,6 +159,8 @@ def caculateResultAndTime(name,fuction,randomseed):
158159
print("查找结果:",result,' 花费时间:',endTime-startTime)
159160
print("-------------------- 结束\n")
160161

162+
163+
161164

162165

163166

0 commit comments

Comments
 (0)