This is my first attempt at building a linked-list-based stack and queue. It is the result of ~90 minutes of trying to implement some of what we're being taught in Algorithms class.
I would like feedback on possible optimisation and oversights. I did try to segment and comment the code the best I could.
This was written and tested on DevCpp, just in case it contains certain quirks or behaves differently on other IDEs.
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class charNode //basic node containing a character and a single link
{
private:
char val;
charNode* next;
public :
charNode();
charNode(char c);
void setVal(char c);
void setNext(charNode* n);
char getVal();
charNode* getNext();
};
class charStack //prototype for stack of char nodes
{
private:
charNode* head;
int counter;
public :
charStack(); //default constructor
charStack(char c); //constructor that sets a value
~charStack(); //destructor
void setHead(charNode* h); //assigns a pointer to the top node of the stack
charNode* getHead(); //returns the address of the top node in stack
void decCounter(); //decrements counter of nodes
void incCounter(); //increments counter of nodes
int getCounter(); //returns the number of nodes in stack
void push(char c); //adds a node to the top of the stack
char pop(); //removes top node and returns its character value
int search(char c); //returns position of character in stack, or 0 if not found.
};
class charQueue //prototype for queue of char nodes
{
private:
charNode* rear;
charNode* front;
int counter;
public :
charQueue(); //default constructor
charQueue(char c); //constructor that initializes the queue with one node
charQueue(char r, char f); //constructor that initializes the queue with two nodes
~charQueue(); //destructor
void setRear(charNode* r); //assigns a pointer to the rear of the queue
void setFront (charNode* f); //assigns a pointer to the front of the queue
void incCounter(); //increments counter of nodes
void decCounter(); //decrements counter of nodes
charNode* getRear(); //returns the address of the rear node in queue
charNode* getFront(); //returns the address of the front node in queue
int getCounter(); //returns the number of nodes in queue
void add(char c); //adds a node to the rear of the queue
char remove(); //removes a node from the front of the queue and returns its value
bool isEmpty(); //checks whether the queue is empty
int search(char c); //returns position of character in queue, or 0 if not found.
};
// Implementation for methods in the node class
charNode::charNode() {this->next=NULL;}
charNode::charNode(char c) {this->val=c; this->next=NULL;}
void charNode::setVal(char c) {this->val=c;}
void charNode::setNext(charNode* n) {this->next=n;}
char charNode::getVal() {return this->val;}
charNode* charNode::getNext() {return this->next;}
//end of node method implementation
// Implementation for methods in the stack class
charStack::charStack(){this->head=NULL; this->counter=0;}
charStack::charStack(char c) {this->head= new charNode(c); this->counter=1;}
charStack::~charStack()
{
charNode* tmp=this->head;
while(1)
{
if(tmp==NULL) break;
head=head->getNext();
delete tmp;
tmp= head;
}
this->counter=0;
}
int charStack::getCounter() {return this->counter;}
charNode* charStack::getHead() {return this->head;}
void charStack::decCounter() {this->counter--;}
void charStack::incCounter() {this->counter++;}
void charStack::setHead(charNode* h) {this->head=h;}
void charStack::push(char c)
{
if (this->head==NULL) {this->head=new charNode(c); this->counter=1; return;}
charNode* tmp= new charNode(c);
tmp->setNext(this->head);
this->head=tmp;
this->incCounter();
//diagnostic cout << endl<< "pushed (" << c << ")! Counter= " << counter << endl;
}
char charStack::pop()
{
if (this->head==NULL) return ' ';
charNode* tmp= this->head;
this->head= this->head->getNext();
char res=tmp->getVal();
delete tmp;
this->decCounter();
//diagnostic cout << endl<< "popped (" << res << ")! Counter= " << counter << endl;
return res;
}
int charStack::search(char c)
{
charNode* tmp= this->getHead();
bool found=false; int loc=0;
while((tmp!=NULL)&&(!found)) {loc++; found=(tmp->getVal()==c); tmp=tmp->getNext();}
if(found) return loc; else return 0;
}
// end of stack method implementation
// Implementation for methods in the stack class
charQueue::charQueue() {this->rear=NULL; this->front=NULL; this->counter=0;}
charQueue::charQueue(char c) {rear=new charNode(c); front=rear; counter=1;}
charQueue::charQueue(char r, char f) {rear= new charNode(r); front= new charNode(f); rear->setNext(front); counter =2;}
charQueue::~charQueue()
{
while(rear!=NULL)
{
charNode* tmp=rear->getNext();
delete rear;
rear=tmp;
}
front=NULL;
counter=0;
}
void charQueue::setRear(charNode* r) {this->rear=r;}
void charQueue::setFront (charNode* f) {this->front =f;}
charNode* charQueue::getRear() {return this->rear;}
charNode* charQueue::getFront() {return this->front; }
void charQueue::incCounter() {this->counter++;}
void charQueue::decCounter() {this->counter--;}
int charQueue::getCounter() {return this->counter; }
void charQueue::add(char c)
{
if(rear==NULL) {rear= new charNode(c); front=rear; counter=1; return;}
charNode* tmp=new charNode(c); tmp->setNext(rear); rear=tmp; this->incCounter();
}
char charQueue::remove()
{
if(rear==NULL) return ' ';
if(this->counter==1) {char res=this->rear->getVal(); this->~charQueue(); return res;}
charNode* tmp=rear;
while(tmp->getNext()!=front) tmp=tmp->getNext();
char res=front->getVal();
delete front; front=tmp; front->setNext(NULL); this->decCounter();
return res;
}
bool charQueue::isEmpty() {return this->getCounter()==0;}
int charQueue::search(char c)
{
charNode* tmp= this->getRear();
bool found=false; int loc=this->getCounter()+1;
while((tmp!=NULL)&&(!found)) {loc--; found=(tmp->getVal()==c); tmp=tmp->getNext();}
if(found) return loc; else return 0;
}
bool isCap(char c)
{
return (((int)c>=(int)'A')&&((int)c<=(int)'Z'));
}
//end of queue method implementation
//main
int main()
{
//using stack of char nodes to reverse a string input
charStack* s=new charStack();
string name; cout << "enter your name: "; cin >> name;
for(int i=0; i<name.length(); i++) s->push(name[i]);
cout << "character to find: "; char f; cin >> f; int loc=s->search(f);
if(!loc) cout << "character not found!" << endl;
else cout << "character found in position #" << s->getCounter()-loc+1<< endl;
cout << "your name reversed: "; while(1) {char c=s->pop(); if(c==' ')break; cout<<c;} cout << endl;
//using a queue of char nodes to detect and display capital letters in a string input
string pass; cout << "Type in a squence of capital and small letters:"; cin >> pass;
charQueue* caps=new charQueue();
for(int i=0; i<pass.length(); i++) if(isCap(pass[i])) caps->add(pass[i]);
cout << "capital character to find: "; cin >> f; loc=caps->search(f);
if(!loc) cout << "character not found!" << endl;
else cout << "character is the capital #" << loc<< endl;
cout << "The string you entered has these capital letters: "; while(!caps->isEmpty()) cout << caps->remove(); cout << endl;
//end of program
system("pause"); return 0;
}//end of main