Question

Using C++,

Using C++,

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

#include<iostream>
#include<cstdlib>
using namespace std;

// struct declaration
struct Node {
int data;
Node *next;
};

/*
add a node to linkedlist
*/
void addNode(Node *ll,int d){
while(ll->next != NULL){
ll = ll->next;
}
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = d;
temp->next = NULL;
ll->next = temp;
}

/*
function to display linkedlist
*/
void display(Node *ll){
while(ll!=NULL){
cout << ll->data << "->";
ll = ll->next;
}
cout << endl;
}

/*
function to count the number of times the integer 5 occurs in the given linked list
*/
int count5(Node *top){
// base case if top is null return zero
if(top == NULL){
return 0;
}
// if data is 5 add 1 to recursive call
else if(top->data == 5){
return 1+count5(top->next);
}
else{
return count5(top->next);
}
}

/*
function the add a numer n to the linked list integers
*/
void recAddn(Node *top, int n){
if(top == NULL){
return;
}
else{
top->data = top->data + n;
return recAddn(top->next,n);
}
}
/*
reverse string function
*/
void reverseString()
{
char c;
scanf("%c", &c);
if( c != '$')
{
reverseString();
cout << c;
}
}

/*
function to return the sum of integers in the linked list
*/
int sum(Node *top){
if(top == NULL){
return 0;
}
else{
return top->data + sum(top->next);
}
}

int main() {
/*top node */
Node *top;
top = (struct Node*)malloc(sizeof(struct Node));
top->data = 1;
top->next = NULL;
// add 10 nodes through for loop
for(int i=2;i<11;i++){
addNode(top,i);
}
addNode(top,5);
// print the linked list
display(top);
cout << "Count of 5 in linkedlist = " << count5(top) << endl;
// call the sum function
cout << "Sum of integers in linkedList: " << sum(top) << endl;
// call the recAddn function with n = 5
recAddn(top,5);
// dispaly the linked list after adding n
cout << "Linked List after calling recAddn : "<< endl;
display(top);
cout << "Enter a sentence:";
reverseString();
}

/*
sample output
1->2->3->4->5->6->7->8->9->10->5->
Count of 5 in linkedlist = 2
Sum of integers in linkedList: 60
Linked List after calling recAddn :
6->7->8->9->10->11->12->13->14->15->10->
Enter a sentence:abcd$
dcba
*/

Add a comment
Know the answer?
Add Answer to:
Using C++,
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
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