链式队列定义、插入、删除

#include <stdio.h>
#include <stdlib.h>

#define ElemType int
typedef struct QNode
{
    ElemType data;//定义队列中的元素
    struct QNode *next;
}QNode;

typedef struct LinkQueue
{
    QNode *front;
    QNode *rear;
}LinkQueue;//定义一个队列,队列只有一个头指针和一个尾指针


//队列的初始化
void InitQueue(LinkQueue *q)
{
    q->front = (LinkQueue*)malloc(sizeof(QNode));
    if(!q->front)
    {
        exit(0);
    }
    q->rear = q->front;
    q->front->next = NULL;
}

//队列的插入
void InsertQueue(LinkQueue *p,ElemType *e)
//队列为p,要插入的元素为e,队列的rear指针指向队列的最后一个元素。队列的front指针指向头结点,头结点无元素
{
    QNode *ptr;
    ptr = (LinkQueue*)malloc(sizeof(QNode));
    ptr->data = *e;
    ptr->next = NULL;
    p->rear->next = ptr;
//    p->rear = p->rear->next;
    p->rear = ptr;
}

//队列的删除
void DeleteQueue(LinkQueue *p,ElemType *e)
{
    QNode *s;
    if(p->front == p->rear)
    {
        return;
    }
    s = p->front->next;
    *e = s->data;
    p->front->next = s->next;
 //   if(s == p->rear)
    if(p->front->next == NULL)//用这条也可以是等价的
    {
        p->front = p->rear;
    }
    free(s);
}

//销毁一个队列
void DestoryQueue(LinkQueue *p)
{
    while(p->front)
    {
        p->rear = p->front->next;//p->front->next == NULL
        free(p->front);//释放p的头结点
        p->front = p->rear;//把p的front和rear都置为NULL
    }

}
int main()
{
    int i;
    LinkQueue p;
    ElemType e;
    InitQueue(&p);
    for(i = 1;i <= 10;i++)
    {
        InsertQueue(&p,&i);
    }

    for(i = 1;i <= 10;i++)
    {
        DeleteQueue(&p,&e);
        printf("%2d",e);
    }
    return 0;
}

 输入一个字符串以字符'#'结束,在屏幕上打印出来

#include <stdio.h>
#include <stdlib.h>

#define M 100
#define ElemType char
typedef struct QNode
{
    ElemType data;//定义队列中的元素
    struct QNode *next;
}QNode;

typedef struct LinkQueue
{
    QNode *front;
    QNode *rear;
}LinkQueue;//定义一个队列,队列只有一个头指针和一个尾指针


//队列的初始化
void InitQueue(LinkQueue *q)
{
    q->front = (LinkQueue*)malloc(sizeof(QNode));
    if(!q->front)
    {
        exit(0);
    }
    q->rear = q->front;
    q->front->next = NULL;
}

//队列的插入
void InsertQueue(LinkQueue *p,ElemType *e)
//队列为p,要插入的元素为e,队列的rear指针指向队列的最后一个元素。队列的front指针指向头结点,头结点无元素
{
    QNode *ptr;
    ptr = (LinkQueue*)malloc(sizeof(QNode));
    ptr->data = *e;
    ptr->next = NULL;
    p->rear->next = ptr;
//    p->rear = p->rear->next;
    p->rear = ptr;
}

//队列的删除
void DeleteQueue(LinkQueue *p,ElemType *e)
{
    QNode *s;
    if(p->front == p->rear)
    {
        return;
    }
    s = p->front->next;
    *e = s->data;
    p->front->next = s->next;
 //   if(s == p->rear)
    if(p->front->next == NULL)//用这条也可以是等价的
    {
        p->front = p->rear;
    }
    free(s);
}

//销毁一个队列
void DestoryQueue(LinkQueue *p)
{
    while(p->front)
    {
        p->rear = p->front->next;//p->front->next == NULL
        free(p->front);//释放p的头结点
        p->front = p->rear;//把p的front和rear都置为NULL
    }

}
int main()
{
    int i,len = 0;
    LinkQueue p;
    ElemType e;
    char c;
    InitQueue(&p);
    printf("输入字符串:\n");
    scanf("%c",&c);
    while(c != '#')
    {
        InsertQueue(&p,&c);
        len++;
        scanf("%c",&c);
    }
    printf("字符串为:\n");
    for(i = 1;i <= len;i++)
    {
        DeleteQueue(&p,&e);
        printf("%c",e);
    }
    return 0;
}

  

 

posted on 2014-12-15 20:14 BeatificDevin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/devinblog/p/4165780.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值