c语言 队列删除头结点,[转]队列插入、删除节点有关操作C语言代码

#include

#include

typedef int elemType;

struct sNode{

elemType data;

struct sNode *next;

};

struct queueLK{

struct sNode *front;

struct sNode *rear;

};

void initQueue(struct queueLK *hq)

{

hq->front = hq->rear = NULL;

return;

}

void enQueue(struct queueLK *hq, elemType x)

{

struct sNode *newP;

newP = malloc(sizeof(struct sNode));

if(newP == NULL){

printf("内存空间分配失败! ");

exit(1);

}

newP->data = x;

newP->next = NULL;

if(hq->rear == NULL){

hq->front = hq->rear = newP;

}else{

hq->rear =

hq->rear->next = newP;

}

return;

}

elemType outQueue(struct queueLK *hq)

{

struct sNode *p;

elemType temp;

if(hq->front == NULL){

printf("队列为空,无法删除! ");

exit(1);

}

temp = hq->front->data;

p = hq->front;

hq->front = p->next;

if(hq->front == NULL){

hq->rear = NULL;

}

free(p);

return temp;

}

elemType peekQueue(struct queueLK *hq)

{

if(hq->front == NULL){

printf("队列为空,无法删除! ");

exit(1);

}

return hq->front->data;

}

int emptyQueue(struct queueLK *hq)

{

if(hq->front == NULL){

return 1;

}else{

return 0;

}

}

void clearQueue(struct queueLK *hq)

{

struct sNode *p = hq->front;

while(p != NULL){

hq->front =

hq->front->next;

free(p);

p = hq->front;

}

hq->rear = NULL;

return;

}

int main(int argc, char* argv[])

{

struct queueLK q;

int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};

int i;

initQueue(&q);

for(i = 0; i < 8; i++){

enQueue(&q, a[i]);

}

printf("%d ", outQueue(&q)); printf("%d ",

outQueue(&q));

enQueue(&q, 68);

printf("%d ", peekQueue(&q)); printf("%d ",

outQueue(&q));

while(!emptyQueue(&q)){

printf("%d ", outQueue(&q));

}

printf(" ");

clearQueue(&q);

system("pause");

}

typedef int elemType;

struct queue{

elemType *queue;

int front, rear, len;

int maxSize;

};

void againMalloc(struct queue *q)

{

elemType *p;

p = realloc(q->queue, 2 *

q->maxSize * sizeof(elemType));

if(!p){

printf("空间分配失败! ");

exit(1);

}

q->queue = p;

if(q->rear != q->maxSize -1){

int i;

for(i = 0; i <= q->rear;

i++){

q->queue[i+q->maxSize] =

q->queue[i];

}

q->rear += q->maxSize;

}

q->maxSize = 2 * q->maxSize;

return;

}

void initQueue(struct queue *q, int ms)

{

if(ms <= 0){

printf("ms值非法! ");

exit(1);

}

q->maxSize = ms;

q->queue = malloc(ms * sizeof(elemType));

if(!q->queue){

printf("内存空间分配失败! ");

exit(1);

}

q->front = q->rear = 0;

return;

}

void enQueue(struct queue *q, elemType x)

{

if((q->rear + 1) % q->maxSize ==

q->front){

againMalloc(q);

}

q->rear = (q->rear + 1) %

q->maxSize;

q->queue[q->rear] = x;

return;

}

elemType outQueue(struct queue *q)

{

if(q->front == q->rear){

printf("队列为空,无法删除! ");

exit(1);

}

q->front = (q->front +1) %

q->maxSize;

return q->queue[q->front];

}

elemType peekQueue(struct queue *q)

{

if(q->front == q->rear){

printf("队列为空,无法删除! ");

exit(1);

}

return q->queue[(q->front +1) %

q->maxSize];

}

int emptyQueue(struct queue *q)

{

if(q->front == q->rear){

return 1;

}else{

return 0;

}

}

void clearQueue(struct queue *q)

{

if(q->queue != NULL){

free(q->queue);

q->queue = NULL;

q->front = q->rear = 0;

q->maxSize = 0;

}

return;

}

int main(int argc, char* argv[])

{

struct queue q;

int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};

int i;

initQueue(&q, 5);

for(i = 0; i < 8; i++){

enQueue(&q, a[i]);

}

printf("%d ", outQueue(&q)); printf("%d ",

outQueue(&q));

enQueue(&q, 68);

printf("%d ", peekQueue(&q)); printf("%d ",

outQueue(&q));

while(!emptyQueue(&q)){

printf("%d ", outQueue(&q));

}

printf(" ");

clearQueue(&q);

system("pause");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值