C语言实现队列

真的很衰!。。。

链式:

#include<bits/stdc++.h>


typedef struct node{
	int elem;
	struct node *next;
}DataNode;

typedef struct{
	int count;
	DataNode *front;
	DataNode *rear;
}LinkNode,*HeadNode;


int Init(HeadNode *L)
{
	HeadNode p=(HeadNode)malloc(sizeof(LinkNode));
	DataNode *q=(DataNode *)malloc(sizeof(DataNode));
	q->next = NULL;
	p->count =0;
	p->front =q;
	p->rear =q;
	(*L) = p;
	return 0; 
}

int EnQueue(HeadNode *L,int elem)
{
	DataNode *p=(DataNode *)malloc(sizeof(DataNode));
	p->elem = elem;
	p->next = NULL; 
	(*L)->rear->next = p;
	(*L)->rear = p;
	
	
	(*L)->count += 1;
	return 0;
}

int DeQueue(HeadNode *L,int *elem)
{
	DataNode *p=(*L)->front->next;
	*elem = p->elem ;
	(*L)->front->next = p->next;
	
	if(p == (*L)->rear)
	{
		(*L)->rear = (*L)->front;
	}
	free(p);
	(*L)->count -= 1;
	 return 0;
}

int Length(HeadNode L)
{
	return L->count ;
	
 } 
int  Empty(HeadNode L)
{
	if(L->count == 0)
	{
		return 1;
	}
	return 0;
}

int GetHead(HeadNode L,int *elem)
{
	(*elem) = L->front->elem;
	return 0;
}

int Clear(HeadNode *L)
{
	while((*L)->count !=0)
	{
		DataNode *p=(*L)->front->next;
		(*L)->front->next = p->next ;
		if(p=(*L)->rear)
		{
			(*L)->rear = (*L)->front;
		}
		free(p);
		(*L)->count -= 1;
	}
	
	return 0;
}
int Destory(HeadNode *L)
{
	while((*L)->front != NULL)
	{
		(*L)->rear=(*L)->front->next;
		free((*L)->front);
		(*L)->front = (*L)->rear;
	}
	free(*L);
	return 0;
}
int Traverse(HeadNode L)
{
	DataNode *p=L->front->next;
	
	for(int i=1;i<=L->count;i++)
	{
		printf("%d\t",p->elem);	
		p=p->next;
	}
	printf("\n");
	return 0;
}

int main()
{
	HeadNode L;
	Init(&L);
	int a,n;
	int elem;
	scanf("%d",&n);
	
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a);
		EnQueue(&L,a);
	} 
	DeQueue(&L,&elem);
	printf("%d\n",elem);
	Traverse(L);
	printf("%d\n",L->count );
	Traverse(L);
	printf("%d\n",L->count );
	scanf("%d",&n);
	
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a);
		EnQueue(&L,a);
	} 
	DeQueue(&L,&elem);
	printf("%d\n",elem);
	Traverse(L);
	return 0;
}

顺序:

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 100
#define CRE_SIZE 20
typedef struct{
	int *elem;
	int front;
	int rear;
}SqQueue;

int Init(SqQueue *Q)
{
	Q->elem = (int *)malloc(MAX_SIZE*sizeof(int));
	Q->front = 0 ;
	Q->rear = 0 ;
	//Q->front = Q->rear = 0;

	return 0; 
}

int EnQueue(SqQueue *Q,int elem)
{
	if((Q->rear + 1)%MAX_SIZE == Q->front )
	{
		return -1;
	}
	Q->elem[Q->rear]=elem;
	Q->rear = (Q->rear + 1)%MAX_SIZE;
	
	return 0;
}

int DeQueue(SqQueue *Q,int *elem)
{
	*elem = Q->elem[Q->front];
	Q->front = (Q->front+1)%MAX_SIZE;
	return 0;
}

int Detory(SqQueue *Q)
{
	free(Q->elem);
	Q->elem = NULL;
	Q->front =0;
	Q->rear = 0;
	
	return 0; 
}

int Clear(SqQueue *Q)
{
	Q->front = 0;
	Q->rear = 0;
	return 0;
}

int GetHead(SqQueue Q)
{
	return Q.elem[Q.front];
}

int QueueLength(SqQueue Q)
{
	
	return (Q.rear - Q.front + MAX_SIZE)%MAX_SIZE;
}

int QueueEmpty(SqQueue Q)
{
	if((Q.rear - Q.rear + MAX_SIZE)%MAX_SIZE == 0)
	{
		return 1;
	} 
	return 0;
 } 
 
 int Traverse(SqQueue Q)
 {
 	int begin=Q.front;
 	while(begin != Q.rear )
 	{
 		printf("%d\t",Q.elem[begin]);
 		begin+=1;
	 }
	 printf("\n");
	 return 0;
 }
 int main()
 {
	SqQueue Q;
	Init(&Q);
	int n,a;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a);
		EnQueue(&Q,a);
	}
 	Traverse(Q);
 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值