链表

作者: qianyewhy | 来源:发表于2017-09-11 05:51 被阅读6次
 '# -*- coding: utf-8 -*-'

class Lnode(object):
    # data:节点保存的数据
    # next:相当于next指针,指向下一个节点
    def __init__(self, data, pnext=None):
        self.data = data
        self.next = pnext

    def __repr__(self):
        # 用来定义node的字符输出
        return str(self.data)


class Linklist(object):
    def __init__(self):
        self.head = None
        self.length = 0

    # 在链表头部添加节点
    def prepend(self, value):
        if self.head is None:
            self.head = Lnode(value, None)
        else:
            newhead = Lnode(value, None)
            newhead.next = self.head
            self.head = newhead
            self.length += 1

    # 在链表尾部添加节点
    def append(self, value):
        if self.head is None:
            self.head = Lnode(value, None)
        else:
            cursor = self.head
            while cursor.next is not None:
                cursor = cursor.next
            cursor.next = Lnode(value, None)
            self.length += 1

    # 在链表的指定位置插入节点
    def insert(self, index, value):
        if self.head is None and index != 1:
            return
        if index <= 0 or index > self.getlength():
            return
        elif index == 1:
            self.prepend(value)
        elif index == self.getlength():
            self.append(value)
        else:
            cursor = self.head
            node = Lnode(value, None)
            for i in range(1, index - 1):
                cursor = cursor.next
            node.next = cursor.next
            cursor.next = node

    # 删除指定位置的节点
    def delnode(self, index):
        if self.head is None:
            return
        if index <= 0 or index > self.getlength():
            return
        elif index == 1:
            self.head = self.head.next
            self.length -= 1
        elif index == self.getlength():
            cursor = self.head
            for i in range(1, self.getlength() - 1):
                cursor = cursor.next
            cursor.next = None
            self.length -= 1
        else:
            cursor = self.head
            for i in range(1, index - 1):
                cursor = cursor.next
            t = cursor.next
            cursor.next = t.next
            self.length -= 1

    # 删除值为value的链表节点元素
    def delvalue(self, value):
        if self.head is None:
            return
        elif self.head.data == value:
            self.head = self.head.next
            self.length -= 1
        else:
            pre = self.head
            cursor = pre.next
            while cursor is not None:
                if cursor.data == value:
                    pre.next = cursor.next
                    cursor = cursor.next
                    self.length -= 1
                else:
                    pre = cursor
                    cursor = cursor.next

    # 按序号查找节点值
    def getindex(self, index):
        if self.head is None:
            return
        elif index <= 0 or index > self.getlength():
            return
        elif index == 1:
            return self.head.data
        else:
            cursor = self.head
            for i in range(1, index):
                cursor = cursor.next
            return cursor.data
        # else:
        #     counter = 1
        #     cursor = self.head
        #     while cursor is not None:
        #         if index == counter:
        #             return cursor.data
        #         else:
        #             counter += 1
        #             cursor = cursor.next

    # 求链表的长度
    def getlength(self):
        if self.head is None:
            return 0
        else:
            count = 1
            cursor = self.head
            while cursor.next is not None:
                count += 1
                cursor = cursor.next
        return count

    # 单链表逆序
    def diverse(self):
        if self.head is None:
            return
        elif self.head.next is None:
            return self.head
        else:
            pre = None
            cursor = self.head
            while cursor is not None:
                ne = cursor.next
                cursor.next = pre
                pre = cursor
                cursor = ne
            self.head = pre

    # 删除链表中重复元素
    def delrepret(self):
        if self.head is None:
            return
        if self.head.next is None:
            return self.head
        else:
            data = self.head.data
            # print(data)
            count = 1
            dic = {data: 1}
            pre = self.head
            cursor = pre.next
            while cursor is not None:
                print(cursor.data)
                if dic.get(cursor.data) is None:
                    dic[cursor.data] = dic.setdefault(cursor.data, 1)
                    pre = cursor
                    cursor = cursor.next
                    print(dic)
                else:
                    pre.next = cursor.next
                    cursor = cursor.next
                    self.length -= 1

    # 判断链表是否为空
    def isempty(self):
        if self.head is None or self.getlength() == 0:
            return True
        else:
            return False

    # 删除列表
    def dellist(self):
        if self.head is None or self.getlength() == 0:
            return
        else:
            cursor = self.head
            while cursor is not None:
                cursor.data = None
                cursor = cursor.next
            self.head = None

    # 打印链表元素
    def print(self):
        if self.head is None:
            return
        else:
            cursor = self.head
            while cursor is not None:
                print(cursor.data, end=' ')
                cursor = cursor.next
            print()


if __name__ == "__main__":

    linklist = Linklist()
    print(linklist.isempty())
    for i in range(1, 5):
        linklist.append(i)
    linklist.prepend(1)
    linklist.print()
    print("按序号查找")
    print(linklist.getindex(4))
    print("&&&&&&&&&&&&&&&&")
    linklist.insert(3, 5)
    linklist.print()
    linklist.insert(1, 1)
    linklist.print()
    linklist.insert(2, 2)
    print(linklist.isempty())
    linklist.print()
    print("删除重复元素")
    linklist.delrepret()
    print("输出删除元素后的链表")
    linklist.print()
    # linklist.diverse()
    # linklist.print()
    linklist.delnode(5)
    # linklist.delvalue(5)
    # print("&&&&&&&&&&&&")
    # linklist.print()
    # print("**********")
    # linklist.dellist()
    # print("@@@@@@@@@@@@")
    # length = linklist.getlength()
    # print(length)
    linklist.print()

相关文章

  • 链表基础

    链表基础 链表长度 链表为空 链表结构 链表增加

  • 双向链表&双向循环链表

    链表分为:单链表、单向循环链表、双向链表、双向循环链表本节主要说明:双向链表、双向循环链表 定义结点 一、双向链表...

  • 算法与数据结构:链表

    链表 链表还分为单向链表和双向链表, 但是这篇文章只说单向链表 , 下次再讲双向链表 . 链表和数组的区别 ? 链...

  • 链表

    链表 单链表反转链表中环的检测两个有序链表合并删除链表倒数第n个节点求链表的元素总个数 一.单向链表 链表共有特征...

  • 五、双向链表

    双向链表 此前介绍的链表,也叫做单向链表使用双向链表可以提升链表的综合性能 修改之前的单链表的源码: 双向链表 –...

  • 链表

    内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 表 双向链表 循环链表 链表数据结...

  • 数据与算法结构

    线性表 顺序表 链表(物理上离散,逻辑上连续) 链表的类别 单链表 循环链表 双链表 链表的操作 顺序表与链表的比...

  • 数据结构——链表

    本文所讲的链表是单链表,链表采用无头链表 科普下:一般链表可以分为有头节点的链表与无头节点的链表 有头节点的链表:...

  • 链表

    文章结构 链表的定义 链表的插入和删除操作 链表的特性 常见的链表结构 自定义链表 链表的经典操作 使用链表实现L...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

网友评论

      本文标题:链表

      本文链接:https://www.haomeiwen.com/subject/dydxsxtx.html