队列的一些基本操作

main.c:

#include "Quene.h"
#include <stdio.h>

 

int main()
{
    Quene *q = createQuene();
    int i = 0;
    for(i = 0 ;i < 10;i++)
    {
        push(q,i);
    }
    
    Pop(q);
    Pop(q);
    
    
    Gettop(q);
    return 0;
}

Quene.c:

#include <stdlib.h>
#include "Quene.h"
#include <assert.h>


Quene *createQuene()
{

    struct Quene *q = (struct Quene *)malloc(sizeof(Quene));
    assert(q);
    q->head = NULL;
    q->tail = NULL;
    
    return q;
    
    
    
}
int QueneEmpty(Quene *q)
{
    
    assert(q);
    
    return q->head == NULL;
    
    
}

int push(Quene *q,int data)
{
    assert(q);
    struct Quenenode *node = (struct Quenenode *)malloc(sizeof(Quenenode));
    assert(node);
    node->data = data;
    node->next = NULL;
    
    if(q->head == NULL)
    {
        q->head = node;
        q->tail = node;
    }
    else
    {
        q->tail->next = node;
        q->tail = node;
        
    }
    return ok;
    
}
int Pop(Quene *q)
{
    assert(q);
    if(QueneEmpty(q) != 1)
    {
        Quenenode *node = q->head;
        q->head = node->next;
        free(node);
        
        if (QueneEmpty(q))  // 空队的情况下 将队尾置空
            q->tail = NULL;
        
    }
    return ok;
    
}

int Gettop(Quene *q)
{
    assert(q);
    
    if (QueneEmpty(q)) 
    
    return error;
    printf ("data = %d\n", q->head->data);
}

int Destroy(Quene *q)
{
    assert(q);
    
    while(q != NULL)
    {
        Pop(q);
    }
    free(q);
    return 0;
    
}

Quene.h:

#ifndef _QUENE_H_
#define _QUENE_H_

#define  ok    0
#define  error 1


typedef struct Quenenode
{
    int data;
    struct Quenenode *next;
}Quenenode;

typedef struct Quene
{
    struct Quenenode *head;
    struct Quenenode *tail;
}Quene;

Quene *createQuene();

int QueneEmpty(Quene *q);

int push(Quene *,int );

int Pop(Quene *q);

int Gettop(Quene *q);

int Destroy(Quene *q);

#endif

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值