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

This needs to be in C++. Linked List. This is an example I was given and I have

ID: 664990 • Letter: T

Question

This needs to be in C++. Linked List. This is an example I was given and I have no idea how to go about it.

Part1:

Create a double-linked list that contains the numbers 1-10, one number per node.

Starting at the head node, traverse the list. Add all numbers and display the result.

Starting at the tail node, traverse the list in reverse order. Multiply all numbers and display the result

Part 2:

Create four separate lists from the list created in Part I. The lists should consist of:

Numbers divisible by 2.

Numbers divisible by 3.

Numbers divisible by 5.

Numbers divisible by 5.

Display the contents of each of the four lists.

I know how to create the nodes, but adding and multiplying after I traverse the lists is beyond me. Here is what I have.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include

using namespace std;

struct node{
int data;
node* next;
node* prev;

};

void PrintForward(node* head){
node* temp = head; //points to the very first value

while(temp != NULL){
cout

void PrintReverse(node* tail){
node* temp = tail; //points to the last

while(temp != NULL){
cout

int main(){
node* head;
node* tail;
node* n;

n = new node; //use n to reference the new node
n->data = 1; //sets the value data that is in all nodes equal to 1
n->prev = NULL;
head = n; //all point to the n value because they are the first values
tail = n;

n = new node;
n->data = 2;
n->prev = tail; //this makes prev which used to be null, linked to tail which should be the last value in the first node. esseitnally it links the 2nd node to the 1st
tail->next = n; //makes the 1st node link to the 2nd node by the 1st nodes next pointer
tail = n; //then you link tail to n to link it to the current node

n = new node;
n->data = 3;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 4;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 5;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 6;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 7;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 8;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 9;
n->prev = tail;
tail->next = n;
tail = n;

n = new node;
n->data = 10;
n->prev = tail;
tail->next = n;
tail = n;
tail->next = NULL;

int answer;

cout<<"Print the list going forward: "< PrintForward(head);

PrintReverse(tail);
  
return 0;
}

Explanation / Answer

Answer:

I solved the first part some statements are missing on the code which you have posted. Hence I developed a new code. Use this

#include <stdlib.h>

#include <iostream.h>

//using namespace std;

struct node

{

    int data1;

    node* next1;

    node* prev1;

};

class Node1

{

    public:

        Node1();

        ~Node1();

        void setKopa1();

        void printForward1();

     private:

        node* head1;

        node* tail1;

        node* n;

};

Node1::Node1()

{

    setKopa1();

}

Node1::~Node1()

{

    delete n;

}

void Node1::setKopa1()

{

    int lenghth1;

    do

    {

        cout << "input 10 elements: ";

        cin >> lenghth1;

        if(lenghth1<2)

        cout << "list must have 2elements!" <<endl;

    }

    while(lenghth1<2);

    int fill;

    cout << "Input "<< lenghth1 <<" elements: "<<endl;

    for (int i=0; i<lenghth1; i++)

    {

        cin>>fill;

        n = new node;

        n->data1 = fill;

        if (i==0)

        {

            n->prev1= NULL;

            head1 = n;

            tail1 = n;

        }

        else if (i+1==lenghth1)

        {

            n->prev1= tail1;

            tail1->next1 = n;

            tail1 = n;

            tail1->next1 = NULL;

        }

        else

        {

            n->prev1= tail1;

            tail1->next1 = n;

            tail1 = n;

        }                          

    }

}

void Node1::printForward1()

{

    node* temp = head1;

    while(temp != NULL)

    {

        cout << temp->data1 << " ";

        temp = temp-> next1;

    }

    cout << endl;

}

int main()

{

    Node1 a;

    a.printForward1();   

    system("pause");

    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