算法导论 python代码 第十章

# author Ttssxuan
# Chapter 10.1

class Queue:
    '''
    the queue class

    Properties:
        head - the head of the queue
        tail - the tail of the queue
        length - the length of the queue
        arr - the queue entity
    '''
    def __init__(self, length):
        self.head = 0
        self.tail = 0
        self.length = length
        self.arr = [0] * self.length
    def __setitem__(self, i, x):
        self.arr[i] = x
    def __getitem__(self, i):
        return self.arr[i]
        

def enqueue(q, x):
    '''
    enqueue a element x to the queue q

    Parameters:
        q - the queue needs add a element
        x - the element needs add into the queue
    Returns:
        none
    '''
    
    if q.tail == q.length - 1:
        q.tail = 0
    else:
        q.tail += 1
    # determine whether overflow
    if q.tail == q.head:
        raise Exception("overflow")
    q[q.tail - 1] = x

def dequeue(q):
    '''
    get an element in the queue

    Parameters:
        q - the queue
    Returns:
        the last element of the queue
    '''
    if q.head == q.tail:
        raise Exception("underflow")
    x = q[q.head]
    if q.head == q.length - 1:
        q.head = 1
    else:
        q.head = q.head + 1
    return x

# test
q = Queue(10)
# enqueue
for i in range(0, 5):
    enqueue(q, i)
print(q.head, q.tail)
print(q.arr)
# dequeue
for i in range(0, 4):
    print(dequeue(q))
print(q.head, q.tail)
print(q.arr)


# author Ttssxuan
# chapter 10.1
# the stack

class Stack:
    '''
    the stack class

    Properties:
        top - the end of the stack
    '''
    def __init__(self):
        self.top = 0
        self.arr = []
    def __getitem__(self, i):
        return self.arr[i - 1]
    def __setitem__(self, i, x):
        self.arr.append(x)
    def __str__(self):
        return str(self.arr)

def stack_empty(s):
    '''
    determine whether the stack is empty

    Parameters:
        s - the stack
    Returns:
        if the stack is empty, gets True, otherwise gets False
    '''
    if s.top == 0:
        return True
    else:
        return False

def push(s, x):
    '''
    push x at the end of the stack s

    Parameters:
        s - the stack
        x - the value needs to push into the stack
    Returns:
        none
    '''
    s.top += 1
    s[s.top] = x

def pop(s):
    '''
    pop an element from stack s

    Parameters:
        s - the stack
    Returns:
        if the element exsit, return it, or raise excepthion
    '''
    if stack_empty(s):
        raise Exception("underflow")
    else:
        s.top -= 1
        return s[s.top + 1]

# test
s = Stack()
print(stack_empty(s))
push(s, 1)
print(s)
push(s, 2)
print(s)
print(pop(s))
print(pop(s))
# this will be underflow
print(pop(s))

# author Ttssxuan
# 10.1-5
# the implement of the deque


class Deque:
    '''
    the deque class

    Properties:
        head - the head of the deque
        tail - the tail of the deque
        length - the length of the deque
        arr - the queue entity
    '''
    def __init__(self, length):
        self.head = 0
        self.tail = 0
        self.length = length
        self.arr = [0] * self.length
    def __setitem__(self, i, x):
        self.arr[i] = x
    def __getitem__(self, i):
        return self.arr[i]
    def __str__(self):
        return str(self.arr)

def enqueue(q, x):
    '''
    append an element at the tail of the deque

    Parameters:
        q - the deque needs add a element
        x - the element needs add into the deque
    Returns:
        none
    '''
    q.tail = (q.tail + 1) % q.length
    # determine whether overflow
    if q.tail == q.head:
        raise Exception("overflow")
    q[q.tail - 1] = x

def enqueue_head(q, x):
    '''
    append an element at the head of the deque

    Parameters:
        q - the deque
        x - the value insert into the head
    Returns:
        none
    '''
    q.head = (q.head - 1) % q.length
    if q.head == q.tail:
        raise Exception("overflow")
    else:
        q[q.head] = x

def dequeue(q):
    '''
    get an element at the head of the deque

    Parameters:
        q - the deque
    Returns:
        the last element of the deque
    '''
    if q.head == q.tail:
        raise Exception("underflow")
    x = q[q.head]
    q.head = (q.head + 1) % q.length
    return x

def dequeue_tail(q):
    '''
    get an element at the tail of the deque

    Parameters:
        q - the deque
    Returns:
        the last element of the deque
    '''
    if q.tail == q.head:
        raise Exception("underflow")
    
    q.tail = (q.tail - 1) % q.length
    return q[q.tail]

# test
q = Deque(10)
# test enqueue
enqueue(q, 1)
enqueue(q, 2)
enqueue_head(q, 3)
enqueue_head(q, 4)
print(q)
print(q.head, q.tail)
# test dequeue
print(dequeue(q), q.head, q.tail)
print(dequeue_tail(q), q.head, q.tail)
print(dequeue(q), q.head, q.tail)
print(q)
print(dequeue_tail(q), q.head, q.tail)

# author Ttssxuan
# chapter 10.2
# the linked list


class Node:
    '''
    the linked list node

    Properties:
        key - the value of the node
        next - the pointer of the node
        prev - the pointer of the prev
    '''
    def __init__(self):
        self.value = None
        self.prev = None
        self.next = None
    def __str__(self):
        return str(self.value)

class Linked_list:
    '''
    the linked_list

    Properties:
        head - the head of the linked list
    '''
    def __init__(self):
        self.head = None
    def __str__(self):
        x = self.head
        re = ""
        while x != None:
            re += " " + str(x.value)
            x = x.next
        return re


def list_search(l, k):
    '''
    search the node whose value equels k

    Parameters:
        l - the linked list
        k - the value needs to be search
    Returns:
        if exist return the node, else return None
    '''
    x = l.head
    while x != None and x.value != k:
        x = x.next
    return x

def list_insert(l, x):
    '''
    insert the x into the given linked list

    Parameters:
        l - the linked list
        x - the node needs to be inserted into the linked list
    Returns:
        none
    '''
    x.next = l.head
    if l.head != None:
        l.head.prev = x
    l.head = x
    x.prev = None

def list_delete(l, x):
    '''
    delete the the node x in the given linked list

    Parameters:
        l - the linked list
        x - the node needs to be deleted from the linked list
    Returns:
        none
    '''
    if x.prev != None:
        x.prev.next = x.next
    else:
        l.head = x.next
    if x.next != None:
        x.next.prev = x.prev

# test
l = Linked_list()
# make the linked list
print("make the linked list")
for i in range(0, 10):
    x = Node()
    x.value = i
    list_insert(l, x)
print(l)
# search the value
print("search in the linked list")
print(list_search(l, 2))
print(list_search(l, 5))
# delete element
print("delete the element in the linked list")
print("delete 3")
list_delete(l, list_search(l, 3))
print(l)
print("delete 7")
list_delete(l, list_search(l, 7))
print(l)

# author Ttssxuan
# chapter 10.2
# the linked list with sentinels


class Node:
    '''
    the linked list node

    Properties:
        key - the value of the node
        next - the pointer of the node
        prev - the pointer of the prev
    '''
    def __init__(self):
        self.value = None
        self.prev = None
        self.next = None
    def __str__(self):
        return str(self.value)

class Linked_list:
    '''
    the linked_list

    Properties:
        head - the head of the linked list
    '''
    def __init__(self):
        x = Node()
        self.nil = x
        x.prev = x
        x.next = x
        
    def __str__(self):
        x = self.nil.next
        re = ""
        while x != self.nil:
            re += " " + str(x.value)
            x = x.next
        return re

def list_delete_sentinels(l, x):
    '''
    delete an element for a linked list with sentinel

    Parameters:
        l - the linked list
        x - the element need to be deleted
    Returns:
        none
    '''
    x.prev.next = x.next
    x.next.prev = x.prev

def list_search_sentinels(l, k):
    '''
    search an element whose value equals k in the linked list with sentinel

    Parameters:
        l - the linked list
        k - the value need to search in the linked list
    Returns:
        the element whose value equals k
    '''
    x = l.nil.next
    while x != l.nil and x.value != k:
        x = x.next
    return x

def list_insert_sentinels(l, x):
    '''
    insert an element into the linked list with sentinel

    Parameters:
        l - the linked list
        x - the node needs to be inserted into the linked list
    Returns:
        none
    '''
    x.next = l.nil.next
    l.nil.next.prev = x
    l.nil.next = x
    x.prev = l.nil



# test
l = Linked_list()
# make the linked list
print("make the linked list")
for i in range(0, 10):
    x = Node()
    x.value = i
    list_insert_sentinels(l, x)
print(l)
# search the value
print("search in the linked list")
print(list_search_sentinels(l, 2))
print(list_search_sentinels(l, 5))
# delete element
print("delete the element in the linked list")
print("delete 3")
list_delete_sentinels(l, list_search_sentinels(l, 3))
print(l)
print("delete 7")
list_delete_sentinels(l, list_search_sentinels(l, 7))
print(l)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值