链表表示队列图解,认识链表结构图解

首页 > 经验 > 作者:YD1662022-11-04 00:01:30

定义一个新节点,然后将新节点赋值给队列的头。

出队

Node * Delete(Node *S) { if (S->front != S->rear) { S->front = S->front->next; return S; } else { cout << "队列为空"; return 0; } }

链表表示队列图解,认识链表结构图解(5)

即将队列尾指向队列尾下一个:即 S->front = S->front->next;

显示队列头

void Show(Node *S) { cout << "队头元素为:"; cout << S->front->next->data << endl; }

链表表示队列图解,认识链表结构图解(6)

因为链表的头指向的是空,所以显示出头的下一个元素,即

S->front->next->data

第一种代码实现

#include<iostream> using namespace std; #define MaxSize 20 struct Node { int data; //数据域 Node *front; //尾 Node *rear; //头 Node *next; //下一个 }; Node *Init() { Node *S = new Node; S->front = S->rear=new Node; S->front->next = NULL; return S; } Node *Enter(Node *S) { Node *P = new Node; cout << "输入将要入队列的值:"; cin>>P->data; P->next = NULL; S->rear->next = P; S->rear = P; return S; } Node * Delete(Node *S) { if (S->front != S->rear) { S->front = S->front->next; return S; } else { cout << "队列为空"; return 0; } } void Show(Node *S) { cout << "队头元素为:"; cout << S->front->next->data << endl; } int main() { Node *S = Init(); S=Enter(S); S=Enter(S); S=Enter(S); Show(S); S=Delete(S); Show(S); system("pause"); }

链表表示队列图解,认识链表结构图解(7)

第二种代码实现

#include<iostream> using namespace std; #define MaxSize 20 struct Node { int data; //数据域 Node *next; //下一个 }; struct List { Node *front; //尾 Node *rear; //头 }; List *Init() { List *S = new List; S->front = S->rear=new Node; S->front->next = NULL; return S; } List *Enter(List *S) { Node *P = new Node; cout << "输入将要入队列的值:"; cin>>P->data; P->next = NULL; S->rear->next = P; S->rear = P; return S; } List* Delete(List *S) { if (S->front != S->rear) { S->front = S->front->next; return S; } else { cout << "队列为空"; return 0; } } void Show(List *S) { cout << "队头元素为:"; cout << S->front->next->data << endl; } int main() { List *S = Init(); S=Enter(S); S=Enter(S); S=Enter(S); Show(S); S=Delete(S); Show(S); system("pause"); }

链表表示队列图解,认识链表结构图解(8)

上一页123下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.