HI below is the code for the Abstract Data Type of LINKED LISTS The main.cpp, wh
ID: 3636367 • Letter: H
Question
HI below is the code for the Abstract Data Type of LINKED LISTS
The main.cpp, which has all the function call etc, is not complete, i.e. it does not fully test all the functions in list.h
Could you please complete the main ?
List.h
#include <iostream>
#include <fstream>
using namespace std;
struct Node;
class listClass
{private: Node* PtrTo(int Position); int Size; Node* Head;
public:
listClass();
~listClass();
bool ListIsEmpty(); int ListLength();
void ListInsert(int NewPosition,int NewItem,bool& Success); void ListDelete(int Position,bool& Success); void ListRetrieve(int Position,int& DataItem,bool& Success);
void ListDisplay(); void SaveLinkedList(char* SaveFileName); void RestoreLinkedList(char* SaveFileName); };
****************************************************
List.cpp
#include "List.h"
struct Node
{
int Item;
Node* Next;
};
listClass::listClass():Size(0),Head(NULL)
{ }
listClass::~listClass()
{
bool Success;
while(!ListIsEmpty())
ListDelete(1,Success);
}
/****************************/
bool listClass::ListIsEmpty()
{
return(Size==0);
}
int listClass::ListLength( )
{
return Size;
}
Node* listClass::PtrTo(int Position)
{ if( (Position<1) || (Position> ListLength( ) ) )
{
return NULL;
}
else
{
Node* Trav=Head;
for(int Skip=1;Skip Trav=Trav->Next;
return Trav;
}
}
void listClass::ListInsert(int NewPosition,int NewItem,bool& Success)
{
int NewLength=ListLength()+1;
Success=( (NewPosition>=1) && (NewPosition<=NewLength));
if(Success)
{
Size=NewLength;
Node* NewPtr=new Node;
Success=(NewPtr!=NULL);
if(Success)
{
NewPtr->Item=NewItem;
if(NewPosition==1)
{//insert new node at beginning of List
NewPtr->Next=Head;
Head=NewPtr;
}
else
{
Node* Prev=PtrTo(NewPosition-1);
NewPtr->Next=Prev->Next;
Prev->Next=NewPtr;
}
}
}
}
void listClass::ListRetrieve(int Position,int& DataItem,bool& Success)
{
Success=( (Position>=1) && (Position<=ListLength()));
if(Success)
{
Node* Cur=PtrTo(Position);
DataItem=Cur->Item;
}
}
/*********************************/
void listClass::ListDisplay()
{
Node* Cur=Head;
while(Cur!=NULL)
{
cout<Item<<' ';
Cur=Cur->Next;
}
}
void listClass::ListDelete(int Position,bool& Success)
{ Node* Cur;
Success=( (Position>=1) && (Position<=ListLength( ) ) );
if(Success)
{ --Size;
if(Position==1)
{//first node
Cur=Head;
Head=Head->Next;
}
else
{
Node* Prev=PtrTo(Position-1);
Cur=Prev->Next;
Prev->Next=Cur->Next;
}
Cur->Next=NULL;
delete Cur;
Cur=NULL;
}
}
void listClass::SaveLinkedList(char* SaveFileName)
{
ofstream F(SaveFileName);
//traverse the list to end, writing out each item
Node* Cur;
for(Cur=Head; Cur!=NULL; Cur = Cur->Next)
{F<Item<<" ";}
F.close( );
}
void listClass::RestoreLinkedList(char* SaveFileName)
{
int NextValue;
ifstream F(SaveFileName, ios::binary);
if(F>>NextValue)
{//treat first value as a special case
cout< Head=new Node;
Head->Item=NextValue;
Head->Next=NULL;
Node* Tail=Head;
while(F>>NextValue)
{
Tail->Next=new Node;
Tail=Tail->Next;
Tail->Item=NextValue;
Tail->Next=NULL;
cout<
}
}
else
Head=NULL;
F.close();
}
*************************************************************************************
main.cpp
#include "List.h"
void main()
{ listClass *list = new listClass();
bool success;
list->ListInsert(1, 10, success);
list->ListInsert(2, 20, success);
list->ListInsert(3, 30, success);
list->ListInsert(4, 40, success);
list->ListDelete(3, success);
list->ListDisplay();
// list->SaveLinkedList("list.txt"); // list->RestoreLinkedList("list.txt"); }
Explanation / Answer
Here Is the Onlne c++ compiler
http://codepad.org/
main.cpp
#include "List.h"
#include
#include
using namespace std;
//void add(int element);
int main (char** argv, int argc)
{
List* MyList = new List();
bool quit = false;
string value;
int element;
while(quit==false)
{
cin>>value;
if(value == "add")
{
cin>>element;
MyList->add(element);
}
if(value=="quit")
{
quit = true;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.