# python数据结构顺序存储初始化顺序表_python数据结构之线性表的顺序存储结构

# !/usr/bin/env python

# -*- coding: utf-8 -*-

__author__ = 'MrHero'

class Node(object):

"""

"""

def __init__(self, data=None):

self.data = data

self.next = None

class LKList(object):

"""

"""

def __init__(self):

"""

:return:

"""

self.L = Node(None)

self.L.next = None

self.length = 0

def is_empty(self):

"""

:return:

"""

return self.length == 0

def get_length(self):

"""

:return:

"""

return self.length

def insert(self, i, elem):

"""

:param i: 指定的位置

:param elem: 插入的元素elem

:return:

"""

j = 0

p = self.L

while j < i-1 and p is not None: # 查找第 i-1 个节点

j += 1

p = p.next

if p is None: # 未找到逻辑位序为 i-1 的节点

raise IndexError("Index is out of range!")

else: # 找到逻辑位序为 i-1 的节点

tmp = Node(elem)

tmp.next = p.next

p.next = tmp

self.length += 1

def delete(self, i):

"""

:param i: 指定节点

:return: 删除的指定节点元素值

"""

if self.is_empty():

raise IndexError("The list is empty!")

elif 0 < i <= self.length:

j = 1

p = self.L

while j < i and p:

p = p.next

j += 1

delelte_node = p.next

p.next = delelte_node.next

self.length -= 1

return delelte_node.data

else:

raise IndexError("Index is out of range!")

def get_elem(self, i):

"""

:param i:

:return:返回某个节点的值

"""

if self.is_empty():

raise IndexError("The list is empty")

elif 0 < i <= self.length:

j = 0

p = self.L

while j < i and p:

p = p.next

j += 1

print p.data

else:

raise IndexError("Index is out of range!")

def locate_elem(self, elem):

"""

:param elem:

:return: 返回第一个值等于elem的位置

"""

j = 0

p = self.L

while p is not None and p.data != elem:

p = p.next

j += 1

if p is Node:

return -1

else:

return j

def create_dict_list_H(self, list):

"""

:param list:

:return:

"""

p = self.L

for i in range(len(list)):

tmp = Node(list[i])

tmp.next = p.next

p.next = tmp

self.length += 1

def create_dict_list_E(self, list):

"""

:param list:

:return:

"""

p = self.L

r = p

for i in range(len(list)):

tmp = Node(list[i])

r.next = tmp

r = tmp

self.length += 1

r.next = None

def show_lklist(self):

if self.is_empty():

raise IndexError("It's a empty list!")

else:

j = 1

p = self.L

while j <= self.length and p:

p = p.next

if p is not None:

print p.data

j += 1

if __name__ == '__main__':

lk = LKList()

#

# lk.create_dict_list_E([1, 2, 3, 4])

# print "-----"

# lk.get_elem(1)

# lk.get_elem(2)

# lk.get_elem(3)

# lk.get_elem(4)

# print "-------"

# lk.show_lklist()

# lk.insert(3, 5)

# print "-------"

# lk.show_lklist()

# lo = lk.locate_elem(5)

# print "location is %d" % lo

# lk.delete(4)

# print "-------"

# lk.show_lklist()

11-06 2353
10-20 529
09-08 109
08-27 286
01-19 5895
04-06 84
12-10 1752
12-09 485
08-18 3386
06-19 168
08-02 1万+
06-20 56
02-28 4704
02-23 100