关于python的循环队列,链表倒置,快速排序和链表环问题

循环队列:

class XhDl():
    def __init__(self,num):
        self.tou=0
        self.wei=0
        self.queue=[None]*num
        self.size=num
    def rudui(self):
        if (self.wei+1)%self.size==self.tou:
            print("满了")
        else:
            data=int(input("请输入入队数据:"))
            self.queue[self.wei]=data
            self.wei=(self.wei+1)%self.size
    def chudui(self):
        if self.tou==self.wei:
            print("空了")
        else:
            print("出队数据为:",self.queue[self.tou])
            self.tou=(self.tou+1)%self.size
    def dayin(self):
        if self.wei>self.tou:
            for i in range(self.tou,self.wei):
                print(self.queue[i],end="")
        elif self.tou>self.wei:
            for j in rnage(self.tou,self.size):
                print(self.queue[j],end="")
            for k in range(0,self.wei):
                print(self.queue[k],end="")
        else:
            print("空队")
dl=XhDl(5)
while True:
    bh=int(input("编号"))
    if bh==1:
        dl.rudui()
    elif bh==2:
        dl.chudui()
    elif bh==3:
        dl.dayin()
    else:
        exit()

链表倒置

class Node():
    def __init__(self,num,next=None):
        self.num=num
        self.next=next
class LinkList():
    def __init__(self):
        self.head=None
    def produce(self):
        wei=None
        for i in range(10):
            one=Node(i+1)
            if self.head==None:
                self.head=one
                wei=one
            else:
                wei.next=one
                wei=one
    def dayin(self):
        p=self.head
        while p!=None:
            print(p.num)
            p=p.next
    def charu(self):
        wz=int(input("输入插入位置:"))
        data=int(input("输入插入数据:"))
        if wz==1:
            one=Node(data)
            one.next=self.head
            self.head=one
        else:
            i=1
            p=self.head
            while i<wz-1 and p!=None:
                p=p.next
                i+=1
            if p==None:
                print("位置异常")
            else:
                one=Node(data)
                one.next=p.next
                p.next=one
    def shanchu(self):
        if self.head==None:
            print("空链表")
        else:
            wz=int(input("请输入删除位置:"))
            if wz==1:
                print("你删除的内容是",self.head.num)
                self.head=self.head.next
            else:
                i=1
                p=self.head
                while i<wz-1 and p!=None:
                    p=p.next
                    i+=1
                if p==None or p.next==None:
                    print("删除位置不存在")
                else:
                    print("删除内容是",p.next.num)
                    p.next=p.next.next
    def daoxu(self):
        if self.head==None or self.head.next==None:
            print("不用倒叙")
        else:
            current=self.head
            pre=None
            nextNode=self.head.next
            while nextNode != None:
                current.next=pre
                pre=current
                current=nextNode
                nextNode=nextNode.next
            current.next=pre
            self.head=current
lb=LinkList()
lb.produce()
while True:
    bh=int(input("请输入编号:"))
    if bh==1:
        lb.charu()
    elif bh==2:
        lb.shanchu()
    elif bh==3:
        lb.dayin()
    elif bh==4:
        lb.daoxu()
    else:
        break

快速排序

a=[5,4,3,2,1,6]
def partion(a,low,high):
    key=a[low]
    while low<high:
        while low<high and a[high]>=key:
            high-=1
        else:
            a[low]=a[high]
        while low<high and a[low]<key:
            low+=1
        else:
            a[high]=a[low]
    a[low]=key
    return low
def sort (a,low,high):
    if low<high:
        mid=partion(a,low,high)
        sort(a,low,mid-1)
        sort(a,mid+1,high)
sort(a,0,len(a)-1)
print(a)

链表环问题

class Lb():
    def __init__(self,ks):
        self.ks=ks
        self.next=None
def zm(head):
    f=head
    s=head
    bj=False
    if head==None:
        print("空")
        return False
    while f.next.next!=None and s.next!=None:
        f=f.next.next
        s=s.next
        s1=s
        f1=f
        if s==f:
            bj=True
            print("存在环")
            break
    i=0
    while bj==True and s1.next!=f1.next.next:
        s1=s1.next
        f1=f1.next.next
        i+=1
    print("环长为",i)
    if bj==True:
        s=head
        while s!=f:
            s=s.next
            f=f.next
        print("位置为",s.ks)
        return False
    print("不存在")
    return False
node1 = Lb(1)
node2 = Lb(2)
node3 = Lb(3)
node4 = Lb(4)

node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node1
zm(node1)

猜你喜欢

转载自blog.csdn.net/qq_34681895/article/details/84556010