.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int datatype;
typedef struct //定义队列
{
datatype data[MAXSIZE];
int front;
int rear;
}*queue,Queue;
//顺序队列
queue create();
int enqueue(queue l,datatype e);
void outout(queue l);
int dequeue(queue l);
//循环队列
int loop_enqueue(queue l,datatype e);
void loop_outout(queue l);
int loop_dequeue(queue l);
int loo_count(queue l);
//链式队列
typedef struct Node //定义链表
{
union
{
int len;
datatype data;
};
struct Node *next;
}*linklist;
typedef struct QUE // 嵌套 定义指针
{
linklist front;
linklist rear;
}*linkQueue;
linkQueue link_creat();
linklist data_creat(); //创建其他节点
int link_enqueue(linkQueue q,datatype e);
void link_output(linkQueue q);
int link_dequeue(linkQueue q);
linkQueue free_space(linkQueue q);
.c
#include "2.h"
queue create()
{
queue l = (queue)malloc(sizeof(Queue));
if(l==NULL)
{
return NULL;
}
l->front = l->rear = 0;
return l;
}
int enqueue(queue l,datatype e)
{
if(l==NULL||l->rear ==MAXSIZE)//满
{
printf("入队失败\n");
return -1;
}
l->data[l->rear++]=e;
return 0;
}
void outout(queue l)
{
if(l==NULL||l->front == l->rear)
{
printf("遍历失败\n");
return;
}
for(int i = l->front;i < l->rear;i++)
{
printf("%d\t",l->data[i]);
}
putchar(10);
}
int dequeue(queue l)
{
if(l==NULL||l->front == l->rear)//空
{
printf("出队失败\n");
return -1;
}
printf("出队的元素是%d\n",l->data[l->front++]);
return 0;
}
//循环队列
int loop_enqueue(queue l,datatype e)
{
if(l==NULL||l->front ==(l->rear+1)%MAXSIZE)//满
{
printf("入队失败\n");
return -1;
}
l->data[l->rear]=e;
l->rear = (l->rear+1)%MAXSIZE;
return 0;
}
void loop_outout(queue l)
{
if(l==NULL||l->front == l->rear)
{
printf("遍历失败\n");
return;
}
for(int i = l->front;i != l->rear;i=(i+1)%MAXSIZE)
{
printf("%d\t",l->data[i]);
}
putchar(10);
}
int loop_dequeue(queue l)
{
if(l==NULL||l->front == l->rear)//空
{
printf("出队失败\n");
return -1;
}
printf("出队的元素是%d\n",l->data[l->front]);
l->front = (l->front+1)%MAXSIZE;
return 0;
}
int loo_count(queue l)
{
return (MAXSIZE - l->front + l->rear)%MAXSIZE;
}
//链式队列
linkQueue link_creat()//创建队列 给指针q(队列)申请一个地址让他指向节点的头
{
linkQueue q = (linkQueue)malloc(sizeof(struct QUE));
if(q==NULL)
{
return NULL;
}
linklist L = (linklist)malloc(sizeof(struct Node));//创建的是首节点
if(L==NULL)
{
return NULL;
}
L->len = 0;
L->next = NULL;
q->front = L;
q->rear = L;
return q;
}
linklist data_creat() //创建其他节点
{
linklist s = (linklist)malloc(sizeof(struct Node));
s->data = 0;
s->next = NULL;
return s;
}
int link_enqueue(linkQueue q,datatype e) //尾插头删
{
if(q==NULL)
{
return -1;
}
linklist s = data_creat();
if(s==NULL)
{
return -1;
}
s->data = e;
q->rear->next = s;//现在q->rear还在指向最后一个元素,然后让最后一个元素下一个指向新加进来的s
q->rear = s;//断开q->rear指向最后一个元素的线 让他指向新元素
q->front->len --;
return 0;
}
void link_output(linkQueue q)
{
if(q==NULL || q->front == q->rear)
{
return;
}
linklist p = q->front;
while(p->next !=NULL)
{
p=p->next;
printf("%d\t",p->data);
}
putchar(10);
}
int link_dequeue(linkQueue q) //尾插头删
{
if(q==NULL || q->front == q->rear)
{
return -1;
}
if(q->front->next = q->rear)
{
q->front = q->rear;
}
linklist del = q->front->next;
int d = del->data;
q->front->next = del->next;
free(del);
del==NULL;
q->front->len --;
return d;
}
linkQueue free_space(linkQueue q)
{
if(q==NULL)
{
return NULL;
}
int n = q->front->len;
for(int i =0;i < n ; i++)
{
link_dequeue(q);
}
free(q->front);
q->front = q->rear = NULL;
free(q);
q=NULL;
return q;
}
main.c
#include "2.h"
int main(int argc, const char *argv[])
{
/* queue l = create();
datatype e;
char s[10];
while(1)
{
printf("请输入入队的元素:");
scanf("%d",&e);
enqueue(l,e);
printf("还要继续入队吗? yes/no");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
outout(l);
while(1)
{
dequeue(l);
printf("还要继续出队吗? yes/no");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
outout(l);
*/
/* queue l = create();
datatype e;
char s[10];
while(1)
{
printf("请输入入队的元素:");
scanf("%d",&e);
loop_enqueue(l,e);
printf("还要继续入队吗? yes/no");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
loop_outout(l);
while(1)
{
loop_dequeue(l);
printf("还要继续出队吗? yes/no");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
loop_outout(l);
loop_enqueue(l,1000);
loop_outout(l);
int count = loo_count(l);
printf("循环队列的个数:%d\n",count);
*/
linkQueue q = link_creat();
char s[3];
datatype e;
while(1)
{
printf("请输入要入队的元素:");
scanf("%d",&e);
link_enqueue(q,e);
printf("请问还要继续入队吗? yes/no :");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
link_output(q);
while(1)
{
int d = link_dequeue(q);
printf("出队的元素是%d\n",d);
printf("要继续出队吗? yes/no:");
scanf("%s",s);
if(strcmp(s,"no")==0)
{
break;
}
}
link_output(q);
q = free_space(q);
return 0;
}