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

You are now going to create a LinkedList class, that will work very similarly to

ID: 3686853 • Letter: Y

Question

You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows:

add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last link of the list always set the next pointer to be NULL in order to mark the end of the list.

print (): this method moves through the entire list printing out each integer value stored in the links. cleanup (): updated cleanup function that will automatically delete all links in the linked list, including the data stored on each link

Stack Class used:

#ifndef STACK_H
#define STACK_H

#include <iostream>

struct Stack {
   struct Link {
       void* data;
       Link* next;
      
       void initialize(void* dat, Link* nxt){
           data = dat;
           next = nxt;
       }
   }* head;
  
   void initialize(){
       head = 0;
   }
  
   void push(void* dat){
       Link* newLink = new Link;
       newLink->initialize(dat, head);
       head = newLink;
   }
  
   void* peek(){
       if (head == 0){
           std::cout << "Stack is empty";
       }
       return head->data;
   }
  
   void* pop(){
       if(head == 0)
           return 0;
      
       void* result = head->data;
       Link* oldHead = head;
       head = head->next;
       delete oldHead;
       return result;
   }
  
   void cleanup(){
       if (head == 0){
           std::cout << "Stack is empty";
       }
       else {
           std::cout << "Stack is not empty";
       }
   }
};
#endif

Explanation / Answer


Sample code logic to create linked list similar to stack

#include<iostream>
using namespace std;


// Creating a NODE Structure
struct node
{
int data;
struct node *next;
};

// Creating a class STACK
class stack
{
struct node *top;
public:
stack() // constructure
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"nPUSH Operationn";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"nNew item is inserted to the stack!!!";

}

// POP Operation
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"nPOP Operation........nPoped value is "<<temp->data;
delete temp;
}

// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"nThe stack isn";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULLn";
}

// Main function
int main()
{
stack s;
int choice;
while(1)
{
cout<<"n-----------------------------------------------------------";
cout<<"nttSTACK USING LINKED LISTnn";
cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";
cout<<"nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
cout<<"Please enter correct choice(1-4)!!";
break;
}
}
return 0;
}

sample logic to clear linked list
/* Function to delete the entire linked list */
void deleteList(struct node** head_ref)
{
/* deref head_ref to get the real head */
struct node* current = *head_ref;
struct node* next;

while (current != NULL)
{
next = current->next;
free(current);
current = next;
}

/* deref head_ref to affect the real head back
in the caller. */
*head_ref = NULL;
}

note-by using the above code samples with required modification the given question can be answered.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote