forked from Jack-Lee-Hiter/AlgorithmsByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74ffd86
commit e42c321
Showing
3 changed files
with
175 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
class Node: | ||
def __init__(self, initdata): | ||
self.data = initdata | ||
self.next = None | ||
|
||
def getData(self): | ||
return self.data | ||
|
||
def getNext(self): | ||
return self.next | ||
|
||
def setData(self, newdata): | ||
self.next = newdata | ||
|
||
def setNext(self, nextNode): | ||
self.next = nextNode | ||
|
||
|
||
temp = Node(93) | ||
temp.setData(10) | ||
print(temp.getNext()) | ||
|
||
# 定义一个无序链表 | ||
class UnorderedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
def isEmpty(self): | ||
return self.head == None | ||
|
||
def add(self, item): | ||
temp = Node(item) | ||
temp.setNext(self.head) | ||
self.head = temp | ||
|
||
def size(self): | ||
current = self.head | ||
count = 0 | ||
while current != None: | ||
count += 1 | ||
current = current.getNext() | ||
return count | ||
|
||
def search(self, item): | ||
current = self.head | ||
found = False | ||
while current != None and not found: | ||
if current.getData() == item: | ||
found = True | ||
else: | ||
current = current.getNext() | ||
return found | ||
|
||
def remove(self, item): | ||
current = self.head | ||
previous = None | ||
found = False | ||
while not found: | ||
if current.getData() == item: | ||
found = True | ||
else: | ||
previous = current | ||
current = current.getNext() | ||
|
||
if previous == None: | ||
self.head = current.getNext() | ||
else: | ||
previous.setNext(current.getNext()) | ||
|
||
myList = UnorderedList() | ||
myList.add(31) | ||
myList.add(77) | ||
myList.add(17) | ||
myList.add(93) | ||
myList.add(26) | ||
myList.add(54) | ||
print(myList.search(17)) | ||
myList.remove(54) | ||
print(myList.search(54)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from numpy import * | ||
|
||
# 代码来自机器实战 | ||
def loadDataSet(fileName): | ||
dataMat = []; labelMat = [] | ||
fr = open(fileName) | ||
|