/***************************************************************
* Name: Rafael Orta
* Course: Computer Science & Programming
* Class: CS04225
* Assignment Date: 02/27/23
* File Name: LinkedLists.cpp
*****************************************************************
* Purpose: Simple demonstration of the creation and traversal of a
* single linked list
*****************************************************************/
#include <iostream>
#include <string>
using namespace std;
// Creating a struct that we will use as template to create the nodes.
struct node
{
string fName;
node *next;
};
// Function prototype to traverse and print the list.
void printing(node*&, node*&, node*&);
void dequeue(node*&, node*&, node *&);
int main()
{
// Creating the pointers to manage the link list.
node *n;
node *t;
node *h;
// enqueuing
cout << "\n ** Enqueuing **" << endl;
n = new node; // Creates a memory structure of type node and points the cursor n to it.
n -> fName = "Rafael Orta"; // Assigns a name of the fname member of the struct.
t = n; // The tail points to the new allocated node, we will be changing this pointer as we add nodes.
h = n; // The head points to the new allocated node, we will not change this pointer.
n = new node; // Creates a memory structure of type node and points the cursor n to it.
n -> fName = "Kristina Orta"; // Assigns a name of the fname member of the struct.
t -> next = n; // This is making the next member of the struck to point to the newly created node.
t = t -> next; // This is moving the tail pointer to thew new node, I could also do t = n;
n = new node;
n -> fName = "Andrea Orta";
t -> next = n; // This is making the next member of the struck to point to the newly created node.
t = t -> next; // This is moving the tail pointer to thew new node, I could also do t = n;
n = new node;
n -> fName = "Samantha Orta";
t -> next = n; // Moving the tail pointer to the new node
t = t -> next;
n -> next = NULL;
printing(h, n, t);
dequeue(h, n, t);
return 0;
}
// Function to traverse and print the list.
void printing(node *&h, node *&n, node *&t)
{
cout << "\n\n** Traversing and displaying **" << endl << endl;
n = h; // pointing the cursor n to the head of the linked list
while (n->next != NULL) // Print as long as you don't reach the tail
{
cout << n->fName << " " << endl; // Printing
n = n->next; // Moving to the next element on the list
}
cout << n->fName << " " << endl; // Print the data on the tail.
}
void dequeue(node *&h, node *&n, node *&t)
{
node * temp;
cout << "\n\n** Starting to clean the queue ** " << endl << endl;
for (int x = 0 ; x < 4 ; x++)
{
temp = h; // Pointing temp to the first node to dequeue
n = h;
cout << "Dequeing: " << n->fName << endl ;
h = h->next;
delete temp;
}
cout << "\n\n** The queue has been cleaned" << endl << endl;
n = NULL;
h = NULL;
temp = NULL;
}