Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 3 Create four (4) arrays of 100 unsigned integers. Using the random numb

ID: 3717075 • Letter: P

Question

Project 3

Create four (4) arrays of 100 unsigned integers.

Using the random number generator, load all four arrays with random numbers valued 0 to 99. Do Not Sort.

Create:             Stack/LIFO in array and linked list form

Queue/FIFO in array and linked list form

Insert at head (push), Delete at head (pop)

Insert at head(front), Delete at tail (back)

All Stacks and Queues have 10 elements. (isEmpty/isFull)

Prime/fill your (4) stacks and queues with the first 10 elements of your (4) input arrays.

After priming you will need to delete/clear an element before inserting the next one.

Print to your output file each input array (10 x10 grid) followed by the output of queues (print each one as you delete it OR store it in an output array of 100 element and then print as 10 x 10)

Explanation / Answer

#include<iostream>

#include <fstream>

#include <cstdlib>

#include <time.h>

using namespace std;

//Forward Declaration

class LinkedList;

//Create class

class Node{

//Access specifier

public:

//Declare variables

int data;

//Declare pointers

Node*next;

//Access specifier

public:

//Constructor

Node():data(0),next(NULL){}

//Constructor

Node(int d):data(d),next(NULL){}

//Friend class

friend class LinkedList;

};

//Define class

class LinkedList{

public:

Node*head;

Node*tail;

int size;

//Access specifier

public:

//Constructor

LinkedList():head(NULL),tail(NULL),size(0){}

//Copy constructor

LinkedList(const LinkedList&L2){

Node*temp = L2.head;

while(temp!=NULL){

insertAtEnd(temp->data);

temp = temp->next;

}

}

//Define function to insert data at front

void insertAtFront(int data){

if(head==NULL){

head = tail = new Node(data);

}

else{

Node *n = new Node(data);

n->next = head;

head = n;

}

}

//Define function to insert data at the end

void insertAtEnd(int data){

if(head==NULL){

head = tail = new Node(data);

}

else{

tail->next = new Node(data);

tail = tail->next;

}

}

//Define function to insert data at given position

void insert(int data,int position){

if(position==0){

insertAtFront(data);

}

else{

Node*temp = head;

int count=1;

while(count<position){

temp = temp->next;

count = count + 1;

}

Node*nextNode = temp->next;

Node*newNode = new Node(data);

temp->next = newNode;

newNode->next = nextNode;

}

}

//Define function to insert data in sorted

void sortedInsert(int data){

int pos=0;

if(head==NULL){

insertAtFront(data);

return;

}

Node *temp=head;

while(temp!=NULL && data>temp->data){

temp=temp->next;

pos++;

}

if(pos==0){

insertAtFront(data);

}

else

{

insert(data,pos);

}

}

//Define function to insert element in reverse sorted order

void ReverseSortedInsert(int data){

int pos=0;

Node *temp=head;

while(temp!=NULL && data<temp->data){

temp=temp->next;

pos++;

}

if(pos==0){

insertAtFront(data);

}else{

insert(data,pos);

}

}

//Define function to print the list elements

void print(){

Node*temp = head;

int count=0;

while(temp!=NULL){

if(count==10){

cout<<endl;

count=0;

}

cout<<temp->data<<"-->";

temp = temp->next;

count++;

}

cout<<endl;

}

//Destructor

~LinkedList(){

Node* temp = head;

while(temp!=NULL){

Node*next = temp->next;

delete temp;

temp = next;

}

head = NULL;

}

//Define function to check list is empty

bool isEmpty()

{

//Check condition

if(head==NULL)

//Return

return true;

//Return

return false;

}

//Define function to remove the element from front

int removeFirst()

{

//Set value

int retVal =0;

//Check condition

if (head == NULL)

//Return

return 0;

//else

else {

//Check condition

if (head == tail) {

//Set the return value

retVal = head->data;

//Set value

head = NULL;

//Update

tail = NULL;

}

//If more than 1 element

else {

//Set return value

retVal = head->data;

//Update

head = head->next;

}

}

//Return

return retVal;

}

//Define function to remove element from the last

int removeLast() {

//Set the return value

int retVal=0;

//Check condition

if (tail == NULL)

//Return

return 0;

//Else

else {

//Check only 1 element in list

if (head == tail) {

//Set the retun value

retVal = head->data;

//Update head

head = NULL;

//Update

tail = NULL;

}

//If more than 1 element

else {

                     

//Set value

retVal = tail->data;

//Declare

Node* pvTail = head;

//Loop

while (pvTail->next != tail)

//Next element

pvTail = pvTail->next;

//Set value

tail = pvTail;

//Set next

tail->next = NULL;

}

}

//Return the deleted element

return retVal;

}

};

//Define function to print array into the file object

void arrayPrint(int *arr,int n, ofstream &fp){

int count=0;

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

if(count==10){

fp<<endl;

count=0;

}

fp<<arr[i]<<" ";

count++;

}

fp<<endl<<endl;

}

//main

int main()

{

//Declare 4 arrays for inputs

int arr1[100];

int arr2[100];

int arr3[100];

int arr4[100];

//Declare 4 arrays for outputs

int *outArr1;

int *outArr2;

int *outArr3;

int *outArr4;

//Open output file

ofstream fID("output.txt");

//Create 4 stacks

LinkedList stack1,stack2,stack3,stack4;

//Create 4 queues

LinkedList queue1,queue2,queue3,queue4;

//Seed

srand(time(NULL));

//Loop to initialize all 4 arrays

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

//Generate random value for first array

arr1[i]=rand()%100;

//Generate random value for second array

arr2[i]=rand()%100;

//Generate random value for third array

arr3[i]=rand()%100;

//Generate random value for fourth array

arr4[i]=rand()%100;

}

//Loop to insert first 10 elements from input arrays

for(int kk=0;kk<10;kk++)

{

//Insert elements into stacks

stack1.insertAtFront(arr1[kk]);

stack2.insertAtFront(arr2[kk]);

stack3.insertAtFront(arr3[kk]);

stack4.insertAtFront(arr4[kk]);

//Insert elements into queues

queue1.insertAtEnd(arr1[kk]);

queue2.insertAtEnd(arr2[kk]);

queue3.insertAtEnd(arr3[kk]);

queue4.insertAtEnd(arr4[kk]);

}

//Initialize output arrays

outArr1 = new int[100];

outArr2 = new int[100];

outArr3 = new int[100];

outArr4 = new int[100];

//Set the index

int aa=0;

for(int kk=10;kk<100;kk++)

{

//Delete element from queues

int tp1 = queue1.removeFirst();

int tp2 = queue2.removeFirst();

int tp3 = queue3.removeFirst();

int tp4 = queue4.removeFirst();

//Store deleted element into output arrays

outArr1[aa] = tp1;

outArr2[aa] = tp2;

outArr3[aa] = tp3;

outArr4[aa] = tp4;

aa++;

//Insert elements into queues

queue1.insertAtEnd(arr1[kk]);

queue2.insertAtEnd(arr2[kk]);

queue3.insertAtEnd(arr3[kk]);

queue4.insertAtEnd(arr4[kk]);

}

//Loop to delete remaining elements from queue

while(!queue1.isEmpty())

{

//Delete element from queues

int tp1 = queue1.removeFirst();

int tp2 = queue2.removeFirst();

int tp3 = queue3.removeFirst();

int tp4 = queue4.removeFirst();

//Store deleted element into output arrays

outArr1[aa] = tp1;

outArr2[aa] = tp2;

outArr3[aa] = tp3;

outArr4[aa] = tp4;

aa++;

}

//Call function to print input arrays

fID<<"Array 1:"<<endl;

arrayPrint(arr1, 100, fID);

fID<<" Array 2:"<<endl;

arrayPrint(arr2, 100, fID);

fID<<" Array 3:"<<endl;

arrayPrint(arr3, 100, fID);

fID<<" Array 4:"<<endl;

arrayPrint(arr4, 100, fID);

//Call function to print output arrays.

fID<<" Output Array 1:"<<endl;

arrayPrint(outArr1, 100, fID);

fID<<" Output Array 2:"<<endl;

arrayPrint(outArr2, 100, fID);

fID<<" Output Array 3:"<<endl;

arrayPrint(outArr3, 100, fID);

fID<<" Output Array 4:"<<endl;

arrayPrint(outArr4, 100, fID);

//Close file

fID.close();

//Pause window

system("pause");

return 0;

}