#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
//入队
void Enqueue(int x)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
if (front == NULL && rear == NULL)
{
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
//出队
void Dequeue()
{
struct Node* temp = front;
if (front == NULL)
{
printf("Queue is empty\n");
}
if (front == rear)
{
front = rear = NULL;
}
else
{
front = front->next;
}
free(temp);
}
//返回队首
int Front()
{
if (front == NULL)
{
printf("Queue is empty\n");
}
return front->data;
}
void Print()
{
struct Node* temp = front;
while (temp != NULL)
{
printf("%d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
Enqueue(2); Print();
Enqueue(4); Print();
Enqueue(6); Print();
Dequeue(); Print();
Enqueue(8); Print();
}
数据结构-Queue-链表实现
最新推荐文章于 2024-08-08 17:05:23 发布