循环队列的链表表示

本文介绍了一种使用链表实现的队列数据结构——链式队列。详细讲解了链式队列的基本操作,包括入队、出队、获取队首元素及清空队列等,并提供了完整的C++模板实现。

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

 #include"LinkedList.h"
#include
"Queue.h"
template
<class T>
class LinkedQueue
{
public:
    LinkedQueue():rear(
NULL),front(NULL){}
    ~LinkedQueue(){makeEmpty();}
    bool EnQueue(
const T& x);
    bool DeQueue(T
& x);
    bool getFront(T
& x)const;
    void makeEmpty();
    bool 
IsEmpty()const{return(front==rear)?true:false;}
    
int getSize()const;
//    bool IsFull()const{return((rear+1)%maxSize==front)?true:false;}
    friend ostream
& operator<<(ostream& os,LinkedQueue<T>& Q);  
protected:
    LinkNode
<T> *front, *rear;
};

template
<class T>
void LinkedQueue
<T>::makeEmpty()
{
    LinkNode
<T> *p;
    
while(front!=NULL)
    {
        p
=front;
        front
=front->link;
        delete p;
    }
};

template
<class T>
bool LinkedQueue
<T>::EnQueue(const T& x)
{
    
if(front==NULL)
    {
        front
=rear=new LinkNode<T>(x);//空队列时,新结点成为队列的第一个结点,既是对头也是队尾
        
if(front==NULL) return false;
    }
    
else
    {
        rear
->link=new LinkNode<T>(x);//非空时在链尾追加新的结点并更新队尾指针
        
if(rear->link==NULL) return false;
        rear
=rear->link;
    }
    return 
true;
};

template
<class T>
bool LinkedQueue
<T>::DeQueue(T& x)
{
    
if(IsEmpty()==true) return false;
    LinkNode
<T> *p=front;
    x
=front->data;
    front
=front->link;
    delete p;
    return 
true;
};

template
<class T>
bool LinkedQueue
<T>::getFront(T& x)const
{
    
if(IsEmpty()==true) return false;
    x
=front->data;
    return 
true;
};

template
<class T>
int LinkedQueue<T>::getSize()const
{
    LinkNode
<T> *p=front;
    
int k=0;
    
while(p!=NULL)
    {
        p
=p->link;
        k
++;
    }
    return k;
};

template
<class T>
ostream
& operator<<(ostream& os,LinkedQueue<T>& Q)
{
    os
<<"队列中的元素个数有"<<Q.getSize()<<endl;
    LinkNode
<T> *p=Q.front;
    
int i=0;
    
while(p!=NULL)
    {
        os
<<++i<<":"<<p->data<<endl;
        p
=p->link;
    }
    return os;
}
### Java 实现循环队列 #### 基于数组的循环队列 为了实现一个高效的循环队列,可以利用数组来存储元素,并通过两个指针 `front` 和 `rear` 来追踪队首和队尾位置。当 `rear` 到达数组末尾时,将其重置为数组起始位置,从而形成环形结构。 ```java public class CircularQueue<T> { private T[] elements; private int front = 0; private int rear = -1; private final int capacity; @SuppressWarnings("unchecked") public CircularQueue(int capacity) { this.capacity = capacity; this.elements = (T[]) new Object[capacity]; } public boolean isFull() { return (rear + 1) % capacity == front; } public boolean isEmpty() { return front == (rear + 1) % capacity && size() == 0; } public void enqueue(T element) throws Exception { if (isFull()) throw new Exception("Queue Overflow"); rear = (rear + 1) % capacity; elements[rear] = element; } public T dequeue() throws Exception { if (isEmpty()) throw new Exception("Queue Underflow"); T item = elements[front]; elements[front] = null; front = (front + 1) % capacity; return item; } public int size() { if (rear >= front) { return rear - front + 1; } else { return rear + 1 + capacity - front; } } } ``` 此代码展了如何创建固定容量的循环队列并处理入队 (`enqueue`) 和出队 (`dequeue`) 操作[^1]。 #### 基于单向链表循环队列 另一种方法是采用单向链表作为底层数据结构。这种方式允许动态调整队列长度而不受预设容量限制。对于基于链表循环队列而言,在每次插入新节点之后都将最后一个节点接回第一个节点以保持闭环特性。 ```java class Node<E> { E data; Node<E> next; Node(E data) { this.data = data; this.next = null; } } public class LinkedListCircularQueue<E> { private Node<E> head; private Node<E> tail; private int count; public LinkedListCircularQueue() { head = null; tail = null; count = 0; } public boolean isEmpty() { return count == 0; } public void enqueue(E value) { Node<E> newNode = new Node<>(value); if (tail == null) { head = newNode; tail = newNode; newNode.next = head; // Make it circular. } else { tail.next = newNode; tail = newNode; tail.next = head; // Keep the circle closed after adding a node. } ++count; } public E dequeue() throws Exception { if (isEmpty()) throw new Exception("Cannot remove from an empty queue"); E result = head.data; if (head == tail) { // If there's only one element in the queue, head = null; // then set both pointers to null and break the loop. tail = null; } else { head = head.next; tail.next = head; // Update last pointer so that it points back at the first. } --count; return result; } public int getSize() { return count; } } ``` 上述实现了使用单向链表构建的循环队列类,其中包含了基本的操作如加入(`enqueue`)、移除(`dequeue`)以及查询当前大小(`getSize`)[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值