队列的链式存储
作者:
zzu
,
2024-03-27 21:01:01
,
所有人可见
,
阅读 2
#include <iostream>
using namespace std;
const int N = 100010;
struct Node {
int val;
Node *next;
Node (): next(NULL){}
Node(int _val): val(_val), next(NULL){}
};
void print(Node *front)
{
for(auto p = front; p->val; p = p->next)
{
cout << p->val << " ";
}cout << endl;
}
int main()
{
Node *front = new Node(), *rear = front;
rear->val = 1;
rear->next = new Node();
rear = rear->next;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
rear->val = 2;
rear->next = new Node();
rear = rear->next;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
rear->val = 3;
rear->next = new Node();
rear = rear->next;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
auto a = front;
front = front->next;
delete a;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
auto b = front;
front = front->next;
delete b;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
auto c = front;
front = front->next;
delete c;
if(front != rear )
{
cout << "队列不为空" << endl;
print(front);
cout << "对头是" << front->val << endl;
}else
cout << "队列为空" << endl;
return 0;
}