顺序队列(头删尾插) 循环队列(头删尾插) 链式队列(头删尾插)
顺序队列(头删尾插)
1> 队列:只允许在表尾插入、表头删除的操作受限的线性表
#ifndef __SHUNXUDUILIE_H__
#define __SHUNXUDUILIE_H__
#include
#include
#include
#define MAXSIZE 3
typedef int datatype;
typedef struct{int front;int rear;datatype data[MAXSIZE];
}Queue;
Queue*Creat();
int InputQueue(Queue*L,datatype value);
void Output(Queue*L);
int OutQueue(Queue*L);
#endif
#include"shunxuduilie.h"
Queue*Creat()
{Queue*p=(Queue*)malloc(sizeof(Queue));if(p==NULL){printf("创建失败\n");return NULL;}p->front=0;p->rear=0;return p;
}
//尾插头删
int InputQueue(Queue*L,datatype value)
{if(L->rear==MAXSIZE){printf("队列已满\n");return -1;}L->data[L->rear++]=value;printf("%d已出队\n",L->data[L->rear]);return 0;
}
void Output(Queue*L)
{for(int i=L->front;i<L->rear;i++){printf("%d\t",L->data[i]);}
}
//出队
int OutQueue(Queue*L)
{if(L->front==L->rear){printf("队列为空\n");return -1;}printf("\n%d出队\n",L->data[L->front]);L->front++;return 0;
}
#include"shunxuduilie.c"
int main(int argc, const char *argv[])
{Queue*L=Creat();if(L==NULL){printf("队列创建失败\n");return -1;}while(1){datatype value;printf("输入入队值:");scanf("%d",&value);int ret=InputQueue(L,value);if(ret==-1){break;}char ch[10];printf("\n入队y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}}printf("队列:");Output(L);while(1){char ch[10];printf("\n出队y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}int ret=OutQueue(L);if(ret==-1){break;}}printf("队列:");Output(L);return 0;
}
循环队列(头删尾插)
循环队列解决顺序队列的假溢出问题
#ifndef __LOOPDUILIE_H__
#define __LOOPDUILIE_H__
#include
#include
#include
#define MAXSIZE 4
typedef int datatype;
typedef struct{int front;int rear;datatype data[MAXSIZE];
}Queue;
Queue*Creat();
int InputQueue(Queue*L,datatype value);
void Output(Queue*L);
int OutQueue(Queue*L);
#endif
#include"Loopduilie.c"
int main(int argc, const char *argv[])
{Queue*L=Creat();if(L==NULL){printf("队列创建失败\n");return -1;}while(1){datatype value;printf("输入入队值:");scanf("%d",&value);int ret=InputQueue(L,value);if(ret==-1){break;}char ch[10];printf("\n入队y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}}printf("队列:");Output(L);while(1){char ch[10];printf("\n出队y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}int ret=OutQueue(L);if(ret==-1){break;}}printf("队列:");Output(L);return 0;
}
#include"Loopduilie.h"
Queue*Creat()
{Queue*p=(Queue*)malloc(sizeof(Queue));if(p==NULL){printf("创建失败\n");return NULL;}p->front=0;p->rear=0;return p;
}
//尾插头删
int InputQueue(Queue*L,datatype value)
{if((L->rear+1)%MAXSIZE==L->front){printf("队列已满\n");return -1;}L->data[L->rear]=value;printf("%d已入队\n",L->data[L->rear]);L->rear=(L->rear+1)%MAXSIZE;return 0;
}
void Output(Queue*L)
{for(int i=L->front;i!=L->rear;i=(i+1)%MAXSIZE){printf("%d\t",L->data[i]);}
}
//出队
int OutQueue(Queue*L)
{if(L->front==L->rear){printf("队列为空\n");return -1;}printf("\n%d出队\n",L->data[L->front]);L->front=(L->front+1)%MAXSIZE;return 0;
}
链式队列(头删尾插)
2> 链式队列的插入等价于单向链表的尾插,删除等价于单向链表的头删
队列的队尾等价于单向链表的尾结点,队列队头等价于单向链表的头结
## 3>
链式队列的插入等价于单向链表的头插,删除等价于单向链表的尾
删
## 队列的队尾等价于单向链表的头结点,队列队头等价于单向链表的尾结点
#ifndef __LINKDILIE_H__
#define __LINKDILIE_H__
#include
#include
#include
typedef int datatype;
typedef struct node{union{int len;datatype data;};struct node*next;
}Node;
typedef struct{Node *front;Node *rear;
}queue;
//出队 头删
int DelRear(queue*L);
void Output(queue*L);
//队列尾插 先进先出
int Enqueue(queue*L,datatype value);
queue*Creat();
#endif
#ifndef __LINKDILIE_H__
#define __LINKDILIE_H__
#include
#include
#include
typedef int datatype;
typedef struct node{union{int len;datatype data;};struct node*next;
}Node;
typedef struct{Node *front;Node *rear;
}queue;
//出队 头删
int DelRear(queue*L);
void Output(queue*L);
//队列尾插 先进先出
int Enqueue(queue*L,datatype value);
queue*Creat();
#endif
#include"Linkduilie.c"
int main(int argc, const char *argv[])
{queue*L=Creat();if(L==NULL){return -1;}while(1){char ch[10];datatype value;printf("\n输入入队的元素:");scanf("%d",&value);Enqueue(L,value);printf("还需要继续y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}}printf("\n队列头到尾:");Output(L);while(1){char ch[10];printf("\n还需要出队y/n?");scanf("%s",ch);if(strcmp(ch,"n")==0){break;}if(DelRear(L)==-1){break;}}printf("\n队列头到尾:");Output(L);return 0;
}
#include"Linkduilie.h"
queue*Creat()
{Node*L=(Node*)malloc(sizeof(Node));if(L==NULL){return NULL;}L->len=0;queue*p=(queue*)malloc(sizeof(queue));if(p==NULL){return NULL;}p->front=L;p->rear=L;return p;
}//队列尾插 先进先出
int Enqueue(queue*L,datatype value)
{Node*p=(Node*)malloc(sizeof(Node));if(p==NULL){return -1;}p->data=value;p->next=NULL;printf("%d入队\n",p->data);//插入L->rear->next=p;L->rear=p;L->front->len++;return 0;
}
void Output(queue*L)
{
/* Node*p=L->front;while(p->next){p=p->next;printf("%d\t",p->data);}*/int n=L->front->len;Node*p=L->front;for(int i=0;i<n;i++){p=p->next;printf("%d\t",p->data);}
/* int n=L->front->len;for(int i=0;ifront=L->front->next;printf("%d\t",L->front->data);}*/
}
//出队 头删
int DelRear(queue*L)
{if(L->front->len==0){printf("空栈\n");return -1;}Node*q=L->front->next;L->front->next=q->next;L->front->len--;free(q);q=NULL;return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
