Need help with a C++ Project. Hello, instructions are as followed: Download thre
ID: 3600061 • Letter: N
Question
Need help with a C++ Project.
Hello, instructions are as followed:
Download three files:
sequence.h, sequence.cpp, and lab8driver.cpp from google drive, then revise "sequence.cpp" to complete the functions which are currently stubs: (You only need to finish filling in code for sequence.cpp!)
File folder download link: https://drive.google.com/drive/folders/0B03onwxKddUyQ0U3aWxWbl9FZFU?usp=sharing
(1) the destructor
(2) "insert" method
(3) "remove" method
Run your revised "Sequence" class with client file "lab8driver.cpp" so that it demonstrates that eachoperation which is part of type "Sequence" is implemented correctly. The program should demonstrate that items can be inserted (and removed) from the sequence.
Sample output:
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 400
Flag: true Item: 500
Flag: true Item: 700
Flag: true Item: 300
Flag: false Item: 300
Item: 300 Position: 3
Item: 800 Position: 4
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 400
Flag: true Item: 500
Flag: true Item: 700
Flag: true Item: 300
Value returned by 'is_empty': true
Value returned by 'length': 0
Flag: true Item: 10
Flag: true Item: 20
Flag: true Item: 30
Flag: true Item: 40
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 10
Flag: true Item: 20
Flag: true Item: 30
Flag: true Item: 40
Press any key to continue...
Explanation / Answer
//You can find my changes with CheggEA,, I made changes in sequence.cpp,,
/*************************************************************************
*****
Implementation file for "Sequence" ADT (linked list version)
**************************************************************************
****/
using namespace std;
#include <iostream>
#include "sequence.h"
using namespace std;
/*------------------------------------------------------------------------
-----
Name: default constructor
Purpose: Initialize sequence to empty (create header node, set Num to
0).
--------------------------------------------------------------------------
---*/
Sequence::Sequence()
{
Num = 0;
Head = new Node;
Head->Next = NULL;
}
/*------------------------------------------------------------------------
-----
Name: destructor
Purpose: De-initialize sequence (reset sequence, de-allocate header
node).
--------------------------------------------------------------------------
---*/
Sequence::~Sequence()
{
// (1) Incomplete operation
//
// De-allocate the memory for the data nodes
// De-allocate the memory for the dummy header node
// Set Head to NULL
//Added by CheggEA
Node *cur = Head,*next;
while (cur)
{
next = cur->Next;
delete cur;
cur = next;
}
}
/*------------------------------------------------------------------------
-----
Name: reset
Purpose: Re-initialize sequence (de-allocate all data nodes).
--------------------------------------------------------------------------
---*/
void Sequence::reset()
{
Node * Temp1 = Head->Next;
Node * Temp2;
while (Temp1 != NULL)
{
Temp2 = Temp1;
Temp1 = Temp1->Next;
delete Temp2;
}
Head->Next = NULL;
Num = 0;
}
/*------------------------------------------------------------------------
-----
Name: copy constructor
Purpose: Initialize a new sequence by copying an existing sequence.
--------------------------------------------------------------------------
---*/
Sequence::Sequence( const Sequence& Source )
{
bool Flag;
EType X;
Num = 0;
Head = new Node;
Head->Next = NULL;
for (unsigned I=0; I<Source.Num; I++)
{
Flag = Source.retrieve( X, I );
Flag = insert( X, I );
}
}
/*------------------------------------------------------------------------
-----
Name: assignment operator
Purpose: Assign into a sequence by copying an existing sequence.
--------------------------------------------------------------------------
---*/
Sequence& Sequence::operator=( const Sequence& RHS )
{
bool Flag;
EType X;
if (this != &RHS)
{
reset();
for (unsigned I=0; I<RHS.Num; I++)
{
Flag = RHS.retrieve( X, I );
Flag = insert( X, I );
}
}
return *this;
}
/*------------------------------------------------------------------------
-----
Name: find
Purpose: Locate the first instance of a specified item.
Returns: The item's position if it was located; Num otherwise.
--------------------------------------------------------------------------
---*/
unsigned Sequence::find( const EType& X ) const
{
unsigned Position = 0;
Node * Temp = Head->Next;
while ((Position < Num) && !(X == Temp->Item) )
{
Position++;
Temp = Temp->Next;
}
return Position;
}
/*------------------------------------------------------------------------
-----
Name: retrieve
Purpose: Retrieve the item at position I (unless I is not valid).
Returns: True if the item was retrieved; false otherwise.
--------------------------------------------------------------------------
---*/
bool Sequence::retrieve( EType& X, unsigned I ) const
{
bool Flag = false;
unsigned Position = 0;
Node * Curr = Head->Next;
if (I < Num)
{
while (Position < I)
{
Position++;
Curr = Curr->Next;
}
X = Curr->Item;
Flag = true;
}
return Flag;
}
/*------------------------------------------------------------------------
-----
Name: remove
Purpose: Remove the item at position I (unless I is not valid).
Returns: True if the item was removed; false otherwise.
--------------------------------------------------------------------------
---*/
bool Sequence::remove( EType& X, unsigned I )
{
bool Flag = false;
unsigned Position = 0;
Node * Prev = Head;
Node * Curr = Head->Next;
if (I < Num)
{
while (Position < I)
{
Position++;
Prev = Curr;
Curr = Curr->Next;
}
X = Curr->Item;
// (2) Incomplete operation
//
// Link Curr's predecessor to Curr's successor
// De-allocate the memory for Curr
// Decrement Num by 1
//Added by CheggEA
Prev->Next = Curr->Next;
delete Curr;
--Num;
Flag = true;
}
return Flag;
}
/*------------------------------------------------------------------------
-----
Name: insert
Purpose: Insert X into the sequence at position I. If the sequence
is full, allocate additional space before inserting the item.
Returns: True if the item was inserted; false otherwise.
--------------------------------------------------------------------------
---*/
bool Sequence::insert( const EType& X, unsigned I )
{
bool Flag = false;
unsigned Position = 0;
Node * Prev = Head;
Node * Curr = Head->Next;
Node * Temp;
if (I <= Num)
{
Temp = new Node; // Line 1
if (Temp != NULL)
{
while (Position < I)
{
Position++;
Prev = Curr;
Curr = Curr->Next;
}
Temp->Item = X; // Line 2
//(3) Incomplete operation
// Redirect the links
//
// Increment Num by 1
//Added by CheggEA
Prev->Next = Temp;
Temp->Next = Curr;
++Num;
Flag = true;
}
}
return Flag;
}
-----------------------------------------------------------------------
//output same as posted,,but still posting what I got
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 400
Flag: true Item: 500
Flag: true Item: 700
Flag: true Item: 300
Flag: false Item: 300
Item: 300 Position: 3
Item: 800 Position: 4
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 400
Flag: true Item: 500
Flag: true Item: 700
Flag: true Item: 300
Value returned by 'is_empty': true
Value returned by 'length': 0
Flag: true Item: 10
Flag: true Item: 20
Flag: true Item: 30
Flag: true Item: 40
Value returned by 'is_empty': false
Value returned by 'length': 4
Flag: true Item: 10
Flag: true Item: 20
Flag: true Item: 30
Flag: true Item: 40