We studies Pointer Based Linked List and it’s Class implementation.

Question

We studies Pointer Based Linked List and it’s Class implementation.
In this assignment, you are asked to modify the pointer Based Linked List Class program by implementing and adding the following two functions.
⦁ Re Erse function/method
Implement a Reverse function/method that takes an instance(object) of a Linked class A as an input; it then creates a copy of the reverse of the link list object A, say instance (object) B. Object B will have a linked list where the linked in A is listed in Reverse order.
2.Replicate function/method
Replicate adds new linked to list so that linked are replicate the number of times specific by parameter count. For example, if linked list data member has the following values
“1”,”5”,”7”)
Then the call Replicate (3) should update the link list as shown below.
(“1”,”1”,”1”,”5”,”5”,”5”,”7”,”7”,”7”)
That is, you are replicating each linked by the number specified in call.

Things to do :

  • Create a linked list object, say A, using a for loop as shown in class.
  • Print this via PrintListFunction.
  • Call Reverse function and print the reverse list in object, say B.
  • Call Replicate with a value, say 3, and print the modified linked list in object A.

Note: You must all to Reverse function. Replicate in bonus 100 points. So if you do both you will get 200 points, otherwise 100.

Summary

Here we created two functions naming as reverse and Replicate. The replicate function will work as it will replicate the element which will pass to the function to the ‘n’ times. Next, we have the additional function reverse which will work as it will return a list in which there are the elements of the real list which will be in the reverse order. This list will be different from the original list.

Explanation

Basically here we take the class name as a linked. Which will have every element I the list. Next, we have a function that will take the data and will put it into the nude which will be at the end of the list. Another we use a function that is called reverse which will work as it will have all the data in the list and will put it into the array and will take the data from the end of the list and array put into a new list which is called as a linked list and show that on the screen.

Next, we have a function call as replicate which is used for the passing of parameter we will pass the ‘’n’ . ‘’n’ is the number that how many times the list will be replicated. Once the list is replicated it will then display on the screen.

Code

#include <iostream>
using namespace std;
/* to execute the linked list */
class linked
{
    
    /* to have the data in each linked of the element of the list */
    int data;
    /* to indicate to the next linked */
    linked *next;
    /* default constructor */
    linked(){
        data=0; next=NULL; }
};
class list 
{
    public:
    /* to point to the starting and ending linked respectively */
    linked *head; linked *tail;
    /* default constructor */
    list(){
     head=NULL; tail=NULL; }
    /* to add an element in the list */
    void insert(int d){
        linked *n=new linked; n->data=d; n->next=NULL;
        if(head==NULL){
            head=n; tail=n; }
        else{
            tail->next=n; tail=n;
    } }
    /* to show the list contents */
    void display(linked *h){
        linked *temp=h;
        cout<<"List contents:"<<endl;
        while(temp!=tail){
            cout<<temp->data<<" -> ";
            temp=temp->next; }
        cout<<tail->data<<endl<<endl;
    }
    /* to reverse the elements of the list */
    void Reverse(){
        linked *temp=head;
        list l1;
        int arr[100]; int cc=0;
        /* copying  the contetns of the list into an array */
        while(temp!=tail){
            arr[cc++]=temp->data;
            temp=temp->next; }
        arr[cc++]=tail->data;
        /* inserting into a new list in reverse order */
        for(int i=cc-1;i>=0;i--){
            l1.insert(arr[i]); }
        cout<<"Displaying the contents after reversing:"<<endl;
        l1.display(l1.head);
    }
    /* to replicate the elements of the list */
    void Replicate(int t){
        linked *temp=head;
        list l1;
        while(temp!=NULL){
            for(int i=0;i<t;i++)
                l1.insert(temp->data);
            temp=temp->next;  }
        cout<<"Displaying the contents after replication:"<<endl;
        l1.display(l1.head);  }
};
/* main code */
int main()
{
    list l; l.insert(100); l.insert(200); l.insert(300); l.insert(400); l.insert(500);
    l.display(l.head);
    l.Reverse(); l.Replicate(2);
}

Output

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *