zoukankan      html  css  js  c++  java
  • 链式队列实现

    三个文件:main.c student.c student.h

    student.h文件如下:

    #ifndef STUDENT_H_
    #define STUDENT_H_
    
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    #include<stdlib.h>
    
    #define CMD_ADD_NODE  '1'
    #define CMD_DEL_NODE  '2'
    #define CMD_SHOW_NODE '3'
    #define CMD_QUIT_NODE  '4' 
    
    #define MAX 10
    
    typedef struct
    {
        char name[20];
        int grade;
    }Item;
    typedef struct node
    {
        Item item;
        struct node *next;
    }Node;
    typedef struct
    {
        Node *front; /* 队头指针 */
        Node *rear;  /* 队尾指针 */
        int items;
    }Queue;
    
    
    static inline void print(const Item item)
    {
        printf("%s, %d
    ", item.name, item.grade);
        return ;
    }
    
    int QueueAdd( Queue *queue,Item item);
    int QueueDel(Queue *queue, Item *item);
    int QueueShow(const Queue *queue);
    
    #endif

    main.c文件如下:

    #include"student.h"
    
    
    
    int main(int argc, char **argv)
    {
        char cmd = 0;
        Queue queue={NULL,NULL,0};
        Item item;
    
        //Initialize(&queue);
        while(1)
        {
            printf("The Menun Is:
    ");
            printf("1.add
    ");
            printf("2.delete
    ");
            printf("3.show
    ");
            printf("4.quit
    ");
            printf("Enter your choice(1~4):");
    
            cmd = getchar();
            while( (getchar()) != '
    ' )
                continue;//跳过输入行的多余部分
            while( cmd != CMD_ADD_NODE && cmd != CMD_DEL_NODE
                               && cmd != CMD_SHOW_NODE && cmd != CMD_QUIT_NODE)
            {
                printf("Please input 1~4:");
                cmd = getchar();
                while( (getchar()) != '
    ' )
                        continue;//跳过输入行的多余部分
            }
    
            switch(cmd)
            {
                case CMD_ADD_NODE  :
                    printf("Enter name and grade:");
                    scanf("%s%d",item.name,&(item.grade));
                    while( (getchar()) != '
    ' )
                        continue;//跳过输入行的多余部分
                    QueueAdd(&queue, item);
                    break;
    
                case CMD_DEL_NODE  :
                    QueueDel(&queue, &item);
                    printf("Delete:");
                    print(item);
                    break;
    
                case CMD_SHOW_NODE  :
                    QueueShow(&queue);
                    break;
    
                case CMD_QUIT_NODE  :
                    exit(0);
                    break;
    
                default:
                    break;
            }
            printf("
    ");
        }
        return 0;
    }

    student.c文件如下:

    #include "student.h"
    
    static int  QueueIsEmpty(const  Queue *queue)
    {
            return (queue->items == 0);
    }
    
    static int QueueIsFull(const Queue *queue)
    {
            return (queue->items == MAX);
    }
    
    /* 插到队尾 */
    int QueueAdd(Queue *queue, Item item)
    {
        Node *node;
    
        if( QueueIsFull(queue) )
        {
                fprintf(stderr,"The queue is full!!!
    ");
                return -1;
        }
    
        node = (Node *)malloc(sizeof(Node));
        if(node == NULL)
        {
            fprintf(stderr, "Cannot creat a node!!!
    ");
            return -1;
        }
        node->item = item;
        node->next = NULL;
    
        if( QueueIsEmpty(queue) )
        {
                queue->front = node;
        }
        else
        {
                queue->rear->next = node;
        }
        queue->rear = node;
        queue->items ++;
    
        return 0;
    }
    
    /* 在队头删除 */
    int QueueDel( Queue *queue,  Item *item )
    {
            Node *node = queue->front;
    if( QueueIsEmpty(queue) )
            {
                    fprintf(stderr, "The queue is empty!!!
    ");
                    return -1;
            }
    
            *item = node->item;    
            queue->front = node->next;
            queue->items --;
            
            free(node);
    
            return 0;
    }
    
    int  QueueShow( const Queue *queue )
    {
            Node *node = queue->front;
    
            if( QueueIsEmpty(queue) )
            {
                    fprintf(stderr, "The queue is empty!!!
    ");
                    return -1;
            }
    
            printf("All of the student:
    ");
            while(node)
            {
                    print(node->item);
                    node = node->next;
            }
            printf("
    ");
    
            return 0;
    }
  • 相关阅读:
    <EditText /> This text field does not specify an inputType or a hint
    phpmailer【PHP邮件】的用法
    IOS-Quartz2D(Paths元素)
    IOS-网络(网页开发-UIWebView,HTML,CSS,JavaScript,OC和JS代码互调)
    IOS学习路线图
    Android学习必备--java工具15个
    IOS-网络(ASI使用)
    IOS-网络(ASIHTTPRequest的使用简介)
    IOS-网络(AFNetworking)
    IOS-网络(文件压缩和解压缩)
  • 原文地址:https://www.cnblogs.com/zhoutian220/p/4004013.html
Copyright © 2011-2022 走看看