In: Computer Science
Flow this Code to answer question below
#include <iostream>
using std::cout;
struct Node {
int data;
Node *link;
Node(int data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs
this->data = data;
link = p;
}
};
class linked_list
{
private:
Node *head,*current;
public:
//constructor
linked_list() {
head = nullptr;//the head pointer
current = nullptr;//acts as the tail of the list
}
//destructor - IMPORTANT
~linked_list() {
current = head;
while( current != nullptr ) {//the loop stops when both current and head are NULL
head = head->link;
delete current;
current = head;
}
}
void addLast(int n) {// to add a node at the end of the list
if(head == nullptr){
head = new Node(n);
current = head;
} else {
if( current->link != nullptr)
current = current->link;
current->link = new Node(n);
current = current->link;//You may decide whether to keep this line or not for the function
}
}
void print() { // to display all nodes in the list
Node *tmp = head;
while (tmp != nullptr) {
cout << tmp->data << std::endl;
tmp = tmp->link;
}
}
};
int main() {
linked_list a;
a.addLast(1);
a.addLast(2);
a.print();
return 0;
}
Question:
Create the node structure and the linked list class. The sample class has implemented the following member methods:
o constructor
o destructor
o print(),
o addLast()
2. Implement the following member methods:
▪ addFirst (T data) // Adds a node with data at the beginning of the list
▪ pop() // Removes the first node of the list. Note: Don't forget to delete/reallocate the removed dynamic memory
▪ contains(T data) // Returns true or false if a node contains the data exists in the list
▪ update(T oldDate, T newData) // Updates the oldData of a node in the list with newData.
▪ size() // Returns the number of nodes in the list
▪ remove( T data) //Removes the node that contains the data. Note, you will need to check if the node exists. Again, don't forget to delete/re-allocate the dynamic memory
3. (Extra Credit Part) Implement the following additional member methods
▪ insertAfter(int n, T data) //Adds a node after the n-th node. Note, you will need to check if the n th node exists, if not, do addLast().
▪ merge(const LinkedList &linkedlist) //Merges this object with linkedlist object. In other words, add all nodes in linkedlist to this object.
#include <iostream>
#include <cstddef>
using std::cout;
template <typename T>
struct Node {
T data;
Node *link;
Node(T data)
{
this->data = data;
link = NULL;
}
Node(T data, Node *p ) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs
this->data = data;
link = p;
}
};
template <typename T>
class linked_list
{
Node<T> *head,*current;
public:
//default constructor
linked_list() {
head = NULL;//the head pointer
current = NULL;//acts as the tail of the list
}
//destructor - IMPORTANT
~linked_list() {
current = head;
while( current != NULL ) {//the loop stops when both current and head are NULL
head = head->link;
delete current;
current = head;
}
}
void addLast(int n) {// to add a node at the end of the list
if(head == NULL){
head = new Node<T>(n);
current = head;
} else {
if( current->link != NULL)
current = current->link;
current->link = new Node<T>(n);
current = current->link;//You may decide whether to keep this line or not for the function
}
}
void print() { // to display all nodes in the list
Node<T> *tmp = head;
while (tmp != NULL) {
cout << tmp->data << "\n";
tmp = tmp->link;
}
}
// addFirst Method
void addFirst(T data)
{
Node<T> *temp=new Node<T>(data);
temp->link=head;
head=temp;
if(current==NULL)
current=head;
}
// pop
T pop()
{
if(head==NULL)
return NULL;
Node<T> *temp=head;
head=head->link;
if(head==NULL) // if there is now node remaining
current=NULL;
T data=temp->data;
delete temp;
return data;
}
// contains()
bool contains(T data)
{
Node<T> *temp=head;
while(temp!=NULL)
{
if(temp->data==data)
return true;
temp=temp->link;
}
return false;
}
// update()
void update(T oldData,T newData){
Node<T> *temp=head;
while(temp!=NULL)
{
if(temp->data==oldData)
{
temp->data=newData;
break;
}
temp=temp->next;
}
}
// size()
int size()
{
Node<T> *temp=head;
int count=0;
while(temp!=NULL)
{
count++;
temp=temp->link;
}
return count;
}
// remove method
void remove(T data)
{
if(contains(data))
{
Node<T> *temp=head,*pre=NULL;
while(temp!=NULL)
{
if(temp->data==data)
break;
pre=temp;
temp=temp->link;
}
if(pre==NULL) // remove head
pop();
else
{
pre->link=temp->link;
delete temp;
}
}
}
//insert after n
void insertAfter(int n,T data)
{
Node<T> *node=new Node<T>(data);
if(size()<=n)
addLast(data);
else
{
Node<T> *temp=head,*pre;
while(n>0)
{
n--;
pre=temp;
temp=temp->link;
}
pre->link=node;
node->link=temp;
}
}
void merge(const linked_list &linkedlist)
{
// first list is empty
if(head==NULL)
head=linkedlist.head;
else
{
Node<T> *temp=head;
while(temp->link!=NULL)
temp=temp->link;
temp->link=linkedlist.head;
}
}
};
int main() {
linked_list<int> a;
a.addLast(1);
a.addLast(2);
cout<<"\nList is : \n";
a.print();
a.addFirst(3);
cout<<"\nList after addFirst() is : \n ";
a.print();
a.pop();
cout<<"\nList after pop() is : \n";
a.print();
cout<<"\nSize of List is : "<<a.size();
a.insertAfter(2,5);
cout<<"\nList after insertAfter() is : \n";
a.print();
a.remove(5);
cout<<"\nList after remove(5) is : \n";
a.print();
cout<<"\nIs List contains(5) : "<<a.contains(5);
linked_list<int> b;
b.addLast(10);
b.addLast(20);
cout<<"List b is:\n";
b.print();
a.merge(b);
cout<<"list after merging a and b is :\n";
a.print();
return 0;
}
I tested and the output is
List is :
1
2
List after addFirst() is :
3
1
2
List after pop() is :
1
2
Size of List is : 2
List after insertAfter() is :
1
2
5
List after remove(5) is :
1
2
5
List after remove(5) is :
1
2
Is List contains(5) : 0List b is:
10
20
list after merging a and b is :
1
2
10
20
Hope this is helpful
Please please upvote if helpful and please dont downvote please