队列实现 (双向循环链表 C++)

队列是很简单的,但是用数组实现可能更好点。。(其实我觉得数组在多个队列的时候更难)

然后我是第一次写双向循环链表。指向太乱了。

我这里是按照自己的想法,建立了一个头节点,一个尾节点,然后按照队列顺序正向插入到两个节点之间。输出和弹出队列的时候从后面操作。

下面上代码:

//
//  main.cpp
//  queue
//
//  Created by Alps on 14-7-28.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#include <iostream>
#define ElementType int

using namespace std;

struct Node;
typedef Node* PtrToNode;
typedef PtrToNode Queue;

struct Node{
    ElementType X;
    PtrToNode Pre;
    PtrToNode Next;
};

Queue createQueue(void){
    Queue Q;
    Queue Q2;
    Q2 = (Queue)malloc(sizeof(Queue));
    Q = (Queue)malloc(sizeof(Queue));
    Q->X = 0;
    Q->Next = Q2;
    Q->Pre = Q2;
    Q2->Next = Q;
    Q2->Pre = Q;
    return Q;
}

int isEmpty(Queue Q){
    return Q->Next->Next == Q;
}

void intoQueue(Queue Q, ElementType element){
    Queue tmp;
    Queue tmp1;
    tmp1 = (Queue)malloc(sizeof(Queue));
//    Queue switchTmp;
    tmp = (Queue)malloc(sizeof(Queue));
    tmp->X = element;
    tmp->Next = Q->Next;
    Q->Next->Pre = tmp;
    Q->Next = tmp;
    tmp->Pre = Q;
}

void outQueue(Queue Q){
    Queue tmp;
    tmp = Q->Pre->Pre;
    Q->Pre->Pre = tmp->Pre;
    tmp->Pre->Next = Q->Pre;
    free(tmp);
}

ElementType headQueue(Queue Q){
    if (Q == NULL) {
        printf("please create queue first!\n");
        return 0;
    }
    if (!isEmpty(Q)) {
        return Q->Pre->Pre->X;
    }else{
        printf("The queue is empty!\n");
        return 0;
    }
}

int makeEmpty(Queue Q){
    if (Q == NULL) {
        printf("please create queue first!\n");
        return -1;
    }
    while (!isEmpty(Q)) {
        outQueue(Q);
    }
    return 0;
}

void Print(Queue Q){
    if (!isEmpty(Q)) {
        Queue tmp = Q->Pre->Pre;
        while (tmp != Q) {
            printf("%d ",tmp->X);
            tmp = tmp->Pre;
        }
        printf("\n");
    }
}

int main(int argc, const char * argv[])
{
    Queue Q = createQueue();
    if (isEmpty(Q)) {
        printf("The queue is empty !\n");
    }else{
        printf("The queue is not empty!\n");
    }
    intoQueue(Q, 2);
    intoQueue(Q, 4);
    intoQueue(Q, 6);
    Print(Q);
    outQueue(Q);
    Print(Q);
    makeEmpty(Q);
    Print(Q);
//    printf("%d\n",headQueue(Q));
    return 0;
}

这个代码比较乱 = = ,多包涵了,我以后想想简单点的方法实现。其实单链表也可以,但是那样操作就不是O(1)了,所以才用双链表。

队列实现 (双向循环链表 C++)

时间: 2024-07-29 11:24:32

队列实现 (双向循环链表 C++)的相关文章

双向循环链表-C语言版

源文件部分: #include<stdio.h> #include<string.h> #include<malloc.h> typedef int Elemtype; #include"Delist.h" int main() { Dlnode head=NULL; instruction(head); return 0; } 头文件部分: typedef struct DLnode { Elemtype data; struct DLnode *

C语言通用双向循环链表操作函数集

说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  概念 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序通过链表中的指针链接次序实现.链表由一系列存储结点组成,结点可在运行时动态生成.每个结点均由两部分组成,即存储数据元素的数据域和存储相邻结点地址的指针域.当进行插入或删除操作时,链表只需修改相关结点的指针域即可,因此相比线性

第33课 双向循环链表的实现

1. DTLib中双向链表的设计思路 (1)数据结点之间在逻辑上构成双向循环,这有别于Linux内核链表的实现. (2)头结点仅用于结点的定位,而Linux内核链表是将头结点作为循环的一部分. 2. 实现思路 (1)通过模板定义DualCircleList类,继承自DualLinkList类 (2)在DualCircleList内部使用Linux内核链表进行实现(另类实现) (3)使用struct list_head定义DualCircleList的头结点 (4)特殊处理:循环遍历时忽略头结点

双向循环链表 初始化 插入 删除

#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR -1 #define TRUE 1 #define FALSE -1 #define NULL 0 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define LEN sizeof(DuLNode)

双向循环链表

//双向循环链表 typedef int datatype; //方便修改 //当然也可以写成模板来适应更多的数据类型 struct dclink{ datatype data;//数据定义 struct dclink *pre; struct dclink *next;//前驱和后继指针 }; class DCLink { public: DCLink();//default constructor DCLink(datatype data);//单参constructor void add(

算法导论13:双向循环链表 2016.1.13

今天这个又打了很长时间,本来觉得数据结构就是那样,不过是一种思维,但是实际上真正自己打和想象中差距还是很大,需要考虑到各种细节. 今天这个问题有一个比较有意思的应用,就是“约瑟夫环问题”. 具体可以参见百度百科: http://baike.baidu.com/link?url=poA1Aanlptc6yzP1puYhSw_0RQjRAplhPfHwk6eoiqMNxw6WigCEbexxZ8a9SUbrMGokpPbKNzVYw308xjeEw_ 读完问题就可以发现,这个问题用链表就是一个很完美

C++__双向循环链表(练习)

双向循环链表 link.h #ifndef LINK_H_ #define LINK_H_ #define HEADER 0 #define TAIL -1 typedef int data_type; enum LINK_OP { LINK_ERR = -1, LINK_OK }; class LINK { private: LINK *last; data_type data; LINK *next; public: LINK(); LINK(data_type data); virtual

c语言编程之双向循环链表

双向循环链表就是形成两个环,注意每个环的首尾相连基本就可以了. 程序中采用尾插法进行添加节点. 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define element int 4 typedef struct Node{ 5 element data; 6 struct Node *next; 7 struct Node *prior; 8 }*pNode; 9 10 //build a new double loop list 11

线性表.04.链式存储结构(双向循环链表)

以下是用双向循环链表实现的线性表 #include <stdio.h> #include <stdlib.h> #include <time.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int ElemType;//ElemType这里假设为int,可以根据需要进行更改 typedef int Status;//Status是函数的类型,其值是函数结果状态代码,如OK等 t