added more code
This commit is contained in:
154
CPE212/Project_04/list.cpp
Normal file
154
CPE212/Project_04/list.cpp
Normal file
@ -0,0 +1,154 @@
|
||||
#include <iostream>
|
||||
#include <new>
|
||||
#include "list.h"
|
||||
|
||||
|
||||
Node list;
|
||||
List::List()
|
||||
{
|
||||
head = NULL;
|
||||
num=0;
|
||||
}
|
||||
|
||||
List::~List()
|
||||
{
|
||||
delete head;
|
||||
}
|
||||
|
||||
void List::Append(string newword)
|
||||
{
|
||||
Node* tempPtr=new Node;
|
||||
tempPtr->word= newword;
|
||||
tempPtr->next= NULL;
|
||||
//num++;
|
||||
if (head==NULL)
|
||||
{
|
||||
head=tempPtr;
|
||||
num++;
|
||||
return;
|
||||
//head->next=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
Node *current=head;
|
||||
while(current ->next) current=current->next;
|
||||
current->next = tempPtr;
|
||||
}
|
||||
|
||||
num++;
|
||||
}
|
||||
|
||||
void List::InsertAt(int pos, string newword)
|
||||
{
|
||||
Node* prev = new Node();
|
||||
Node* curr = new Node();
|
||||
Node* newNode = new Node();
|
||||
newNode->word = newword;
|
||||
|
||||
int tempPos = 0; // Traverses through the list
|
||||
|
||||
curr = head; // Initialize current to head;
|
||||
if(head != NULL)
|
||||
{
|
||||
while(curr->next != NULL && tempPos != pos)
|
||||
{
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
tempPos++;
|
||||
}
|
||||
if(pos==0)
|
||||
{
|
||||
|
||||
}
|
||||
else if(curr->next == NULL && pos == tempPos+1)
|
||||
{
|
||||
Append(newword);
|
||||
// Call function to append
|
||||
}
|
||||
else if(pos > tempPos+1 || pos>num || pos<0) throw ListBadPosition();
|
||||
//Position not valid
|
||||
else
|
||||
{
|
||||
prev->next = newNode;
|
||||
newNode->next = curr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
head = newNode;
|
||||
newNode->next=NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void List::Delete(string someword)
|
||||
{
|
||||
Node *prev=head;
|
||||
Node *current=head->next;
|
||||
int wordNotFound=0;
|
||||
while (current != NULL)
|
||||
{
|
||||
// If current word is in list, break out of loop
|
||||
if(current->word == someword) break;
|
||||
else
|
||||
{
|
||||
prev=current;
|
||||
prev=current->next;
|
||||
}
|
||||
}
|
||||
if(current==NULL) throw ListNotFound();
|
||||
// Error handling: if current word is not in list, throw ListNotFound
|
||||
else
|
||||
{
|
||||
prev->next = current ->next;
|
||||
delete current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void List::Replace(string oldword, string newword)
|
||||
{
|
||||
Node *current=head;
|
||||
int oldWordNotFound=0;
|
||||
while (current != NULL)
|
||||
{
|
||||
// if oldword is found in list, replace oldword with newword
|
||||
if(current->word==oldword)
|
||||
{
|
||||
current->word = newword;
|
||||
return;
|
||||
}
|
||||
// if oldword not= to current word, increment wordNotFound
|
||||
oldWordNotFound++;
|
||||
current = current -> next;
|
||||
if(oldWordNotFound==num)
|
||||
break;
|
||||
}
|
||||
// if oldWordNotFound==num, throw ListNotFound
|
||||
|
||||
throw ListNotFound();
|
||||
}
|
||||
|
||||
int List::Length() const
|
||||
{
|
||||
return num;
|
||||
}
|
||||
|
||||
bool List::Find(string someword) const
|
||||
{
|
||||
Node *current=head;
|
||||
int wordNotFound=0;
|
||||
while (current != NULL)
|
||||
{
|
||||
// if current word does not equal someword, increment wordNotFound
|
||||
if(current->word != someword)
|
||||
{
|
||||
wordNotFound++;
|
||||
}
|
||||
// If current word is in list, return true
|
||||
if(current->word == someword) return true;
|
||||
current = current->next;
|
||||
}
|
||||
// if wordNotFound == num, return false
|
||||
if(wordNotFound == num) return false;
|
||||
}
|
101
CPE212/Project_04/list.h
Normal file
101
CPE212/Project_04/list.h
Normal file
@ -0,0 +1,101 @@
|
||||
//
|
||||
// list.h -- 2010 Fall CPE 212 -- Project04 -- Lists
|
||||
//
|
||||
// DO NOT MODIFY OR SUBMIT THIS FILE
|
||||
//
|
||||
|
||||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class ListEmpty { /*** Empty Error Class ***/ };
|
||||
class ListFull { /*** Empty Error Class ***/ };
|
||||
class ListNotFound { /*** Empty Error Class ***/ };
|
||||
class ListBadPosition { /*** Empty Error Class ***/ };
|
||||
|
||||
struct Node
|
||||
{
|
||||
string word; // A word in a phrase
|
||||
Node* next; // Pointer to next node in sequence
|
||||
};
|
||||
|
||||
class List // List ADT used to store words in a phrase
|
||||
{
|
||||
private:
|
||||
Node* head; // Pointer to head of word list
|
||||
int num; // Number of words stored in list
|
||||
|
||||
public:
|
||||
/***** Constructor and Destructor *****/
|
||||
List();
|
||||
// Description of List()
|
||||
// Constructor sets state of list to empty
|
||||
|
||||
~List();
|
||||
// Description of ~List()
|
||||
// Destructor must deallocate all list nodes
|
||||
|
||||
|
||||
/***** Transformer Methods *****/
|
||||
void Append(string newword);
|
||||
// Description of Append(...)
|
||||
// Appends newword to the end of the current phrase
|
||||
// Error Handling: If list is full, throw ListFull object.
|
||||
|
||||
void InsertAt(int pos, string newword);
|
||||
// Description of InsertAt(...)
|
||||
// Inserts newword at position pos.
|
||||
// Positions numbered starting with zero (0 <= pos <= num).
|
||||
// Hint: pos == num ==> append
|
||||
// Word previously at pos should now be at position pos+1
|
||||
// Error Handling: If list is full, throws ListFull object
|
||||
// If pos out of range, throws ListBadPosition object
|
||||
|
||||
void Delete(string someword);
|
||||
// Description of Delete(...)
|
||||
// Deletes the first instance of someword if it is present.
|
||||
// Error Handling: If someword is not in list, throws ListNotFound object
|
||||
|
||||
void Replace(string oldword, string newword);
|
||||
// Description of Replace(...)
|
||||
// Replaces the first instance of oldword with newword.
|
||||
// Error Handling: If oldword not found, throws ListNotFound object
|
||||
|
||||
|
||||
/***** Observer Methods *****/
|
||||
int Length() const;
|
||||
// Description of Length()
|
||||
// Returns number of words in the list
|
||||
// Error Handling: none
|
||||
|
||||
bool Find(string someword) const;
|
||||
// Description of Find(...)
|
||||
// Returns true if someword is in list, false otherwise
|
||||
// Error Handling: none
|
||||
|
||||
|
||||
// DO NOT MODIFY OR SUBMIT Print()
|
||||
void Print() const
|
||||
// Description of Print()
|
||||
// Prints the entire phrase to stdout
|
||||
{
|
||||
Node* temp = head; // Temp variable for scanning through list starting at head
|
||||
|
||||
cout << "Head {"; // Output open brace
|
||||
while (temp != NULL) // While there are words that must be printed...
|
||||
{
|
||||
cout << ' ' << temp->word; // ... print the next word
|
||||
temp = temp->next; // ... and advance to the next node.
|
||||
}
|
||||
|
||||
cout << " }"; // Output close brace
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
BIN
CPE212/Project_04/list.o
Normal file
BIN
CPE212/Project_04/list.o
Normal file
Binary file not shown.
168
CPE212/Project_04/main.cpp
Normal file
168
CPE212/Project_04/main.cpp
Normal file
@ -0,0 +1,168 @@
|
||||
//
|
||||
// main.cpp -- 2010 Fall -- Project04 -- Lists
|
||||
//
|
||||
// Driver program for List ADT -- The text files (read by this code) contain a series of commands
|
||||
// that will help you test your List ADT code by triggering various List class methods.
|
||||
//
|
||||
// DO NOT MODIFY OR SUBMIT THIS FILE
|
||||
//
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <new>
|
||||
#include <cstddef>
|
||||
#include "list.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
ifstream inputs; // Input file for commands
|
||||
char op; // Hold operation and optional char input
|
||||
string value, value2; // Values input from file
|
||||
string comment; // Holds comment from file
|
||||
int pos; // Holds position value from file
|
||||
List* ptr = NULL; // Will point to List object
|
||||
|
||||
// Output usage message if one input file name is not provided
|
||||
if (argc != 2)
|
||||
{
|
||||
cout << "Usage:\n project04 <inputfile>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Attempt to open input file -- terminate if file does not open
|
||||
inputs.open(argv[1]);
|
||||
if (!inputs)
|
||||
{
|
||||
cout << "Error - unable to open input file" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Input and echo header comment from file
|
||||
getline(inputs, comment); // Input and echo the comment appearing in the test file
|
||||
cout << endl << '#' << comment << endl << endl;
|
||||
|
||||
// Process commands from input file
|
||||
inputs >> op; // Attempt to input first command
|
||||
while (inputs)
|
||||
{
|
||||
switch (op) // Process operation input from file
|
||||
{
|
||||
case '#': // Test file comment
|
||||
getline(inputs, comment); // Input and echo the comment appearing in the test file
|
||||
cout << '#' << comment << endl;
|
||||
break;
|
||||
|
||||
case 'c': // Parameterized Constructor
|
||||
cout << endl << "List()";
|
||||
try
|
||||
{
|
||||
ptr = new List(); // Attempt to create a List object
|
||||
cout << " -- Successful" << endl;
|
||||
}
|
||||
catch ( std::bad_alloc )
|
||||
{
|
||||
cout << "Failed : Terminating now..." << endl;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'a': // Append
|
||||
inputs >> value;
|
||||
cout << "Append(" << value << ")";
|
||||
try
|
||||
{
|
||||
ptr->Append(value);
|
||||
cout << " -- successful";
|
||||
}
|
||||
catch (ListFull)
|
||||
{
|
||||
cout << " -- Failed Full List";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case '-': // Delete word
|
||||
inputs >> value;
|
||||
cout << "Delete(" << value << ") -- ";
|
||||
try
|
||||
{
|
||||
ptr->Delete(value);
|
||||
cout << "successful";
|
||||
}
|
||||
catch (ListNotFound)
|
||||
{
|
||||
cout << "Failed Not In List";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case '+': // Insert at position
|
||||
inputs >> pos >> value;
|
||||
cout << "InsertAt(" << pos << ", " << value << ") -- ";
|
||||
try
|
||||
{
|
||||
ptr->InsertAt(pos, value);
|
||||
cout << "Successful" << endl;
|
||||
}
|
||||
catch (ListBadPosition)
|
||||
{
|
||||
cout << "Failed Bad Position" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'p': // Print List
|
||||
cout << "Print() -- ";
|
||||
ptr->Print();
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case 'r': // Replace word
|
||||
inputs >> value >> value2;
|
||||
try
|
||||
{
|
||||
cout << "Replace(" << value << ", " << value2 << ") -- ";
|
||||
ptr->Replace(value, value2);
|
||||
cout << "Successful" << endl;
|
||||
}
|
||||
catch (ListNotFound)
|
||||
{
|
||||
cout << "Failed Not Found in List" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case '?': // Find in List
|
||||
inputs >> value;
|
||||
if ( ptr->Find(value) )
|
||||
{
|
||||
cout << "Find(" << value << ") -- True" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Find(" << value << ") -- False" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'l': // Length of List
|
||||
cout << "Length() -- " << ptr->Length() << endl;
|
||||
break;
|
||||
|
||||
case 'd': // Destructor
|
||||
delete ptr;
|
||||
ptr = NULL;
|
||||
cout << "~List()" << endl << endl;
|
||||
break;
|
||||
|
||||
default: // Error
|
||||
cout << "Error - unrecognized operation '" << op << "'" << endl;
|
||||
cout << "Terminating now..." << endl;
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
inputs >> op; // Attempt to input next command
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // End main()
|
||||
|
BIN
CPE212/Project_04/main.o
Normal file
BIN
CPE212/Project_04/main.o
Normal file
Binary file not shown.
15
CPE212/Project_04/makefile
Normal file
15
CPE212/Project_04/makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# Project04 makefile
|
||||
|
||||
project04: list.o main.o
|
||||
g++ list.o main.o -o project04
|
||||
|
||||
list.o: list.h list.cpp
|
||||
g++ -c list.cpp
|
||||
|
||||
main.o: list.h main.cpp
|
||||
g++ -c main.cpp
|
||||
|
||||
clean:
|
||||
rm *.o project04
|
||||
|
||||
|
38
CPE212/Project_04/p04input1.txt
Normal file
38
CPE212/Project_04/p04input1.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# p04input1.txt -- Test List(), ~List(), Length(), Append()
|
||||
|
||||
# Test Append()
|
||||
c
|
||||
p
|
||||
a See
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a run
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
# Test Length()
|
||||
c
|
||||
l
|
||||
p
|
||||
a Observe
|
||||
l
|
||||
p
|
||||
a Spot
|
||||
l
|
||||
p
|
||||
a running
|
||||
l
|
||||
p
|
||||
a swiftly
|
||||
l
|
||||
p
|
||||
a away
|
||||
l
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
|
44
CPE212/Project_04/p04input2.txt
Normal file
44
CPE212/Project_04/p04input2.txt
Normal file
@ -0,0 +1,44 @@
|
||||
# p04input2.txt -- Test List(), ~List(), Append(), Find()
|
||||
|
||||
# Test successful find
|
||||
c
|
||||
p
|
||||
a See
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a run
|
||||
p
|
||||
? See
|
||||
p
|
||||
? Spot
|
||||
p
|
||||
? run
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
# Test unsuccessful find
|
||||
c
|
||||
p
|
||||
a Observe
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a running
|
||||
p
|
||||
a swiftly
|
||||
p
|
||||
a away
|
||||
p
|
||||
? slowly
|
||||
p
|
||||
? run
|
||||
p
|
||||
? spot
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
|
51
CPE212/Project_04/p04input3.txt
Normal file
51
CPE212/Project_04/p04input3.txt
Normal file
@ -0,0 +1,51 @@
|
||||
# p04input3.txt -- Test List(), ~List(), Delete()
|
||||
|
||||
# Test successful delete
|
||||
c
|
||||
p
|
||||
a See
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a run
|
||||
p
|
||||
a swiftly
|
||||
p
|
||||
a away
|
||||
p
|
||||
- run
|
||||
p
|
||||
- Spot
|
||||
p
|
||||
- See
|
||||
p
|
||||
- away
|
||||
p
|
||||
- swiftly
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
# Test unsuccessful delete
|
||||
c
|
||||
p
|
||||
a See
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a run
|
||||
p
|
||||
a slowly
|
||||
p
|
||||
a away
|
||||
p
|
||||
- Observe
|
||||
p
|
||||
- spot
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
|
38
CPE212/Project_04/p04input4.txt
Normal file
38
CPE212/Project_04/p04input4.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# p04input4.txt -- Test Replace()
|
||||
|
||||
# Test successful replace
|
||||
c
|
||||
p
|
||||
a See
|
||||
p
|
||||
a Spot
|
||||
p
|
||||
a run
|
||||
p
|
||||
p
|
||||
r run walk
|
||||
p
|
||||
r See Observe
|
||||
p
|
||||
r Spot Dot
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
# Test unsuccessful replace
|
||||
c
|
||||
a See
|
||||
a Spot
|
||||
a run
|
||||
a swiftly
|
||||
a away
|
||||
p
|
||||
r spot goat
|
||||
p
|
||||
r goat boat
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
|
31
CPE212/Project_04/p04input5.txt
Normal file
31
CPE212/Project_04/p04input5.txt
Normal file
@ -0,0 +1,31 @@
|
||||
# p04input5.txt -- Test InsertAt()
|
||||
|
||||
# Test successful InsertAt
|
||||
c
|
||||
a See
|
||||
a Spot
|
||||
a run
|
||||
p
|
||||
+ 0 Zero
|
||||
p
|
||||
+ 1 One
|
||||
p
|
||||
+ 4 Four
|
||||
p
|
||||
+ 5 Five
|
||||
p
|
||||
+ 7 Seven
|
||||
p
|
||||
|
||||
# Test unsuccessful InsertAt
|
||||
+ -1 Bad
|
||||
p
|
||||
+ 10 Ten
|
||||
p
|
||||
+ 20 Twenty
|
||||
p
|
||||
d
|
||||
|
||||
|
||||
|
||||
|
BIN
CPE212/Project_04/project04
Normal file
BIN
CPE212/Project_04/project04
Normal file
Binary file not shown.
BIN
CPE212/Project_04/project04_materials.zip
Normal file
BIN
CPE212/Project_04/project04_materials.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user