Question

1. Write a function using C++ which will determine the length of a circularly linked list....

1. Write a function using C++ which will determine the length of a circularly linked list.

2. Determine if the expression is true or false. Give a reason for your conclusion.

(a) Is n lg(n + 17) ∈ O(n 2 )?

(b) Is n 2 ∈ O(n √ n)?

(c) Is 3n ∈ O(n 3 )?

(d) Is 2n ∈ O(3n )?

(e) Is lg(lg(lg(n))) ∈ O(n)?

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

The basic structure of the program will fist have to make a circulay linked list

struct N {

    int data;

    struct N* next;

};

Once we have this Structure we'll write a loop to create a linked list.

The below function will add nodes to the circular linked list the process to do that is

Step 1: Create a Node from Structure

Step 2: Make the last element to point it

Step 3: Put the link of the next node in the new one as the header of the linked list to make it circular

void push(struct N** head, int data)

{

    struct N* ptr1 = (struct N*)malloc(sizeof(struct N));

    struct N* tmp = *head;

    ptr1->data = data;

    ptr1->next = *head_ref;

if (*head != NULL) {

        while (tmp->next != *head)

            tmp = tmp->next;

        tmp->next = ptr1;

    } else

        ptr1->next = ptr1;

    *head = ptr1;

} The input needs the link of the head and the data in the node to so the operations as shown in the code above.

Now to count the number of nodes or what we can call as length of the list.

int length(struct N* head)

{

    struct N* tmp = head;

    int result = 0;

    if (head != NULL) {

do{

            tmp = tmp->next;

            result++;

        } while (tmp != head);

}

    return result;

}

Now in the mail program just calling length(head) where head is point of the linked list we want to find the length of.

We are instructed to solve only one problem at a time on the platform.

If any doubts in the solution do put in comments

Add a comment
Know the answer?
Add Answer to:
1. Write a function using C++ which will determine the length of a circularly linked list....
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
  • 1)    Which of the following are in O (n): a) n + lg n        b) n...

    1)    Which of the following are in O (n): a) n + lg n        b) n + 2n        c) n + n2            d) 1000 n + 4500 lg n + 54 n 2)    What is a stack? Give an ADT. 3)    How would you implement the push method for a Stack implemented as a Linked List? 4)    How would you implement the pop method for a Stack implemented as a Linked List?

  • Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list....

    Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list. It must return a boolean true if the list contains a cycle, or false otherwise. HasCycle has a reference to a Node object, that points to the head of a linked list, as parameter.

  • Write a function to insert a name at the end of a linked list? (C) I...

    Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • In python Programming Exercise 1 Write a function that will have a list as an input,...

    In python Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n),...

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

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • For each of the following functions, indicate the class Θ(g(n)) the function belongs to. ( Use...

    For each of the following functions, indicate the class Θ(g(n)) the function belongs to. ( Use the simplest g(n) possible in your answers). Prove your assertions. a. ( n2 + 1)10 b. 2n+1 + 3n-1 c. [ log2 n ] d. 2n lg(n+2)2 + ( n+2)2 lg n/2 e. ( 10n2 + 7n + 3)1/2

  • Can you please help with the below? 1)   Which of the following is true about using...

    Can you please help with the below? 1)   Which of the following is true about using a 2-3-4 tree? a.   It is designed to minimize node visits while keeping to an O(log n) search performance b.   It is designed to self-balance as new values are inserted into the tree c.   As soon as a node becomes full, it performs the split routine d.   None of the above 2)   Which of the following is true about a binary search tree? a.  ...

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