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

Implement the same Doublestack functionality using a linked list instead of an a

ID: 441659 • Letter: I

Question

Implement the same Doublestack functionality using a linked list instead of an array. The push method will add a new link at the head of the list, and pop will remove the item at the of list. You can use two classes that work together: class link a single node in the linked list public: double data; the data stored in this link Link* next; the next link in the chain Link(double , Link*n) constructor copy parameters to data members class Doublestack public: same methods as stack based class private: Link * first; pointer to head of list int capacity; max number of links allowed int size; how many links in the list To add an new double stored in x to the head of the list you can do: first = new link (x, first); You should also check = before you do this that capacity is not exceeded, and remember to increment the size data Removing the first in the list is almost as easy: first = first next; moves the first pointer to the next link. Of course your cannot o this is first is null, and before your do the statement above you need to remember the double stored in the first link so you return it, an your need to remember a pointer to the first link so you can delete it.

Explanation / Answer

#include<iostream.h>
#include<stdlib.h>
class stack
{
int element;
stack* next;
public:
stack* push(stack*,int);
stack* pop(stack*);
void stack_display(stack*);
}*head,object;
stack* stack::push(stack* head,int key)
{
stack* temp,*temp1;
temp1=head;
temp=new stack;
temp->element=key;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
while(head->next!=NULL)
head=head->next;
head->next=temp;
head=temp1;
}
return head;
}
stack* stack::pop(stack* head)
{
stack* temp;
if(head!=NULL)
{
temp=head;
if(head->next==NULL)
{
cout<<" the pooped element from the stack is: "<<head->element<<endl;
return NULL;
}
while(head->next->next!=NULL)
head=head->next;
cout<<"the popped element from the stack is "<<head->next->element;
cout<<endl;
head->next=head->next->next;
head=temp;
return head;
}
else
{
cout<<" it is impossible to pop an element from the stack as ";
return head;
}
}
void stack::stack_display(stack* head)
{
if(head!=NULL)
{
while(head->next!=NULL)
{
cout<<head->element<<"->";
head=head->next;
}
cout<<head->element;
cout<<endl;
}
else
cout<<"the stack is empty ";
}
void choice()
{
int key,ch;
cout<<" choose the operation to be performed ";
cout<<" 1.push 2.pop 3.exit ";
cin>>ch;
head=NULL;
while(1)
{
switch(ch)
{
case 1:

cout<<"