Programim C++ detyre, krijim dhe testing i nje double list link
Tirana Center of Technology
—————————driver.cpp———————-
//driver.cpp
//written to test doubly-linked list ADT
//ItemType must have << and >> operators defined
#include <fstream>
#include <iostream>
#include <string>
#include “sortlist.h”
using namespace std;
template <class ItemType>
void PrintAscending(ostream& dataFile, SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// dataFile is open for output
//post: each component in list has been written to dataFile
// dataFile is still open
template <class ItemType>
void PrintDescending(ostream& dataFile, SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// dataFile is open for output
//post: each component in list has been written, in reverse order, to dataFile
// dataFile is still open
void DisplayMenu(int& choice);
//pre: listA and listB have been initialized
//post: displays a menu for list operations
template <class ItemType>
void Insert(SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// item to insert is entered by the user in the function
//post: Calls the ADT InsertItem and prints a message indicating
// success or failure
template <class ItemType>
void Delete(SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// item to delete is entered by the user within the function
//post: Calls the ADT delete function and prints a message
// indicating success or failure.
template <class ItemType>
void Retrieve(SortedType<ItemType> list);
//pre: none
//post: prompts user for item to retrieve from list; displays message
// indicating whether or not item was found in the list
int main()
{
SortedType<int> listA, listB, listC;
int choice;
while (true)
{
DisplayMenu(choice);
switch (choice)
{
case 0: return 0;
case 1: Insert(listA, “A”);
break;
case 2: Insert(listB, “B”);
break;
case 3: Delete(listA, “A”);
break;
case 4: listA.MakeEmpty();
break;
case 5: Retrieve(listA);
break;
case 6: listC = listB = listA;
break;
case 7: listA.RemoveFirst();
break;
case 8: listA.RemoveLast();
break;
case 9: PrintAscending(cout, listA, “A”);
PrintAscending(cout, listB, “B”);
PrintAscending(cout, listC, “C”);
break;
case 10: PrintDescending(cout, listA, “A”);
PrintDescending(cout, listB, “B”);
PrintDescending(cout, listC, “C”);
break;
default: return 0;
}//end switch
}//end while
}//end main
template <class ItemType>
void PrintAscending(ostream& dataFile, SortedType<ItemType>& list, string listName)
{
ItemType item;
int length = list.LengthIs();
if (length == 0)
{
dataFile << “List ” << listName << ” is empty.\n”;
return;
}
dataFile << “List ” << listName << ” contains the following items:\n”;
list.ResetList();
for (int count=0; count<length; count++)
{
list.GetNextItem(item);
dataFile << item;
dataFile << ” “;
}
dataFile << endl;
}
template <class ItemType>
void PrintDescending(ostream& dataFile, SortedType<ItemType>& list, string listName)
{
ItemType item;
int length = list.LengthIs();
if (length == 0)
{
dataFile << “List ” << listName << ” is empty.\n”;
return;
}
dataFile << “List ” << listName << ” contains the following items:\n”;
list.ResetListBackward();
for (int count=0; count<length; count++)
{
list.GetPreviousItem(item);
dataFile << item;
dataFile << ” “;
}
dataFile << endl;
}
void DisplayMenu(int& choice)
{
cout << “Please select from the menu.\n\n”;
cout << “1\tInsert an item to List A.\n”;
cout << “2\tInsert an item to List B.\n”;
cout << “3\tDelete an item from List A.\n”;
cout << “4\tDelete all items from List A. (Make listA empty)\n”;
cout << “5\tRetrieve an item from List A.\n”;
cout << “6\tPerform assignment: listC = listB = listA\n”;
cout << “7\tDelete first element of List A.\n”;
cout << “8\tDelete last element of List A.\n”;
cout << “9\tPrint lists in ascending order.\n”;
cout << “10\tPrint lists in descending order.\n”;
cout << “0\tExit program\n\n”;
cin >> choice;
}
template <class ItemType>
void Insert(SortedType<ItemType>& list, string listName)
{
ItemType item;
cout << “Please enter the item to insert: “;
cin >> item;
if (list.InsertItem(item))
cout << item << ” has been inserted into list ” << listName << “.\n”;
else
cout << “Unable to add ” << item << ” to ” << listName << “.\n”;
}
template <class ItemType>
void Delete(SortedType<ItemType>& list, string listName)
{
ItemType item;
cout << “Please enter the item to delete: “;
cin >> item;
if (list.DeleteItem(item))
cout << item << ” has been deleted from list ” << listName << “.\n”;
else
cout << item << ” was not in the list. List has not been changed.\n”;
}
template <class ItemType>
void Retrieve(SortedType<ItemType> list)
{
ItemType item;
bool found;
cout << “Please enter the item to be retrieved:\n”;
cin >> item;
list.RetrieveItem(item, found);
if (found)
cout << item << ” was in the list.\n”;
else
cout << item << ” was not in the list.\n”;
}