数据结构队列实验

#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
#include"linkqueue.h"
int main()
{
    int n;
    int i;
	int x;
    seqQueue *Q;
    linkQueue *M;
   do
    {
		printf("请选择你想要实现的功能\n");
        printf("------1.使用顺序队列---\n");
		printf("------2.使用链式队列---\n");
		scanf("%d",&n);
        switch(n)
		{
        case 1:
        system("CLS");
        farhanshu(&Q);
        system("Pause");
		break;
        case 2:
        system("CLS");
        queueFun_2(&M);
        system("Pause");
        break;
        default:
			printf("Selection is error!\n");
			system("Pause");
        }
	} while (1);
  return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define MAXLEN  100
typedef struct 
{
   int data[MAXLEN];
   int front;
   int rear;
}seqQueue;
//初始化队列

void initQueue(seqQueue *Q)
{
    Q->front=0;
    Q->rear=0;//使得两个指针为空
}

//判断是否队列为空
int ifvoid(seqQueue *Q)
{
    if(Q->front==Q->rear)
    return 1;
    else
    return 0;
}
//判断一个队列是否满了
int iffull(seqQueue *Q)
{
   int x;
   x=(Q->rear+1)%MAXLEN;//之所以要加1 是因为判断一个队列满的条件分为两种 一种是front在最前面 rear在最后面  这样子算满
   if(x==Q->front)//  还有一种是rear在前面 front在后面 这种情况是rear和front之间要差一个元素
   return 1;
   else
   return 0;
}
//求一个队列的长度

int queuelength(seqQueue *Q)
{
    return (Q->rear-Q->front+MAXLEN)%MAXLEN;//求队列长度的公式  解决假溢出问题  
    //一种是front在前面 大小为rear-front 另一种为rear在前面  长度为rear maxlen-front 所以两种情况下 (rear-front+maxlen)%maxlen
}
//入队

int inqueue(seqQueue *Q,int x)
{
    if(iffull(Q)==0)
    return 0;
   Q->data[Q->rear]=x;
   Q->rear=(Q->rear+1)%MAXLEN;//解决假溢出问题
   return 1;
}
//出队
int outqueue(seqQueue *Q)
{
    int *e;
    if(ifvoid(Q)==1)
    return 1;
    *e=Q->data[Q->front];
    Q->front=(Q->front-1)%MAXLEN;//解决假溢出问题
    return 0;
}


void ShowQueue(seqQueue*Q)
{
    int i;
	//遍历队头到队尾中的每个元素,并将其打印输出
    printf("当前队列输出为");
	for( i=Q->front; i<Q->rear; i++)
	{
		printf("%d ",Q->data[i]);
	}
	printf("\n");
}


//取队头元素
int GetHead(seqQueue *Q,int *e)
{
	if(Q->front==Q->rear)
	{
		return 1;
	}
	*e=Q->data[Q->front];
	return 0;
}


//第八个算法
void farhanshu(seqQueue *Q)
{
    initQueue(Q);
    int x;
    printf("请输入你想要输入的元素,按-999退出");
    while(x!=-999)
    {
        scanf("%d",&x);
        if(x%2!=0)
        {
        inqueue(Q,x);
        ShowQueue(Q);
        }
        else if(x%2==0)
        {
        outqueue(Q);
        ShowQueue(Q);
        }
        else
        {
            ShowQueue(Q);
            break;
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE sizeof(node)

typedef struct LNode
{
    int data;
    struct LNode *next;
}node;

typedef struct
{
    node *front;
    node *rear;
}linkQueue;

/******链式队列基本操作******/

//初始化队列
void initialQueue_2(linkQueue *Q)
{
    Q->front = (node *)malloc(SIZE);
    Q->rear = Q->front;
    Q->front->next = NULL;
}

//判断队列是否为空
bool queueEmpty_2(linkQueue *Q)
{
    return(Q->front == Q->rear);
}
//输出队列
void print(linkQueue* q)
{
   node * p = q->front->next;
 
  if (q->front==q->rear) {
    printf("空队\n");
  }
  printf("当前队列中的元素为");
  while (p) {
    printf("%d ", p->data);
    p = p->next;
  }
  printf("\n\n");
}

//取队头元素
void queueFront_2(linkQueue *Q, int *x)
{
    if(queueEmpty_2(Q))
        printf("空队列,无法取队头元素\n");
    else
        *x = Q->front->next->data;
}

//入队
void enQueue_2(linkQueue *Q, int x)
{
    node *P;
    P = (node *)malloc(SIZE);
    P->data = x;
    P->next = NULL;
    Q->rear->next = P;
    Q->rear = P;
}

//出队
void outQueue_2(linkQueue *Q)
{
    node *u;
    if(queueEmpty_2(Q))
        printf("当前队空 无法执行出队操作\n");
    else
    {
        u = Q->front->next;
        Q->front->next = u->next;
        free(u);
        if(Q->front->next == NULL)
            Q->rear = Q->front;
    }
}

//求队列中元素个数
int queueNumber_2(linkQueue *Q)
{
    int i;
    node *p;
    i = -1;
    p = Q->front;
    while((!queueEmpty_2(Q)) && Q->rear->next != p)
    {
        p = p->next;
        i++;
    }
    return i;
}

/*****算法实现*****/
void queueFun_2(linkQueue *Q)
{
     int m, x;
    initialQueue_2(Q);

    printf("请输入元素值(输入0结束)\n");
    do
    {
        scanf("%d", &m);

        if(m == 0)
            break;
        else if(m % 2 != 0)
        {
            enQueue_2(Q, m);
            print(Q);
        }
        else
        {
            outQueue_2(Q);
            print(Q);
        }
    }while(1);

    if(Q->front==Q->rear)
        printf("队列为空\n");
    else
    {
        printf("队列中的元素为\n");
        while(!queueEmpty_2(Q))
        {
            queueFront_2(Q, &x);
            printf("%d\t", x);
            outQueue_2(Q);
        }
        printf("\n");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值