SlideShare a Scribd company logo
1 of 8
Download to read offline
c++ Computational Complexity
/* filling in the
* following three functions:
*
* 1) void disp(Node* head)
* Given a head pointer to a list, display the list using cout.
* If the list is empty, you should output "HEAD->NULL" (without the enclosing ").
* If the list contains one node whose data value is 3, you should output "HEAD->3->NULL".
* If the list contains three nodes and the data values are 1, 2, and 3, respectively, then you
should output
* "HEAD->1->2->3->NULL"
*
* 2) Node* add(Node* head, int data)
* Given the old head pointer to a list, create a new node using the value of data, and add the new
node to the front of
* the list. Return the new head pointer.
* Example: If the original list is "HEAD->1->2->NULL", then after adding 5, it should
become "HEAD->5->1->2->NULL". The
* return value should be the address of node 5.
*
* 3) void magic(Node* p1, Node* p2)
* p1 and p2 each points to a node from a same list. p1 may point to a later node than p2. Your
task is to calculate how
* many nodes are between p1 and p2. If there are k nodes in between (excluding nodes pointed
to by p1 and p2), then the
* result is k (k could be 0). Replace the data fields of all nodes between p1 and p2 by k.
* Example: The original list is "HEAD->1->7->5->0->3->NULL". p1 points to node 1 (the
node with value 1), p2 points to
* node 0. There are two nodes in between p1 and p2. These two nodes' data fields need to be
replaced by k=2. The
* resulting list should be "HEAD->1->2->2->0->3->NULL".
*
* You should modify sections labelled as "fill in here". Please leave the other sections (e.g., the
main
* function and all function protocols) intact.
*/
#include
using namespace std;
struct Node {
int data;
Node* link;
};
void disp(Node* head) {
// fill in here
}
Node* add(Node* head, int data) {
// fill in here
}
void magic(Node* p1, Node* p2) {
// fill in here
}
main() {
Node* head = NULL;
Node *p4, *p7;
for(int i = 0; i < 10; i++) {
head = add(head, i);
if (i==4) p4 = head;
if (i==7) p7 = head;
}
// at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0-
>NULL
// p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node
containing 7)
magic(p4, p7);
disp(head);
// between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by
k=2
// the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL
// housekeeping (free all nodes)
Node* tmp;
while (head) {
tmp = head;
head = head->link;
delete tmp;
}
}
Comments
Expert Answer
c++ Computational Complexity
/* Your task is to fill in the
* following three functions:
*
* 1) void disp(Node* head)
* Given a head pointer to a list, display the list using cout.
* If the list is empty, you should output "HEAD->NULL" (without the enclosing ").
* If the list contains one node whose data value is 3, you should output "HEAD->3->NULL".
* If the list contains three nodes and the data values are 1, 2, and 3, respectively, then you
should output
* "HEAD->1->2->3->NULL"
*
* 2) Node* add(Node* head, int data)
* Given the old head pointer to a list, create a new node using the value of data, and add the new
node to the front of
* the list. Return the new head pointer.
* Example: If the original list is "HEAD->1->2->NULL", then after adding 5, it should
become "HEAD->5->1->2->NULL". The
* return value should be the address of node 5.
*
* 3) void magic(Node* p1, Node* p2)
* p1 and p2 each points to a node from a same list. p1 may point to a later node than p2. Your
task is to calculate how
* many nodes are between p1 and p2. If there are k nodes in between (excluding nodes pointed
to by p1 and p2), then the
* result is k (k could be 0). Replace the data fields of all nodes between p1 and p2 by k.
* Example: The original list is "HEAD->1->7->5->0->3->NULL". p1 points to node 1 (the
node with value 1), p2 points to
* node 0. There are two nodes in between p1 and p2. These two nodes' data fields need to be
replaced by k=2. The
* resulting list should be "HEAD->1->2->2->0->3->NULL".
*
* You should modify sections labelled as "fill in here". Please leave the other sections (e.g., the
main
* function and all function protocols) intact.
*/
#include
using namespace std;
struct Node {
int data;
Node* link;
};
void disp(Node* head) {
// fill in here
}
Node* add(Node* head, int data) {
// fill in here
}
void magic(Node* p1, Node* p2) {
// fill in here
}
main() {
Node* head = NULL;
Node *p4, *p7;
for(int i = 0; i < 10; i++) {
head = add(head, i);
if (i==4) p4 = head;
if (i==7) p7 = head;
}
// at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0-
>NULL
// p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node
containing 7)
magic(p4, p7);
disp(head);
// between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by
k=2
// the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL
// housekeeping (free all nodes)
Node* tmp;
while (head) {
tmp = head;
head = head->link;
delete tmp;
}
}
Solution
#include
using namespace std;
struct Node {
int data;
Node* link;
};
void disp(Node* head) {
if(head==NULL)
{
cout<<"head->NULL";
return;
}
cout<<"head->";
while(head!=NULL)
{
cout<data;
cout<<"->";
head=head->link;
}
cout<<"NULL";
// fill in here
}
Node* add(Node* head, int data) {
Node* tmp = new Node();
tmp -> data = data;
tmp -> link = head;
return tmp;
// fill in here
}
void magic(Node* p1, Node* p2) {
int k=0,flag=1;
Node* temp1=p1;
Node* temp2=p2;
while(p1!=NULL)
{
if(p1!=p2)
{
p1=p1->link;
k++;
}
else if(p1==p2) break;
}
if(p1== NULL)
{
k=0;flag=1;
p1=temp1;
while(p2!=NULL)
{
if(p1!=p2)
{
p2=p2->link;
k++;
}
else if(p1==p2) break;
}
flag=2;
}
else
p1=temp1;
if(flag==1)
{
while(p1!=p2)
{
p1=p1->link;
p1->data=k;
}
}
else if(flag==2)
{
p2=temp2;
while(p1!=p2)
{
p2=p2->link;
p2->data=k;
}
}
}
main() {
Node* head = NULL;
Node *p4, *p7;
for(int i = 0; i < 10; i++) {
head = add(head, i);
if (i==4) p4 = head;
if (i==7) p7 = head;
}
// at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0-
>NULL
// p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node
containing 7)
magic(p4, p7);
disp(head);
// between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by k=2
// the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL
// housekeeping (free all nodes)
Node* tmp;
while (head) {
tmp = head;
head = head->link;
delete tmp;
}
}

More Related Content

Similar to c++ Computational Complexity filling in the following three .pdf

#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdflakshmijewellery
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfarihantelehyb
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Template LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdfTemplate LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdffatoryoutlets
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfkavithaarp
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfangelfragranc
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfCreate a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfhadpadrrajeshh
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfrohit219406
 

Similar to c++ Computational Complexity filling in the following three .pdf (20)

#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Template LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdfTemplate LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfCreate a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdf
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
week-13x
week-13xweek-13x
week-13x
 

More from amitbagga0808

Describe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfDescribe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfamitbagga0808
 
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfChapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfamitbagga0808
 
Compare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfCompare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfamitbagga0808
 
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfAnatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfamitbagga0808
 
A red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfA red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfamitbagga0808
 
You have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfYou have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfamitbagga0808
 
An organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfAn organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfamitbagga0808
 
Which level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfWhich level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfamitbagga0808
 
Why is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfWhy is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfamitbagga0808
 
This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfamitbagga0808
 
What factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfWhat factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfamitbagga0808
 
What is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfWhat is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfamitbagga0808
 
What are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfWhat are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfamitbagga0808
 
What are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfWhat are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfamitbagga0808
 
on the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfon the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfamitbagga0808
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfamitbagga0808
 
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfProve that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfamitbagga0808
 
Not sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfNot sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfamitbagga0808
 
In each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfIn each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfamitbagga0808
 
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfi need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfamitbagga0808
 

More from amitbagga0808 (20)

Describe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdfDescribe the role of soil microbes in the formation and maintenance .pdf
Describe the role of soil microbes in the formation and maintenance .pdf
 
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdfChapter 9Problem P 9-1The narrative and systems flowchart for.pdf
Chapter 9Problem P 9-1The narrative and systems flowchart for.pdf
 
Compare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdfCompare and contrast the thick and thin filaments.SolutionTher.pdf
Compare and contrast the thick and thin filaments.SolutionTher.pdf
 
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdfAnatomy can be studied in many ways. Regional anatomy is the study of.pdf
Anatomy can be studied in many ways. Regional anatomy is the study of.pdf
 
A red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdfA red blood cell enters the right atrium from the superior vena cava..pdf
A red blood cell enters the right atrium from the superior vena cava..pdf
 
You have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdfYou have been hired by Planet Poachers to write a Java console appli.pdf
You have been hired by Planet Poachers to write a Java console appli.pdf
 
An organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdfAn organism that has a normal diploid autosome set with an XXY sex c.pdf
An organism that has a normal diploid autosome set with an XXY sex c.pdf
 
Which level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdfWhich level of Orange Book protection is considered mandatory protec.pdf
Which level of Orange Book protection is considered mandatory protec.pdf
 
Why is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdfWhy is it an advantage for the epiglottis to fit over the trachea.pdf
Why is it an advantage for the epiglottis to fit over the trachea.pdf
 
This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdf
 
What factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdfWhat factors are likely to be involved in setting the limit to how lo.pdf
What factors are likely to be involved in setting the limit to how lo.pdf
 
What is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdfWhat is alternative splicing What is the difference between mRNA sp.pdf
What is alternative splicing What is the difference between mRNA sp.pdf
 
What are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdfWhat are histones and their role in regulating transcription. .pdf
What are histones and their role in regulating transcription. .pdf
 
What are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdfWhat are the cellular differences between fungi and protistsSol.pdf
What are the cellular differences between fungi and protistsSol.pdf
 
on the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdfon the basis of the above data, list the media in order according to.pdf
on the basis of the above data, list the media in order according to.pdf
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdfProve that is L is regular and h is a homomorphism, then h(L) is.pdf
Prove that is L is regular and h is a homomorphism, then h(L) is.pdf
 
Not sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdfNot sure how to do this case analysis please help me do it!1.Are t.pdf
Not sure how to do this case analysis please help me do it!1.Are t.pdf
 
In each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdfIn each of the following situations, data are obtained by conducting .pdf
In each of the following situations, data are obtained by conducting .pdf
 
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdfi need a summary book report on thw Iwoby richard wheelerSolut.pdf
i need a summary book report on thw Iwoby richard wheelerSolut.pdf
 

Recently uploaded

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

c++ Computational Complexity filling in the following three .pdf

  • 1. c++ Computational Complexity /* filling in the * following three functions: * * 1) void disp(Node* head) * Given a head pointer to a list, display the list using cout. * If the list is empty, you should output "HEAD->NULL" (without the enclosing "). * If the list contains one node whose data value is 3, you should output "HEAD->3->NULL". * If the list contains three nodes and the data values are 1, 2, and 3, respectively, then you should output * "HEAD->1->2->3->NULL" * * 2) Node* add(Node* head, int data) * Given the old head pointer to a list, create a new node using the value of data, and add the new node to the front of * the list. Return the new head pointer. * Example: If the original list is "HEAD->1->2->NULL", then after adding 5, it should become "HEAD->5->1->2->NULL". The * return value should be the address of node 5. * * 3) void magic(Node* p1, Node* p2) * p1 and p2 each points to a node from a same list. p1 may point to a later node than p2. Your task is to calculate how * many nodes are between p1 and p2. If there are k nodes in between (excluding nodes pointed to by p1 and p2), then the * result is k (k could be 0). Replace the data fields of all nodes between p1 and p2 by k. * Example: The original list is "HEAD->1->7->5->0->3->NULL". p1 points to node 1 (the node with value 1), p2 points to * node 0. There are two nodes in between p1 and p2. These two nodes' data fields need to be replaced by k=2. The * resulting list should be "HEAD->1->2->2->0->3->NULL". * * You should modify sections labelled as "fill in here". Please leave the other sections (e.g., the main * function and all function protocols) intact.
  • 2. */ #include using namespace std; struct Node { int data; Node* link; }; void disp(Node* head) { // fill in here } Node* add(Node* head, int data) { // fill in here } void magic(Node* p1, Node* p2) { // fill in here } main() { Node* head = NULL; Node *p4, *p7; for(int i = 0; i < 10; i++) { head = add(head, i); if (i==4) p4 = head; if (i==7) p7 = head; } // at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0- >NULL // p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node containing 7) magic(p4, p7); disp(head); // between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by k=2 // the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL // housekeeping (free all nodes) Node* tmp; while (head) {
  • 3. tmp = head; head = head->link; delete tmp; } } Comments Expert Answer c++ Computational Complexity /* Your task is to fill in the * following three functions: * * 1) void disp(Node* head) * Given a head pointer to a list, display the list using cout. * If the list is empty, you should output "HEAD->NULL" (without the enclosing "). * If the list contains one node whose data value is 3, you should output "HEAD->3->NULL". * If the list contains three nodes and the data values are 1, 2, and 3, respectively, then you should output * "HEAD->1->2->3->NULL" * * 2) Node* add(Node* head, int data) * Given the old head pointer to a list, create a new node using the value of data, and add the new node to the front of * the list. Return the new head pointer. * Example: If the original list is "HEAD->1->2->NULL", then after adding 5, it should become "HEAD->5->1->2->NULL". The * return value should be the address of node 5. * * 3) void magic(Node* p1, Node* p2) * p1 and p2 each points to a node from a same list. p1 may point to a later node than p2. Your task is to calculate how * many nodes are between p1 and p2. If there are k nodes in between (excluding nodes pointed to by p1 and p2), then the * result is k (k could be 0). Replace the data fields of all nodes between p1 and p2 by k. * Example: The original list is "HEAD->1->7->5->0->3->NULL". p1 points to node 1 (the node with value 1), p2 points to * node 0. There are two nodes in between p1 and p2. These two nodes' data fields need to be
  • 4. replaced by k=2. The * resulting list should be "HEAD->1->2->2->0->3->NULL". * * You should modify sections labelled as "fill in here". Please leave the other sections (e.g., the main * function and all function protocols) intact. */ #include using namespace std; struct Node { int data; Node* link; }; void disp(Node* head) { // fill in here } Node* add(Node* head, int data) { // fill in here } void magic(Node* p1, Node* p2) { // fill in here } main() { Node* head = NULL; Node *p4, *p7; for(int i = 0; i < 10; i++) { head = add(head, i); if (i==4) p4 = head; if (i==7) p7 = head; } // at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0- >NULL // p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node containing 7) magic(p4, p7); disp(head);
  • 5. // between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by k=2 // the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL // housekeeping (free all nodes) Node* tmp; while (head) { tmp = head; head = head->link; delete tmp; } } Solution #include using namespace std; struct Node { int data; Node* link; }; void disp(Node* head) { if(head==NULL) { cout<<"head->NULL"; return; } cout<<"head->"; while(head!=NULL) { cout<data; cout<<"->"; head=head->link; } cout<<"NULL"; // fill in here }
  • 6. Node* add(Node* head, int data) { Node* tmp = new Node(); tmp -> data = data; tmp -> link = head; return tmp; // fill in here } void magic(Node* p1, Node* p2) { int k=0,flag=1; Node* temp1=p1; Node* temp2=p2; while(p1!=NULL) { if(p1!=p2) { p1=p1->link; k++; } else if(p1==p2) break; } if(p1== NULL) { k=0;flag=1; p1=temp1; while(p2!=NULL) { if(p1!=p2) { p2=p2->link; k++; } else if(p1==p2) break; } flag=2; } else
  • 7. p1=temp1; if(flag==1) { while(p1!=p2) { p1=p1->link; p1->data=k; } } else if(flag==2) { p2=temp2; while(p1!=p2) { p2=p2->link; p2->data=k; } } } main() { Node* head = NULL; Node *p4, *p7; for(int i = 0; i < 10; i++) { head = add(head, i); if (i==4) p4 = head; if (i==7) p7 = head; } // at this point, we have created the following list: HEAD->9->8->7->6->5->4->3->2->1->0- >NULL // p4 now points to node 4 (the node containing 4); p7 now points to node 7 (the node containing 7) magic(p4, p7); disp(head); // between p4 and p7, there are k=2 nodes, and these two nodes' values will be replaced by k=2 // the output should be: HEAD->9->8->7->2->2->4->3->2->1->0->NULL
  • 8. // housekeeping (free all nodes) Node* tmp; while (head) { tmp = head; head = head->link; delete tmp; } }