Data structures and algorithms
List/.classpath
List/.project
List org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature
List/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.8
List/bin/Lists.class
public synchronized class Lists { static final int QUESIZE = 20; static final int THROTTLE = 5; static Transaction initQue; public void Lists(); public static void main(String[]) throws java.io.IOException; static void loadQue(java.util.Scanner); static void procQue(Transaction[], ListVars); }
List/bin/ListVars.class
public synchronized class ListVars { int dllCount; int transCt; int queAddNx; int queProcNx; public void ListVars(); }
List/bin/productdata.txt
02 VGA_Monitor_square 05 LED_Monitor_square 06 LED_Monitor_wide 10 Flat_Screen_TV_portable 14 Flat_Screen_TV_table 16 Flat_Screen_TV_floor 17 Flat_Screen_TV_wall 20 Computer_Stand_M12 21 Computer_Stand_M22 23 Computer_Stand_M27 25 Computer_Stand_M47
List/bin/productdata2.txt
| 01 | Chair |
| 02 | Ottoman |
| 03 | Recliner |
| 04 | Stool |
| 05 | Bench |
| 06 | Couch |
| 07 | Bed |
| 08 | Futon |
| 09 | Hammock |
| 10 | Mattress |
| 11 | Television |
| 12 | Radio |
| 13 | Desk |
| 14 | Table |
| 15 | Bookcase |
| 16 | Cabinet |
| 17 | Hutch |
| 18 | Chest |
| 19 | Coat_rack |
| 20 | Nightstand |
| 21 | Shelf |
| 22 | Safe |
| 23 | Lamp |
| 24 | Curtain |
| 25 | Blinds |
| 26 | Comforter |
| 27 | Pillow |
List/bin/Record.class
public synchronized class Record { int productID; String prodType; public void Record(); }
List/bin/Transaction.class
public synchronized class Transaction { int transID; String transType; public void Transaction(); }
List/bin/transactions.txt
06 P 10 P 14 D 16 P 17 D 20 P 22 P
List/bin/transactions2.txt
| 02 | P |
| 05 | P |
| 19 | D |
| 06 | D |
| 21 | D |
| 07 | P |
| 08 | D |
| 23 | P |
| 10 | P |
| 11 | P |
| 12 | D |
| 09 | P |
| 13 | D |
| 03 | D |
| 14 | D |
| 15 | D |
| 17 | D |
| 25 | P |
| 18 | D |
| 20 | P |
| 01 | P |
| 22 | P |
| 16 | D |
List/productdata.txt
01 Chair 02 Ottoman 03 Recliner 04 Stool 05 Bench 06 Couch 07 Bed 08 Futon 09 Hammock 10 Mattress 11 Television 12 Radio 13 Desk 14 Table 15 Bookcase 16 Cabinet 17 Hutch 18 Chest 19 Coat_rack 20 Nightstand 21 Shelf 22 Safe 23 Lamp 24 Curtain 25 Blinds 26 Comforter 27 Pillow
List/src/Lists.java
List/src/Lists.java
import
java
.
io
.
*
;
import
java
.
util
.
*
;
public
class
Lists
{
static
final
int
QUESIZE
=
20
;
// size of the processing queue
static
final
int
THROTTLE
=
5
;
// number of transactions to load into the queue at a time
static
Transaction
initQue
=
new
Transaction
();
// used to initialize the Queue.
public
static
void
main
(
String
[]
args
)
throws
IOException
{
ListVars
v
=
new
ListVars
();
// a list of variables used throughout the program
Transaction
[]
transQue
=
new
Transaction
[
QUESIZE
];
// array used for transaction queue
/* Arrays.fill(transQue, initQue); /* since the contents of the queue are objects, each member points to an instantiation
of the Transaction class, so an instance must be associated with each queue Member.
This can be done with a loop, or using the fill static method of the Arrays class. */
?????
// Use a loop to initialize the transaction queue
{
?????
}
String
iRec
;
// used to hold the input record from the productdata file
Scanner
iRecScan
;
// scanner to scan for data in iRec
File
iData
=
new
File
(
"productdata.txt"
);
Scanner
iDataScan
=
new
Scanner
(
iData
);
// scanner to scan for data from iData
File
iTrans
=
new
File
(
"transactions.txt"
);
Scanner
iTranScan
=
new
Scanner
(
iTrans
);
// scanner to scan for data in iTrans
if
(
iDataScan
.
hasNext
())
// While there are records to read from iData
{
// read the 1st record, thus starting the doubly linked list
iRecScan
=
new
Scanner
(
iDataScan
.
nextLine
());
// and set up parsing of the record using a Scanner
v
.
?????
=
new
Record
();
// create the first list member (the next and prev pointers remain null for 1st record
v
.
?????
.
productID
=
iRecScan
.
nextInt
();
v
.
?????
.
prodType
=
iRecScan
.
next
();
v
.
dllBegin
=
v
.
?????
;
// the beginning of the list and
v
.
dllEnd
=
v
.
?????
;
// the end of the list point to the 1st member
v
.
dllCount
++
;
while
(
iDataScan
.
hasNext
())
// While there are more records to load
{
iRec
=
iDataScan
.
nextLine
();
// save the record in the work String iRec
iRecScan
=
new
Scanner
(
iRec
);
// set up parsing of the record
v
.
current
=
new
?????
;
// create a new member to add to the list
v
.
dllEnd
.
next
=
v
.
?????
;
// point the previous end of the list to this new member
v
.
current
.
prev
=
v
.
?????
;
// point the new member to the previous end of the list
v
.
current
.
productID
=
iRecScan
.
nextInt
();
v
.
current
.
prodType
=
iRecScan
.
next
();
v
.
dllCount
++
;
v
.
dllEnd
=
v
.
?????
;
// save this member's address as the new end of the list
// (recall that the identifier of an object simply holds the address of the object)
}
}
while
(
iTranScan
.
hasNext
())
// While there are more transactions to load
{
loadQue
(
iTranScan
,
transQue
,
v
);
// load the trans queue with "THROTTLE" trans at a time
procQue
(
transQue
,
v
);
// process any unprocessed trans in the trans queue
}
// PRINT Doubly Linked List
v
.
current
=
?????
;
// point to the beginning of the list
while
(
v
.
current
!=
null
)
// if you're pointing to a member, print it
{
System
.
out
.
println
(
"ID = "
+
v
.
current
.
productID
+
"; Prod Type = "
+
v
.
current
.
prodType
+
"."
);
v
.
current
=
??????
;
// point to the next list member; a null will indicate you're at the end of the list
}
System
.
out
.
println
(
"Total members = "
+
v
.
dllCount
);
iDataScan
.
close
();
iTranScan
.
close
();
}
// E n d M A I N
static
void
loadQue
(
Scanner
iTranScan
,
?????
transQue
,
ListVars
v
)
throws
IOException
{
int
transCt
;
// trans count processed in the current run of this method
transCt
=
0
;
do
{
transQue
[
v
.
?????
]
=
new
Transaction
();
// the queue is an array of Transaction objects, so must instantiate a new object
transQue
[
v
.
?????
].
transID
=
iTranScan
.
nextInt
();
transQue
[
v
.
?????
].
transType
=
iTranScan
.
next
();
transCt
++
;
System
.
out
.
println
(
"Trans ID "
+
transQue
[
v
.
queAddNx
].
transID
+
" Trans Type: "
+
transQue
[
v
.
queAddNx
].
transType
+
"."
);
v
.
?????
=
(
v
.
queAddNx
<
QUESIZE
-
1
)
?
++
v
.
queAddNx
:
0
;
/* point to next entry to add into; If already pointing to the last
array element (QUESIZE - 1), then point back to index 0. */
System
.
out
.
println
(
"v.queAddNx = "
+
v
.
queAddNx
);
}
while
(
iTranScan
.
hasNext
()
&&
(
transCt
<
THROTTLE
));
// exit the loop if you hit end of file or the throttle limit
v
.
?????
+=
transCt
;
// accumulate all trans loaded in all runs of this method
}
// E n d l o a d Q u e
static
void
procQue
(
Transaction
[]
transQue
,
ListVars
v
)
{
int
x
;
for
(
x
=
0
;
x
<
QUESIZE
;
x
++
)
System
.
out
.
println
(
"Trans ID "
+
transQue
[
x
].
transID
+
" Trans Type: "
+
transQue
[
x
].
transType
+
"."
);
System
.
out
.
println
(
"Total Transaction Count = "
+
v
.
transCt
+
"."
);
while
(
transQue
[
v
.
?????
].
transID
!=
-
1
)
// While there is another transaction to process
{
//
// Process the transaction type HERE
//
// THEN do the following
transQue
[
v
.
?????
].
transID
=
-
1
;
// use -1 to mark the queue member as processed
v
.
queProcNx
=
(
v
.
queProcNx
<
QUESIZE
-
1
)
?
++
v
.
queProcNx
:
0
;
/* If the pointer to the next entry to process increments to
the last element (QUESIZE - 1), then back to 0. */
}
}
// E n d p r o c Q u e
}
// E n d C L A S S
List/src/ListVars.java
List/src/ListVars.java
public
class
ListVars
{
?????
current
=
null
;
// Pointer to current record to process in Linked List
?????
dllBegin
=
null
;
// Pointter to begining record of Linked List
?????
dllEnd
=
null
;
// Pointter to last record of Linked List
int
dllCount
=
0
;
// Number of records in the Linked List
int
transCt
=
0
;
// Number of transactions in queue to be processed
int
queAddNx
=
0
;
// Next index position in queue to insert into next
int
queProcNx
=
0
;
// Next index position in queue to process
}
List/src/Record.java
List/src/Record.java
public
class
Record
{
????
next
=
null
;
//Each record points to the NEXT record
????
prev
=
null
;
//Each record points to the Previous record
int
productID
=
0
;
//Each record has an integer id
String
prodType
=
null
;
}
List/src/Transaction.java
List/src/Transaction.java
public
class
Transaction
{
int
transID
=
-
1
;
// Transid matches a product id that the transaction is against. (-1 = not set)
String
transType
=
"."
;
// 'D' to Delete; 'P' to print; '.' = no operation to perform
}
List/transactions.txt
02 P 05 P 19 D 06 D 21 D 07 P 08 D 23 P 10 P 11 P 12 D 09 P 13 D 03 D 14 D 15 D 17 D 25 P 18 D 20 P 01 P 22 P 16 D