注册 登录
LUPA开源社区 返回首页

SevenxWasly的个人空间 http://www.lupaworld.com/?439842 [收藏] [复制] [分享] [RSS]

我的博客

基于C语言链表的队列功能演示

热度 1已有 982 次阅读2012-3-11 19:48 |系统分类:IT技术|

#include 

struct list_queue_node
{
    int data;
    struct list_queue_node *next;
};

typedef struct list_queue_node node;
typedef node *link;

link front = NULL;
link rear = NULL;

void add_queue(int value)
{
    link new_node;
    new_node = (link) malloc(sizeof(node));
    new_node->data = value;
    new_node->next = NULL;

    if (rear == NULL)
    {
        front = new_node;
        rear = new_node;
    }
    else
    {
        rear->next = new_node;
        rear = new_node;
    }
}

int delete_queue()
{
    link top;
    int temp;
    if (front != NULL)
    {
        top = front;
        front = front->next;
        temp = top->data;
        free(top);
        return temp;
    }
    else
        return -1;
}

void main(void)
{
    int select;
    int i;
    int temp;
    link point;

    printf("1-> Input a data---------------\n");
    printf("2-> Output a data--------------\n");
    printf("3-> Exit-----------------------\n");
    scanf("%d", &select);
    printf("\n");

    if (select != 3)
    {
        do
        {
            switch (select)
            {
                case 1:
                    printf("Please input the value==>");
                    scanf("%d", &temp);
                    add_queue(temp);
                    printf("The content of the queue:");
                    point = front;
                    while (point != NULL)
                    {
                        printf("[%d] ", point->data);
                        point = point->next;
                    }
                    break;
                case 2:
                    if ((temp = delete_queue()) == -1)
                        printf("The queue is empty");
                    else
                    {
                        printf("The output value is [%d]\n", temp);
                        printf("The content of queue:");
                        point = front;
                        while (point != NULL)
                        {
                            printf("[%d]", point->data);
                            point = point->next;
                        }
                    }
                    break;
            }
            printf("\n");
            printf("1-> Input a data---------------\n");
            printf("2-> Output a data--------------\n");
            printf("3-> Exit-----------------------\n");
            scanf("%d", &select);
            printf("\n");
        } while (select != 3);
    }
}
附:系统工程师QQ群59151639

发表评论 评论 (1 个评论)

回复 headindotcn 2013-8-8 13:41
  

facelist

您需要登录后才可以评论 登录 | 注册
验证问答 换一个 验证码 换一个

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部