Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Correct code and screen shot of the output. This is the code for singly linked list: #include #include //srand, rand #include //clock_t, clock, CLOCKS_PER_SEC #include

Correct code and screen shot of the output. image text in transcribed
This is the code for singly linked list:
#include
#include //srand, rand
#include //clock_t, clock, CLOCKS_PER_SEC
#include
#include
#include
using namespace std;
// implementing a doubly linked list
class Node{
private:
int data;
Node* nextNodePtr;
Node* prevNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return data;
}
void setNextNodePtr(Node* nodePtr){
nextNodePtr = nodePtr;
}
Node* getNextNodePtr(){
return nextNodePtr;
}
void setPrevNodePtr(Node* nodePtr){
prevNodePtr = nodePtr;
}
Node* getPrevNodePtr(){
return prevNodePtr;
}
};
class Stack{
private:
Node* headPtr;
Node* tailPtr;
public:
Stack(){
headPtr = new Node();
tailPtr = new Node();
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
Node* getHeadPtr(){
return headPtr;
}
Node* getTailPtr(){
return tailPtr;
}
bool isEmpty(){
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void push(int data){
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(0);
Node* lastNodePtr = tailPtr->getPrevNodePtr();
if (lastNodePtr == 0){
headPtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(0);
}
else{
lastNodePtr->setNextNodePtr(newNodePtr);
newNodePtr->setPrevNodePtr(lastNodePtr);
}
tailPtr->setPrevNodePtr(newNodePtr);
}
int pop(){
Node* lastNodePtr = tailPtr->getPrevNodePtr();
Node* prevNodePtr = 0;
int poppedData = -100000; //empty stack
if (lastNodePtr != 0){
prevNodePtr = lastNodePtr->getPrevNodePtr();
poppedData = lastNodePtr->getData();
}
else
return poppedData;
if (prevNodePtr != 0){
prevNodePtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(prevNodePtr);
}
else{
headPtr->setNextNodePtr(0);
tailPtr->setPrevNodePtr(0);
}
return poppedData;
}
int peek(){
Node* lastNodePtr = tailPtr->getPrevNodePtr();
if (lastNodePtr != 0)
return lastNodePtr->getData();
else
return -100000; // empty stack
}
void IterativePrint(){
Node* currentNodePtr = headPtr->getNextNodePtr();
while (currentNodePtr != 0){
cout getData()
currentNodePtr = currentNodePtr->getNextNodePtr();
}
cout
}
};
int main(){
int StackSize;
cout
cin >> StackSize;
int maxValue;
cout
cin >> maxValue;
int numTrials;
cout
cin >> numTrials;
srand(time(NULL));
//cout
using namespace std::chrono;
double totalPushingTime = 0;
double totalPoppingTime = 0;
for (int trials = 1; trials
Stack integerStack; // Create an empty stack
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i = 0; i
int value = 1 + rand() % maxValue;
//cout
integerStack.push(value);
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration pushingTime_micro = t2 - t1;
totalPushingTime += pushingTime_micro.count();
//cout
//cout
//integerStack.PrintToptoBottom();
// to read an element at a particular index (before delete)
//cout
t1 = high_resolution_clock::now();
while (!integerStack.isEmpty())
integerStack.pop();
t2 = high_resolution_clock::now();
duration poppingTime_micro = t2 - t1;
totalPoppingTime += poppingTime_micro.count();
}// trials
cout
cout
//cout
return 0;
}
Implement Stack ADT as a Singly Linked List without using the insert AtIndex, deleteElement, readlndex functions of the Singly Listed List. That is, the push, pop and peek operations should not call insertAtIndex(0, data), deleteElement(0) and readlndex(0) functions. The push, pop and peek operations should be directly implemented to insert an element in the beginning of the linked list, to delete an element from the beginning of the linked list and to read the element value from the beginning of the linked list. To help you out, the implementation of the push function is given in the startup code provided. Your task is to implement the peek and pop functions like this without calling any other function. You can notice that the insertAtIndex, deleteElement and readIndex functions have been removed from the Stack class and you should not use them. After implementing the pop and peek functions, you will be comparing the actual run-time of the push and pop operations of a Stack implemented as a Singly Linked List (with insertions and deletions in the beginning of the Linked List) with that of a Stack implemented as a Doubly Linked List (with insertions and deletions at the tail/end of the Linked List). The main function provided to you has the timers setup for this purpose. Your task is to just run the main functions and measure the average time taken (in microseconds) for the push and pop operations with the Stack as a Singly Linked List and with the Stack as a Doubly Linked List for the following values of the parameters: (i) # elements to be pushed = 1000. 1(000. I (XXXX), 10(XXXX); (ii) inainiu m value for any element-50000 and (iii) # trials-50 You need to include the following as part of your answer to this question: 0) the code for the Singly Linked List-based implementation of the Stack class (ii) a table presenting the actual run-times for the parameters mentioned abovie (ii) snapshots of the actual run-times for thhe above cases (iv) interprctation of the difference/similarity in the actual run-times for the push and pop operations between the Singly Linked List and Doubly Linked List implementation of the Stack ADT

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

Students also viewed these Databases questions

Question

How to use a circuit to create a boelean expression

Answered: 1 week ago

Question

=+f. Audience Engagement encourage consumer participation.

Answered: 1 week ago

Question

=+d. Emotional Approach appeal to consumers' emotions.

Answered: 1 week ago