Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can I get all the answer to each part of this question? This is the code for singly linked list-based code: #include #include #include #include
Can I get all the answer to each part of this question?
This is the code for singly linked list-based code:
#include
#include
#include
#include
#include
#include
using namespace std;
// implementing the dynamic List ADT using Linked List
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return data;
}
void setNextNodePtr(Node* nodePtr){
nextNodePtr = nodePtr;
}
Node* getNextNodePtr(){
return nextNodePtr;
}
};
class Stack{
private:
Node *headPtr;
public:
Stack(){
headPtr = new Node();
headPtr->setNextNodePtr(0);
}
Node* getHeadPtr(){
return headPtr;
}
bool isEmpty(){
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void push(int data){
// This is the direct implementation of the push function without
// calling the insertAtIndex(0, data) function
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(currentNodePtr);
headPtr->setNextNodePtr(newNodePtr);
}
int peek(){
// Implement the peek function directly without calling the readElement function
}
int pop(){
// Implement the pop function directly without calling the deleteElement function
}
};
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;
}
This is the doubly linked list code:
#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
Implement Stack ADT as a Singly Linked List without using the insertAtlndex, deleteElement, readIndex 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 link 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 Stac as a Doubly Linked List for the following values of the parameters: (i) # elements to be pushed= 1000, 10000. I (XXXX), 1()(XXXX); (ii) maximum value for any element 50(00 and (iii) # trials-50. ed list, to delete an You need to include the following as part of your answer to this question (i) 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 ahove ii) snapshots of the actual run-times for the above cases. iv) interprctiation of the difference/similarity in the actual run-times for the push and pop operations bet ween the Singly Linked List and Doubly Linked List implementation of the Stack ADT //cout
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started