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

Using C++, create a singly linked list having 5 nodes when instantiated. Create

ID: 3903619 • Letter: U

Question

Using C++, create a singly linked list having 5 nodes when instantiated. Create the following methods for your linked list:

1) InsertBeginning(): This function will accept a value from the user, which will be used to create a new node at the beginning of the linked list.

2) InsertEnd(): This function will accept a value from the user, which will be used to create a new node at the end of the linked list.

3) Insert(): This function will accept a value and a location from the user, and will then add a node with the provided value in the location specified by the user.

4) DeleteBeginning(): This function will delete the first node of the linked list.

5) DeleteEnd(): This function will delete the last node of the linked list.

6) Delete(): This function will take a user input specifying which node to delete.

7) View(): This function will display the linked list.

This program should be menu driven, and once it is run, it will first display the initial linked list, and then ask the user for his/her option. The options will be the functions you are going to implement. The menu should be in a loop and will only end, if the user presses the option to terminate the program. After each operation is performed, the resulting linked list will be displayed to the user, before asking for his/her option again. Remember to consider every possible outcome of the program, and handle it accordingly.

Please include a SinglyLinkedList.hpp, SinglyLinkedList.cpp, and main.cpp.

Please display code from the compiler and the output. Thank you!

Explanation / Answer

Below is your code.Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments

SinglyLinkedList.hpp

#include<iostream>

using namespace std;

class Node{

public:

int data;

Node *next;

};

class SinglyLinkedList{

private :

Node *head, *tail;

public:

SinglyLinkedList();

void insertBeginning(int data);

void insertEnd(int data);

void deleteBeginning();

void deleteEnd();

void delete_(int pos);

void view();

};

SinglyLinkedList.cpp

#include<iostream>

#include "SinglyLinkedList.hpp"

using namespace std;

SinglyLinkedList::SinglyLinkedList()

{

head=NULL;

tail=NULL;

}

void SinglyLinkedList::deleteBeginning()

{

if(head!=NULL)

{

Node *temp=head;

head=head->next;

if(head==NULL)

tail=NULL;

temp->next=NULL;

delete temp;

}

}

void SinglyLinkedList::deleteEnd()

{

Node *trav=head;

if(trav==NULL)

return;

if(trav->next==NULL)

{

head=NULL;

tail=NULL;

return;

}

while(trav->next->next!=NULL)

{

trav=trav->next;

}

Node *temp=trav->next;

trav->next=NULL;

delete temp;

}

void SinglyLinkedList::insertEnd(int data)

{

Node *node=new Node();

node->data=data;

node->next=NULL;

if(head==NULL)

{

head=node;

tail=node;

}

else{

tail->next=node;

tail=node;

}

}

void SinglyLinkedList::insertBeginning(int data)

{

Node *node=new Node();

node->data=data;

node->next=NULL;

if(head==NULL)

{

head=node;

tail=node;

}

else{

node->next=head;

head=node;

}

}

void SinglyLinkedList::delete_(int pos)

{

if(head==NULL)

return ;

if(pos<=0)

return;

if(pos==1)

{

head=head->next;

if(head==NULL)

tail=NULL;

}

else{

int curPos=1;

Node *trav=head;

while(trav!=NULL && curPos<pos-1)

{

trav=trav->next;

curPos++;

}

if(trav==NULL)

return;

//else

Node *temp=trav->next;

if(trav->next==NULL)

return;

trav->next=temp->next;

delete temp;

}

}

void SinglyLinkedList::view()

{

Node *trav=head;

cout<<"List -> ";

while(trav!=NULL)

{

cout<<trav->data;

if(trav->next!=NULL)

cout<<" --> ";

trav=trav->next;

}

cout<<endl;

}

main.cpp

#include<iostream>

#include "SinglyLinkedList.hpp"

using namespace std;

int main()

{

SinglyLinkedList *list=new SinglyLinkedList();

list->insertBeginning(1);

list->insertEnd(2);

list->insertEnd(3);

list->insertEnd(4);

list->insertEnd(5);

list->view();

list->deleteEnd();

list->deleteBeginning();

list->view();

list->delete_(3);

list->view();

cin.get();

return 0;

}

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