The Unordered List Abstract Data Type

本文介绍无序列表的概念及操作,详细讲解了通过链表实现无序列表的方法,包括节点(Node)类的设计和无序列表(UnorderedList)类的各种操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The structure of an unordered list, as described above, is a collection of items where each item holds a relative position with respect to the others. Some possible unordered list operations are given below.

List() creates a new list that is empty. It needs no parameters and returns an empty list.

add(item) adds a new item to the list. It needs the item and returns nothing. Assume the item is not already in the list.

remove(item) removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.

search(item) searches for the item in the list. It needs the item and returns a boolean value.

isEmpty() tests to see whether the list is empty. It needs no parameters and returns a boolean value.

size() returns the number of items in the list. It needs no parameters and returns an integer.

append(item) adds a new item to the end of the list making it the last item in the collection. It needs the item and returns nothing. Assume the item is not already in the list.

index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.

insert(pos,item) adds a new item to the list at position pos. It needs the item and returns nothing. Assume the item is not already in the list and there are enough existing items to have position pos.

pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.

pop(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.

Implementing an Unordered List: Linked Lists

The Node Class

The basic building block for the linked list implementation is the node. Each node object must hold at least two pieces of information. First, the node must contain the list item itself. We will call this the data field of the node. In addition, each node must hold a reference to the next node.

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()
print(temp.data)

The Unordered List Class
As we suggested above, the unordered list will be built from a collection of nodes, each linked to the next by explicit references. As long as we know where to find the first node (containing the first item), each item after that can be found by successively following the next links. With this in mind, the UnorderedList class must maintain a reference to the first node. Listing 2 shows the constructor. Note that each list object will maintain a single reference to the head of the list.

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

class UnorderedList:
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def add(self,item):#在头部加
        temp = Node(item)#创建一个NODE
        temp.setNext(self.head)#把头加到后面
        self.head = temp#把新头给新的NODE

    def size(self):
        current = self.head
        count = 0#初始化为0
        while current != None:#当头不为空的时候
            current = current.getNext()#向下移动
            count = count + 1#
        return count#循环结束后返回值

    def search(self,item):
        current = self.head#从头开始找
        found = False#设置found为否
        while current != None and not found:#当Head不为空,并且也没找到
            if current.getData() == item:#找到
                found = True#找到,结束循环
            else:
                current = current.getNext()#如果没有找到,去下一个

        return found#循环结束后返回FOUND

    #去除
    def remove(self,item):
        current = self.head#指针为当前
        previous = None#前一个为NODE
        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())#把目前的前一个的后一个设置为目前的后一个,就去除掉了目前这个节点


temp = Node(93)
temp.getData()
print(temp.data)

linklist=UnorderedList()

linklist.add(20)
linklist.add(10)
linklist.size()
linklist.search(10)
linklist.remove(10)
print(linklist.size())
print(linklist.search(10))

总结:

  • LINKEDLIST:是由NODE组成的。初始化,下一个指针为空
  • NODE包括:变量:VAL, NEXT指针,
    它的性质:设置他的VAL,得到他的VAL,设置他的指针,得到他的指针

LINKEDLIST: 变量head为空,一开始没头
性质:
isEmpty是否为空,判断是否有头结点

add:在前面添加节点
1. 先创建一个NODE,
2. 再把这个NODE的下一个设为链表的头
3. 再把链表现在的头给新的NODE

size 得到大小
1. 把目前的NODE设置为头
2. 初始化COUNT=0
3. 当目前NODE不为空,开始循环
目前的NODE 为下一个NODE
COUNT+1
4. 当所有NODE遍历完,返回COUNT

search(被搜索的)
1.把当前的NODE设置为头
2. FOUND为否
3. 当目前节点不为空,FOUND为否的时候 开始循环
当目前节点的值为被搜索的值时
found为TRUE
如果不是:
目前为下一个
4. 当循环结束的时候,返回FOUND

remove(要被去除的):先找-》再删除
1.把当前的NODE设置为头
2.前一个为空
3.found=false
4. 当没有发现的时候循环
当目前节点的值为被搜索的值时
found为TRUE
如果不是:
目前为下一个
5.找到后退出循环, 如果 前一个NODE为NODE, 说明此时找的的节点为头结点。
那么把链表的头给下一个(说明去除了之前的头结点)
如果不是, 设置前一个节点的后一个节点为目前的下一个,去除掉了目前的接点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值