栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构 链队列

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 链队列

有头节点

#include
typedef long long ll;

const ll INF=0x3f3f3f;
typedef struct LinkNode{
	int data;
	struct LinkNode* next;
}LinkNode;

typedef struct LinkQueue{
	LinkNode* front;
	LinkNode* rear;
}LinkQueue;

LinkQueue* InitQueue() {
	LinkQueue* Q=(LinkQueue*)malloc(sizeof(LinkQueue));;
	Q->front=Q->rear=(LinkNode*)malloc(sizeof(LinkNode));
	Q->front->next=NULL;
	return Q;
}

int IsEmpty(LinkQueue* Q) {
	int flag=0;
	if(Q->front=Q->rear) flag=1;
	return flag;
}

void EnQueue(LinkQueue* Q, int x) {
	LinkNode* s=(LinkNode*)malloc(sizeof(LinkNode));
	s->next=NULL;
	s->data=x;
	Q->rear->next=s;
	Q->rear=s;
}

int DeQueue(LinkQueue* Q) {
	if(Q->front==Q->rear) return INF;
	else {
		int x;
		LinkNode* s=Q->front->next;
		x=s->data;
		Q->front->next=s->next;
		if(Q->rear==s) Q->rear=Q->front;
		free(s);
		return x;
	}
}

int main() {
	LinkQueue* Q=InitQueue();
	int x;
	int n=3;
	for(int i=0;i
		scanf("%d", &x);
		EnQueue(Q, x);
	}

	for(int i=0;i
		x=DeQueue(Q);
		printf("%dn", x);
	}

    return 0;
}

无头节点

#include

typedef struct LinkNode {
    int data;
    struct LinkNode* next;
} LinkNode;

typedef struct LinkQueue {
    struct LinkNode* front;
    struct LinkNode* rear;
} LinkQueue;

LinkQueue* InitQueue() {
    LinkQueue* Q=(LinkQueue*)malloc(sizeof(LinkQueue));
    Q->front=NULL;
    Q->rear=NULL;
    return Q;
}

void EnQueue(LinkQueue* Q, int x) {
    LinkNode* s=(LinkNode*)malloc(sizeof(LinkNode));
    s->data=x;
    s->next=NULL;
    if(Q->front==NULL) Q->front=s;
    else Q->rear->next=s;
    Q->rear=s;
}

int DeQueue(LinkQueue* Q) {
    int x=-1111;
    if(Q->rear==NULL) return x;
    else {
        LinkNode* s=(LinkNode*)malloc(sizeof(LinkNode));
        s=Q->front;
        x=s->data;
        if(Q->front==Q->rear) Q->front=Q->rear=NULL;
        else  Q->front=s->next;
        free(s);
    }
    return x;
}

int main() {
    LinkQueue* Q=InitQueue();
    int n=3;
    for(int i=0; i
        int x;
        scanf("%d", &x);
        EnQueue(Q, x);
    }
    for(int i=0; i
        int x=DeQueue(Q);
        printf("%dn", x);
    }
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号