欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

队列的链表实现

程序员文章站 2024-03-18 11:08:22
...

先用结构体描述链表

typedef struct Node * link;
typedef struct Node
{
    int data;
    link next;
}node;

再用结构体描述队列

typedef struct queue *qlink;
typedef struct queue
{
    link front;
    link rear;
}queue;

写出链表的相关函数

link createlist()
{
    link head = (link)malloc(sizeof(node));
    head->next = NULL;
    return head;
}
link newnode(int data)
{
    link newnode = (link)malloc(sizeof(node));
    newnode->data = data;
    return newnode;
}
void insertlistbytail(link last,int data)//尾插法
{
    link p = (link)malloc(sizeof(node));
    p->data = data;
    p->next = NULL;
    last->next = p;
}
void deletelistbyhead(link head)//删除队首元素
{
    link q = head->next;
    head->next = q->next;
    free(q);
}
void printlist(link head)
{
    link p =head->next;
    while(p)
    {
        printf("%d\n",p->data);
        p = p->next;
    }
}

初始化队列

qlink createqueue()
{
    qlink myqueue = (qlink)malloc(sizeof(queue));
    myqueue->front = createlist();
    myqueue->rear = myqueue->front;
    return myqueue;
}

入队

void EnterQueue(qlink myqueue,int data)
{
    insertlistbytail(myqueue->rear,data);
    myqueue->rear = myqueue->rear->next;
}

出队

void DeleteQueue(qlink myqueue)
{
    deletelistbyhead(myqueue->front);
}

打印队列

void printqueue(qlink myqueue)
{
    link q = myqueue->front->next;
    while(q)
    {
        printf("%d\t",q->data);
        q = q->next;
    }
}

检测队列是否为空

void QueueEmpty(qlink myqueue)
{
    if(myqueue->front->next == 0)
    {
        printf("队列为空\n");
    }
    else
    {
        printf("队列非空\n");
    }
}

输出队首元素

int QueueFirst(qlink myqueue)
{
    printf("队首元素为:%d\n",myqueue->front->next->data);
    return myqueue->front->next->data;
}

输出队尾元素

int QueueLast(qlink myqueue)
{
    printf("队尾元素为:%d\n",myqueue->rear->data);
    return myqueue->rear->data;
}

完整代码

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
typedef struct Node * link;
typedef struct Node
{
    int data;
    link next;
}node;
link createlist()
{
    link head = (link)malloc(sizeof(node));
    head->next = NULL;
    return head;
}
link newnode(int data)
{
    link newnode = (link)malloc(sizeof(node));
    newnode->data = data;
    return newnode;
}
void insertlistbytail(link last,int data)
{
    link p = (link)malloc(sizeof(node));
    p->data = data;
    p->next = NULL;
    last->next = p;
}
void deletelistbyhead(link head)
{
    link q = head->next;
    head->next = q->next;
    free(q);
}
void printlist(link head)
{
    link p =head->next;
    while(p)
    {
        printf("%d\n",p->data);
        p = p->next;
    }
}
typedef struct queue *qlink;
typedef struct queue
{
    link front;
    link rear;
}queue;
qlink createqueue()
{
    qlink myqueue = (qlink)malloc(sizeof(queue));
    myqueue->front = createlist();
    myqueue->rear = myqueue->front;
    return myqueue;
}
void EnterQueue(qlink myqueue,int data)
{
    insertlistbytail(myqueue->rear,data);
    myqueue->rear = myqueue->rear->next;
}
void DeleteQueue(qlink myqueue)
{
    deletelistbyhead(myqueue->front);
}
void printqueue(qlink myqueue)
{
    link q = myqueue->front->next;
    while(q)
    {
        printf("%d\t",q->data);
        q = q->next;
    }
}
void QueueEmpty(qlink myqueue)
{
    if(myqueue->front->next == 0)
    {
        printf("队列为空\n");
    }
    else
    {
        printf("队列非空\n");
    }
}
int QueueFirst(qlink myqueue)
{
    printf("队首元素为:%d\n",myqueue->front->next->data);
    return myqueue->front->next->data;
}
int QueueLast(qlink myqueue)
{
    printf("队尾元素为:%d\n",myqueue->rear->data);
    return myqueue->rear->data;
}
int main()
{
    qlink myqueue;
    myqueue = createqueue();
    QueueEmpty(myqueue);
    EnterQueue(myqueue,1);
    QueueEmpty(myqueue);
    EnterQueue(myqueue,2);
    EnterQueue(myqueue,3);
    EnterQueue(myqueue,4);
    EnterQueue(myqueue,5);
    EnterQueue(myqueue,6);
    DeleteQueue(myqueue);
    DeleteQueue(myqueue);
    QueueFirst(myqueue);
    QueueLast(myqueue);
    printqueue(myqueue);
    return 0;
}