10.2: Looking at the queue code
- Page ID
- 34688
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)
We have already learned how to use linked lists, and how to use that concept in creating and managing a Stack. So, we wil take a look at the code for a Queue.
struct QNode {
int data;
QNode* next;
};
This piece should look familiar. In the linked list we called it a Node, in the stack the example code called it a StackNode. It is simply a structure that contains our data and a pointer to the next item in the list/stack/queue.
Also - understand that the struct can contain multiple data types, it is not restricted to a single int. For example:
struct QNode {
int data;
string fName;
string lName;
float cost;
QNode* next;
};
When we use these data types we manage the first, next and last points (and any other pointers we need to manage).
Adapted from: "Queue – Linked List Implementation" by Gaurav Kumar is licensed under CC BY-SA 4.0