Ok, I updated the SearchableStack Class using the local value since its better practice.
Here is everything you should need - and thanks for helping!
Test #1 (finding something in the stack):
I Push: 1, 2, 3, 4 onto the stack.
Then I Print and its correct: 1234
Then I find: 3
Output:
Enter item to find: 3
In Find
in FIRST while
in FIRST if
in SECOND while
Item IS in the Stack!
But then when I print the Stack after this "find"
Output:
Enter item to find: 3
1
2
3
0
Yikes!
Test #2 (find something not in Stack):
I Push: 1, 2, 3, 4 onto the stack.
Then I Print and its correct: 1234
Then I find: 9
Output:
Enter item to find: 9
Enter item to find: 9
In Find
in FIRST while
in FIRST while
in FIRST while
in FIRST while
in THIRD while
in THIRD while
in THIRD while
in THIRD while
@@@ END OF FUNCTION hmm, no return before here?
Item is NOT in the Stack!
Then I print:
But then when I print the Stack after this "find"
Output:
Enter item to find: 3
3
2
1
0
My Code:
SearchableStack.h
Code:
//SearchableStack.h
#include "Stack.h"
using namespace std;
class SearchableStack : public Stack
{
public:
SearchableStack();
bool find(int);
private:
//Stack tempStack;
}
Stack.h:
Code:
//Stack.h
//Stack Wrapper Class
#include <iostream>
#include "LList.h"
class Stack
{
public:
Stack();
bool isEmpty();
bool isFull();
void push(int);
int pop();
void print();
protected:
int current;
LList list;
};
LList.h:
Code:
//Linked List
//LList.h
#include <iostream>
//#include "Stack.h"
using namespace std;
//const int SIZE = 5;
struct intNode;
typedef intNode* intNodePtr;
struct intNode
{
int value;
intNodePtr next;
};
class LList
{
public:
LList();
~LList();
bool IsEmpty();
void MakeEmpty();
int Length();
int GetItem(int loc);
void Add(int item);
bool DeleteItem(int loc);
void Print();
private:
intNodePtr list;
int size;
};
SearchableStack.cpp
Code:
//SearchableStack.cpp
#include <iostream>
#include "SearchableStack.h"
; //i have no freaking clue...
using namespace std;
SearchableStack::SearchableStack()
{
while(isEmpty() != true)
{
pop();
}
}
bool SearchableStack::find(int n)
{
int temp;
Stack tempStack2;
cout<<"In Find"<<endl;
while(!isEmpty())
{
cout<<"in FIRST while"<<endl;
temp = pop();
tempStack2.push(temp);
if(temp == n)
{
cout<<"in FIRST if"<<endl;
while(!tempStack2.isEmpty())
{
cout<<"in SECOND while"<<endl;
push(tempStack2.pop());
}
return true;
}
}
while(!tempStack2.isEmpty())
{
cout<<"in THIRD while"<<endl;
push(tempStack2.pop());
}
cout<<"@@@ END OF FUNCTION hmm, no return before here?"<<endl;
return false;
}
Stack.cpp
Code:
//Stack.cpp
//Stack Wrapper Class
#include "Stack.h"
Stack::Stack()
{
current = 0;
list.MakeEmpty();
}
bool Stack::isEmpty()
{
if(list.IsEmpty() == true)
return true;
else
return false;
}
bool Stack::isFull()
{
return false;
}
int Stack::pop()
{
if(current == 0)
return 0;
list.DeleteItem(current);
current--;
return current;
}
void Stack::push(int item)
{
current++;
list.Add(item);
}
void Stack::print()
{
list.Print();
}
LList.cpp
Code:
//Linked List
//LList.cpp
#include "LList.h"
LList::LList()
{
list = NULL;
size = 0;
}
LList::~LList()
{
MakeEmpty();
}
bool LList::IsEmpty()
{
if(list == NULL)
return true;
else
return false;
}
void LList::MakeEmpty()
{
intNodePtr tempPtr;
while(list != NULL)
{
tempPtr = list;
list = list->next;
delete tempPtr;
}
size = 0;
}
int LList::Length()
{
return size;
}
int LList::GetItem(int loc)
{
if(loc > size || loc == 0) // If location is out of the list range
return -1; // Exit the function
intNodePtr tempPtr;
int tempCounter = 1; //Temp Counter to keep track of how many nodes we have passed
tempPtr = list;
if(loc == 1) //If we are looking for the first item in the list
return tempPtr->value; //Return the value of the first one
else
{
while(tempCounter < loc)
{
tempPtr = tempPtr->next;
tempCounter++;
}
return tempPtr->value;
}
}
void LList::Add(int item)
{
size++;
if(list == NULL)
{
list = new intNode;
list->value = item;
list->next = NULL;
}
else
{
intNodePtr tempPtr;
tempPtr = list;
while(tempPtr->next != NULL)
{
tempPtr = tempPtr->next;
}
tempPtr->next = new intNode;
tempPtr = tempPtr->next;
tempPtr->value = item;
tempPtr->next = NULL;
}
}
bool LList::DeleteItem(int loc)
{
intNodePtr tempPtr1 = list;
intNodePtr tempPtr2 = list;
for(int i = 0; i < loc - 2; i++)//make this loc -1 if need be (dierbach responce)
{
if (tempPtr2 != NULL)
{
tempPtr2 = tempPtr2->next;
}
}
if(tempPtr2 == NULL)
return false;
else
{
if(loc == 1)
list = list->next;
else
{
if (tempPtr1 != NULL)
{
tempPtr1 = tempPtr2->next;
tempPtr2->next = (tempPtr1->next);
}
}
size--;
delete tempPtr1;
return true;
}
}
void LList::Print()
{
if(size == 0)
{
cout<<"List is empty.."<<endl;
return;
}
intNodePtr tempPtr;
tempPtr = list;
if(size == 1)
cout<<tempPtr->value<<endl;
else
{
cout<<tempPtr->value<<endl;
while(tempPtr->next != NULL)
{
tempPtr = tempPtr->next;
cout<<tempPtr->value<<endl;
}
}
}
If you need anything else please ask.
Thanks!!