C语言队列的插入与删除

C语言队列的插入与删除,第1张

#include <stdio.h>

#include <stdlib.h>

#define MAXQSIZE 100     //最大队列长度

#define OK 1

#define ERROR 0

#define OVERFLOW -2

typedef struct

{

int *base

int front

int rear     //尾指针,若队列不空,指向队列尾元素的下一个位置

}SqQueue

void InitQueue(SqQueue *Q)

{

Q->front = Q->rear = 0

if (Q->base == NULL) {

Q->base = (int*)malloc(sizeof(int)* MAXQSIZE)

}

}

void DesQueue(SqQueue *Q) {

free(Q->base)

Q->base = NULL

Q->front = Q->rear = 0

}

int QueueLength(SqQueue *Q)

{

if (Q->base == NULL) return ERROR

return (Q->rear - Q->front + MAXQSIZE) % MAXQSIZE

}

void display(SqQueue *Q)

{

int i

if (Q->base == NULL) {

printf("\n ERROR ")

return

}

for (i = Q->front i != Q->rear i++) {

i = i % MAXQSIZE

printf("%3d", Q->base[i])

}

printf("\n")

}

int InQueue(SqQueue *Q, int e)

{

if (Q->base == NULL) return ERROR

if ((Q->rear + 1) % MAXQSIZE == Q->front)

return OVERFLOW

Q->base[Q->rear] = e

Q->rear = (Q->rear + 1) % MAXQSIZE

return OK

}

int DeQueue(SqQueue *Q, int m)

{

int i = 0

if (Q->base == NULL) return ERROR

if (Q->front == Q->rear)

return ERROR

while (i != m && Q->front != Q->rear)

{

printf("\n%dDeleted\n", Q->base[Q->front])

Q->front = (Q->front + 1) % MAXQSIZE

i++

}

if (i != m) {

printf("\n ERROR ")

return ERROR

}

return OK

}

void main()

{

int m, n, d, i

SqQueue Q = { 0, 0, 0 }

InitQueue(&Q)

printf("请输入要插入的元素个数:")

scanf("%d", &m)

printf("要插入的元素:")

for (i = 1 i <= m i++)

{

scanf("%d", &n)

InQueue(&Q, n)

}

printf("插入元素后,队列中的元素为:")

display(&Q)

printf("队列长度为:")

printf("%d\n", QueueLength(&Q))

printf("输入要删除的元素个数:")

scanf("%d", &d)

DeQueue(&Q, d)

printf("\n删除元素后,队列中元素为:")

display(&Q)

printf("\n")

DesQueue(&Q)

}

#include<stdio.h>

#include<malloc.h>

struct link_cqueue

{

int data

struct link_cqueue *next

}

//初始化循环链队列

struct link_cqueue *init_link_cqueue()

{

struct link_cqueue *rear

rear=NULL /*队尾指针设置为空*/

return rear

}

//(1)插入(即入队)算法:

struct link_cqueue *EnCQueue(struct link_cqueue *rear, int x)

{ //设循环链队列的队尾指针为rear,x为待插入的元素

struct link_cqueue *p

p=(struct link_cqueue *)malloc(sizeof(struct link_cqueue))

p->data=x

if(rear==NULL) //如为空队,建立循环链队列的第一个结点

{

rear=p

rear->next=p //链接成循环链表

}

else //否则在队尾插入p结点

{

p->next=rear->next

rear->next=p

rear=p

}

return rear

}

//(2)删除(即出队)算法:

struct link_cqueue *DeCQueue(struct link_cqueue *rear)

{ //设循环链队列的队尾指针为rear

if (rear==NULL) //空队

printf("队列为空无法删除!\n")

else if(rear->next==rear) //队中只有一个结点

rear=NULL

else

rear->next=rear->next->next //rear->next指向的结点为循环链队列的队头结点

return rear

}

//循环队列的输出

void print_link_cqueue(struct link_cqueue *rear)

{

struct link_cqueue *p

if(!rear)

printf("队列为空!\n")

else

{

printf("%5d",rear->next->data)

p=rear->next

while(p!=rear)

{

printf("%5d",p->next->data)

p=p->next

}

}

printf("\n")

}

main()

{

struct link_cqueue *rear

int x

int c

rear=init_link_cqueue()

do

{

printf("请选择入队或出队 *** 作:1:入队;2:出队;3:输出!\n")

scanf("%d",&c)

if(c==1)

{

printf("请输入要入队的元素:")

scanf("%d",&x)

rear=EnCQueue(rear,x)

}

else if(c==2)

{

rear=DeCQueue(rear)

}

else if(c==3)

print_link_cqueue(rear)

else

printf("选择错误,请重新选择")

}while(1)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11629916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存