Python 链表实践

数据结构之链表

  1. 建立链表元素类
  2. 建立链表类
  3. 为链表类建立方法函数

1.建立链表元素类

链表是由一个个元素连接而成的。因此第一步,咱们先建立一个链表元素类,来表示咱们的链表上的元素。接着咱们经过 __init__ 方法给它定义两个属性,self.valueself.nextpython

class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None
复制代码

2.建立链表类

一个链表在最初被建立的时候,它至少须要一个元素。bash

链表是由元素连接而成,它的第一个元素,咱们称它为头部元素。因此咱们在__init__方法里给链表类定义了一个属性,self.head = head数据结构

class LinkedList(object):
	def __init__(self, head=None):
		self.head = head
复制代码

3.为链表类建立函数方法

咱们将为链表类建立以上四个方法。app


append方法

​ 在链表的后面增长一个元素。链表中不能像列表那样经过索引定位每一个元素。因此须要不断调用 链表元素的.next方法来不断获取下一个元素,最后获取到最后一个元素。而后在最后一个元 素的.next属性里指向新增的元素。函数

def append(self, new_element):
	current = self.head
	if self.head:
		while current.next:
			current = current.next
		current.next = new_element
	else:
		self.head = new_element
复制代码

get_position方法

​ 获取链表中与传入参数对应的元素位置。例:get_position(1) 将会获取链表中第一个元素。ui

​ 这里咱们依然须要经过循环调用.next属性来遍历链表。不一样的是咱们须要定义一个变量counter来记录咱们遍历的链表元素顺序。咱们还须要在传入的参数获取不到链表元素时返回Nonespa

def get_position(self, position):
	counter = 1
	current = self.head
	if position < 1:
		return None
	While current and counter <= position:
		if counter == position:
			return current
		current = current.next
		counter += 1
	return None
复制代码

insert方法

insert方法有两个参数,new_elementpositionnew_element表示要插入的元素,position表示要插入链表的位置。code

​ 一样的咱们须要使用循环,也须要counter来记录咱们遍历链表的顺序。一般咱们插入元素时,都是从链表的中间插入。因此先不考虑把元素插入链表头部的状况。如今咱们来想象一下,假设咱们想把元素new_element插入到链表中第三个位置,也就是position为3的时候,咱们须要获取什么? 咱们实际只须要获取到它的前一个元素(咱们假设这个元素为element2),也就是position为2的元素,而后把element2.next赋值给new_element.next,而后再把new_element赋值给element2.next。因而,咱们便完成了元素的插入,最后再考虑插入头部的状况,咱们直接把self.head赋值给new_element.next索引

def insert(self, new_element, position):
	counter = 1
	current = self.head
	if position > 1:
		while current and counter < position:
			if counter == position - 1
				new_element.next = current.next
				current.next = new_element
			current = current.next
			counter += 1
	elif position == 1:
		new_element.next = self.head 
		self.head = new_element
复制代码

delete方法

​ delete删除一个value属性与传入参数“value”相同的元素。element

​ 在链表中,要删除一个元素,只须要,把被删除元素前面一个元素指向到被删除元素的后一个元素。这里咱们用previous来表示被删除元素的前一个元素。

def delete(self, value):
	current = self.head
	previous = None
	while current.value != value and current.next:
		previous = current
		current = current.next
	if current.value == value:
		if previous:
			previous.next = current.next
		else:
			self.head = current.next
复制代码

一个完整的示例

class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    def append(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:
            self.head = new_element

    def get_position(self, position):
        counter = 1
        current = self.head
        if position < 1:
            return None
        while current and counter <= position:
            if counter == position:
                return current
            current = current.next
            counter += 1
        return None

    def insert(self, new_element, position):
        counter = 1
        current = self.head
        if position > 1:
            while current and counter < position:
                if counter == position - 1:
                    new_element.next = current.next
                    current.next = new_element
                current = current.next
                counter += 1
        elif position == 1:
            new_element.next = self.head
            self.head = new_element

    def delete(self, value):
        current = self.head
        previous = None
        while current.value != value and current.next:
            previous = current
            current = current.next
        if current.value == value:
            if previous:
                previous.next = current.next
            else:
                self.head = current.next
复制代码
相关文章
相关标签/搜索