队列也是线性表,也是分为两种的:1、顺序队列 2、链队列
顺序队列
#include <stdio.h>
#include <stdlib.h>typedef struct {char *base;int front;int erer;int size;
}SqQueue;void initSqQueue(SqQueue *queue,int size){queue->base=(char*)malloc(sizeof(char)*size);queue->erer=0;queue->front=0;queue->size=size;
}
//入队
void enqueue(SqQueue *queue,char value){if(((queue->erer+1)%queue->size)==queue->front){printf("队满操作\n");}else{queue->base[queue->erer%queue->size]=value;queue->erer=++queue->erer%queue->size;}
}
//出队
void dequeue(SqQueue *queue){if(queue->front!=queue->erer){printf("%c",(queue->base)[queue->front]);queue->front=++queue->front%queue->size;}else{printf("队空操作!\n");}
}int getSize(SqQueue *queue){return (queue->erer-queue->front+queue->size)%queue->size;
}int main(){SqQueue queue;initSqQueue(&queue,10);enqueue(&queue,'I');enqueue(&queue,' ');enqueue(&queue,'L');enqueue(&queue,'o');enqueue(&queue,'v');printf("%d\n",getSize(&queue));system("pause");return 0;
}
链队列
#include <stdio.h>
#include <stdlib.h>typedef struct lqSqNode {char value;struct lqSqNode *next;
}*LinkSQ,lqSqNode;typedef struct LqQueue {lqSqNode *front;lqSqNode *erer;int size;
}LqQueue;
//初始化链队列
void initLqQueue(LqQueue *queue){LinkSQ linksq=(LinkSQ)malloc(sizeof(lqSqNode));linksq->next=NULL;queue->front=linksq;queue->erer=linksq;queue->size=0;
}
//入队
void enqueue(char value,LqQueue *queue){lqSqNode *node=(lqSqNode*)malloc(sizeof(lqSqNode));node->value=value;node->next=NULL;queue->erer->next=node;queue->erer=node;++queue->size;
}
//出队
void dequeue(LqQueue *queue){if(queue->front->next!=NULL){ lqSqNode *node=queue->front->next;
// printf("%c\n",node->value);queue->front->next=node->next;free(node); --queue->size;}else{printf("队空操作!\n");}
}int main(){LqQueue queue;initLqQueue(&queue);enqueue('i',&queue);enqueue('L',&queue);enqueue('k',&queue);enqueue('l',&queue);
// dequeue(&queue);
// dequeue(&queue);
// dequeue(&queue);
// dequeue(&queue);
// dequeue(&queue);printf("%d\n",queue.size);system("pause");return 0;
}