Question

Assume you have a linked list of integer data pointed by head (p-head). head e2 next nextー next next Qu: Write the recursive

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

struct node

{

int data;

struct node* next;

};

struct node* duplicate(struct node* p)

{

struct node* current=p;

if(current==NULL)   

return NULL;

else

{

struct node* List=(struct node*)malloc(sizeof(struct node)); //allocating memory for new node

List->data=current->data; // copying data into that node

List->next=duplicate(current->next); // recursion takes place here

return(List);

}

}

void bubbleSort(struct Node * p)

{

    int swapped, i;

    struct node *ptr1;

    struct node *lptr = NULL;

    if (p == NULL)

        return;

    do

    {

        swapped = 0;

        ptr1 = p;

  

        while (ptr1->next != lptr)

        {

            if (ptr1->data > ptr1->next->data)

            {

                swap(ptr1, ptr1->next); // swap the data values if ptr1->data is greater than ptr1->next->data   

                swapped = 1;

            }

            ptr1 = ptr1->next;

        }

        lptr = ptr1;

    }

    while (swapped);

}

/* function to swap data of two nodes a and b*/

void swap(struct Node *a, struct Node *b)

{

    int temp = a->data;

    a->data = b->data;

    b->data = temp;

}

To implement this function initially include stdbool.h ilibrary.

bool different(struct node* p)
{

struct node* current;
struct node* temp=p;
   while(temp!=NULL)
   {
       current=temp->next;
       while(current!=NULL)
       {
           if(temp->data==current->data ) //checking for the same element.
           {
               return false; //if matches return false
           }
           else // if not matches move the current pointer forward
           {
               current=current->next;
}
       }
       temp=temp->next; // move the temp pointer forward
   }
   return true;
}

Add a comment
Know the answer?
Add Answer to:
Assume you have a linked list of integer data pointed by head (p-head). head e2 next...
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
  • In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list...

    In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • True False Question 2 (3 points) Given a singly linked list with more than two nodes,...

    True False Question 2 (3 points) Given a singly linked list with more than two nodes, to remove the second node from a linked list with only head reference, assume curr - head, next, you do Set curr.next to head.next Oset head. next to curr.next Set head, next to curr Oset head to curr.next TL th Question 3 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last...

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • 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...

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