Stack problems - Print bottom and smallest elements in a stack

Hi, i'm trying to write this program that uses a stack. Im just confused on how to create a function to print the bottom element in a stack. What i tried on my own was:
1
2
3
4
5
void stack::print_ends()
{
     cout << data [top] << " " << data [stack_size];
     
}


but the data[stack_size] prints a club and diamond characters. I dont really understand why this is happening.

And my other problem i am having is how to print the smallest element in the stack. Again, i tried on my own, but it didn't work. I took that function out of my program because i couldnt get it to even compile.
 


I know i need a for loop for this, but i just can't seem to get it. I really could use all the help i can get, and any pointers are appreciated. Here is my full code:

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


#include <cstdlib>
#include <iostream>
#include "stack.h"  

using namespace std;

int main(int argc, char *argv[])
{
    stack stacky; 
    
    stacky.push('P');
    stacky.push('U');
    stacky.push('M');
    stacky.push('P');
    stacky.push('K');
    stacky.push('I');
    stacky.push('N');
    
    
    cout << "The smallest element in the stack is "   ;
    stacky.smallest();
    
    cout << "\n\nThe initial stack top and bottom are:\n";
    stacky.print_ends();
    
    
        
    stacky.pop();
    stacky.pop();
    
    cout << "\n\nThe stack top and bottom after changes are:\n";
    stacky.print_ends();
    
    
    cout << "\n\nThe smallest element in the stack is "   ;
    stacky.smallest();

    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


.h 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;

// Implementation file for the stack

const int stack_size = 1000;

class stack
{
      private:

         char data [stack_size];

         int top;
      public:

         stack ();

         char pop ();

         void push (char item);

         bool empty ();

         bool full ();
         void print_ends();
         char smallest ();
         
};


stack::stack ()
{
   top = -1;
}


char stack::pop ()
{

     if (empty ())
     {
        cout << "\n\nStack error: pop";
        cout << "\nPopping an empty stack";
        cout << "\nReturning a space";
        return ' ';
     }
     else 
     {
        top--;
        return data [top + 1];
     }
}
 

void stack::push (char item)
{

     if (full ())
     {
        cout << "\n\nStack error: push";
        cout << "\nPushing onto a full stack";
     }
     else // OK to push
     {
          top++;
          data [top] = item;
     }
}


bool stack::empty ()
{
     return top == -1;
}

bool stack::full ()
{
     return top == stack_size - 1;
}

void stack::print_ends()
{
     cout << data [top] << " " << data [stack_size];
     
}

char smallest ();
{
     char i;
     small = 0;
     
     for (i = 0, i < stack_size, i++)
         if (i > small)
         
         small = i
     return small;
}




Last edited on
the indexes of an array of 'N' elements go from '0' to 'N-1' , then just use 'stack_size - 1' as index
About the function for the smallest element:

1
2
3
4
5
6
7
8
9
10
11
char smallest (); // Should be char stack::smallest() (without semicolon)
{
     char i; // Should be int i;
     small = 0; // Should be char small = 0;
     
     for (i = 0, i < stack_size, i++) // Semicolons, not commas
         if (i > small) // Should be if (data[i] < small)
         
         small = i // Missing semicolon, and should be small = data[i];
     return small;
}


Also, about the
for (i = 0; i < stack_size; i++)
I'm not sure the i < stack_size part is correct.
You're looping over the entire "data" array...even if the stack doesn't have that many elements yet.
Thanks, that was a lot of help. Ok, so my smallest function now looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char stack::smallest ()
{
     int i;
     char small;
     
     small = 0;
     
     for (i = 0; i < data [stack_size]; i++)
         if (data[i] < small)
         {
         small = data[i];
         }
         
     return small;
}

But i still cannot get it to print the smallest value in the stack. Im guessing im still getting the
data [stack_size] wrong....



Last edited on
Topic archived. No new replies allowed.