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

The following exercise deal with coding a LinkedList. - The methids you are writ

ID: 3755380 • Letter: T

Question

The following exercise deal with coding a LinkedList.
- The methids you are writing are instance methods inside of LinkedList, so you can use the Node class.
- The LinkedList contains a Node<E> head and tail(optional).
- The LinkedList contains a method size() that returns the number of elements in the list.
- The Node objects are singly-linked, and contain public variable next, which references the next node in the list.
- you can use an Iterator if you want but you dont need to.

1. Write a method called concatenate, which takes two listels, which we'll refer to as A and B, and changes A such that it has A's elements first, then B's elements. For example, if A is [1,3,5] and B is [2,4,6], concatenate changes A to [1,3,5,2,4,6].

public void concatenate (LinkedList<E> A, LinkedList<E> B) {

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

struct node {

        int data;

        struct node *nxtPtr;

};

struct node *head1, *head2, *head3;

/*

   * creates node and fill the given data

   */

struct node * nodeCreation(int data) {

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

        ptr->data = data;

        ptr->nxtPtr = NULL;

        return ptr;

}

/* insert node in ascending order */

void insertNode(struct node ** myNode, int data) {

        struct node *lPtr, *mPtr, *nPtr = *myNode;

        lPtr = nodeCreation(data);

        /* insert at the front of the list */

        if (*myNode == NULL || (*myNode)->data > data) {

                *myNode = lPtr;

                (*myNode)->nxtPtr = nPtr;

                return;

        }

        /* insert at the end or middle of the list */

        while (nPtr) {

                mPtr = nPtr;

                nPtr = nPtr->nxtPtr;

                if (!nPtr) {

                        mPtr->nxtPtr = lPtr;

                        break;

                } else if ((data > mPtr->data) && (data < nPtr->data)) {

                        lPtr->nxtPtr = nPtr;

                        mPtr->nxtPtr = lPtr;

                        break;

                }

        }

       return;

}

/* concatenate list 1 and list 2 */

void concatList(struct node **list1, struct node **list2) {

        struct node *temp;

        if (*list1 == NULL) {

                *list1 = *list2;

        } else if (*list2) {

                temp = *list1;

                while (temp->nxtPtr) {

                        temp = temp->nxtPtr;

                }

                temp->nxtPtr = *list2;

        }

        return;

}

/* delete the given list */

struct node * removeList(struct node *ptr) {

        struct node *temp;

        while (ptr){

                temp = ptr->nxtPtr;

                free(ptr);

                ptr = temp;

        }

        return NULL;

}

/* traverse the given list and print the contents in each node */

int listTraversal(struct node *ptr) {

        int i = 0;

        while (ptr) {

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

                ptr = ptr->nxtPtr;

                i++;

        }

        return (i);

}

int main (int argc, char *argv[]) {

        int data, i, n;

        FILE *fp1, *fp2;

        fp1 = fopen(argv[1], "r");

        fp2 = fopen(argv[2], "r");

        if (!fp1 || !fp2) {

                printf("Unable to open file ");

                fcloseall();

                exit(0);

        }

        /* scan data from input file to insert into list1*/

        while (fscanf(fp1, "%d", &data) != EOF) {

                insertNode(&head1, data);

        }

        /* scan data from input file to insert into list2 */

        while (fscanf(fp2, "%d", &data) != EOF) {

                insertNode(&head2, data);

        }

        printf(" Data in First Linked List: ");

        n = listTraversal(head1);

        printf(" No of elements in linked list: %d ", n);

        printf(" Data in Second Linked List: ");

        n = listTraversal(head2);

        printf(" No of elements in linked list: %d ", n);

        concatList(&head1, &head2);

        printf(" Data in concatenated List: ");

        n = listTraversal(head1);

        printf(" No of elements in concatenated list: %d ", n);

        head1 = removeList(head1);

        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