带头结点与不带头结点的链队列的基本操作(草稿)
程序员文章站
2022-07-14 13:10:01
...
#include <stdio.h>
#include <stdlib.h>
//队列是操作受限的线性表,入队只能在队尾,出队只能在队头
typedef struct LinkNode{ //链式队列节点
int data;
struct LinkNode* next;
}LinkNode;
typedef struct { //链式队列
LinkNode* front, *rear;//队列的对头和队尾指针;
}LinkQueue;
//初始化(带头结点)
void initqueue(LinkQueue& Q) {
//初始时,front和rear都指向头结点
Q.rear = Q.front = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool isempty(LinkQueue& Q) { //判断队列是否为空
if (Q.front == Q.rear) //或者说Q.front->next=NULL;
return true;
else return false;
}
void enqueue(LinkQueue& Q, int& n) { //新元素入队,带头结点(将n个节点入队)
int x;
printf("需要入队几个元素?\n");
scanf_s("%d", &n);
for (int i = 1; i <= n; i++){
LinkNode* S = (LinkNode*)malloc(sizeof(LinkNode));
printf("请输入第%d个元素的值\n",i);
scanf_s("%d", &x);
S->next = NULL;
S->data = x;
Q.rear->next = S; //新节点插入到rear之后
Q.rear = S; //修改队尾指针
}
}
bool dequeue(LinkQueue& Q, int& x) { //出队
if (Q.front = Q.rear)return false;
LinkNode* S = Q.front->next;
x =S->data;
Q.front->next = S->next;
if (S == Q.rear) {
Q.rear = Q.front;
}
free(S);
return true;
}
//初始化(不带头结点) 不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点!!!!!!!!
void Initqueue(LinkQueue& Q) {
//初始时,rear和front都指向NULL;
Q.rear = NULL;
Q.front = NULL;
}
bool Isempty(LinkQueue& Q) { //判断队列是否为空,不带头结点!!!
if (Q.front == NULL)return true;
else return false;
}
void Enqueue(LinkQueue& Q, int x) { //新元素入队,不带头结点
LinkNode* S = (LinkNode*)malloc(sizeof(LinkNode));
S->next = NULL;
S->data = x;
if (Q.front == NULL) {
Q.front = S;
Q.rear = S;
}
else {
Q.rear->next = S;
Q.rear = S;
}
printf("队头元素为%d\n", x);
}
bool Dequeue(LinkQueue&Q, int& x) { //不带头结点的队头出队
if (Q.front ==NULL) {
return false;
}
LinkNode* S = Q.front;
x = S->data;
Q.front = S->next;
if (S == Q.rear) {
Q.front = NULL;
Q.rear = NULL;
}
free(S);
return true;
}
int main() {
LinkQueue Q;
int x, n;
initqueue(Q);
if (isempty(Q))printf("空队列");
enqueue(Q,n);
if(dequeue(Q,x)){
printf("出队成功,队头元素为%d\n", x);
}
Initqueue(Q);
if (Isempty(Q))printf("空队列\n");
Enqueue(Q, 4);
if (Dequeue(Q, x)) {
printf("出队成功,队头元素为%d\n", x);
}
}
#include <stdio.h>
#include <stdlib.h>
//队列是操作受限的线性表,入队只能在队尾,出队只能在队头
typedef struct LinkNode //链式队列节点
{
int data;
struct LinkNode* next;
} LinkNode;
typedef struct //链式队列
{
LinkNode* front, *rear;//队列的对头和队尾指针;
} LinkQueue;
//初始化(带头结点)
void initqueue(LinkQueue& Q)
{
//初始时,front和rear都指向头结点
Q.rear = Q.front = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool isempty(LinkQueue& Q) //判断队列是否为空
{
if (Q.front == Q.rear) //或者说Q.front->next=NULL;
return true;
else return false;
}
void enqueue(LinkQueue& Q, int& n) //新元素入队,带头结点(将n个节点入队)
{
int x;
printf("需要入队几个元素?\n");
scanf_s("%d", &n);
//scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
LinkNode* S = (LinkNode*)malloc(sizeof(LinkNode));
if(i==1)/*************/
{
Q.front=S;
}/*****************/
printf("请输入第%d个元素的值\n",i);
scanf_s("%d", &x);
//scanf("%d", &x);
S->next = NULL;
S->data = x;
Q.rear->next = S; //新节点插入到rear之后
Q.rear = S; //修改队尾指针
}
}
bool dequeue(LinkQueue& Q, int& x) //出队
{/*******************************************************************/
if (Q.front == Q.rear)return false;/*********/
LinkNode* S = Q.front;/**********/
x =S->data;
Q.front = S->next;/**********/
if (S == Q.rear)
{
Q.rear = Q.front;
}
free(S);
return true;
}
//初始化(不带头结点) 不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点,不带头结点!!!!!!!!
void Initqueue(LinkQueue& Q)
{
//初始时,rear和front都指向NULL;
Q.rear = NULL;
Q.front = NULL;
}
bool Isempty(LinkQueue& Q) //判断队列是否为空,不带头结点!!!
{
if (Q.front == NULL)return true;
else return false;
}
void Enqueue(LinkQueue& Q, int x) //新元素入队,不带头结点
{
LinkNode* S = (LinkNode*)malloc(sizeof(LinkNode));
S->next = NULL;
S->data = x;
if (Q.front == NULL)
{
Q.front = S;
Q.rear = S;
}
else
{
Q.rear->next = S;
Q.rear = S;
}
printf("队头元素为%d\n", x);
}
bool Dequeue(LinkQueue&Q, int& x) //不带头结点的队头出队
{
if (Q.front ==NULL)
{
return false;
}
LinkNode* S = Q.front;
x = S->data;
Q.front = S->next;
if (S == Q.rear)
{
Q.front = NULL;
Q.rear = NULL;
}
free(S);
return true;
}
int main()
{
LinkQueue Q;
int x, n;
initqueue(Q);
if (isempty(Q))printf("空队列\n");
enqueue(Q,n);
if(dequeue(Q,x))
{
printf("出队成功,队头元素为%d\n", x);
}
Initqueue(Q);
if (Isempty(Q))printf("空队列\n");
Enqueue(Q, 4);
if (Dequeue(Q, x))
{
printf("出队成功,队头元素为%d\n", x);
}
}
上一篇: 链栈与链队列
下一篇: 栈与队列(1)—队列与循环队列