链对列初始化/入队列/出队列/判空

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#define MAXSIZE 100
using namespace std;
//链队列,链队列实质上是单链表,为了操作方便,需要设置队头和队尾两个指针,并放在一个结构体内,
//采用带头结点的单链表,使得队空与非空具有同一结构形式。
//链队列中结点类型
struct node{
    int data ;
    struct node * next ;
};
//链队列类型
struct linkqueue{
    struct node * _front , *_rear ;
};
//初始化链队列
void initlinkqueue(struct linkqueue * p){
    p->_front = (struct node*)malloc(sizeof(struct node)) ;//产生一个头结点并将地址填入头指针域
    p->_rear = p->_front ;
    p->_front->next = NULL ;
}
//入队列算法,不会产生上溢,故可省略队满判断
void addlinkqueue(struct linkqueue * p , int x){
    struct node * temp = (struct node*)malloc(sizeof(struct node)) ;
    temp->data = x ;
    temp->next = NULL ;
    p->_rear->next = temp ;//新结点插入队尾
    p->_rear  = temp ;//修改队尾指针
}

//出队列算法,为了避免在只有一个元素结点与多结点时操作不一致需修改队尾指针,算法中删除的是
//头结点而不是第一个元素结点,让第一个元素结点作为删除后的头结点,这样在物理上尽管删除的是头结点
//,而在逻辑上达到了删除第一个元素结点的目的。
int outlinkqueue(struct linkqueue * p) {
    if(p->_front == p->_rear)
        return NULL ; //队列为空返回NULL
    struct node * temp  = p->_front ;
    p->_front = p->_front->next ;
    free(temp) ;
    return p->_front->data ;//返回原第一个元素结点的值,它在新的队头结点中保存着
}
//读队头元素
int gethead(struct linkqueue *p){
    if(p->_front == p->_rear)
        return NULL ;
    return p->_front->next->data ;
}
//判断队列空
int isempty(struct linkqueue * p){
    if(p->_front == p->_rear)
        return 1 ;
    return 0 ;
}
int main(){
    struct linkqueue *qu ;
   initlinkqueue(qu) ;
   for(int i = 1 ; i<= 9 ; i++){
        addlinkqueue(qu , i) ;
   }
   while(!isempty(qu)){
      cout<<"删除的队头元素: "<<outlinkqueue(qu) << endl;
   }
   return 0 ;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,队列可以使用两种存储结构来实现:数组和链表。 1. 数组实现队列初始化、判队列空、队列队列的操作: ``` // 定义队列结构 typedef struct { int* array; // 存储队列元素的数组 int front; // 队头指针 int rear; // 队尾指针 int size; // 队列的容量 } Queue; // 初始化队列 void initQueue(Queue* q, int capacity) { q->array = malloc(sizeof(int) * capacity); q->front = q->rear = -1; q->size = capacity; } // 判队列空 int isEmpty(Queue* q) { return q->front == -1; } // 队列 void enqueue(Queue* q, int data) { if (q->rear == q->size - 1) { printf("Queue is full\n"); return; } if (isEmpty(q)) { q->front = q->rear = 0; } else { q->rear++; } q->array[q->rear] = data; } // 队列 int dequeue(Queue* q) { if (isEmpty(q)) { printf("Queue is empty\n"); return -1; } int data = q->array[q->front]; if (q->front == q->rear) { q->front = q->rear = -1; } else { q->front++; } return data; } ``` 2. 链表实现队列初始化、判队列空、队列队列的操作: ``` // 定义队列结点 typedef struct Node { int data; struct Node* next; } Node; // 定义队列结构 typedef struct { Node* front; // 队头指针 Node* rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue* q) { q->front = q->rear = NULL; } // 判队列空 int isEmpty(Queue* q) { return q->front == NULL; } // 队列 void enqueue(Queue* q, int data) { Node* newNode = malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (isEmpty(q)) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } // 队列 int dequeue(Queue* q) { if (isEmpty(q)) { printf("Queue is empty\n"); return -1; } Node* temp = q->front; int data = temp->data; q->front = q->front->next; free(temp); if (q->front == NULL) { q->rear = NULL; } return data; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值