Use C++ Plz 3. Sorting. Implement a generic Stack linked list data structure for
ID: 3752495 • Letter: U
Question
Use C++ Plz
3. Sorting. Implement a generic Stack linked list data structure for integers that supports these iin: a. i InsertionSort(), which runs an insertion sort algorithm on the stack Your insertionSort() algorithm should print to the console every time it makes a swap. Print the operation performed ("Swapped a with b") then on the next line print the current contents of the array. Print to the console when the sort has finished("Sort finished!") 1. ii. iii. iv. 2. Push(int data), which pushes values to the head of the stack Pop():, which pops values from the head of the stack and returns it Peek(), which returns a value from the head of the stackExplanation / Answer
#include <bits/stdc++.h>
using namespace std;
struct node
{
double val;
struct node * nxt;
};
class Stack
{
private:
struct node *headd;
public:
Stack ();
~Stack ();
void push(double element);
double pop ();
int isEmpty();
int numOfElements();
void printELements();
};
Stack::Stack ()
{
headd = NULL;
}
Stack::~Stack ()
{
node *temp = headd;
node *nxt;
while (temp != NULL)
{
nxt = temp->nxt;
free (temp);
temp = nxt;
}
headd = NULL;
}
void Stack::push (double val)
{
int countr = 0;
node *iter = headd;
while (iter != NULL)
{
countr++;
iter = iter->nxt;
}
if (countr >=30 )
{
cout<<"Stack is full"<<endl;
return;
}
node *new_Obj;
new_Obj = new node;
new_Obj -> val = val;
new_Obj->nxt = headd;
headd = new_Obj;
return ;
}
double Stack::pop ()
{
if (headd==NULL)
return NULL;
else{
double val = headd -> val;
headd = headd -> nxt;
return val;
}
}
int Stack::isEmpty ()
{
if (headd == NULL){
return 1;
}
else{
return false;
}
}
int Stack::numOfElements ()
{
int countr = 0;
node *iter = headd;
while (iter != NULL){
countr++;
iter = iter->nxt;
}
return countr;
}
void Stack::printELements ()
{
node *iter = headd;
cout<<"Printing the elements of the Stack:"<<endl;
while (iter != NULL){
cout<< (iter->val) <<endl;
iter = iter->nxt;
}
return;
}
int main (){
Stack *stk = new Stack ();
cout<<"Stack is empty or not: "<<( ((stk->isEmpty())==1? "Empty" : "Not Empty") )<<endl;
cout<<endl;
stk -> push(3.5);
stk -> push(4.5);
stk -> push(5);
stk -> push(6.5);
stk -> push(1.5);
stk -> push(2.3);
stk -> push(8.9);
stk -> push(10.2);
cout<<"Stack is empty or not: "<<( ((stk->isEmpty())==1? "Empty" : "Not Empty") )<<endl;
cout<<endl;
cout<<"Number of elemnts in the Stack: "<<(stk->numOfElements())<<endl;
cout<<endl;
stk -> printELements();
cout<<endl;
cout<<"Popping Elements from the stk:"<<endl;
cout<<(stk->pop())<<endl;
cout<<(stk->pop())<<endl;
cout<<endl;
stk -> push(11.3);
cout<<"Stack is empty or not: "<<( ((stk->isEmpty())==1? "Empty" : "Not Empty") )<<endl;
cout<<endl;
cout<<"Number of elemnts in the Stack: "<<(stk->numOfElements())<<endl;
cout<<endl;
stk -> printELements();
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.