-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
174 lines (166 loc) · 5.88 KB
/
driver.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "linker.h"
#include "nodeType.h"
//Stylesheet:
//*******************************************************************************************************
// * PROGRAMMED BY : Ngoc Dang Tran
// * CLASS : CS1B
// * SECTION : MW: 9:30 - 11:50
// * FILE NAME : driver.cpp
// * ASSIGNMENT : Linked List
//*******************************************************************************************************
// PURPOSE : Purpose of this lab is to familiarize yourself with Linked List (P.2)
//*******************************************************************************************************
//* The program below displays a menu on the screen, which allows the user to activate any of the modules
/*
**********************************************************
* MAIN MENU *
* <0> Initialize List *
* <1> IsEmptyList *
* <2> Print *
* <a> Print src *
* <b> Print dst *
* <3> Length *
* <4> DestroyList *
* <5> Search *
* <6> Insert First *
* <7> Insert Last *
* <8> Delete Node *
* <9> Copy List *
* <Q> Quit *
**********************************************************
Enter your choice (1-8):*/
//**********************************************************************************************************
//Main program
int main()
{
//Data table
nodeType *srcHead = NULL;
nodeType *srcTail = NULL;
nodeType *dstHead = NULL;
nodeType *dstTail = NULL;
char choice, discard;
int num, searchItem;;
int srcCount, dstCount;
bool empty = false;
bool quit = false;
int newItem = 0;
//Display menu
do
{
system("clear");
cout << left << "**********************************************************" << endl;
cout << "* MAIN MENU *" << endl;
cout << "* <0> Initialize List *" << endl;
cout << "* <1> IsEmptyList *" << endl;
cout << "* <2> Print *" << endl;
cout << "* <a> Print src *" << endl;
cout << "* <b> Print dst *" << endl;
cout << "* <3> Length *" << endl;
cout << "* <4> DestroyList *" << endl;
cout << "* <5> Search *" << endl;
cout << "* <6> Insert First *" << endl;
cout << "* <7> Insert Last *" << endl;
cout << "* <8> Delete Node *" << endl;
cout << "* <9> Copy List *" << endl;
cout << "* <Q> Quit *" << endl;
cout << left << "**********************************************************" << endl;
cout << " Enter your choice (1-9) and Q/q to quit: ";
cin >> choice;
cin.ignore();
//Switch Cases
switch(choice)
{
case '0':
initializeList(srcHead, srcTail, srcCount);
initializeList(dstHead, dstTail, dstCount);
break;
case '1':
empty = isEmptyList(srcHead);
if(!empty)
cout << "\nThe list isn't empty!" << endl;
else
cout << "\nThe list is empty!" << endl;
break;
case '2':
//Check if the list is empty
if(srcHead == NULL)
cout << "The list is empty. Initialize first!" << endl;
else
{
cout << "Please enter (a/b): ";
cin >> choice;
if(choice == 'a')
{
cout << "\nsrcList: ";
print(srcHead);
}
else
{
cout << "\ndstList: ";
print(dstHead);
}
}
break;
case '3':
if(srcHead == NULL)
cout << "The list is empty. Initialize first!" << endl;
else
{
length(srcCount);
cout << "\nLength: " << srcCount << endl;
}
break;
case '4':
if(srcHead == NULL)
cout << "The list is empty. Initialize first!" << endl;
else
destroyList(srcHead, srcTail, srcCount);
break;
case '5':
if(srcHead == NULL)
cout << "The list is empty. Initialize first!" << endl;
else
{
cout << "Enter number to search: ";
cin >> searchItem;
cin.ignore();
if(search(srcHead, searchItem))
cout << searchItem << " is in the list!" << endl;
else
cout << searchItem << " isn't in the list!" << endl;
}
break;
case '6':
cout << "\nEnter value for newItem to insert forward: ";
cin >> newItem;
insertFirst(srcHead, srcTail, srcCount, newItem);
break;
case '7':
cout << "\nEnter value for newItem to insert backward: ";
cin >> newItem;
insertLast(srcHead, srcTail, srcCount, newItem);
break;
case '8':
cout << "\nEnter the number to be " << "deleted: ";
cin >> num;
deleteNode(srcHead, srcTail, srcCount, num);
break;
case '9':
if(srcHead == NULL)
cout << "The list is empty. Initialize first!" << endl;
else
copyList(srcHead, srcTail, srcCount, dstHead, dstTail, dstCount);
break;
case 'Q':
quit = true;
break;
default:
cout << "You have entered an invalid choice. Please enter a number in the range 1-9." << endl;
break;
}
cout << "Press any key to continue..";
cin >> discard;
cin.ignore();
} while(!quit);
return 0;
}