链表和队列编程2

【题目】利用链表实现一先进先出的队列结构

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:链表实现先进先出的队列结构
Funcion List: 
*****************************************************/
<strong>/*******************方法1***************************/</strong>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define MAX 5

struct stack_data
{
    int top;
    int bottom;
    int quece[MAX];
};
typedef struct stack_data Quece;
typedef struct stack_data* link;

enum return_result{PUSH_NO, PUSH_OK, POP_NO, POP_OK, EXIT_FALL};

void is_malloc_ok(link *quece)
{
    if(*quece == NULL)
    {
        exit (EXIT_FALL);
    }
}

void create_quece( link *quece)
{
    *quece = (link)malloc(sizeof(Quece));
    is_malloc_ok(quece);
}

void init_quece(link *quece)
{
    (*quece)->top = -1;  
    (*quece)->bottom = -1;
}

int push_quece(link *quece, int num)
{
    int i;
    if((*quece)->bottom == -1)
    {
        if((*quece)->top >= MAX - 1)
        {
            printf("quece is full!\n");
            return PUSH_NO;
        }
    }
    else
    {
        if((*quece)->top - (*quece)->bottom >= MAX - 1)
        {
            printf("quece is full!\n");
            return PUSH_NO;
        }
    }
    if((*quece)->top == MAX - 1)
    {
        for(i = (*quece)-> bottom; i <= (*quece)->top; i++)
        {
            (*quece)->quece[i - (*quece)->bottom] = (*quece)->quece[i];
        }

        (*quece)->top = (*quece)->top - (*quece)->bottom + 1;
        (*quece)->bottom = 0;
        ((*quece)->top)++;
        ((*quece)->quece[(*quece)->top]) = num;
        return PUSH_OK;
    }
    else
    {
        ((*quece)->top)++;
        ((*quece)->quece[(*quece)->top]) = num;
        return PUSH_OK;
    }
}

int pop_quece(link *quece, int *num)
{
    if((*quece)->bottom >= (*quece)->top)
    {
        printf("quece is empty!\n");
        return POP_NO;
    }
    else
    {
        ((*quece)->bottom)++;
        *num = ((*quece)->quece[(*quece)->bottom]);
        return POP_OK;
    }
}

int main()
{
    link quece;;
    int i, num1, num;

    create_quece(&quece);
    init_quece(&quece);

    srand(time(NULL));

    while(1)
    {
        num = rand() % 100;
        if(num > 50)
        {
            if(push_quece(&quece, num) == PUSH_OK)  
            {
                printf("push done!  %d\n", num);
            }
        }
        else
        {
            if(pop_quece(&quece,&num1) == POP_OK)   
            {
                printf("pop done! %d\n",num1);
            }
        }
        sleep(2);
    }

    return 0;
}
/*******************************方法2*************************/
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Desciption:用双向链表实现队列的进出
Funcion List: 
*****************************************************/

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

struct node
{
    int num;
    struct node *prior;
    struct node *next;
};
typedef struct node Node;
typedef struct node * link;

void is_malloc_ok(link *head)
{
    if(*head == NULL)
    {
        printf("malloc is error!\n");
        exit(-1);
    }
}

void create_link(link *head)
{
    *head = (link)malloc(sizeof(Node));
    is_malloc_ok(head);
    (*head)->prior = *head;
    (*head)->next = *head;
}

void create_new_node(link *new_node, int i)
{
    *new_node = (link)malloc(sizeof(Node));
    is_malloc_ok(new_node);
    (*new_node)->num = i;
}

void push_quece(link *head, link *new_node)
{
    (*new_node)->next = (*head)->next;
    (*new_node)->prior = (*head);
    (*head)->next->prior = (*new_node);
    (*head)->next = (*new_node);

    printf("push number:%d\n", (*new_node)->num);

}

void pop_quece(link *head)
{
    link tmp;
    tmp = (*head)->prior;

    if(tmp == *head)
    {
        printf("quece is empty!\n");
        return;
    }

    (*head)->prior = tmp->prior;
    tmp->prior->next = *head;

    printf("pop number:%d\n", tmp->num);
    free(tmp);
    tmp = (*head)->prior;
}
int main()
{
    int i,num;
    link head = NULL;
    link new_node = NULL;

    create_link(&head);

    srand(time(NULL));

    while(1)
    {
        i = rand() % 100;
        if(i > 50)
        {
            create_new_node(&new_node,i);
            push_quece(&head, &new_node);
        }
        else
        {
            pop_quece(&head);
        }
        sleep(1);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值