Question

Write a C++ recursive function that counts the number of nodes in a singly linked list....

Write a C++ recursive function that counts the number of nodes in a singly linked list.

(a) Test your function using different singly linked lists. Include your code.

(b) Write a recurrence relation that represents your algorithm.

(c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.

Homework Answers

Answer #1

#include<iostream>

#include<cstdlib>

using namespace std;

struct Node

{

int data;

struct Node *next;

};

// This function prints contents of linked list starting from

// the given node

void printList(Node *n)

{

while (n != NULL)

{

cout<< n->data<<" ";

n = n->next;

}

cout<<endl;

}

// function to count the size

int size(Node *head) {

if(head == NULL)

return 0;

return 1 + size(head->next); // recursive call

}

int main()

{

Node* head = NULL;

Node* second = NULL;

Node* third = NULL;

// allocate 3 nodes in the heap

head = new Node;

second = new Node;

third = new Node;

head->data = 1; //assign data in first node

head->next = second; // Link first node with second

second->data = 2; //assign data to second node

second->next = third;

third->data = 3; //assign data to third node

third->next = NULL;

printList(head);

cout<<"Size of list: "<<size(head)<<endl;

return 0;

}

b)

T(n) = 1 + T(n-1)

c)

T(n) = 1 + T(n-1)

= 1 + 1 + T(n-2)

= 1 + 1+ 1+... n times

= O(n)

Know the answer?
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
Similar Questions
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
c++ code for function that take doubly linked list as parameter and deleted all nodes in...
c++ code for function that take doubly linked list as parameter and deleted all nodes in even position from first to last
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
Implement a function that detects if there is a “loop” in a singly linked list. A...
Implement a function that detects if there is a “loop” in a singly linked list. A “loop” is defined as a link from one node to itself or to another node that is before the node in the list. The function may only make one pass through the list. It must return 1 if a “loop” exists; 0 otherwise. Be sure to test your function! You may start with code that you have written for other labs. This is a...
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a recursive function count_digits that counts all the digits in a string. Program: C
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
How would I go about implementing a function in C++ that swaps the front node and...
How would I go about implementing a function in C++ that swaps the front node and tNode with each other in a singly linked list that has more than two nodes, but I can't swap data? Also what would the Big-O complexity be for this?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT