[英]C++ Queue from Linked List
晚上好。 我一直在嘗試用C ++實現一個Queue類,以先前創建的Linked List類為基礎。 鏈接鏈表:
#include <cstddef>
#include <iostream>
#include <cstdio>
using namespace std;
template <class T>
class LinkedList {
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
MakeEmpty();
}
struct Node {
T value;
Node *next;
};
Node* getHead() {
return head;
}
void Print();
void Insert();
void MakeEmpty();
private:
Node *head; // Head of the linked list.
};
隊列類:
#include "LinkedList.h"
template <class T>
class Queue {
public:
Queue() {
LinkedList<T>::Node *tnode = Q.getHead();
}
~Queue() {
Q.MakeEmpty();
}
void Enqueue( T x ) {
LinkedList<T>::Node *cnode = Q.getHead();
//Find the last element of Q
while( cnode -> next != NULL ) {
cnode = cnode -> next;
}
//Add x to the end of the queue
Q.Insert( x );
}
void Dequeue() {
LinkedList<T>::Node *hnode = Q.getHead();
//Rest of function
}
void Print() {
Q.PrintList();
}
private:
LinkedList<T> Q;
};
你可能已經注意到了,我正在制作模板類。 編譯時,我被告知tnode(在Queue類的構造函數中找到)尚未在范圍中聲明。 對於如何解決這個問題,有任何的建議嗎?
編輯1:我得到的錯誤信息是:
RCQueue.h:在構造函數'Queue :: Queue()'中:
RCQueue.h:8:28:錯誤:'tnode'未在此范圍內聲明LinkedList :: Node * tnode = Q.getHead();
我的構造函數的主要目的是將LinkedList類中的“head”指針初始化為NULL。 我也很好奇如何宣布在另一個模板類中聲明的結構的變量。
Enqueue Algorithm :
1. Create a newNode with data and address.
2. if queue i.e front is empty
i. front = newnode;
ii. rear = newnode;
3. Else
i.rear->next = newnode;
ii.rear = newnode;
Dequeue Algorithm :
1. if queue is i.e front is NULL printf("\nQueue is Empty \n");
2. Else next element turn into front
i. struct node *temp = front ;
ii. front = front->next;
iii.free(temp);
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *front = NULL;
node *rear =NULL;
void Enque(int data)
{
node *newnode = new node;
newnode->data = data;
newnode ->next = NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next = newnode;
rear = newnode;
}
}
void Deque()
{
struct node *temp;
if (front == NULL)
{
printf("\nQueue is Empty \n");
return;
}
else
{
temp = front;
front = front->next;
if(front == NULL) rear = NULL;
free(temp);
}
}
void display()
{
node *temp=front;
if(front==NULL)
{
printf("\nQueue is Empty \n");
}
else
{
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
cout<<endl;
}
在Queue
引用的LinkedList
中每次使用Node
類型時都需要typename
,因為它依賴於模板參數T
具體來說,
template <class T>
class Queue {
public:
Queue() {
typename LinkedList<T>::Node *tnode = Q.getHead();
}
~Queue() {
Q.MakeEmpty();
}
void Enqueue( T x ) {
typename LinkedList<T>::Node *cnode = Q.getHead();
//Find the last element of Q
while( cnode -> next != NULL ) {
cnode = cnode -> next;
}
//Add x to the end of the queue
Q.Insert( x );
}
void Dequeue() {
typename LinkedList<T>::Node *hnode = Q.getHead();
//Rest of function
}
void Print() {
Q.PrintList();
}
private:
LinkedList<T> Q;
};
注意在使用LinkedList<T>::Node
之前添加了typename
。
當然,您還會聽到有關在Queue
類中調用的LinkedList
中缺少MakeEmpty()
定義的抱怨,因此只需為其添加定義即可。
有關為什么需要typename
更多信息, 這篇文章很清楚地解釋了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.