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

Complete the problem below using valid C syntax. 6. (15 points) Suppose you desi

ID: 3835442 • Letter: C

Question

Complete the problem below using valid C syntax.

6. (15 points) Suppose you design a linked list so that it doesn't have a ta Instead, the list is circular, and the "tail" in this case doesn't have a NULL pointer. Instead, it points back to the head. Here's a drawing to show you what such a list would look like. tail: head data: 10 data: 15 data: 11 data: 20 next: next: next: next: struct list int data; struct list next (a) (8 points) Using the struct declaration shown above, implement a function that inserts a node into the list. You may insert the node anywhere you wish, but the circular list of connections must be preserved. You must use the following prototype, which assumes the head of the list is passed as an argument, the data to insert is passed as an argument, and the return value is the new head of the list. struct list insert (struct list 'head, int data)

Explanation / Answer

struct list * insert(struct list *head, int data){

    // creating new node

    struct list *newNode = (struct list *)malloc(sizeof(struct list));

    newNode->data = data;
    newNode->next = NULL;

    if(head == NULL){
        head = newNode;
        head->next = head;
        return head;
    }

    // inserting new node after head
    newNode->next = head->next;
    head->next = newNode;

    return head;
}


struct list *find(struct list *head, int data){

    if(head == NULL)
        return NULL;

    if(head->data == data){
        return head;
    }

    struct list *temp = head->next;
    while(temp != head){
        if(temp->data == data)
            return temp;
        temp = temp->next;
    }
}

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