文章目录
队列的链式定义
typedef struct LNode
{
struct LNode *next;
int data;
}LNode;
typedef struct
{
LNode *front,*rear;
}LinkQueue;
队列的链式实现分类
分为有头节点和无头节点
有头节点的链式队列
链式队列初始化(有头节点)
void InitLinkQueue(LinkQueue *Q)
{
Q->front=Q->rear=(LNode *)malloc(sizeof(LNode));
Q->front->next=NULL;
}
判断队空(有头节点)
bool IsLinkQueueEmpty(LinkQueue *Q)
{
if(Q->front==Q->rear)
return true;
else
return false;
}
链式队列的创建(有头节点)
void CreateLinkQueue(LinkQueue *Q)
{
int data;
printf("请输入整数:");
scanf("%d",&data);
bool ishead=true;
while(data!=9999)
{
LNode *newNode=(LNode *)malloc(sizeof(LNode));
newNode->data=data;
newNode->next=NULL;
if(ishead)
{
Q->front->next=newNode;
ishead=false;
}else
{
Q->rear->next=newNode;
}
Q->rear=newNode;
printf("请输入整数:");
scanf("%d",&data);
}
}
打印链式队列(有头节点)
void PrintLinkQueue(LinkQueue *Q)
{
LNode *node=Q->front->next;
int i=1;
while(node!=NULL)
{
printf("第%d位:%d\n",i,node->data);
node=node->next;
i++;
}
}
入队(有头节点)
bool EnLinkQueue(LinkQueue *Q,int e)
{
LNode *newNode=(LNode *)malloc(sizeof(LNode));
newNode->data=e;
newNode->next=NULL;
Q->rear->next=newNode;
Q->rear=newNode;
return true;
}
出队(有头节点)
int DeLinkQueue(LinkQueue *Q)
{
if(IsLinkQueueEmpty(Q))return -1;
LNode *newNode=Q->front->next;
Q->front->next=newNode->next;
int e=newNode->data;
if(newNode==Q->rear)//如果是最后一个节点出队(第一次没写出来)
Q->rear=Q->front;
free(newNode);
return e;
}
获取队头元素(有头节点)
int GetTopLinkQueue(LinkQueue *Q)
{
if(IsLinkQueueEmpty(Q))return -1;
LNode *newNode=Q->front->next;
int e=newNode->data;
return e;
}
测试代码
int main(int argc, const char * argv[])
{
LinkQueue *Q=(LinkQueue *)malloc(sizeof(LinkQueue));
InitLinkQueue(Q);
CreateLinkQueue(Q);
PrintLinkQueue(Q);
EnLinkQueue(Q,3);
printf("EnLinkQueue(Q,3)\n");
PrintLinkQueue(Q);
DeLinkQueue(Q);
printf("DeLinkQueue(Q)\n");
PrintLinkQueue(Q);
int e=GetTopLinkQueue(Q);
printf("GetTopLinkQueue(Q)=%d\n",e);
return 0;
}
测试结果
请输入整数:11
请输入整数:12
请输入整数:13
请输入整数:14
1请输入整数:15
请输入整数:9999
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
EnLinkQueue(Q,3)
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
第6位:3
DeLinkQueue(Q)
第1位:12
第2位:13
第3位:14
第4位:115
第5位:3
GetTopLinkQueue(Q)=12
Program ended with exit code: 0
无头节点的链式队列
链式队列初始化(无头节点)
void InitLinkQueue_WithoutHead(LinkQueue *Q)
{
Q->front=Q->rear=NULL;
}
判断队空(无头节点)
bool IsLinkQueueEmpty_WithoutHead(LinkQueue *Q)
{
if(Q->front==NULL)
return true;
else
return false;
}
链式队列的创建(无头节点)
void CreateLinkQueue_WithoutHead(LinkQueue *Q)
{
int data;
printf("请输入整数:");
scanf("%d",&data);
bool ishead=true;
while(data!=9999)
{
LNode *newNode=(LNode *)malloc(sizeof(LNode));
newNode->data=data;
newNode->next=NULL;
if(ishead)
{
Q->front=newNode;
ishead=false;
}else
{
Q->rear->next=newNode;
}
Q->rear=newNode;
printf("请输入整数:");
scanf("%d",&data);
}
}
打印链式队列(无头节点)
void PrintLinkQueue_WithoutHead(LinkQueue *Q)
{
LNode *node=Q->front;
int i=1;
while(node!=NULL)
{
printf("第%d位:%d\n",i,node->data);
node=node->next;
i++;
}
}
入队(无头节点)
bool EnLinkQueue_WithoutHead(LinkQueue *Q,int e)
{
LNode *newNode=(LNode *)malloc(sizeof(LNode));
newNode->data=e;
newNode->next=NULL;
Q->rear->next=newNode;
Q->rear=newNode;
return true;
}
出队(无头节点)
int DeLinkQueue_WithoutHead(LinkQueue *Q)
{
if(IsLinkQueueEmpty_WithoutHead(Q))return -1;
LNode *newNode=Q->front;
Q->front=newNode->next;
int e=newNode->data;
if(newNode==NULL)//如果是最后一个节点出队(第一次没写出来)
Q->rear=Q->front=NULL;
free(newNode);
return e;
}
获取队头元素(无头节点)
int GetTopLinkQueue_WithoutHead(LinkQueue *Q)
{
if(IsLinkQueueEmpty_WithoutHead(Q))return -1;
LNode *newNode=Q->front;
int e=newNode->data;
return e;
}
测试代码
LinkQueue *Q=(LinkQueue *)malloc(sizeof(LinkQueue));
InitLinkQueue_WithoutHead(Q);
CreateLinkQueue_WithoutHead(Q);
PrintLinkQueue_WithoutHead(Q);
EnLinkQueue_WithoutHead(Q,3);
printf("EnLinkQueue_WithoutHead(Q,3)\n");
PrintLinkQueue_WithoutHead(Q);
DeLinkQueue_WithoutHead(Q);
printf("DeLinkQueue_WithoutHead(Q)\n");
PrintLinkQueue_WithoutHead(Q);
int e=GetTopLinkQueue_WithoutHead(Q);
printf("GetTopLinkQueue_WithoutHead(Q)=%d\n",e);
return 0;
测试数据
请输入整数:11
请输入整数:12
请输入整数:13
请输入整数:14
1请输入整数:15
请输入整数:9999
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
EnLinkQueue_WithoutHead(Q,3)
第1位:11
第2位:12
第3位:13
第4位:14
第5位:115
第6位:3
DeLinkQueue_WithoutHead(Q)
第1位:12
第2位:13
第3位:14
第4位:115
第5位:3
GetTopLinkQueue_WithoutHead(Q)=12
Program ended with exit code: 0