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

2 Write block comments in the code on what its doing. Also the functionality of

ID: 3566566 • Letter: 2

Question

2 Write block comments in the code on what its doing. Also the functionality of the whole program itself

3. Output (Both the images are one ouput, sorry couldn't fit the images together)

(Also the red color texts are user inputs.)

1. Write code in C++ 2 Write block comments in the code on what its doing. Also the functionality of the whole program itself 3. Output (Both the images are one ouput, sorry couldn't fit the images together) (Also the red color texts are user inputs.) Implement a doubly linked list to store positive integers (duplicate numbers are allowed). To implement the list, you will need a class for the list and a class for the nodes. The node class assigns capabilities to store a single integer, as well as pointers to the next and the previous nodes in the list. Your class list will need a LAST (TAIL) pointer (pointing at the last element of the list, a HEAD pointer, and a CURRENT pointer. The CURRENT pointer will act as a ''slider'' that point to an element of the list and can be moved to the LEFT or to the RIGHT. When the list is first initiated, all three of the pointers are NULL. Once the first element is added, all of them will point to this sole element in the list. When implementing the functions, pay special attention to the following cases: . The list is empty . The list contains a single element . List contains more than one element ERROR HANDLING: If an operation tries to access an element before the head node or after the last node, print an error message starting with ''Error!''. If the user types a command that is not supported, print an error message starting with ''Error!''

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

/* Linked list node */

struct node

{

    int data;

    struct node* next;

};

/* Function to create a new node with given data */

struct node *newNode(int data)

{

    struct node *new_node = (struct node *) malloc(sizeof(struct node));

    new_node->data = data;

    new_node->next = NULL;

    return new_node;

}

/* Function to insert a node at the beginning of the Doubly Linked List */

void push(struct node** head_ref, int new_data)

{

    /* allocate node */

    struct node* new_node = newNode(new_data);

    /* link the old list off the new node */

    new_node->next = (*head_ref);

    /* move the head to point to the new node */

    (*head_ref)    = new_node;

}

/* Adds contents of two linked lists and return the head node of resultant list */

struct node* addTwoLists (struct node* first, struct node* second)

{

    struct node* res = NULL; // res is head node of the resultant list

    struct node *temp, *prev = NULL;

    int carry = 0, sum;

    while (first != NULL || second != NULL) //while both lists exist

    {

        // Calculate value of next digit in resultant list.

        // The next digit is sum of following things

        // (i) Carry

        // (ii) Next digit of first list (if there is a next digit)

        // (ii) Next digit of second list (if there is a next digit)

        sum = carry + (first? first->data: 0) + (second? second->data: 0);

        // update carry for next calulation

        carry = (sum >= 10)? 1 : 0;

        // update sum if it is greater than 10

        sum = sum % 10;

        // Create a new node with sum as data

        temp = newNode(sum);

        // if this is the first node then set it as head of the resultant list

        if(res == NULL)

            res = temp;

        else // If this is not the first node then connect it to the rest.

            prev->next = temp;

        // Set prev for next insertion

        prev = temp;

        // Move first and second pointers to next nodes

        if (first) first = first->next;

        if (second) second = second->next;

    }

    if (carry > 0)

      temp->next = newNode(carry);

    // return head of the resultant list

    return res;

}

// A utility function to print a linked list

void printList(struct node *node)

{

    while(node != NULL)

    {

        printf("%d ", node->data);

        node = node->next;

    }

    printf(" ");

}

/* Drier program to test above function */

int main(void)

{

    struct node* res = NULL;

    struct node* first = NULL;

    struct node* second = NULL;

    // create first list 7->5->9->4->6

    push(&first, 6);

    push(&first, 4);

    push(&first, 9);

    push(&first, 5);

    push(&first, 7);

    printf("First List is ");

    printList(first);

    // create second list 8->4

    push(&second, 4);

    push(&second, 8);

    printf("Second List is ");

    printList(second);

    // Add the two lists and see result

    res = addTwoLists(first, second);

    printf("Resultant list is ");

    printList(res);

   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