The Queue, as a Linked List - Switching List Types - Algorithms C++ Edition (2015)

Algorithms C++ Edition (2015)

Part 2: Switching List Types

Chapter 2c: The Queue, as a Linked List

The Data Structure

This is similar to the Linked List Stack. You’ll need two Linked List nodes - the Front and the Back. You also need an integer to keep track of the Item Count. You may also have to design the Linked List Nodes as well.

// A Singly Linked List Node has:

// - the Next Node (Point to the Next Node)

// - the Data Item (choose what data type you want)

CompositeStructure Node {

String ID

String NEXTID

(choose a data type) ITEM

}

// Abstract Data Type: Queue, as a Linked List

// The Queue Has:

// - the Front (Linked List Node)

// - the Back (Linked List Node)

CompositeStructure LinkedListQueue {

Node FRONT

Node BACK

Integer COUNT

}

Now, let’s look at how the Enqueue() and Dequeue() methods work, in detail:

Linked List Stack: Enqueue() Function

The Enqueue() function from a Linked List Queue adds items into the back of the Queue. It does this in the following procedure, in this particular order:

1) Make a new Local Variable. Set it as a Node. This will be the New Linked List Back.

2) Set your input item to be the New Back Node’s data

3) Make sure to set the new back’s next node to null

4) Set the New Back as the Linked List’s Back of Queue. If this queue was previously empty, the New Node will also be the new Front.

5) And Finally, Increment the Item Count

After the Enqueue() procedure successfully inserts an item, you should have that item at the back of the linked list and the item count should be increased by 1.

Linked List Queue: Dequeue() Function

The Dequeue() method from a Linked List Queue removes the item at the very front of the Queue. The function returns that item as the Function Output. It does this in the following procedure, in this particular order:

1) Check if the Front Node isn’t Null (if the Front Node Exists)

2) If so, either return NULL or report a queue underflow error

3) If there’s an item to dequeue, create a Local Variable. Set it to be your soon-to-be return item (it should be the Front Node!)

4) Get the Node NEXT to the Front. This will NOW be the new LinkedList Front.

5) Decrement the Item Count

6) And Finally, the function returns your item and ends.

After the dequeue() procedure successfully removes and retrieves an item, the item should be the item that was at the very front of the Linked List. Also, the item count should be decreased by 1.

C++ 02a: The Queue, as a Circular Array

The Data Structure, in C++

Again, by default, we’ll set the Array Size to 10.

// Abstract Data Type: Queue, as an Array

// The Queue Has:

// - the Actual Array (with Size and Data Type)

// - the Index of the Front Item (Integer)

// - the Index of the Back Item(Integer)

// - the Max Size of the Queue (Integer)

// - the Current Item Count (Integer)

class ArrayQueue {

int FRONT;

int BACK;

int MAXSIZE = 10;

int COUNT;

<chosen data type> ARRAY[10];

public:

// INPUT: - nothing

// OUTPUT: - nothing

// EFFECT: Constructor: Array Queue

ArrayQueue() {

this->FRONT = 0;

this->BACK = 0;

this->COUNT = 0;

};

// INSERT ENQUEUE() FUNCTION HERE

//________________

// INSERT DEQUEUE() FUNCTION HERE

//________________

};

Array Queue: Enqueue() Function, in C++

// INPUT: - an item (chosen Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Array Queue

void enqueue(<chosen data type> item) {

// 1) Check if there’s room in the array for the extra item

if (this->MAXSIZE <= this->COUNT) {

// 2) If not, print a message and return null

std::cout << "Queue Overflow!\n" ;

return;

}

// 3) If there’s room in the array, we add the item to the queue.

// Set the new item’s index as the current Back Index

// (so the item is the very last item in the Queue)

this->ARRAY[BACK] = item;

// 4) Increment the Back Index.

// If the Back Index becomes bigger than or equal to the Array Size,

// set the Back Index to 0 (it’s now at the start of the Circular Array).

this->BACK +=1;

if (this->BACK >= MAXSIZE) this->BACK = 0;

// 5) And Finally, Increment the Item Count

this->COUNT += 1;

};

Array Queue: Dequeue() Function, in C++

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type)

// from our Array Queue

<chosen data type> dequeue() {

// 1) Check the Array Count if there are any items to dequeue.

if (this->COUNT <= 0) {

// 2) If not, print a message and return null

std::cout << "Queue Underflow!\n";

return NULL;

}

// 3) If there’s an item to dequeue, create a Local Variable.

// Set it to be your return item

// (it should be the item at the front of the queue!)

<chosen data type> r = this->ARRAY[FRONT];

// Remove the item at the Front of the Queue afterwards

this->ARRAY[FRONT] = NULL;

// 4) Increment the Front Index.

// If the Front Index becomes bigger than or equal to the Array Size,

// set the Front Index to 0 (it’s now at the start of the Circular Array).

this->FRONT+=1;

if (this->FRONT >= MAXSIZE) this->FRONT = 0;

// 5) Decrement the Item Count

this->COUNT -= 1;

// 6) And Finally, the function returns your item and ends.

return r;

}

C++ 02b: The Queue, as a Linked List

The Data Structure, in C++

First, we have class definitions for our Linked List Queue - for both a Node and the actual Linked List Queue. We also have constructors for both classes. We use the same Node Class from the LinkedListStack from above. And just like the Stack version, you need both of them together; the Queue requires nodes to be made.

// A Singly Linked List Node has:

// - the Next Node (Point to the Next Node)

// - the Data Item (choose what data type you want)

class Node {

public:

Node* NEXT;

<chosen data type> ITEM;

// INPUT: - a data item

// OUTPUT: - nothing

// EFFECT: Constructor: Linked List Node, with the Data Item as its input

Node(<chosen data type> item) {

this->ITEM = item;

};

};

// Abstract Data Type: Queue, as a Linked List

// The Queue Has:

// - the Front (Linked List Node)

// - the Back (Linked List Node)

class LinkedListQueue {

Node* x = new Node(NULL);

Node* FRONT = x;

Node* BACK = x;

int COUNT;

public:

// INPUT: - nothing

// OUTPUT: - nothing

// EFFECT: Constructor: Linked List Queue,

LinkedListQueue() {

this->COUNT = 0;

};

// INSERT ENQUEUE() FUNCTION HERE

//________________

// INSERT DEQUEUE() FUNCTION HERE

//________________

};

Linked List Queue: Enqueue() Function, in C++

// INPUT: - an item (some Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Linked List Queue

void enqueue(<chosen data type> INPUT) {

// 1) Make a new Local Variable. Set it as a Node.

// This will be the New Linked List back.

Node* NEWBACK = new Node(INPUT);

// 2) Set your input item to be the New Back Node’s data

// (already done in Constructor)

// 3) make sure to set the new back’s next node to null

NEWBACK->NEXT = NULL;

// 4) Set the New Back as the Linked List’s Back of Queue

this->BACK->NEXT = NEWBACK;

this->BACK = NEWBACK;

// If this queue was previously empty,

// the New Node will also be the new Front

if (this->COUNT <= 0) {

this->FRONT = NEWBACK;

}

// 5) And Finally, Increment the Item Count

this->COUNT += 1;

Linked List Queue: Dequeue() Function, in C++

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type) from our Linked List Queue

<chosen data type> dequeue() {

// 1) Check if the Front Node isn’t Null (if the Front Node Exists)

if (this->COUNT <= 0) {

// 2) If so, either return NULL

// or report a queue underflow error

// (we do both here)

std::cout << "Stack Underflow!" << "\n";

return NULL;

}

// (assume error() reports an error for you)

// 3) If there’s an item to dequeue, create a Local Variable.

// Set it to be your soon-to-be return item

// (it should be the Front Node!)

<chosen data type> r = FRONT->ITEM;

// PROOF: return item loaded

std::cout << "Return Item Loaded: " << r << "\n";

// 4) Get the Node NEXT to the Front.

// This will NOW be the new LinkedList Front.

this->FRONT = this->FRONT->NEXT;

// 5) Decrement the Item Count

this->COUNT -= 1;

// 7) And Finally, the function returns your item and ends.

return r;

}

C++ Workshop #2

Message Queue

Now we’re going to see the Queue in action.

Part 1:

1) Set up an IDE of your choice (online IDE’s such as rextester.com, ideone.com, or codepad.org will do as well)

2) In the above portion of your code, make sure you have these lines included:

#include <iostream>

using namespace std;

These allow your C++ code to use strings and other basic data types.

3) Copy-paste either the ArrayQueue or the LinkedListQueue class from the previous chapters, along with their respective enqueue() and dequeue() methods. You may remove the Print PROOF statements if you wish.

4) In all places of your code that require you to choose your data type, set them as a string:

<chosen data type> ====> string

5a) If you chose ArrayQueue, to avoid errors, change the following lines from the dequeue() method below:

this->ARRAY[FRONT] = NULL;

to

this->ARRAY[FRONT] = "";

5b) If you chose LinkedListQueue, to avoid errors, change the following lines from the constructor method below:

Node* x = new Node(NULL);

to

Node* x = new Node("DEFAULT");

6) Afterwards, copy these lines as your main() method:

int main()

{

// Fill Blank as ArrayStack* or LinkedListStack*

// DO NOT forget the star! *

____ q;

// Fill blank as ArrayStack() or LinkedListStack()

// depending on whatever you chose previously

// It's a Constructor: DO NOT forget the parentheses! ()

q = new ____;

q->enqueue("HI ");

q->enqueue("THERE! ");

q->enqueue("How's ");

q->enqueue("it ");

q->enqueue("going? ");

q->enqueue("Did ");

q->enqueue("you ");

q->enqueue("know ");

q->enqueue("you're ");

q->enqueue("my ");

q->enqueue("Best Friend? ");

string a[12];

for(int i = 0; i < 12; i++) {

a[i] = q->dequeue();

std::cout << a[i] ;

}

}

What happened?

Notice how, after running the code, the message clearly displays in perfect order.

If you used an Array Queue, we tried to enqueue 10 strings - but the 11th string was not enqueued. What happened? A queue overflow. Next, the iterating code tried to do dequeue 12 times. But once there was nothing to dequeue, a Queue Underflow occurred.

If you used a Linked List Queue, we manage to enqueue all 11 strings. How? Linked Lists have no list sizes, remember? But then again, once there was nothing to dequeue, a Queue Underflow also occurred.

Part 2: Switching List Types

If you switch to the other list type (array or linked list), you should have the same results. Try it out, and note the differences between the two data structures.

REFERENCE SECTION:

REF-01: The Stack

Array Stack, Data Structure in Pseudocode

// Abstract Data Type: Stack, as an Array

// The Stack Has:

// - the Actual Array (with Size and Data Type)

// - the Max Size of the Stack (Integer)

// - the Current Item Count (Integer)

CompositeStructure ArrayStack {

Integer MAXSIZE

Integer COUNT

(choose a data type) ARRAY[MAXSIZE]

}

Array Stack: Push() Function in Pseudocode

// INPUT: - an item (some Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Array Stack

void push(<chosen data type> ITEM) {

// 1) Check if there’s room in the array for the extra item

if (this.MAXSIZE < this.COUNT) {

// 2) If there’s no room in the array,

// …either report a stack overflow error (if fixed-size array)

// (assume error() reports an error for you)

error(“Stack Overflow”)

// …or increase the array size (if variable array)

// (assume increaseArray() increases array size by 1)

this.increaseArray()

}

// 3) If there’s room in the array, add the item to the stack.

// Set the new item’s index as the current Item Count

this.ARRAY[COUNT] = ITEM

// 4) And Finally, Increment the Item Count

this.COUNT += 1

}

Array Stack: Pop() Function in Pseudocode

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type) from our Array Stack

<chosen data type> pop() {

// 1) Check the Array Count if there are any items to pop.

if (this.COUNT <= 0) {

// 2) If not, either return NULL

return NULL

// or report a stack underflow error

// (assume error() reports an error for you)

error(“Stack Underflow”)

// 3) If there’s an item to pop, Decrement the Item Count

this.COUNT -= 1

// 4) Create a Local Variable.

// Set it to be your return item

// (it should be the item at the end of the stack!)

<chosen data type> r = this.ARRAY[COUNT]

// Remove the item at the End of the Stack afterwards

this.ARRAY[COUNT] = null

// 5) And Finally, the function returns your item and ends.

return r

}

Note how we decremented the Item Count BEFORE we outputted our item and ended the function. If you recall, function returns should be the very last thing in a function procedure - as it ends the function.

For experienced programmers, you can also opt to forego the local variable if you wish, depending on what language you wish to use and how much performance you want.

Linked List Stack, Data Structure in Pseudocode

// A Singly Linked List Node has:

// - the Next Node (Point to the Next Node)

// - the Data Item (choose what data type you want)

CompositeStructure Node {

Node NEXTID

(choose a data type) ITEM

}

// Abstract Data Type: Stack, as a Linked List

// The Stack Has:

// - a Head (Linked List Node)

CompositeStructure LinkedListStack {

Node HEAD

Integer COUNT

}

Now, let’s look at how the Push() and Pop() methods work, in detail:

Linked List Stack: Push() Function in Pseudocode

// INPUT: - an item (some Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Linked List Stack

void push(<chosen data type> INPUT) {

// 1) Make a new Local Variable. Set it as a Node.

// This will be the New Linked List head.

Node NEWHEAD = new Node

// 2) Set your input item to be the New Head Node’s data

NEWHEAD.ITEM = INPUT

// 3) Set the previous head’s next node to the New Head

NEWHEAD.NEXT = this.HEAD

// 4) Set the New Head as the Linked List’s Stack Head

this.HEAD = NEWHEAD

// 5) And Finally, Increment the Item Count

this.COUNT += 1

}

Linked List Stack: Pop() Function

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type) from our Array Stack

<chosen data type> pop() {

// 1) Check if the Head Node isn’t Null (if the Head Node Exists)

if (this.HEAD = NULL) {

// 2) If so, either return NULL

return NULL

}

// or report a stack underflow error

// (assume error() reports an error for you)

error(“Stack Underflow”)

}

// 3) If there’s an item to pop, create a Local Variable.

// Set it to be your soon-to-be return item

// (it should be the Head Node!)

<chosen data type> r = HEAD.ITEM

// 4) Get the Node NEXT to the Head.

// This will NOW be the new LinkedList Head.

this.HEAD = this.HEAD.NEXT

// 5) Decrement the Item Count

this.COUNT -= 1

// 7) And Finally, the function returns your item and ends.

return r

}

REF-02: The Queue

Array Queue: Data Structure in Pseudocode

// Abstract Data Type: Queue, as an Array

// The Queue Has:

// - the Actual Array (with Size and Data Type)

// - the Index of the Front Item (Integer)

// - the Index of the Back Item(Integer)

// - the Max Size of the Queue (Integer)

// - the Current Item Count (Integer)

CompositeStructure ArrayQueue {

Integer FRONT

Integer BACK

Integer MAXSIZE

Integer COUNT

(choose a data type) ARRAY[MAXSIZE]

}

Array Queue: Enqueue() Function in Pseudocode

// INPUT: - an item (some Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Array Queue

void enqueue(<chosen data type> ITEM) {

// 1) Check if there’s room in the array for the extra item

if (this.MAXSIZE < this.COUNT) {

// 2) If there’s no room in the array,

// …either report a Queue overflow error (if fixed-size array)

// (assume error() reports an error for you)

error(“Queue Overflow”)

// …or increase the array size (if variable array)

// (assume increaseArray() increases array size by 1)

this.increaseArray()

}

// 3) If there’s room in the array, we add the item to the queue.

// Set the new item’s index as the current Back Index

// (so the item is the very last item in the Queue)

this.ARRAY[BACK] = ITEM

// 4) Increment the Back Index.

// If the Back Index becomes bigger than or equal to the Array Size,

// set the Back Index to 0 (it’s now at the start of the Circular Array).

this.BACK +=1

if (this.BACK >= MAXSIZE) this.BACK = 0

// 5) And Finally, Increment the Item Count

this.COUNT += 1

}

Array Queue: Dequeue() Function in Pseudocode

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type) from our Array Queue

<chosen data type> dequeue() {

// 1) Check the Array Count if there are any items to dequeue.

if (this.COUNT <= 0) {

// 2) If not, either return NULL

return NULL

// or report a queue underflow error

// (assume error() reports an error for you)

error(“Queue Underflow”)

// 3) If there’s an item to dequeue, create a Local Variable.

// Set it to be your return item

// (it should be the item at the front of the queue!)

<chosen data type> r = this.ARRAY[FRONT]

// Remove the item at the Front of the Queue afterwards

this.ARRAY[FRONT] = null

// 4) Increment the Front Index.

// If the Front Index becomes bigger than or equal to the Array Size,

// set the Front Index to 0 (it’s now at the start of the Circular Array).

this.FRONT+=1

if (this.FRONT >= MAXSIZE) this.FRONT = 0

// 5) Decrement the Item Count

this.COUNT -= 1

// 6) And Finally, the function returns your item and ends.

return r

}

Linked List Queue: Data Structure in Pseudocode

// A Singly Linked List Node has:

// - the Next Node (Point to the Next Node)

// - the Data Item (choose what data type you want)

CompositeStructure Node {

String ID

String NEXTID

(choose a data type) ITEM

}

// Abstract Data Type: Queue, as a Linked List

// The Queue Has:

// - the Front (Linked List Node)

// - the Back (Linked List Node)

CompositeStructure LinkedListQueue {

Node FRONT

Node BACK

Integer COUNT

}

Now, let’s look at how the Enqueue() and Dequeue() methods work, in detail:

Linked List Queue: Enqueue() Function in Pseudocode

// INPUT: - an item (some Data Type)

// OUTPUT: - nothing

// EFFECT: inserts an item (some Data Type) onto our Linked List Queue

void enqueue(<chosen data type> INPUT) {

// 1) Make a new Local Variable. Set it as a Node.

// This will be the New Linked List back.

Node NEWBACK = new Node

// 2) Set your input item to be the New Back Node’s data

NEWBACK.ITEM = INPUT

// 3) Set the previous back’s next node to the New Back

NEWBACK.NEXT = this.back

// 4) Set the New Back as the Linked List’s Back of Queue

this.BACK = NEWBACK

// If this queue was previously empty,

// the New Node will also be the new Front

if (this.COUNT <= 0) {

this.FRONT = NEWBACK

}

// 5) And Finally, Increment the Item Count

this.COUNT += 1

}

Linked List Queue: Dequeue() Function in Pseudocode

// INPUT: - nothing

// OUTPUT:- an item (some Data Type)

// EFFECT: removes and retrieves an item (some Data Type) from our Linked List Queue

<chosen data type> dequeue() {

// 1) Check if the Front Node isn’t Null (if the Front Node Exists)

if (this.FRONT = NULL) {

// 2) If so, either return NULL

return NULL

}

// or report a queue underflow error

// (assume error() reports an error for you)

error(“QueueUnderflow”)

}

// 3) If there’s an item to dequeue, create a Local Variable.

// Set it to be your soon-to-be return item

// (it should be the Front Node!)

<chosen data type> r = FRONT.ITEM

// 4) Get the Node NEXT to the Front.

// This will NOW be the new LinkedList Front.

this.FRONT = this.FRONT.NEXT

// 5) Decrement the Item Count

this.COUNT -= 1

// 7) And Finally, the function returns your item and ends.

return r

}

BONUS CHAPTERS:

BONUS CHAPTER 01a: Lists

At one point, computers will eventually have a collection of data to deal with - regardless whether they are objects or basic data.

Thus, we have lists.

Lists in General

As an Abstract Data Type, lists are essentially a series of data items connected together.

Computers will generally go through data item on the lists one by one.

There are two general types of lists: linked lists and arrays. Most programming languages will usually include either type of list.

Linked Lists v.s. Arrays. Which list to use?

IMPORTANT NOTE: The ironic thing about most programming languages is the fact that they ALL have arrays and linked lists available. But, what if a colleague or boss asks you which one to use? See, sometimes making the slightest decisions like these will be such big deals that make or break an app - and sometimes, even job applications, careers and startups.

In general, use Linked Lists for quickly adding and deleting data items for that list, no matter what order they are. Whether the item is first, last, or in the middle of the list, it takes the same time and effort for the computer to add/remove data items. Also, if you also don’t know how big your list will be, linked lists are preferable.

In general, use Arrays for any computing where you need to access or process each element in the list. The indices in the Array will help your computer process the list faster, as well as give your computer multi-process potential for that list. Also, use arrays if each item in the list needs an index. For example, if you wanted to randomly get list items, you can only do it in an array (in a linked list, how would the computer know what item to get? Think about it…)

Bottom line:

Linked Lists = any size, faster add/remove

Arrays = fixed size, faster access/process

Linked Lists v.s. Arrays: Data Management

In general, if given the same data type to store in lists, arrays take less memory/storage space than linked lists. You’ll find out about this later, but linked lists generally need slightly more data in its structure than arrays. However, in lists with very high number of items, this slight difference is actually significant.

View Programming: Master Handbook Series Here

BONUS CHAPTER 01b: Linked Lists

The concept of linked lists is actually very simple.

Think of linked lists as a chain - literally. In a linked list, each “link” in the chain will have its item, followed by whatever “link” is next. If a certain link has no links next, then that link is at the end of the chain - or rather, the end of the linked list.

Here’s an example of what a doubly linked list would look like visually:

[ ID01 ][ item ][ Prev: NULL][ Next: ID03]

[ ID04 ][ item ][ Prev: ID03][ Next: NULL]

[ ID03 ][ item ][ Prev: ID01][ Next: ID04]

There are three “links” in the chain, with link ID04 being the last one. Why is ID04 last? Easy - because there is no link after it.

And note how the IDs go from 01, then 04, and 03, and they’re not in order. However, in linked lists, the numbers DO NOT tell you the order. I REPEAT. The NUMBERS DO NOT tell you the order

It’s because of one crucial fact about how the ordering in linked lists are set up.

“The Ordering of a Linked List is this: The very first node (no nodes before it), then its next one, then the next one, and so on - until there’s a node with nothing after it.”

Building Linked Lists From Scratch:

If you ever had to design a linked list from scratch, the concept is simple. A single link in a linked list consists of the actual data, then the ID of whatever the next or previous nodes are. Singly linked list nodes only point to the next node, while Doubly Linked List nodes point to both next and previous nodes.

In our pseudocode below, there are four key fields in a doubly linked list node: the ID, the previous node’s ID, the next node’s ID, and the actual data item.

// Abstract Data Type: Linked List

// Each element in the Linked List is represented by a Node.

// A Node has:

// - an ID (String)

// - the ID of the Previous Node (String)

// - the ID of the Next Node (String)

// - the Data Item (choose what data type you want)

CompositeStructure Node {

String ID

String PREVID

String NEXTID

(choose a data type) ITEM

}

Usually, you can only use one data type per each linked list. The reason is simply how the computer system itself processes and organizes data (you’ll learn more about this if you know how Computer Systems themselves work).

View Programming: Master Handbook Series Here

BONUS CHAPTER 01c: Arrays

If you look up the word ‘array’ in the dictionary, you’ll find out that it’s an elaborate, and sometimes beautiful, arrangement of items in a particular order.

Emphasize the phrase “arrangement of items in a particular order”.

And in programming, you now know the key point of Arrays - they’re a list of data items sorted in a particular order.

Here’s an example of what an array with a size of 5 would look like visually:

[ 0 ][item A]

[ 1 ][item B]

[ 2 ][ NULL ]

[ 3 ][item D]

[ 4 ][ NULL ]

Here, you can see how quickly and easily you can add or get data items from an array. If you know the index where you stored your data, just access the array, the index, then the data is yours.

Also, arrays sizes are usually fixed. In some programming languages, there are also Dynamic or Variable-Size Arrays. But depending on how the programming language works, the data items are usually re-added into arrays that have as much room as the number of items.

Building Arrays From Scratch:

If you ever had to design a data array from scratch, the concept is simple too. But unlike linked lists, there aren’t usually any individual nodes. You simply build the array-type list itself. It requires two main things: the length or size of the array and the data type you want to store in it.

// Abstract Data Type: Array

// An Array has:

// - an Array Size

// - the Type of Data Item to Store (choose what data type you want)

CompositeStructure Array {

Integer SIZE

(choose a data type) ITEM

}

And just like linked lists, you can only use one data type per each array. Unlike linked lists, however, arrays are strict about having only one data type per array. The reason for this is how the computer assigns data memory/storage for that array. Having one data type per each array makes the array much more structured and easy to access.

View Programming: Master Handbook Series Here