我是靠谱客的博主 动人小海豚,最近开发中收集的这篇文章主要介绍Python写数据结构:队列(链式存储结构),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#!/usr/bin/python3.5
#_*_coding:utf-8_*_

class Node:
    def __init__(self,value):
        self.data = value
        self.next = None

class Queue:
    def __init__(self):
        self.front = Node(None)
        self.rear = self.front

    def enQueue(self,element):
        n = Node(element)
        self.rear.next = n
        self.rear = n

    def deQueue(self):
        if self.empty():
            print('队空')
            return
        temp = self.front.next
        self.front.next = self.front.next.next
        if self.rear == temp:
            self.rear = self.front
        del temp

    def getHead(self):
        if self.empty():
            print('队空')
            return
        return self.front.next.data

    def empty(self):
        return self.rear == self.front

    def printQueue(self):
        cur = self.front.next
        while cur != None:
            print(cur.data)
            cur = cur.next

    def clear(self):
        while self.front.next != None:
            temp = self.front.next
            self.front.next = temp.next
        self.rear = self.front

    def length(self):
        cur = self.front.next
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

if __name__ == '__main__':
    queue = Queue()

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Node
    {
        public int data;
        public Node next;
    }
    class Queue
    {
        private Node front;
        private Node rear;
        private int len;
        public Queue()
        {
            // 头结点、尾指针
            this.front = new Node();
            this.front.next = null;
            this.rear = this.front;
            this.len = 0;
        }
        public void enQueue(int data)
        {
            Node n = new Node();
            n.data = data;
            n.next = null;
            this.rear.next = n;
            this.rear = n;
            this.len++;
            Console.WriteLine("入队:" + data);
        }
        public void deQueue()
        {
            if (empty())
            {
                Console.WriteLine("队列为空");
                return;
            }
            Node temp = this.front.next;
            this.front.next = this.front.next.next;
            if (this.rear == temp)
            {
                this.rear = this.front;
            }
            Console.WriteLine("出队:" + temp.data);
            temp = null;
            this.len--;
        }

        public void printQueue()
        {
            if (len == 0)
            {
                Console.WriteLine("队列为空");
                return;
            }
            Node cur = this.front.next;
            Console.Write("打印队列:");
            while (cur != null)
            {
                Console.Write(cur.data + " ");
                cur = cur.next;
            }
            Console.WriteLine();
        }
        public void getLength()
        {
            Console.WriteLine("队列长度:" + this.len);
        }
        public bool empty()
        {
            return this.len == 0;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Queue queue = new Queue();
            queue.enQueue(1);
            queue.enQueue(2);
            queue.enQueue(3);
            queue.enQueue(4);
            queue.enQueue(5);
            queue.printQueue();
            queue.getLength();
            Console.WriteLine();
            queue.deQueue();
            queue.deQueue();
            queue.printQueue();
            queue.deQueue();
            queue.deQueue();
            queue.deQueue();
            queue.deQueue();
        }
    }
}

 

最后

以上就是动人小海豚为你收集整理的Python写数据结构:队列(链式存储结构)的全部内容,希望文章能够帮你解决Python写数据结构:队列(链式存储结构)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部