原创 队列的c语言实现

2008-11-5 16:13 5082 9 10 分类: 软件与OS

队列是一种与栈相反的数据结构,它是先进先出(first in, first out),同样它也是一种运算受限的结构。


队列的抽象数据类型:


ADT QUEUE =


{


InitQueue(&Q)


//create a queue


DestroyQueue(&q)


//destroy a queue


clearQueue(&q)


//clear the queue


............


}



队列的c语言实现


1:


顺序队列:


/* ******************************************
* FileName:squeue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description: The program creat a queue and lots
* of operating
* ******************************************/


/* header file */
#include <stdio.h>
#define MAX 20


/* creat a struct */
typedef struct squeue
{
    int date[MAX];      /*declartion a array */
    int front,rear;     /* declarion a front and a rear pointer */


}sq;


/* assign queue each elem */
void Assign_squeue(sq *p, int count)
{
    int i; /* declaration a cycle variable */


    if (p->rear > MAX - 1)
        {
            printf("Overflow!\n");
            return;
        }


    printf("Please enter each elem:\n");
    for (i = 0; i < count; i++)
        {
            scanf("%d", &p->date[++p->rear]);
        }
}



/* insert a new elem in the queue */
void Insert_squeue(sq *p, int num)
{
    if (p->rear > MAX - 1)
        {
            printf("Over flow!\n");
            return;
        }


    else
        {
            p->date[++p->rear] = num;
        }


}



/* Delete a elem */
void Delete_squeue(sq *p)
{
    int x;
    if (p->front == p->rear)
        {
            printf("Empty queue!\n");
            return;
        }
    else
        {
            x = p->date[++p->front];
            printf("P->front = %d\n",p->front);
            printf("The deleted number is:%d\n",x);
        }
}



/* main program */
int main()
{
    sq *p = NULL;      /* declaration a pointer */
    int num;    /* decalration a number of queue */


    p->front = -1;
    p->rear = p->front;


    printf("Please enter the length of queue:\n");
    scanf("%d", &num);


    Assign_squeue(p, num); /* assign array date */


    /* insert a number in the queue */
    printf("Please enter which number you want to insert:\n");
    scanf("%d", &num);
    Insert_squeue(p, num);


    /* delete a number */
    Delete_squeue(p);


    getch();
    return 0;


}



2:


循环队列:


/* ******************************************
* FileName:cycle_queue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description:The program is a cycle queue
*********************************************/


/* header file */
#include <stdio.h>
#define MAX 20


/* creat a struct */
typedef struct cycle_queue
{
    int date[MAX];
    int front,rear;
}cq;


/* assign queue */
void Assign_CycleQueue(cq *p, int n)
{
    int i;
    int x;


    if (n < 1 || n > MAX - 1)
        {
            printf("Overflow!\n");
            return;
        }


   printf("Please enter each elem:\n");
   for (i = 0; i < n; i++)
        {
            scanf("%d", &x);
            p->date[p->rear++] = x;
        }
}


/* insert a number */
void Insert_CycleQueue(cq *p, int num)
{
    if ((p->rear + 1) % MAX == p->front)
        {
            printf("full queue!\n");
            return;
        }  
    else
        {
            p->date[++p->rear] = num;
        }
}



/* delete number */
void Delete_CycleQueue(cq *p)
{
    int x;
    if ((p->front + 1) % MAX == p->rear)
        {
            printf("empty queue!\n");
            return;
        }



   x = p->date[p->front];
   printf("p->front = %d\n", p->front);
   printf("The deleted number is:%d\n",x);
}


/* main program */
int main()
{

    int num;
    cq *p = NULL;
    p->front = 0;
    p->rear = p->front;


    printf("Please enter the length of queue:\n");
    scanf("%d", &num);


    Assign_CycleQueue(p,num);


    printf("Please enter the inserting number:\n");
    scanf("%d",&num);


    Insert_CycleQueue(p, num);
    Delete_CycleQueue(p);


    getch();
    return 0;
}


3.


链队:


/* ******************************************
* FileName:cycle_queue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description:The program is a cycle queue
*********************************************/


/* header file */
#include <stdio.h>
#define MAX 20


/* creat a struct */
typedef struct cycle_queue
{
    int date[MAX];
    int front,rear;
}cq;


/* assign queue */
void Assign_CycleQueue(cq *p, int n)
{
    int i;
    int x;


    if (n < 1 || n > MAX - 1)
        {
            printf("Overflow!\n");
            return;
        }


   printf("Please enter each elem:\n");
   for (i = 0; i < n; i++)
        {
            scanf("%d", &x);
            p->date[p->rear++] = x;
        }
}


/* insert a number */
void Insert_CycleQueue(cq *p, int num)
{
    if ((p->rear + 1) % MAX == p->front)
        {
            printf("full queue!\n");
            return;
        }  
    else
        {
            p->date[++p->rear] = num;
        }
}



/* delete number */
void Delete_CycleQueue(cq *p)
{
    int x;
    if ((p->front + 1) % MAX == p->rear)
        {
            printf("empty queue!\n");
            return;
        }



   x = p->date[p->front];
   printf("p->front = %d\n", p->front);
   printf("The deleted number is:%d\n",x);
}


/* main program */
int main()
{

    int num;
    cq *p = NULL;
    p->front = 0;
    p->rear = p->front;


    printf("Please enter the length of queue:\n");
    scanf("%d", &num);


    Assign_CycleQueue(p,num);


    printf("Please enter the inserting number:\n");
    scanf("%d",&num);


    Insert_CycleQueue(p, num);
    Delete_CycleQueue(p);


    getch();
    return 0;
}

PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

用户810706 2010-4-14 22:56

链队怎么和顺序队列一样啊
相关推荐阅读
wqd520 2008-11-19 21:10
嵌入式Linux开发需要的参考资料
引导:如需获得对 vmlinux 和 zimage 之间区别的极好解释,请在 Alessandro Rubini 编写的“Kernel Configuration: dealing with the ...
wqd520 2008-11-05 16:11
修改了一个抓包的程序(c语言队列)
修改了一个抓包的程序(c语言队列)05 年底写了个抓包的小工具,做某些方面的测试用。但是在大流量的时候,存在一些问题,经常会漏掉一些包没有分析,即使接到了预定义好的包,也没有办法作出某些响应。周末没事...
wqd520 2008-11-05 16:08
数据结构C语言实现系列——队列
#include <stdio.h>#include <stdlib.h>typedef int elemType;/*****************************...
wqd520 2008-10-22 11:45
嵌入式软件工程师的技能修炼
点穴: 深入了解至少一种处理器/控制器的体系结构,熟悉其汇编指令。      掌法: 精通C语言,掌握加载技术和编译链接知识。 兵刃: 了解常用存储、通讯和人机接口设备,熟练编写设备驱动程序。 阵法:...
wqd520 2008-10-15 11:42
嵌入式系统硬件抽象层的建立及软件的可移植性设计
摘要:在阐述嵌入式系统软件设计方法的基础上,介绍嵌入式系统底层软件可移值性设计和硬件抽象层的建立;举例说明利用此思想的嵌入式软件的设计及测试过程。     关键词:设备驱动程序 嵌入式系统 软件设计 ...
我要评论
1
9
关闭 站长推荐上一条 /3 下一条