patterncppMinor
Linked-list-based stack and queue
Viewed 0 times
stackbasedandlistlinkedqueue
Problem
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
#include
#include
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;
pu
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
#include
#include
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;
pu
Solution
Some tips that I think can improve your code:
- As the data encapsulation you should not return the actual location of your stack in the memory because the user of your stack can corrupt the mechanism of the stack. Instead you should return the data included in the node(Whether it is a char, int, or any other type)(I mean
charNode* getHead();)
- Instead of using a
while(true)loop with abreak;statement in its scope try to figure out a good condition for your loop and avoid usingbreak;statements in scopes. (I mean thewhilein the constructor of your charStack)
- If any unwanted condition is happened try to aware the user of your class not by returning a data like any other normal data. In other words in your
pop()method if the stack is empty and a pop action wants to be executed you return the space character that is ambiguous if actually the space character is in the stack.
Context
StackExchange Code Review Q#150193, answer score: 2
Revisions (0)
No revisions yet.