队列

队列:也是一个线性表(即包括顺序队列和链式队列),先进先出,但限制在两端进行插入删除

   队尾:进行存入操作的一端

   队头:进行删除操作的一端

顺序队列:

//sqqueue.h
#ifndef _SQ_QUEUE_H_H
#define _SQ_QUEUE_H_H

#define N 6

typedef int data_t;
typedef struct{
    data_t data[N];
    int front;//指向头
    int rear;//指向尾的后一个    
}sqqueue;

sqqueue *sqqueue_create();
int sqqueue_free(sqqueue *q);
int sqqueue_in(sqqueue *q, data_t value);
data_t sqqueue_out(sqqueue *q);
int sqqueue_empty(sqqueue *q);
int sqqueue_full(sqqueue *q);
int sqqueue_clear(sqqueue *q);
void sqqueue_show(sqqueue *q);


#endif
//sqqueue.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqqueue.h"

sqqueue *sqqueue_create()
{
    sqqueue *q = (sqqueue *)malloc(sizeof(sqqueue));
    if(q == NULL){
        printf("malloc failed!\n");
        return NULL;
    }
    memset(q->data, 0, sizeof(q->data));
    
    q->front = 0;
    q->rear = 0;
    
    return q;
}
int sqqueue_free(sqqueue *q)
{
    if(q == NULL){
        printf("queue is NULL\n");
        return -1;
    }
    free(q);
}

/*
*ret @ 1-empty
*/
int sqqueue_empty(sqqueue *q)
{
    if(q->front == q->rear){
        return 1;
    }else{
        return 0;
    }
}
/*
*ret @ 1-full
*/
int sqqueue_full(sqqueue *q)
{
    if((q->rear+1)%N == q->front){
        return 1;
    }else{
        return 0;
    }        
}

int sqqueue_in(sqqueue *q, data_t value)
{
    if(sqqueue_full(q)){
        printf("queue is full,not in\n");
        return -1;
    }
    q->data[q->rear] = value;
    q->rear = (q->rear+1)%N;

    printf("\n");
    return 0;
}
data_t sqqueue_out(sqqueue *q)
{
    if(sqqueue_empty(q)){
        printf("queue is empty,not out\n");
        return -1;
    }
    data_t data = q->data[q->front];
    q->front = (q->front+1)%N;
    return data;
}

int sqqueue_clear(sqqueue *q)
{
    return 0;
}
void sqqueue_show(sqqueue *q)
{
    while(!sqqueue_empty(q)){
        printf("%d\t",sqqueue_out(q));
    }
    printf("\n");    
}
//sqqueue_test.c
#include <stdio.h>
#include "sqqueue.h"

int main()
{
    sqqueue *q = sqqueue_create();
    sqqueue_in(q, 10);
    sqqueue_in(q, 20);
    sqqueue_in(q, 30);

    

    sqqueue_show(q);
    sqqueue_free(q);
    return 0;
}

 链式队列:

//lk_sqqueue.h
#ifndef _LK_SQQUEUE_H_
#define _LK_SQQUEUE_H_

typedef int data_t;

typedef struct node{
    data_t data;
    struct node *next;
}linknode,*plinknode;

typedef struct{
    plinknode front;
    plinknode rear;
    int count;
}linkqueue;

linkqueue *lkqueue_create();
int lkqueue_in(linkqueue *q, data_t value);
data_t lkqueue_out(linkqueue *q);
int lkqueue_empty(linkqueue *q);
linkqueue *lkqueue_free();
int lkqueue_nodenum(linkqueue *q);

#endif
//lk_sqqueue.c
#include <stdio.h>
#include <stdlib.h>
#include "lk_sqqueue.h"

linkqueue *lkqueue_create()
{
    linkqueue *q = (linkqueue *)malloc(sizeof(linkqueue));
    if(q == NULL){
        printf("q malloc failed\n");
        return NULL;
    }
    q->front = q->rear = NULL;
    q->count = 0;
    return q;
}

int lkqueue_in(linkqueue *q, data_t value)
{
    plinknode new = (plinknode)malloc(sizeof(linknode));
    if(new == NULL){
        printf("new malloc failed\n");
        return -1;
    }
    new->data = value;
    new->next = NULL;
    if(q->count == 0){
        q->front = q->rear = new;
        q->count++;
    }else{
        q->rear->next = new;
        q->rear = new;
        q->count++;
    }
    
    return 0;
}
data_t lkqueue_out(linkqueue *q)
{
    if(q->front == q->rear){
        data_t ret = q->front->data;            
        free(q->front);
        q->front = q->rear = NULL;
        q->count = 0;
        return ret;
    }
    plinknode tmp = NULL;
    tmp = q->front;
    data_t ret = tmp->data;
    q->front = tmp->next;
    free(tmp);
    
    q->count--;
    
    return ret;
}
int lkqueue_empty(linkqueue *q)
{
    if(q->front == NULL){
        return 1;
    }else{
        return 0;
    }
}
linkqueue *lkqueue_free(linkqueue *q)
{
    plinknode tmp = NULL;
    while(q->front){
        tmp = q->front;
        free(tmp);
        q->front = q->front->next;
    }

    free(q);
    q = NULL;
    
    return q;
    
}
int lkqueue_nodenum(linkqueue *q)
{
    return q->count;
}
//lk_sqqueue_test.c
#include <stdio.h>
#include "lk_sqqueue.h"

int main()
{
    linkqueue *q = lkqueue_create();

    
    lkqueue_in(q, 10);    
    lkqueue_in(q, 20);
    lkqueue_in(q, 30);
    lkqueue_in(q, 40);
    lkqueue_in(q, 50); 
    
    while(!lkqueue_empty(q)){
        printf("%d\t",lkqueue_out(q));
        printf("num = %d\n",lkqueue_nodenum(q));
    }
    printf("P=%p\n",q->front);
    q = lkqueue_free(q);

/*     while(!lkqueue_empty(q)){
        printf("%d\t",lkqueue_out(q));
        printf("num = %d\n",lkqueue_nodenum(q));
    } */
    
    return 0;
}

 

posted @ 2023-04-20 22:30  踏浪而来的人  阅读(32)  评论(0编辑  收藏  举报