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

Create a custom linked list of ShippingBox class The linked list should have fol

ID: 3740421 • Letter: C

Question

Create a custom linked list of ShippingBox class The linked list should have following operations object with attributes: Address, Weight, and isDelivered 1. InsertAtHead 2. InsertAtEnd 3. InsertAtPosition 4. DeleteAtHead 5. DeleteAtEnd 6. DeleteAtPosition AddressDelto the node with the shipping box with the same address as in the argument 8. Search 9. UpdateAtPosition: Update all the values (address, weight, if delivered etc) at that position 10. UpdateByValue(Address): Update the address, weight, if Delivered for the node with the same address as provided in the argument 11. Print

Explanation / Answer

Please upvote if satisfied :)

#include <iostream>
using namespace std;

// ShippingBox class
class ShippingBox {
float weight;
string address;
bool isDelivered;
ShippingBox* next;

public:
ShippingBox() {};
//set data in object
void setData(float weight_,string address_,bool isDelivered_)
{ weight=weight_;
address=address_;
isDelivered=isDelivered_;
  
};
  
//getter for Weight
float getWeight()
{
return weight;
  
};
  
//getter for Address
string getAddress()
{
return address;
  
};
//getter for isDelieved
bool getIsDelivered()
{
return isDelivered;
  
};
  
//set next node
void setNextNode(ShippingBox* next_) { next = next_; };
  
//get data
void printData() { //return this;
cout<<"Weight : "<<weight<< " Address: "<<address<<" Delivered:"<<(isDelivered==true?"Yes":"No");
};
  
//get next data
ShippingBox* getNextNode() { return next; };
};

// List class
class List {
ShippingBox *head;
public:
List() { head = NULL; };
  
void InsertAtHead(float weight_,string address_);
void InsertAtHead(float weight_,string address_,bool isDelivered_);
  
void InsertAtEnd(float weight_,string address_);
void InsertAtEnd(float weight_,string address_,bool isDelivered_);
  
int InsertAtPosition(int i,float weight_,string address_);
int InsertAtPosition(int i,float weight_,string address_,bool isDelivered_);
  
  
void DeleteAtHead();
void DeleteAtEnd();
void DeleteAtPosition(int i);
void DeleteByValue(string address_);
int Search(float weight_,string address_,bool isDelivered_);
void UpdateAtPosition(int position,float weight_,string address_, bool isDelivered_);
void UpdateByValue(string address_,float newW,string newAdd,bool newIsDelivered);
void Print();

};

/**
* Print all the shipping boxes in the list
*/
void List::Print() {

// Let there be a temporary pointer
ShippingBox *tmp = head;

// If there are no nodes in the list, that is the list is empty - we print "Empty"
if ( tmp == NULL ) {
cout << "Empty" << endl;
return;
}

// If there is only one node in the list, then we print that without looping through the list
if ( tmp->getNextNode() == NULL ) {
tmp->printData();
cout << " --> "<< endl;
cout << "End" << endl;
}
else {
// Loop through the list and print all details
do {
tmp->printData();
cout << " --> "<< endl;
tmp = tmp->getNextNode();
}
while ( tmp != NULL );

cout << "End" << endl;
}
}

/**
* Insert a node at the end to the linked list
*/
void List::InsertAtEnd(float weight_,string address_) {
  
InsertAtEnd(weight_,address_, false);

}
void List::InsertAtEnd(float weight_,string address_, bool isDelivered_) {

// Create a new node
ShippingBox* newNode = new ShippingBox();
newNode->setData(weight_,address_,isDelivered_);
newNode->setNextNode(NULL);

// Let there be a temporary pointer
ShippingBox *tmp = head;

if ( tmp != NULL ) {
// If the next node to the head node if not null, it means that there are node present in the list
// therefore looping to the end of the list
while ( tmp->getNextNode() != NULL ) {
tmp = tmp->getNextNode();
}

// Add the new node to the end of the list when we reach there
tmp->setNextNode(newNode);
}
else {
// else there are no nodes in the list, so adding the new node as the head of the list
head = newNode;
}
}
/**
* Insert a node at the head of the linked list
*/
void List::InsertAtHead(float weight_,string address_) {

InsertAtHead(weight_,address_,false);

}
/**
* Insert a node at the head of the linked list
*/
void List::InsertAtHead(float weight_,string address_,bool isDelivered_){
// Create a new node
ShippingBox* newNode = new ShippingBox();
newNode->setData(weight_,address_,isDelivered_);
newNode->setNextNode(head);

if(head==NULL)
{
head=newNode;
return;
}
// Let there be a temporary pointer
ShippingBox *tmp = head->getNextNode();

head=newNode;
}
/**
* Insert a node at a position of the linked list
*/
int List::InsertAtPosition(int i,float weight_,string address_) {

return InsertAtPosition(i,weight_,address_, false);
}
/**
* Insert a node at a position of the linked list
*/
int List::InsertAtPosition(int i,float weight_,string address_,bool isDelivered_) {

// Create a new node
ShippingBox* newNode = new ShippingBox();
newNode->setData(weight_,address_,isDelivered_);
newNode->setNextNode(NULL);

// Let there be a temporary pointer
ShippingBox *tmp = head;
ShippingBox *prev = NULL;
int position=1;

if ( tmp != NULL ) {
// If the next node to the head node if not null, it means that there are node present in the list
// therefore looping to the positio i in the list
while ( tmp->getNextNode() != NULL ) {
  
  
if(position==i)
{

//newNode.setNextNode();
  
prev->setNextNode(newNode);
  
// Add the new node to position in the list
// if(tmp->getNextNode()!=NULL)
newNode->setNextNode(tmp);
  
return i;

}
else{
prev=tmp;
tmp = tmp->getNextNode();
position++;
// cout<<position<<endl;
}

}


}
else {
// else there are no nodes in the list, so adding the new node as the head of the list
head = newNode;
return 1;
}
//the position is after the last node

// position++;
//cout<<i<<position;
if(i==(position+1) || i==(position))
{
tmp->setNextNode(newNode);
return position;
}
else if(i>=(position+2))
{
//when the position is greater than the number of nodes
cout<<"Position does not exist"<<endl;
return -1;
}
}
/**
* Delete a node from the head of linked list
*/
void List::DeleteAtHead() {

// Let there be a temporary pointer
ShippingBox *tmp = head->getNextNode();

if(tmp->getNextNode()==NULL)
{
delete head;
head=NULL;
}
else
{
delete head;
head=tmp;
}
  

}
/**
* Delete a node from the end of linked list
*/
void List::DeleteAtEnd() {

// Let there be a temporary pointer
ShippingBox *tmp = head;

if ( tmp->getNextNode() != NULL ) {
while ( tmp->getNextNode()->getNextNode() != NULL ) {
tmp = tmp->getNextNode();
}

ShippingBox *deletedNode=tmp->getNextNode();
delete deletedNode;
tmp->setNextNode(NULL);
}
else if ( tmp->getNextNode() == NULL ) {
  
  
delete tmp;
head = NULL;
}
}
/**
* Delete a node at a position of linked list
*/
void List::DeleteAtPosition(int position) {


// If linked list is empty
if (head == NULL)
{
cout<<"Empty";
return;
  
}

// Let there be a temporary pointer
struct ShippingBox* tmp = head;
struct ShippingBox* prev = head;

// If head needs to be removed
if (position == 1)
{
head = tmp->getNextNode(); // Change head
delete tmp; // delete old head
return;
}

// Find previous node of the node to be deleted
for (int i=1; (tmp!=NULL && i<position); i++)
{
prev=tmp;
tmp = tmp->getNextNode();
}

// If position is more than number of ndoes
if (tmp == NULL || tmp->getNextNode() == NULL)
return;

// ShippingBox temp->next is the node to be deleted
// Store pointer to the next of node to be deleted
struct ShippingBox *next = tmp->getNextNode();

// Unlink the node from linked list
delete tmp;

prev->setNextNode(next); // Unlink the deleted node from list


}
/**
* Search for a node in a linked list
*/
int List::Search(float weight_,string address_,bool isDelivered_){
  


// Let there be a temporary pointer
ShippingBox *tmp = head;
int i=1;

if ( tmp != NULL ) {
// If the next node to the head node if not null, it means that there are node present in the list
// therefore looping to the end of the list
while ( tmp->getNextNode() != NULL ) {
if(tmp->getWeight()==weight_ && tmp->getAddress()==address_ && tmp->getIsDelivered() == isDelivered_ )
{
cout<<"Found at:"<<i<<endl;
tmp->printData();
cout<<endl;
return i;
}
tmp = tmp->getNextNode();
i++;
}
if(tmp->getWeight()==weight_ && tmp->getAddress()==address_ && tmp->getIsDelivered() == isDelivered_ )
{
cout<<"Found at:"<<i<<endl;
tmp->printData();
cout<<endl;
return i;
}

}
else {
// else there are no nodes in the list, so adding the new node as the head of the list
if(head->getWeight()==weight_ && tmp->getAddress()==address_ && tmp->getIsDelivered() == isDelivered_ )
{
cout<<"Found at 1:"<<endl;
head->printData();
cout<<endl;
return 1;
}
}
  
cout<<"Not Found";
return -1;
}
/**
* Update a node at a position of linked list
*/
void List::UpdateAtPosition(int i,float weight_,string address_,bool isDelivered_) {

int pos=InsertAtPosition(i,weight_,address_,isDelivered_);
if(pos==i)
DeleteAtPosition(i-1);
else
DeleteAtPosition(i+1);
  
}
/**
* Update a node by value of linked list
*/
void List::UpdateByValue(string address_,float newW,string newAdd,bool newIsDelivered){
  
// Let there be a temporary pointer
ShippingBox *tmp = head;
int i=1;

if ( tmp != NULL ) {
// If the next node to the head node if not null, it means that there are node present in the list
// therefore looping to the end of the list
while ( tmp->getNextNode() != NULL ) {
if( tmp->getAddress()==address_ )
{
cout<<"Found at:"<<i<<endl;
tmp->setData(newW,newAdd,newIsDelivered);
tmp->printData();
cout<<endl;
return;
}
tmp = tmp->getNextNode();
i++;
}

}
else {
// else there are no nodes in the list, so adding the new node as the head of the list
if( tmp->getAddress()==address_ )
{
cout<<"Found at :1"<<i<<endl;
tmp->setData(newW,newAdd,newIsDelivered);
tmp->printData();
cout<<endl;
return;
}
}
  
cout<<"Not Found";
return;
}
/**
* Delete a node by value of linked list
*/
void List::DeleteByValue(string address_){
  
// Let there be a temporary pointer
ShippingBox *tmp = head;
ShippingBox *prev = NULL;
int i=1;

if ( tmp != NULL ) {
// If the next node to the head node if not null, it means that there are node present in the list
// therefore looping to the end of the list
while ( tmp->getNextNode() != NULL ) {
if( tmp->getAddress()==address_ )
{
cout<<"Found at:"<<i<<endl;
//tmp->printData();
if(prev!=NULL)
{
prev->setNextNode(tmp->getNextNode());
cout<<endl;
delete tmp;
return;
}
else
{
tmp=head->getNextNode();
delete head;
head=tmp;
return;
}
  
  
}
prev=tmp;
tmp = tmp->getNextNode();
i++;
}

}
else {
// else there are no nodes in the list, so adding the new node as the head of the list
cout<<"Not Found";
return;
}
  
  
}

/**
* Main method to test
*/
int main()
{
// New list
List list;

list.InsertAtHead(1,"Address 1");
list.InsertAtHead(2.1,"Address 2", false);
list.InsertAtHead(1.3,"Address 3", true);
  
cout<<"After adding 3 values at the head:"<<endl;
list.Print();
  
//insert 2 values at the end;
list.InsertAtEnd(3.1,"Address 4");
list.InsertAtEnd(2.1,"Address 5", false);
cout<<"After adding 2 values at the end:"<<endl;
list.Print();
  
//adding 2 values at position 2 and 3
list.InsertAtPosition(2,1.5,"Address 6");
list.InsertAtPosition(3, 2.3,"Address 7", false);
cout<<"After adding 2 values at position 2 and 3:"<<endl;
list.Print();
  
  
//deleting 2 values from head
list.DeleteAtHead();
list.DeleteAtHead();
cout<<"After deleting 2 values from head:"<<endl;
list.Print();
  
  
//deleting 2 values from the end
list.DeleteAtEnd();
list.DeleteAtEnd();
cout<<"After deleting 2 values from the end:"<<endl;
list.Print();
  
//deleting 1 values at position 2
list.DeleteAtPosition(2);
cout<<"After deleting 1 values at position 2:"<<endl;
list.Print();
  
//adding some more values
list.InsertAtPosition(3, 2.8,"Address 8", false);
list.InsertAtPosition(3, 2.4,"Address 9", true);
cout<<"After adding some more values:"<<endl;
list.Print();
  
//searching
cout<<"Searching:"<<endl;
list.Search(2.8,"Address 8", false);
  
//updating
list.UpdateAtPosition(4,7,"Address 7", t);
cout<<"After updating values at 3:"<<endl;
list.Print();
  
  
//updating by value
list.UpdateByValue("Address 9",3,"Address 10", t);
cout<<"After updating:"<<endl;
list.Print();
  
//deleteing by value
list.DeleteByValue("Address 7");
cout<<"After deleting:"<<endl;
list.Print();
  
  

}

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