SlideShare a Scribd company logo
1 of 9
Download to read offline
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

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
 
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
 
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
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 

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
 
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
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 

More from 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
 
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
 
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
 
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

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

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; }