• 链表实现的简单循环队列


    #include <stdio.h>
    #include <stdlib.h>
    struct student
    {
    	char name[10];
    	int age;
    };
    typedef struct student Item;
    
    
    
     struct node
    {
    	Item item;
    	struct node *next;
    
    };
    typedef struct node Node;
    
    
    //typedef struct node
    //{
    //	Item item;
    //	Node *next;  //这里使用的Node在后面定义的Node之前使用是否允许
    //
    //}Node;
    typedef struct queue
    {
    	Node *head;//指向第一个元素
    	Node *tail;//指向最后一个元素
    	int size;//存入的元素的个数
    	int lenth;//可容纳的总共的元素的个数
    }Queue;
    
    void initialize_queue(Queue **que,int len );
    void insert_item(Queue *que,Item item);
    void print_queue(Queue *que);
    void delete_an_item(Queue *que);
    int main(void)
    {
    	struct queue *que=NULL;
    
    	int length=5;
    	initialize_queue(&que,length);
    	Item item[5]={{"11",1},{"22",2},{"33",3},{"44",4},{"55",5}};
    	Item im={"88",8};
    	Item im0={"66",6};
    	Item im1={"00",0};
    	for(int i=0;i<5;i++)
    		insert_item(que,item[i]);
    	print_queue(que);
    	delete_an_item(que);
    	print_queue(que);
    	delete_an_item(que);
    	print_queue(que);
    	insert_item(que,im);
    	print_queue(que);
    	insert_item(que,im0);
    	print_queue(que);
    	insert_item(que,im1);
    	print_queue(que);
    	
    
    	return 0;
    }
    //initialize the queue
    void initialize_queue(Queue **que,int len )
    {
    	//dynamically allocte  a  circulate link list , its lenth is len 
    	*que=(Queue *)malloc(sizeof(Queue));
    	Node *ptr,*ptr0;
    	while((ptr=(Node *)malloc(sizeof(Node)))==NULL)
    	{
    		printf("memory allocated failed");
    		exit(1);
    	}		
    	(*que)->head=(*que)->tail=ptr;
        (*que)->lenth =len;
    	(*que)->size =0;
    	for(int i=1;i<len;i++)
    	{
    		while((ptr0=(Node *)malloc(sizeof(Node)))==NULL)
    		{
    			printf("memory allocated failed");
    			exit(1);
    		}		
    		ptr->next=ptr0;
    		ptr=ptr->next;
    	}   
    	ptr->next =(*que)->head;	
    }
    
    //add an item into the queue
    void insert_item(Queue *que,Item item)
    {
    	//judge whether queue is full
        printf("insert an item:%s,%d
    ",item.name,item.age);
    	if(que->size==que->lenth)
    	{
    		printf("queue is full can't insert an item :%s,%d
    ",item.name,item.age);
    		exit(1);
    	}
    	 
    	que->tail->item=item;
    	(que->size) ++;
    	que->tail = que->tail ->next ;
    	
    	
    }
    void print_queue(Queue *que)
    {
    	printf("all of items in the queue:
    ");
    	Node *ptr;
    	ptr=que->head ;
    	if(que->size==0)
    	{
    		printf("queue is empty");
    	}
    	do
    	{
    		printf("%s,%d
    ",ptr->item .name ,ptr->item .age  );
    		ptr=ptr->next ;
    		
    
    	}while(que->tail !=ptr);
    	
    }
    
    
    void delete_an_item(Queue *que)
    {
    	printf("delete an item:%s,%d from queue
    ",que->head->item .name ,que->head ->item .age );
    	que->head=que->head ->next ;
    	(que->size)--; 
    
    }
    
  • 相关阅读:
    vue开发chrome扩展,数据通过storage对象获取
    Vue手动集成less预编译器
    Google Translate寻找之旅
    Javascript Range对象的学习
    Javascript Promises学习
    SublimeText 建立构建Node js系统
    We're sorry but demo3 doesn't work properly without JavaScript enabled. Please enable it to continue.
    npm安装包出现UNMET DEPENDENCY报错
    (转载)命令行说明中格式 尖括号 中括号的含义
    Linux重启网卡服务Failed to start LSB: Bring up/down networking.
  • 原文地址:https://www.cnblogs.com/jzhao/p/3214463.html
Copyright © 2020-2023  润新知