how do i expand upon selectionSortType so that you are no longer bound to a list
ID: 3938168 • Letter: H
Question
how do i expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you'll be using pointers to create dynamic arrays.
As you'll recall, when you use pointers in a class you need to do three things:
1) Define the Copy Constructor (So that you have a deep copy instead of a shallow copy)
2) Overload the Assignment Operator for Your Class.
2) Add a Deconstructor To Clean Up Your Pointers.
//////////////////////////////////////////////////////// HEADER FILE ///////////////////////////////////
Explanation / Answer
#include <iostream>
#include <conio.h>
struct prsn {
char name[25];
int age;
struct prsn *Next_Person;
};
struct lndlst {
struct prsn *head;
struct prsn *tail;
int nodeCount;
};
void inintlize (lndlst *ll);
void insrtFrnt (lndlst *ll);
void insrtRear (lndlst *ll);
void dlteRear (lndlst *ll);
void setData (prsn *p);
void PrintList (lndlst *ll);
void main ()
{
lndlst ll; //linked list created
inintlize(&ll); //linked list initialized
int choice = 0;
//list is empty, lets create prsn //dynamically
do
{
<span id="IL_AD4" class="IL_AD">cout</span> << " 1: insert item in front" << endl;
cout << " 2: Insert item in rear" << endl;
cout << " 3: Delete item from front" << endl;
cout << " 4: Delete item from rear" << endl;
cout << " 5: Print List" << endl;
cout << " 8: Exit" << endl;
cin >> choice;
if (choice == 1)
{ insrtFrnt(&ll); }
if (choice == 2)
{ insrtRear(&ll); }
if (choice == 3)
{// DeleteFront(&ll); }
if (choice == 4)
{ dlteRear(&ll); }
if (choice == 5)
{ PrintList(&ll); }
}while (choice != 8);
}
void inintlize(lndlst *ll)
{
ll->head = NULL;
ll->tail = NULL;
ll->nodeCount = 0;
}
void setData(prsn *p)
{
cin >> p->name ;
cin >> p->age ;
p->Next_Person = NULL; //just to inintlize
}
void insrtFrnt(lndlst *ll)
{
if (ll->head == NULL && ll->nodeCount == 0) //means empty list
{
prsn *p;
p = <span id="IL_AD5" class="IL_AD">new prsn</span>;
setData(p);
ll->head = p;
ll->tail = p;
ll->nodeCount++;
}
else
{
prsn *p;
p = new prsn;
setData(p);
p->Next_Person = ll->head ;
ll->head = p;
ll->nodeCount++; //increment counter
}
}
void insrtRear(lndlst *ll)
{
if (ll->head == NULL && ll->nodeCount == 0) //means empty list
{
prsn *p;
p = new prsn;
setData(p);
ll->head = p;
ll->tail = p;
ll->nodeCount++;
}
else
{
prsn *p;
p = new prsn;
setData(p);
ll->tail->Next_Person = p; //now point tail of second last element to last
ll->tail = p; //yes tail is now the new element inserted
ll->nodeCount++; //increment counter
}
}
void dlteRear(lndlst *ll)
{
prsn *tempLast;
prsn *temp2ndLast;
tempLast = ll->head ;
if (ll->nodeCount > 0 )
{ //we can use for loop with nodeCount or the following method
while (tempLast->Next_Person != NULL)
{
temp2ndLast = tempLast;
tempLast = tempLast->Next_Person ;
}
temp2ndLast->Next_Person = NULL;
delete tempLast;
ll->nodeCount --;
}
}
void PrintList(lndlst *ll)
{
int i = 0;
struct prsn *tempNode;
tempNode = ll->head ;
cout << "The linked list is..." << endl;
for (i = 0; i < ll->nodeCount ; i++)
{
cout << tempNode->name <<" " <<tempNode->age << endl; ;
tempNode = tempNode->Next_Person ;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.