队列也是线性表,也是分为两种的:1、顺序队列 2、链队列
- #include
- #include
-
- 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;
- }
- package linearList;
-
- /*
- * 实现一个顺序队列
- * */
-
- public class SqQueue
{ - T h[];
- int front;
- int tail;
- int size;
-
- public SqQueue(int size) {
- this.size = size;
- this.h=(T[]) new Object[size];
- }
- //入队
- SqQueue
enqueue(T value){ - if ((tail+1)%size==this.front){
- System.out.println("栈满操作");
- return null;
- }else{
- this.h[tail]=value;
- tail=(tail+1)%size;
- return this;
- }
- }
- //出队
- void dequeue(){
- if (tail!=front){
- System.out.println(h[front]);
- front=(front+1)%size;
- }else{
- System.out.println("空队列操作!");
- }
- }
- int getSize(){
- return (tail-front+size)%size;
- }
- }
- class sqlQueueTest{
- public static void main(String[] args) {
- SqQueue
SqQueue = new SqQueue<>(6); - SqQueue.enqueue("sad").enqueue("asda").enqueue("SAda");
- SqQueue.dequeue();
- SqQueue.dequeue();
- // SqQueue.dequeue();
- System.out.println(SqQueue.getSize());
- }
- }
- #include
- #include
-
- 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;
- }
- package linearList;
- /*
- * 链表队列的实现
- * */
- class lqSqNode
{ - T value;
- lqSqNode
next; -
- public lqSqNode(T value, lqSqNode
next) { - this.value = value;
- this.next = next;
- }
- }
- public class LqQueue
{ - lqSqNode
front; - lqSqNode
tail; -
- public LqQueue() {
- this.front=null;
- this.tail=null;
- }
-
- //入队
- LqQueue
enqueue(T value){ - lqSqNode newNode=new lqSqNode(value,null);
- if (front==null){
- front=newNode;
- tail=newNode;
- }else{
- tail.next=newNode;
- tail=newNode;
- }
- return this;
- }
-
- //出队
- void dequeue(){
- if (front==null){
- System.out.println("空队列操作!");
- }else{
- System.out.println(front.value);
- front=front.next;
- }
- }
-
- }
- class LqQueueTest{
- public static void main(String[] args) {
- LqQueue lqQueue = new LqQueue();
- lqQueue.enqueue("asd").enqueue("kkk").enqueue("ppp");
- lqQueue.dequeue();
- lqQueue.dequeue();
- lqQueue.dequeue();
- lqQueue.dequeue();
- }
- }