队列链式存储方案一
seqList.h
#include<stdlib.h>
#include<stdio.h>struct SEQLINKNODE {struct SEQLINKNODE* next;
};
struct SEQLINKLIST {struct SEQLINKNODE head; //头结点struct SEQLINKNODE* back; //尾结点int size;
};typedef void* LinkQueue;//初始化
LinkQueue Init_SeqLinkList();//从队尾压入元素
void Push_SeqLinkList(LinkQueue seq, void* data);//弹出队头元素
void Pop_SeqLinkList(LinkQueue seq);//取队尾元素
LinkQueue Back_SeqLinkList(LinkQueue seq);//取队头元素
LinkQueue Front_SeqLinkList(LinkQueue seq);//获取大小
int Size_SeqLinkList(LinkQueue seq);//清空
void Clear_SeqLinkList(LinkQueue seq);//销毁
void FreeSpace_SeqLinkList(LinkQueue seq);
seqList.c
#include"seqList.h"//初始化
LinkQueue Init_SeqLinkList()
{struct SEQLINKLIST* seq = malloc(sizeof(struct SEQLINKLIST));if (seq == NULL){return NULL;}seq->head.next = NULL; //指针域为空// 我 seq->back = seq->head;seq->back = &seq->head; //尾结点初始化 就是在头结点seq->size = 0;return seq;}//从队尾压入元素
void Push_SeqLinkList(LinkQueue seq, void* data)
{if (seq == NULL){return;}if (data == NULL){return;}struct SEQLINKLIST* myQueue = seq;struct SEQLINKNODE* node =data;//插入新节点myQueue->back->next = node; //问题1:有点绕node->next = NULL;myQueue->back = node;myQueue->size++;
}//弹出队头元素
void Pop_SeqLinkList(LinkQueue seq)
{if (seq == NULL){return;}struct SEQLINKLIST* myQueue = seq;//第一个有数据的节点struct SEQLINKNODE* pFirst = myQueue->head.next;//更新指针的指向myQueue->head.next = pFirst->next; //问题2:不知道循环截止条件myQueue->size--;
}//取队尾元素
LinkQueue Back_SeqLinkList(LinkQueue seq)
{if (seq == NULL){return NULL;}struct SEQLINKLIST* myQueue = seq;return myQueue->back; //seq->back->next 指向NULL
}//取队头元素
LinkQueue Front_SeqLinkList(LinkQueue seq)
{if (seq == NULL){return NULL;}struct SEQLINKLIST* myQueue = seq;return myQueue->head.next;
}//获取大小
int Size_SeqLinkList(LinkQueue seq)
{if (seq == NULL){return;}struct SEQLINKLIST* myQueue = seq;return myQueue->size;
}//清空
void Clear_SeqLinkList(LinkQueue seq)
{if (seq == NULL){return;}struct SEQLINKLIST* myQueue = seq;myQueue->head.next = NULL;myQueue->size = 0;
}//销毁
void FreeSpace_SeqLinkList(LinkQueue* seq)
{if (seq == NULL){return;}struct SEQLINKLIST* myQueue = seq;free(myQueue);
}
错误实现seqListme.c:
#include"seqList.h"//初始化
SeqLinkList* Init_SeqLinkList()
{SeqLinkList* seq = (SeqLinkList*)malloc(sizeof(SeqLinkList));seq->head->next = NULL;seq->back = seq->head;seq->size = 0;return seq;}//从队尾压入元素
void Push_SeqLinkList(SeqLinkList* seq, void* data)
{if (seq == NULL){return;}if (data == NULL){return;}data = seq->back->next; //问题1:有点绕seq->back->next = NULL;seq->size++;
}//弹出队头元素
void Pop_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return;}seq->head->next = seq->head->next->next; //问题2:不知道循环截止条件seq->size--;
}//取队尾元素
SeqLinkList* Back_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return NULL;}return seq->back; //seq->back->next 指向NULL
}//取队头元素
SeqLinkList* Front_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return NULL;}return seq->head->next;
}//获取大小
int Size_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return;}return seq->size;
}//清空
void Clear_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return;}seq->head->next = NULL;seq->size = 0;
}//销毁
void FreeSpace_SeqLinkList(SeqLinkList* seq)
{if (seq == NULL){return;}free(seq);
}
链式存储队列.c
#define _CRT_SECURE_NO_WARNINGS
#include"seqList.h"
#include<string.h>typedef struct PERSON {void* node;char name[64];int age;
}Person;int main()
{Person p1 = { NULL, "aaa",10 };Person p2 = { NULL,"bbb",20 };Person p3 = { NULL,"ccc",30 };Person p4 = { NULL,"ddd",40 };Person p5 = { NULL,"eee",50 };LinkQueue seq = Init_SeqLinkList();Push_SeqLinkList(seq, &p1);Push_SeqLinkList(seq, &p2);Push_SeqLinkList(seq, &p3);Push_SeqLinkList(seq, &p4);Push_SeqLinkList(seq, &p5);Person* pf = (Person*)Front_SeqLinkList(seq);printf("队头元素Name:%s Age:%d\n", pf->name, pf->age);Person* pb = (Person*)Back_SeqLinkList(seq);printf("队尾元素Name:%s Age:%d\n", pb->name, pb->age);while(Size_SeqLinkList(seq)>0){Person* p=(Person*)Front_SeqLinkList(seq);printf("Name:%s Age:%d\n", p->name, p ->age);Pop_SeqLinkList(seq);}FreeSpace_SeqLinkList(seq);return 0;}
运行结果:
队列链式存储方案二
queueList.h
#include<stdlib.h>
#include<stdio.h>typedef struct QUEUELINKNODE {struct QUEUELINKNODE* next;
}QueueLinkNode;
typedef struct QUEUELINKLIST {struct QUEUELINKNODE head; //头结点。head不需要是指针形式struct QUEUELINKNODE* back; //尾结点int size;
}QueueLinkList;//初始化
QueueLinkList* Init_QUEUELinkList();//从队尾压入元素
void Push_QUEUELinkList(QueueLinkList* QUEUE, void* data);//弹出队头元素
void Pop_QUEUELinkList(QueueLinkList* QUEUE);//取队尾元素
QueueLinkList* Back_QUEUELinkList(QueueLinkList* QUEUE);//取队头元素
QueueLinkList* Front_QUEUELinkList(QueueLinkList* QUEUE);//获取大小
int Size_QUEUELinkList(QueueLinkList* QUEUE);//清空
void Clear_QUEUELinkList(QueueLinkList* QUEUE);//销毁
void FreeSpace_QUEUELinkList(QueueLinkList* QUEUE);
queueList.c
#include<stdlib.h>
#include"queueList.h"//初始化
QueueLinkList* Init_QUEUELinkList()
{QueueLinkList* queue = (QueueLinkList*)malloc(sizeof(QueueLinkList));queue->head.next = NULL;queue->size = 0;//queue->head = &queue->back; 错误queue->back = &queue->head; //正确return queue;
}//从队尾压入元素
void Push_QUEUELinkList(QueueLinkList* queue, void* data)
{if (queue == NULL){return;}if (data == NULL){return;}QueueLinkNode *newnode = data;queue->back->next = newnode;newnode->next = NULL;queue->back = newnode;queue->size++;}//弹出队头元素
void Pop_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}QueueLinkNode *pFirst = queue->head.next; //第一个有数据的节点queue->head.next = pFirst->next; //头指针指向有数据节点的下一个结点queue->size--;
}//取队尾元素
QueueLinkList* Back_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}return queue->back;
}//取队头元素
QueueLinkList* Front_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}return queue->head.next;
}//获取大小
int Size_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}return queue->size;
}//清空
void Clear_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}queue->head.next = NULL;queue->size = 0;
}//销毁
void FreeSpace_QUEUELinkList(QueueLinkList* queue)
{if (queue == NULL){return;}free(queue);
}
链式存储队列2.c
#include"queueList.h"typedef struct PERSON {QueueLinkNode node;char name[64];int age;
}Person;int main()
{Person p1 = { NULL, "aaa",10 };Person p2 = { NULL,"bbb",20 };Person p3 = { NULL,"ccc",30 };Person p4 = { NULL,"ddd",40 };Person p5 = { NULL,"eee",50 };QueueLinkList* queue = Init_QUEUELinkList();Push_QUEUELinkList(queue, &p1);Push_QUEUELinkList(queue, &p2);Push_QUEUELinkList(queue, &p3);Push_QUEUELinkList(queue, &p4);Push_QUEUELinkList(queue, &p5);Person* pf = (Person*)Front_QUEUELinkList(queue);printf("队头元素Name:%s Age:%d\n", pf->name, pf->age);Person* pb = (Person*)Back_QUEUELinkList(queue);printf("队尾元素Name:%s Age:%d\n", pb->name, pb->age);while (Size_QUEUELinkList(queue)>0){Person* p = (Person*)Front_QUEUELinkList(queue);printf("Name:%s Age:%d\n", p->name, p->age);Pop_QUEUELinkList(queue);}FreeSpace_QUEUELinkList(queue);return 0;
}