#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int Data;
struct node* Next;
}Node;
typedef struct HNode{
Node* Front;//分别为节点的指针类型
Node* rear;
int length;
}Quene;
Quene CreateQ (Quene Q)
{
Q.Front=Q.rear=(Node*)malloc(sizeof(Node));
Q.Front->Next=NULL;
return Q;
}
int IsEmpty(Quene *Q);
void AddQ(Quene *Q,int data)
{
Node* k=(Node*)malloc(sizeof(Node));
k->Data=data;
k->Next=NULL;
Q->rear->Next=k;//完成前单元对后单元的连接
Q->rear=k;//进行尾指针的移动
Q->length++;
}
bool DeleteQ(Quene *Q)
{
if(IsEmpty(Q))
{
return false;
}
else
{
Q->Front->Next=Q->Front->Next->Next;//头节点的区域始终没有存数据,在头的Next是第一个
return true;
}
}
int IsEmpty(Quene *Q)
{
if(Q->Front==Q->rear)
return 1;
else
return 0;
}
void PrintQ(Quene Q)
{
while(Q.Front!=Q.rear)
{
printf("%d\n",Q.Front->Next->Data);
Q.Front->Next = Q.Front->Next->Next;
}
return ;
}
int main()
{
Quene Q;
Q.Front=Q.rear=NULL;
Q=CreateQ(Q);
AddQ(&Q,100);
AddQ(&Q,10);
DeleteQ(&Q);
PrintQ(Q);
return 0;
}
C语言链式队列添加元素,创建
于 2024-11-13 22:59:27 首次发布