zoukankan      html  css  js  c++  java
  • 数据结构队列链表实现

    #include <stdio.h>
    #include <malloc.h>
    
    #define QLengh (sizeof(struct queue))
    
    typedef struct queue
    {
    	int element;
    	struct queue *next; 
    }
    *Queue;
    
    typedef struct 
    {
    	Queue front;
    	Queue rear;
    }
    LinkQueue;
    
    void initQueue(LinkQueue *Q)
    {
    	Q->front=Q->rear=malloc(QLengh);	
    	if(Q->rear==NULL)
    	{
    		printf("Out of space
    ");	
    	}
    	else
    	{
    		Q->rear->next=NULL;
    	}
    } 
    
    void insertQueue(LinkQueue *Q,int element)
    {
    	Queue NewQueue=malloc(QLengh);	
    	if(NewQueue==NULL)
    	{
    		printf("Out of space
    ");
    	}	
    	else
    	{
    		NewQueue->element=element;
    		NewQueue->next=NULL; 
    		Q->rear->next=NewQueue;
    		Q->rear=NewQueue;
    	}
    }
    
    int isEmpty(LinkQueue *Q)
    {
    	return (Q->front->next==NULL);
    }
    
    void outQueue(LinkQueue *Q)
    {
    	Queue Qtmp=NULL; 
    	if(isEmpty(Q))
    	{
    		printf("Empty Queue
    ");
    	}
    	else
    	{
    		Qtmp=Q->front;
    		Q->front=Q->front->next;		
    		free(Qtmp);
    	}
    }
    
    void printQueue(LinkQueue *Q)
    {
    	Queue P=Q->front->next;
    	while(P!=NULL)
    	{
    		printf("----------
    ");
    		printf("    %d    
    ",P->element);
    		printf("----------
    ");
    		P=P->next;
    	}
    } 
    
    int main(void)
    {
    	LinkQueue Q;
    	initQueue(&Q);
    	printf("入队演示
    "); 
    	insertQueue(&Q,1);
    	insertQueue(&Q,2);
    	insertQueue(&Q,3);
    	insertQueue(&Q,4);
    	insertQueue(&Q,5);
    	printQueue(&Q);
    	printf("出队演示
    "); 
    	outQueue(&Q);
    	outQueue(&Q);
    	printQueue(&Q);
    }
    

    运行结果:

  • 相关阅读:
    mktemp -t -d用法
    使用getopts处理输入参数
    linux中$1的意思
    linux中的set -e 与set -o pipefail
    在windows 7 和linux上安装xlwt和xlrd
    nginx map使用方法
    Linux crontab下关于使用date命令和sudo命令的坑
    东哥讲义
    ldapsearch使用
    date 命令之日期和秒数转换
  • 原文地址:https://www.cnblogs.com/achao123456/p/7123462.html
Copyright © 2011-2022 走看看