SlideShare a Scribd company logo
Qestion: Please add pre-condition, post-conditions and descriptions for each member function of
the LINKED_LIST_CLASS. Answer with your new version of the program.
#include
using namespace std;
class LIST_NODE
{
public:
int data; // data element of node
LIST_NODE *next; // pointer element of node
};
class LINKED_LIST_CLASS
{
public:
LINKED_LIST_CLASS(); // default constructor
LINKED_LIST_CLASS(LINKED_LIST_CLASS &); // copy constructor
~LINKED_LIST_CLASS(); // destructor
void Add(int); // mutator
void Print(); // accessor
LIST_NODE * Search(int); // accessor
void Remove(int); // mutator
bool Is_Empty(); // accessor
private:
LIST_NODE *front; // pointer to front of list
};
LINKED_LIST_CLASS::LINKED_LIST_CLASS()
{
cout << endl << "The default constructor has been called. ";
front = new LIST_NODE;
front->next = 0; // initialize the next field to null
front->data = -10000;
}
LINKED_LIST_CLASS::LINKED_LIST_CLASS(LINKED_LIST_CLASS & org)
{
cout << endl << "The copy constructor has been called. ";
front = new LIST_NODE;
front->next = 0;
front->data = -10000;
LIST_NODE *p = org.front->next;
LIST_NODE *back = 0;
while(p!=0)
{
if (back == 0)
{
front->next = new LIST_NODE;
back = front->next;
back->next = 0;
back->data = p->data;
}
else
{
back->next = new LIST_NODE;
back = back->next;
back->data = p->data;
back->next = 0;
}
p=p->next;
}
}
LINKED_LIST_CLASS::~LINKED_LIST_CLASS()
{
cout << endl << "The destructor has been called. ";
while (front->next != 0)
{
LIST_NODE *p = front->next;
front->next = front->next->next;
delete p;
}
delete front;
front = 0;
}
void LINKED_LIST_CLASS::Add(int item)
{
LIST_NODE *p = new LIST_NODE;
p->data = item;
if (front->next== 0) // empty list
{
front->next = p;
p->next = 0;
}
else // list has information and is not empty
{
p->next = front->next;
front->next = p;
}
}
void LINKED_LIST_CLASS::Print()
{
cout << endl;
for(LIST_NODE *p = front->next; p != 0; p = p->next)
{
cout << p->data;
if (p->next != 0)
{
cout << "-->";
}
}
cout<next; p!=0; p=p->next)
{
if (p->data == key)
return p;
}
return 0; // key not found in list
}
void LINKED_LIST_CLASS::Remove(int key)
{
LIST_NODE *p = Search(key);
if (Is_Empty())
{
cout << key << " is not in the list. No removal performed! ";
}
else
{
LIST_NODE *q = front;
while (q->next->data != key)
{
q = q->next;
}
q->next = p->next; // CRITICAL STEP!!!!
delete p;
}
}
bool LINKED_LIST_CLASS::Is_Empty()
{
return front->next == 0;
}
int main()
{
LINKED_LIST_CLASS L1;
L1.Add(5);
L1.Add(10);
L1.Add(29);
L1.Print();
LINKED_LIST_CLASS L2 = L1;
L2.Print();
L1.Remove(10);
L1.Print();
return 0;
}
Solution
#include
using namespace std;
class LIST_NODE
{
public:
int data; // data element of node
LIST_NODE *next; // pointer element of node
};
class LINKED_LIST_CLASS
{
public:
LINKED_LIST_CLASS(); // default constructor
LINKED_LIST_CLASS(LINKED_LIST_CLASS &); // copy constructor
~LINKED_LIST_CLASS(); // destructor
void Add(int); // mutator
void Print(); // accessor
LIST_NODE * Search(int); // accessor
void Remove(int); // mutator
bool Is_Empty(); // accessor
private:
LIST_NODE *front; // pointer to front of list
};
LINKED_LIST_CLASS::LINKED_LIST_CLASS()
{
//Pre-condition : front should be null i.e. list should be empty
if(front == 0)
{
cout << endl << "The default constructor has been called. ";
front = new LIST_NODE;
front->next = 0; // initialize the next field to null
front->data = -10000;
//Post-condition : front should not be null
if(Is_Empty())
cout<<"Post-condition failed for default constructor";
}
}
LINKED_LIST_CLASS::LINKED_LIST_CLASS(LINKED_LIST_CLASS & org)
{
//Pre condition : linked list should not be empty
if(!org.Is_Empty())
{
cout << endl << "The copy constructor has been called. ";
front = new LIST_NODE;
front->next = 0;
front->data = -10000;
LIST_NODE *p = org.front->next;
LIST_NODE *back = 0;
while(p!=0)
{
if (back == 0)
{
front->next = new LIST_NODE;
back = front->next;
back->next = 0;
back->data = p->data;
}
else
{
back->next = new LIST_NODE;
back = back->next;
back->data = p->data;
back->next = 0;
}
p=p->next;
}
//Post-condition: Linked list copied to should not be empty
if(Is_Empty())
{
cout<<"Copy constructor failed";
}
}
}
LINKED_LIST_CLASS::~LINKED_LIST_CLASS()
{
if(front != 0) //front should be initialized for it to destroy
{
cout << endl << "The destructor has been called. ";
while (front->next != 0)
{
LIST_NODE *p = front->next;
front->next = front->next->next;
delete p;
}
delete front;
front = 0;
if(front != 0) //front is not destroyed if it is not null
{
cout<< "Destructor failed";
}
}
}
void LINKED_LIST_CLASS::Add(int item)
{
//Pre-condition: item is already added in linked list
if(Search(item) == 0)
{
LIST_NODE *p = new LIST_NODE;
p->data = item;
if (front->next== 0) // empty list
{
front->next = p;
p->next = 0;
}
else // list has information and is not empty
{
p->next = front->next;
front->next = p;
}
//Post-condition : item should be at the front of linked list
if(front->next->data != item)
cout<<"Add operation failed"<next; p != 0; p = p->next)
{
cout << p->data;
if (p->next != 0)
{
cout << "-->";
}
}
cout<next != 0)
{
for(LIST_NODE *p = front->next; p!=0; p=p->next)
{
if (p->data == key)
return p;
}
}
return 0; // key not found in list
}
void LINKED_LIST_CLASS::Remove(int key)
{
//Pre-condition: Item should be present in list
if(Search(key) != 0)
{
LIST_NODE *p = Search(key);
if (Is_Empty())
{
cout << key << " is not in the list. No removal performed! ";
}
else
{
LIST_NODE *q = front;
while (q->next->data != key)
{
q = q->next;
}
q->next = p->next; // CRITICAL STEP!!!!
delete p;
}
//Post-condition: Item should not be present in array
if(Search(key) != 0)
cout<<"Remove failed";
}
}
bool LINKED_LIST_CLASS::Is_Empty()
{
return front->next == 0;
}
int main()
{
LINKED_LIST_CLASS L1;
L1.Add(5);
L1.Add(10);
L1.Add(29);
L1.Print();
LINKED_LIST_CLASS L2 = L1;
L2.Print();
L1.Remove(10);
L1.Print();
return 0;
}

More Related Content

Similar to Qestion Please add pre-condition, post-conditions and descriptions .pdf

Linked lists
Linked listsLinked lists
Linked lists
George Scott IV
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
Stack queue
Stack queueStack queue
Stack queue
Tony Nguyen
 
Stack queue
Stack queueStack queue
Stack queue
James Wong
 
Stack queue
Stack queueStack queue
Stack queue
Harry Potter
 
Stack queue
Stack queueStack queue
Stack queue
Young Alista
 
Stack queue
Stack queueStack queue
Stack queue
Fraboni Ec
 
Stack queue
Stack queueStack queue
Stack queue
Luis Goldster
 
Stack queue
Stack queueStack queue
Stack queue
Hoang Nguyen
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
Anaya Zafar
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
adityastores21
 
Please solve the TODO parts include LinkedListcpph tem.pdf
Please solve the TODO parts  include LinkedListcpph tem.pdfPlease solve the TODO parts  include LinkedListcpph tem.pdf
Please solve the TODO parts include LinkedListcpph tem.pdf
aggarwalopticalsco
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
eyewaregallery
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdf
adityaenterprise32
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 

Similar to Qestion Please add pre-condition, post-conditions and descriptions .pdf (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
 
Please solve the TODO parts include LinkedListcpph tem.pdf
Please solve the TODO parts  include LinkedListcpph tem.pdfPlease solve the TODO parts  include LinkedListcpph tem.pdf
Please solve the TODO parts include LinkedListcpph tem.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdf
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 

More from arihantmobileselepun

How are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdfHow are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdf
arihantmobileselepun
 
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdfHow does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
arihantmobileselepun
 
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdfGerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
arihantmobileselepun
 
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdfFor Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
arihantmobileselepun
 
Find and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdfFind and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdf
arihantmobileselepun
 
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
arihantmobileselepun
 
Assume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdfAssume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdf
arihantmobileselepun
 
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdfDeclining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
arihantmobileselepun
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
arihantmobileselepun
 
A population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdfA population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdf
arihantmobileselepun
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
arihantmobileselepun
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
arihantmobileselepun
 
Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
arihantmobileselepun
 
Write the interval notation for the set of numbers graphed. Solu.pdf
Write the interval notation for the set of numbers graphed.  Solu.pdfWrite the interval notation for the set of numbers graphed.  Solu.pdf
Write the interval notation for the set of numbers graphed. Solu.pdf
arihantmobileselepun
 
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdfHyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
arihantmobileselepun
 
Why doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdfWhy doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdf
arihantmobileselepun
 
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
arihantmobileselepun
 
Which of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdfWhich of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdf
arihantmobileselepun
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
arihantmobileselepun
 

More from arihantmobileselepun (20)

How are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdfHow are the epimysium and tendons relatedA. The epimysium surroun.pdf
How are the epimysium and tendons relatedA. The epimysium surroun.pdf
 
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdfHow does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
How does DNA differ from RNAA. RNA uses only purinesB. RNA uses.pdf
 
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdfGerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
Gerard throws a discus distances of 10 meters, 14.5 meters, 14.8 met.pdf
 
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdfFor Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
For Printing and personal use ONLY. DO NOT Hand in this copy of the a.pdf
 
Find and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdfFind and provide a link to a multi-year capital plan from any Illino.pdf
Find and provide a link to a multi-year capital plan from any Illino.pdf
 
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdfA geneticist is working with a new bacteriophage called phage Y3 that.pdf
A geneticist is working with a new bacteriophage called phage Y3 that.pdf
 
Assume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdfAssume that the four living species in the figure below evolved from.pdf
Assume that the four living species in the figure below evolved from.pdf
 
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdfDeclining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
Declining BalanceThe cost of the asset $10,000.00 The salvage v.pdf
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
aphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdfaphase The spindle contracts and the sister chromatids are separated.pdf
aphase The spindle contracts and the sister chromatids are separated.pdf
 
A population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdfA population of wild deer mice includes individuals with long or sho.pdf
A population of wild deer mice includes individuals with long or sho.pdf
 
Who is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdfWho is it important that the lymphatic system operate separately f.pdf
Who is it important that the lymphatic system operate separately f.pdf
 
Provide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdfProvide a full explanation to the below question.1. Summarize the .pdf
Provide a full explanation to the below question.1. Summarize the .pdf
 
Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
 
Write the interval notation for the set of numbers graphed. Solu.pdf
Write the interval notation for the set of numbers graphed.  Solu.pdfWrite the interval notation for the set of numbers graphed.  Solu.pdf
Write the interval notation for the set of numbers graphed. Solu.pdf
 
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdfHyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
Hyenas are diploid organisms, and their gametes contain 20 chromosome.pdf
 
Why doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdfWhy doesnt hemoglobin have any intermediate conformations between .pdf
Why doesnt hemoglobin have any intermediate conformations between .pdf
 
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
4. What is gyanandromorphy How does this happenSolutionAnswe.pdf
 
Which of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdfWhich of the following requires the mitochondria to create contact po.pdf
Which of the following requires the mitochondria to create contact po.pdf
 
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdfwhy nitrogen fixation is so importantSolutionNitrogen fixation.pdf
why nitrogen fixation is so importantSolutionNitrogen fixation.pdf
 

Recently uploaded

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 

Recently uploaded (20)

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 

Qestion Please add pre-condition, post-conditions and descriptions .pdf

  • 1. Qestion: Please add pre-condition, post-conditions and descriptions for each member function of the LINKED_LIST_CLASS. Answer with your new version of the program. #include using namespace std; class LIST_NODE { public: int data; // data element of node LIST_NODE *next; // pointer element of node }; class LINKED_LIST_CLASS { public: LINKED_LIST_CLASS(); // default constructor LINKED_LIST_CLASS(LINKED_LIST_CLASS &); // copy constructor ~LINKED_LIST_CLASS(); // destructor void Add(int); // mutator void Print(); // accessor LIST_NODE * Search(int); // accessor void Remove(int); // mutator bool Is_Empty(); // accessor private: LIST_NODE *front; // pointer to front of list }; LINKED_LIST_CLASS::LINKED_LIST_CLASS() { cout << endl << "The default constructor has been called. "; front = new LIST_NODE; front->next = 0; // initialize the next field to null front->data = -10000; } LINKED_LIST_CLASS::LINKED_LIST_CLASS(LINKED_LIST_CLASS & org) { cout << endl << "The copy constructor has been called. "; front = new LIST_NODE;
  • 2. front->next = 0; front->data = -10000; LIST_NODE *p = org.front->next; LIST_NODE *back = 0; while(p!=0) { if (back == 0) { front->next = new LIST_NODE; back = front->next; back->next = 0; back->data = p->data; } else { back->next = new LIST_NODE; back = back->next; back->data = p->data; back->next = 0; } p=p->next; } } LINKED_LIST_CLASS::~LINKED_LIST_CLASS() { cout << endl << "The destructor has been called. "; while (front->next != 0) { LIST_NODE *p = front->next; front->next = front->next->next; delete p; } delete front; front = 0; } void LINKED_LIST_CLASS::Add(int item)
  • 3. { LIST_NODE *p = new LIST_NODE; p->data = item; if (front->next== 0) // empty list { front->next = p; p->next = 0; } else // list has information and is not empty { p->next = front->next; front->next = p; } } void LINKED_LIST_CLASS::Print() { cout << endl; for(LIST_NODE *p = front->next; p != 0; p = p->next) { cout << p->data; if (p->next != 0) { cout << "-->"; } } cout<next; p!=0; p=p->next) { if (p->data == key) return p; } return 0; // key not found in list } void LINKED_LIST_CLASS::Remove(int key) { LIST_NODE *p = Search(key); if (Is_Empty())
  • 4. { cout << key << " is not in the list. No removal performed! "; } else { LIST_NODE *q = front; while (q->next->data != key) { q = q->next; } q->next = p->next; // CRITICAL STEP!!!! delete p; } } bool LINKED_LIST_CLASS::Is_Empty() { return front->next == 0; } int main() { LINKED_LIST_CLASS L1; L1.Add(5); L1.Add(10); L1.Add(29); L1.Print(); LINKED_LIST_CLASS L2 = L1; L2.Print(); L1.Remove(10); L1.Print(); return 0; } Solution #include using namespace std;
  • 5. class LIST_NODE { public: int data; // data element of node LIST_NODE *next; // pointer element of node }; class LINKED_LIST_CLASS { public: LINKED_LIST_CLASS(); // default constructor LINKED_LIST_CLASS(LINKED_LIST_CLASS &); // copy constructor ~LINKED_LIST_CLASS(); // destructor void Add(int); // mutator void Print(); // accessor LIST_NODE * Search(int); // accessor void Remove(int); // mutator bool Is_Empty(); // accessor private: LIST_NODE *front; // pointer to front of list }; LINKED_LIST_CLASS::LINKED_LIST_CLASS() { //Pre-condition : front should be null i.e. list should be empty if(front == 0) { cout << endl << "The default constructor has been called. "; front = new LIST_NODE; front->next = 0; // initialize the next field to null front->data = -10000; //Post-condition : front should not be null if(Is_Empty()) cout<<"Post-condition failed for default constructor"; }
  • 6. } LINKED_LIST_CLASS::LINKED_LIST_CLASS(LINKED_LIST_CLASS & org) { //Pre condition : linked list should not be empty if(!org.Is_Empty()) { cout << endl << "The copy constructor has been called. "; front = new LIST_NODE; front->next = 0; front->data = -10000; LIST_NODE *p = org.front->next; LIST_NODE *back = 0; while(p!=0) { if (back == 0) { front->next = new LIST_NODE; back = front->next; back->next = 0; back->data = p->data; } else { back->next = new LIST_NODE; back = back->next; back->data = p->data; back->next = 0; } p=p->next; } //Post-condition: Linked list copied to should not be empty if(Is_Empty()) { cout<<"Copy constructor failed";
  • 7. } } } LINKED_LIST_CLASS::~LINKED_LIST_CLASS() { if(front != 0) //front should be initialized for it to destroy { cout << endl << "The destructor has been called. "; while (front->next != 0) { LIST_NODE *p = front->next; front->next = front->next->next; delete p; } delete front; front = 0; if(front != 0) //front is not destroyed if it is not null { cout<< "Destructor failed"; } } } void LINKED_LIST_CLASS::Add(int item) { //Pre-condition: item is already added in linked list if(Search(item) == 0) { LIST_NODE *p = new LIST_NODE; p->data = item; if (front->next== 0) // empty list { front->next = p; p->next = 0;
  • 8. } else // list has information and is not empty { p->next = front->next; front->next = p; } //Post-condition : item should be at the front of linked list if(front->next->data != item) cout<<"Add operation failed"<next; p != 0; p = p->next) { cout << p->data; if (p->next != 0) { cout << "-->"; } } cout<next != 0) { for(LIST_NODE *p = front->next; p!=0; p=p->next) { if (p->data == key) return p; } } return 0; // key not found in list } void LINKED_LIST_CLASS::Remove(int key) { //Pre-condition: Item should be present in list if(Search(key) != 0) { LIST_NODE *p = Search(key); if (Is_Empty()) { cout << key << " is not in the list. No removal performed! ";
  • 9. } else { LIST_NODE *q = front; while (q->next->data != key) { q = q->next; } q->next = p->next; // CRITICAL STEP!!!! delete p; } //Post-condition: Item should not be present in array if(Search(key) != 0) cout<<"Remove failed"; } } bool LINKED_LIST_CLASS::Is_Empty() { return front->next == 0; } int main() { LINKED_LIST_CLASS L1; L1.Add(5); L1.Add(10); L1.Add(29); L1.Print(); LINKED_LIST_CLASS L2 = L1; L2.Print(); L1.Remove(10); L1.Print(); return 0; }