Stacks

Pages: 12
The files are two separate files but I'm not sure where to declare the sortedInsert function and do I just add #include<stack> in the header file?
I'm not sure where to declare the sortedInsert function

Function prototypes/declarations are in the header file usually, so other translation units/source file that use the function include the header. The function definition is done in a separate source file. The compiler/linker can then sort out the function exists elsewhere.

do I just add #include<stack> in the header file?

There are three basic ways to do custom includes.

1. Include the standard library headers (and other custom headers) in the header file only, and only the headers the function needs to work. In all source files using the function include the function's header and omit the headers included by the header file.

2. Include the function's header only in source files that use the function, AFTER any standard library headers the function needs to work.

3. Combine 1 & 2. The standard library headers use inclusion guards, custom headers should as well.

Most times I do 3.

C++20 will change when finalized how includes are done with modules.

https://modernescpp.com/index.php/c-20-modules
Okay so I added #include <stack> in the header file.

and the source file contains using namespace std;

here is where I added the function declaration in the header file:
1
2
3
4
5
6
7
8
9
10
11
12
void push(int);
void pop(int &);
bool isEmpty();
void topOfStack();
void bottomOfStack();
void size();
void sumOfStack();
void displayList() const;
void sortedInsert();
void sortStack();			
void reversePrint();
void OddOrEvenPrint();


However the source file doesn't compile. Here is what I am getting:
[Error] variable or field 'sortedInsert' declared void // line 1
[Error] 'stack' was not declared in this scope // line 1
[Note] 'std::stack' // line 1
[Error] expected primary-expression before 'int' // line 1
[Error] expected primary-expression before 'int' // line 1


The errors are based solely around this portion of code from the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void DynIntStack::sortedInsert(stack<int> &stack, int top)
{
    if(isEmpty() || top > stack.top()) // base case: stack is empty
    {
    stack.push(top);
    return;
    }
    // remove the top element
    int temp = stack.top();
    stack.pop();
    sortedInsert(stack, top);
    stack.push(temp);
}

void DynIntStack::sortStack()
{
    if(!isEmpty())
    {
        int top = stack.top();
        stack.pop();
        sortStack(stack);
        sortedInsert(stack, top);
    }
}
Last edited on
If I changed my sortStack function and now I am working on my reversePrint function what is the problem with it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void DynIntStack::reversePrint()
{
    int num;
    stack<int> temp;
    
    while (stack.empty() = false)
    {
        num = stack.top();
        stack.pop();
        temp.push(item);
    }
    
    stack = temp;
    return;
}


The variable 'stack' was not declared. But how do I declare it and where?
You need to think about:
- what that variable called "stack" is for
- what lifespan you want it to have
- what parts of your code are going to need access to it

Once you've understood those things, it should be easy to figure out what sort of variable it should be.

Thanks. Don't really know what you mean by that. Have a good day.
By what? If there's something specific you don't understand, then say what it is and I'll try and elaborate.
I'm not sure what any of what you said means. Coding is like a foreign language to me. For my reversePrint function this is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void DynIntStack::reversePrint()
{
StackNode *currentNode;
StackNode *nextNode;
int stackSize = 0;
int temp;

 
if(isEmpty())
{
std::cout<<"The Stack is empty \n";
return;
}
 
currentNode = top;
while(currentNode!= NULL)
{
currentNode = currentNode->next
stackSize++;
 
 
}

for(int pass=0; pass>stackSize; pass++)
{
currentNode = top;
nextNode = currentNode->next;
 
while(nextNode != NULL)
{
 
            if(currentNode -> value < nextNode->value)
   {
                            temp = currentNode -> value;
                            currentNode -> value= nextNode->value;
                            nextNode->value= temp;
                            
            }
            currentNode = nextNode;
            nextNode = nextNode->next;
           }
     
  }
  return;
}


I'm not sure why my source file won't print the stacks in reverse though. Everything looks to be written correctly.

I read somewhere that I need to have a loop at the end printing the list. How do I write that loop?
Last edited on
I'm not sure why my source file won't print the stacks in reverse though.
What does 'reverse' mean? top -> bottom or bottom -> top?

Everything looks to be written correctly.
There is no cout for a node, hence nothing will be printed.

What is the complicated for loop on line 24 good for?
The 'reverse' means that it will be bottom -> top
Registered users can post here. Sign in or register to post.
Pages: 12