Question

14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code:

p = question4(p);

where question4 is the function defined below. Show the contents of p after the function call.

struct node* question4(struct node *list) {
struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL)
a = a ->next;
a->next = b; c = b->next; b->next = NULL;
return c;
}

Answer:

15.   Write a function that takes in a pointer to the front of a linked list and returns 1 if all the nodes in the linked list are in sorted order (from smallest to largest, with repeats allowed), and 0 otherwise. The prototype is given below:

int isSorted(struct node* list) {


16.   Write a function that takes in a pointer to the head of a linked list, a value to insert into the list, val, and a location in the list in which to insert it, place, which is guaranteed to be greater than 1, and does the insertion. If place number of items aren’t in the list, just insert the item in the back of the list. You are guaranteed that the linked list into which the inserted item is being added is not empty. The prototype is given below:

void insertToPlace(struct node* list, int val, int place) {


17.   Write a function that operates on a linked list of integers. Your function should insert a new node containing the value 2 after every node that contains the value 4. Make use of the list node struct and function header below.

void list_42(struct node* list) {


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 14:

Given:

p contains elements 66, 9, 14, 52, 87, 14, and 17

Given snippet:

struct node* question4(struct node *list){

struct node* a = list;

struct node* b = list;

struct node* c;

if(a == NULL) return NULL;

while (a->next != NULL)

            a = a ->next;

a->next = b;

c = b->next;

b->next = NULL;

return c;

}

Content of p after the function call:

9, 14, 52, 87, 14, 17, 66

Explanation:

There are two pointers a and b. they are pointing to head. The loop will move the pointer (a) to the last node.

The pointer b is pointing to first node.

a-> next = b;

It is clear that a is pointing to b which makes the list circular.

c = b->next

the next of b is 9, hence the c will point to 9

b->next = NULL;

return c;

The first element will move to the end.

Here, 66 will be move to end and 9 will become the head node.

Therefore, the final content will be 9, 14, 52, 87, 14, 17, 66

Question 15:

Function that takes a pointer to the front of a linked list and returns 1 when all nodes in the linked list are in sorted order, otherwise returns 0.

Function:

// function prototype

int isSorted (struct node* list)

{

     // temporary pointer

     struct node* temp = list;

     // iterate loop until the null value

     while (temp->next != NULL) {

                // check if current value is greater than next value

                if (temp->value > temp->next->value)

                     // return 0

                     return 0;

                // point to next

                temp = temp->next;

                }

                // return 1

                return 1;

}

Question 16:

Required function:

// function prototype

void insertToPlace (struct node* list, int val, int place)

{

        // initialize current position

        int currentPos = 1;

        // dynamically allocate memory to new value

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

        newVal->value = val;

        newVal->next = NULL;

        struct node* currentNode = list;

        // iterate loop

        while ((currentPos + 1)< place && currentNode->next != NULL) {

                  currentNode = currentNode->next;

                  currentPos++;

         }

         // check if next value of currentNode is null

         if (currentNode->next == NULL)

         {

              // assign new value at the place of null

               currentNode->next = newVal;

          }

          else if ((currentPost + 1) == place)

          {

                 newVal->next = currentNode->next;

                 currentNode->next = newVal;

           }

}

Question 17:

Function that insert a new node containing the value 2 after every node that contains the value 4.

// function prototype

void list_42 (struct node* list)

{

        struct node* newNode = NULL;

        struct node* currentNode = list;

        while (currentNode != NULL) {

                 if (currentNode->data == 4)

                 {

                       newNode = malloc(sizeof(struct node));

                       newNode->data = 2;

                       newNode->next = currentNode->next;

                       currentNode->next = newNode;

                       currentNode = newNode->next;

                    }

                     else

                            currentNode = currentNode->next;

                    }

}

Add a comment
Know the answer?
Add Answer to:
14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • () Given the following structure definition and typedef for a linked list of strings: typedef struct...

    () Given the following structure definition and typedef for a linked list of strings: typedef struct node st node; struct node st { char *word; /* a valid string pointer or NULL */ node *next; /* next node in the list or NULL */ }; Write a C function, free list(), that takes as an argument one of these lists, possibly NULL, and frees all the strings as well as the list itself. Write robust code. void free list(node *list){

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT