0
点赞
收藏
分享

微信扫一扫

队列:使用链表实现一个队列

Go_Viola 2022-03-11 阅读 65
#include<iostream>

using namespace std;

struct Node
{
	int data;
	Node* next;
};

Node* front = NULL;

Node* rear = NULL;

void Enqueue(int x)
{
	Node* temp = new Node;
	temp->data = x;
	temp->next = NULL;
	if(front == NULL && rear == NULL)
	{
		front = rear = temp;
		return;
	}
	rear->next = temp;
	rear = temp;
	
}

void Dequeue()
{
	Node* temp = front;
	if(front == NULL) return;
	if(front == rear)
		front = rear = NULL;
	else
		front = front->next;
		
	delete temp;	
}

bool IsEmpty()
{
	return front == NULL ? true : false;
}

int Front()
{
	return front->data;
}

void Print()
{
	Node* temp = front;
	printf("queue:\n");
	while(temp->next != NULL)
	{
		printf("%d ",temp->data);
		temp = temp->next;
	}
	printf("\n");
}
int main()
{
	Enqueue(4);
	printf("The front of queue is %d\n",Front());
	Print();
	
	Enqueue(2);
	printf("The front of queue is %d\n",Front());
	Print();
	
	Enqueue(11);
	printf("The front of queue is %d\n",Front());
	Print();
	
	Enqueue(61);
	printf("The front of queue is %d\n",Front());
	Print();
	
	Dequeue();
	printf("The front of queue is %d\n",Front());
	Print();
	
	return 0;
}

 

举报

相关推荐

0 条评论