-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch09-linkedList-driver.cpp
More file actions
135 lines (118 loc) · 3.93 KB
/
ch09-linkedList-driver.cpp
File metadata and controls
135 lines (118 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
#include <iostream>
#include <string>
#include "LinkedList.h" // ADT list operations
#include "ArrayList.h"
#include <fstream>
using namespace std;
void listTester(ListInterface<int>* listPtr,ListInterface<int> * arrayPtr)
{
ifstream inFile;
int counter2 = 0;
int value;
//open the text file
inFile.open("Text.txt");
std::cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 1 (true)" << std::endl;
//read in data from the file into both linked list and arraylist
while(!inFile.eof())
{
inFile >> value;
//inserting values into linkedlist
if (listPtr->insert(counter2 + 1,value))
{
try
{
std::cout << "Inserted " << listPtr->getEntry(counter2 + 1) << " at position " << (counter2 + 1) << std::endl;
}
catch (PrecondViolatedExcep except)
{
std::cout << "Exception thrown getting entry inserted at list end!" << std::endl;
std::cout << except.what() << std::endl;
}
}
else
std::cout << "Cannot insert " << listPtr->getEntry(counter2) << " at position " << (counter2 + 1) << std::endl;
//inserting values into arraylist
if (arrayPtr->insert(counter2 + 1, value))
{
try
{
std::cout << "Inserted " << arrayPtr->getEntry(counter2 + 1) << " at position " << (counter2 + 1) << std::endl;
}
catch (PrecondViolatedExcep except)
{
std::cout << "Exception thrown getting entry inserted at list end!" << std::endl;
std::cout << except.what() << std::endl;
}
}
else
std::cout << "Cannot insert " << arrayPtr->getEntry(counter2) << " at position " << (counter2 + 1) << std::endl;
cout << endl;
//increment counter
counter2++;
} // end for
//display unsorted linked list and unsorted arraylist
listPtr->print();
arrayPtr->print();
std::cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 0 (false)" << std::endl;
std::cout << "getLength returns : " << listPtr->getLength() << "; should be 5" << std::endl;
//variables to hold value and index changes in sorting for LinkedList below
int linked_List_Temp;
int linked_List_First;
//generic sorting procedure for linkedlist
for (int i = 1; i <= listPtr->getLength() - 1; i++)
{
linked_List_First = i;
for (int j = i + 1; j <= listPtr->getLength(); j++)
{
if (listPtr->getEntry(j) < listPtr->getEntry(linked_List_First))
{
linked_List_First = j;
}
//swap values from both indexes
linked_List_Temp = listPtr->getEntry(linked_List_First);
listPtr->replace(linked_List_First, listPtr->getEntry(i));
listPtr->replace(i, linked_List_Temp);
}
}
//variables to hold value and index changes in sorting for ArrayList below
int array_List_Temp;
int array_List_First;
//generic sorting procedure for arraylist
for (int i = 1; i <= arrayPtr->getLength() - 1; i++)
{
array_List_First = i;
for (int j = i + 1; j <= arrayPtr->getLength(); j++)
{
if (arrayPtr->getEntry(j) < arrayPtr->getEntry(array_List_First))
{
array_List_First = j;
}
//swap values from both indexes
array_List_Temp = arrayPtr->getEntry(array_List_First);
arrayPtr->replace(array_List_First, arrayPtr->getEntry(i));
arrayPtr->replace(i, array_List_Temp);
}
}
cout << endl;
//print out sorted linked list and sorted array list
cout << "The linked list and array list sorted : " << endl;
listPtr->print();
arrayPtr->print();
// sorted linked list with method
listPtr->sorting();
//print linked List after sort
listPtr->print();
} // end listTester
int main()
{
//linked list created
ListInterface<int>* listPtr = new LinkedList<int>();
//arraylist created
ListInterface<int> *arrayPtr = new ArrayList<int>();
//pass both to listtester array to be processed
listTester(listPtr,arrayPtr);
system("PAUSE");
return 0;
} // end main