List of table do not need to do the following
- add (item): to add a data item to the list, assuming the original item is not present in the list
- remove (item): remove the item from the list, the list is modified, the original item should be stored in a table
- search (item): Find item in the list, return type bool value
- Whether isEmpy () returns the list is empty
- size (): Returns the list contains the number of data items
- append (item): to add a data item to the end of the table, assuming the original item is not present in the list
- index (item): returns the location data item in the table
- insert (pos, item): the data into position pos, assuming item not originally present in the list, while the list has sufficient original multiple data items, item allows occupy the position pos
- pop (): the number of items removed from the end of the list, assuming the original list has at least one data item
- pop (pos): removal position pos items, assuming the presence of the original list position pos
Using a linked list to achieve unordered list
- Storage location of data items and no rules, but if a connection point between the data items, the relative position may be maintained before and after the
- The first and last items need to be explicitly demonstrated, a team is the first team ,, is a tail, no longer back up data
List implementation
- Node Node:
each node comprising at least two information: the data item itself, and a reference information pointing to the next node.
Note next to no sense None of the next node, and this is very important
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.data = newdata
def setNext(self,newnext):
self.next = newnext
temp = Node(93)
temp.getData()
# 可以采用连接节点的方式构建数据集来实现无序表
# 链表的第一个和字后一个节点最重要
# 链表实现: add方法实现
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
# 链表实现size
def size(self):
current = self.head
count = 0
while current !=None:
count+= 1
current = current.getNext()
return count
Ordered list
Is a sorted list of data items according to their properties to determine a position in Kobe list
Operation as orderly as defined in the table
- OrderList (): creates an empty sorted list
- add (item): adding a data item in the table, and keep the overall sequence, the absence of this original
- search(item)
- isEmpty()
- size()
- index(item)
- pop()
pop(pos)
# 习题一 合并两个有序链表
class Solution:
def mergeTwolist(self, l1, l2):
if l1 is None:
return l2
elif l2 is None:
return l1
elif ll1.val > l2.val:
l1=l2
l2 = l1
p = l1
while p is not None:
while l2 is not None (p.next is None or l2.val < p.next.val):
tmp = l2
l2 = l2.next
tmp.next = p.next
p.next = tmp
p = p.next
p = p.next
return l1
# 习题2 删除链表中倒数第n个节点
class Solution:
def removeN(self,head,n):
h = listNode(-1)
h.next = head
p = h
q = h
for _ in range(n+1):
q = q.next
while q != None:
p = p.next
q = q.next
p.next = p.next.next
return h.next