2. use the code below, Compile and run it on your machine. Discuss with your par
ID: 3862218 • Letter: 2
Question
2. use the code below, Compile and run it on your machine. Discuss with your partner or instructor. Goal is to make sure you know how the code works.
3. Work in a team, implement three iteration functions for SortedLinkList class:
void ResetIteration()
bool HasNext()
ItemType * GetNextItem()
1 point
4. Notice that GetNextItem() returns a pointer to ItemType, rather than a copy of ItemType. In this way, it enables sharing the list item between the list and its client. To demonstrate, assign a new value to the returned ItemType and print the list again. You will see that the changed value will be printed. Sharing is essential for the Hangman game.
1 point
5. In client-cc.cpp, comment out the call to Print(). Instead, use the three iteration functions to display the content of the list, in the same format as that of calling Print(). 1 point 6. Demonstrate your solution to instructor to receive credits. Submit your sortedLinkList.cpp. Make sure to list all team member' names as comment on top of the cpp file.
//sortedlinkedlist.cpp
#include
using namespace std;
#include "SortedLinkList.h"
// Constructor
SortedLinkList::SortedLinkList ()
{
head = NULL;
}
bool SortedLinkList::IsEmpty () const
{
return head == NULL;
}
bool SortedLinkList::HasNext() const
{
return false;
}
// case1: empty list
// case2.1: adding after tail node
// case2.2: adding in middle of two nodes
// case3: adding to head
void SortedLinkList::Insert(/* in */ ItemType item)
{
NodeType * ptr, * insertAfter;
NodeType * tmp = new NodeType;
tmp->info = item;
ptr = head;
insertAfter = NULL;
while (ptr!=NULL && ptr->info < item)
{
insertAfter = ptr;
ptr = ptr->link;
}
if (insertAfter!=NULL)
{
// tmp goes after insertAfter: case2.1 and case 2.2
insertAfter->link = tmp;
tmp->link = ptr;
}
else { // tmp becomes new head: case1 and case3
tmp->link = head;
head = tmp;
}
}
void SortedLinkList::Delete (/* in */ ItemType item)
{
NodeType * prev = NULL;
NodeType * ptr = head;
while (ptr!=NULL && ptr->info < item)
{
prev = ptr;
ptr = ptr->link;
}
if (ptr!=NULL && ptr->info == item)
{
// item found at ptr;
// two cases:
// case1: prev exists;
// case2: head is equal to item, hence must be deleted
if (prev!=NULL)
{
prev->link = ptr->link;
ptr->link=NULL;
delete ptr;
}
else // head item is deleted
{
head = ptr->link;
ptr->link=NULL;
delete ptr;
}
}
}
void SortedLinkList::ResetList() const
{
currentpos = head;
}
ItemType * SortedLinkList::GetNextItem()
{
return nullptr;
}
void SortedLinkList::Print () const
{
NodeType * tmp = head;
while (tmp!=NULL)
{
cout<info< tmp = tmp->link;
}
}
// Destructor
SortedLinkList::~SortedLinkList ()
{
Free();
}
void SortedLinkList::Free()
{
NodeType * current = head;
while (current!=NULL)
{
NodeType * tmp;
tmp = current->link;
delete current;
current = tmp;
}
head = NULL;
}
// Copy-constructor
SortedLinkList::SortedLinkList (const SortedLinkList& otherList)
{
cout<<"Copy constructor called"<
NodeType* tmp, * prev, * current;
prev = NULL;
current = otherList.head;
while (current!=NULL)
{
tmp = new NodeType;
tmp->info = current->info;
tmp->link = NULL;
if (prev!=NULL)
{
prev->link = tmp;
prev = tmp;
}
else prev = head = tmp;
current = current->link;
}
}
SortedLinkList & SortedLinkList::operator = (const SortedLinkList & otherList)
{
cout<<"= called"<
Free();
NodeType* tmp, * prev, * current;
prev = NULL;
current = otherList.head;
while (current!=NULL)
{
tmp = new NodeType;
tmp->info = current->info;
tmp->link = NULL;
if (prev!=NULL)
{
prev->link = tmp;
prev = tmp;
}
else prev = head = tmp;
current = current->link;
}
return *this;
}
//client.cpp
#include
#include
#include
#include "sortedLinkList.h"
using namespace std;
int main()
{
SortedLinkList myList;
/* initialize random seed: */
srand (time(NULL));
int newNum;
for (int i=0; i<5-1; ++i)
{
newNum = rand();
cout< cout<<",";
myList.Insert(newNum);
}
newNum = rand();
cout< myList.Insert(newNum);
cout<
myList.Print();
/* test Delete()
int tmp;
cin>> tmp;
myList.Delete(tmp);
myList.Print();
*/
return 0;
}
//sortedlinkedlist.h
#include
//typedef std::string ItemType;
typedef int ItemType;
struct NodeType
{
ItemType info;
NodeType * link;
};
class SortedLinkList
{
public:
bool IsEmpty () const;
bool HasNext() const;
void Insert (/* in */ ItemType item);
void Delete (/* in */ ItemType item);
void ResetList() const;
ItemType* GetNextItem();
void Print () const;
// Constructor
SortedLinkList ();
// Copy-constructor
SortedLinkList (const SortedLinkList& otherList);
// = operator
SortedLinkList & operator = (const SortedLinkList & otherList);
// Destructor
~SortedLinkList ();
private:
void Free();
private:
NodeType* head;
};
//client-cc.cpp
// This program demonstrates deep copy via a copy constructor
#include
#include
#include
#include "sortedLinkList.h"
using namespace std;
int main()
{
SortedLinkList myList;
/* initialize random seed: */
srand (time(NULL));
int newNum;
for (int i=0; i<5-1; ++i)
{
newNum = rand();
cout< cout<<",";
myList.Insert(newNum);
}
newNum = rand();
cout< myList.Insert(newNum);
cout<
cout<<"myList:"< myList.Print();
// Deep copy via the copy constructor
SortedLinkList yourList(myList);
//yourList = myList;
cout< cout<<"yourList:"< yourList.Print();
cout<<"Enter a number to delete from myList:";
int tmp;
cin>> tmp;
myList.Delete(tmp);
cout<<"myList:"< myList.Print();
cout<<"yourList:"< yourList.Print();
/*
yourList = myList;
cout<<"yourList:"< yourList.Print();
*/
return 0;
}
this is all in c++
Explanation / Answer
#include #include #include using namespace std; struct node{ int data; node *next;}*p = NULL, *head = NULL, *q = NULL, *np = NULL;int c = 0;void create(int x) { np = new node; np->data = x; np->next = NULL; if (c == 0) { head = np; p = head; p->next = head; c++; } else if (c == 1) { p = head; q = p; if (np->data data np->next = p; head = np; p->next = np; } else if (np->data > p->data) { p->next = np; np->next = head; } c++; } else { p = head; q = p; if (np->data data) { np->next = p; head = np; do p = p->next; } while (p->next != q); p->next=head; } else if (np->data > p->data) { while (p->next !=head && q->data data) { q = p; p = p->next; if (p->next == head && (p->data data)) { p->next = np; np->next = head; } else if (np->data data) { q->next = np; np->next = p; break; } } } }}void traverse(int i){ node *t = head; int c = 0; while (cRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.