队列,数组实现,指针实现的源码详细讲解

int rear ;//队尾指针
int front;//对头指针
const int MAXQUEUE = 10;//该队列最大能放多少数据
int queue[MAXQUEUE] ;
int InQueue(int value)
{
    if(rear>=MAXQUEUE)
        return 0;
    rear++;//队尾指针
    queue[rear]=value;
    return 1;
}
int OutQueue(int *value)
{
    if(front == rear)
        return 0;
    front++;
    *value = queue[front];
    return 1;
}

//用循环队列来存储数据

int InQueue(int value)
{
    if(front==-1||rear == MAX_SIZE||rear+1==front)//表示该队列已经放满了
        return 0;
    if(rear == MAX_SIZE)//队尾指针满了之后从头又放数据
        rear=0;
    rear++;
    queue[rear]=value;
    return 1;
}

int OutQueue(int *value)
{
    if(rear == front)
        return 0;
        
    front++;
    if(front==MAX_SIZE)
        front=0;
        
    *value = queue[front];
    return 1;
}

-------------------------------------------------------------------
//用指针来实现队列
#include<stdio.h>

struct Queue_node{
int data;
Queue_node *next;
};
Queue_node *rear = NULL;
Queue_node *front = NULL;

int InQueue(int value)
{
        Queue_node *q = new Queue_node;
        if(q==NULL)
        return 0;
        q->data = value;
        q->next=NULL;
        if(front == NULL)
        {
            front = q;
        }
        else
        rear->next = q;//队尾的下一个节点是q
        rear = q;
        return 1;
}
int OutQueue(int *value)
{
    Queue_node *p = NULL;
    if(front ==NULL)
    p = front;
    front = front->next;//队首去掉一个节点
    *value = p->data;
    return 1;
    
}

--------------------------------------------------------
双队列存储数据
#include<stdio.h>
#include<stdlib.h>
struct Queue{//输入限制性
    int data;
    Queue *next;
};

Queue *front = NULL;
Queue *next = NULL;
//输入限制性队列--一边输入两边输出
int InQueue(int value)
{
 Queue *q = new Queue;
 if(q==0) return 0;
 q->data  = value;
 q->next =NULL;
 if(front ==NULL) 
 front = q;
 else rear ->next = q;
 return 1;  
}

int OutQueue(int *value)
{
    Queue * p = NULL;
    if(front ==NULL)
    return 0;
    p = front;
    front= front->next;
    *value = p->data;
    return 1;
}
int OutQueueByrear(int *value);
{
    Queue *p =NULL;
    if(rear == NULL)
    return 0;
    if(rear ==front)
    {
        *value = p->data;
        free(rear);
        rear =NULL;
        front = NULL;
    }else
    {
        p = front;
        while(p->next!= rear)
        p = p->next;
        *value = rear->data;
        free(rear);
        rear=p;
        rear ->next = NULL;
    }
    return 1;
}

//输出限制性的双队列
struct queue{
int data;
queue *next;
};
int OutQueue(int *value)
{
    queue *q = NULL;
    if(front==NULL)
    return 0;
    q = front;
    front = front->next;
    free(q);
    return 1;
}
int InQueueByrear(int *value)
{
    queue *p  = new queue;
    if(p==NULL) return 0;
    p->data = value;
    p->next= NULL;
    if(rear==NULL)
    front = p;
    else rear->next = p;
    rear = p;
    return 1;
}
int InQueueByfront(int *value)
{
    queue *q = new queue;
    if(q==NULL) return 0;
    q->data = value;
    q->next= front;
    front = q;
    if(rear ==NULL)
    rear = q;
    return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值