Stack

The question is : "Make a program for creating of Stack S, after this to set N-th from the bottom element of S with values A, exclude from stack first N-1 from bottom elements." I didn't understand what to do here. Can you help me?

presumably the bottom is the first thing pushed?

this is not worded very well. What does exclude from the stack mean?
I can see 2 ways to try to guess what he means..
1) bulk insert. that is the user can push say 10 elements onto the stack all at once. This does not seem to fully fit, though?

or

2) lock a block of the stack. the user puts in his 10 elements, and the stack stores and hides them, you can't pop them off, they are 'excluded' ?? but still stored. The stack operates as normal from there (push into 11th slot, push into 12th, pop pop works, but pop again acts as if empty). Would you have a function to unlock and either allow pops or bulk pop of the stored items??

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
#include <iostream>
using namespace std;
struct elem
{
	int key; // value of element
	elem* next; //pointer to the next element
} *ST1 = NULL, * ST2 = NULL;
void push(int n, elem*& start) //adding of an element into stack 
{
	elem* p = start;
	start = new elem; //creating of an element 
	start->key = n;
	start->next = p; //new “top” position
}
int pop(int& n, elem*& start)
{
	if (start) //prove the stack is not empty
	{
		n = start->key;
		elem* p = start;
		start = start->next;
		delete p; // deletion of the accessible element
		return 1;
	}
	else return 0; // the stack is empty
}
int main()
{
	int num = 1;
	cout << "Enter a number:\n";
	while (num > 0)
	{
		cin >> num;
		if (num > 0)
		{
			if (num % 2)
				push(num, ST1);
			else
				push(num, ST2);
		}
	}
	cout << "\n Stack with Odd values: ";
	while (pop(num, ST1))
	{
		cout << num << " ";
	}
	cout << "\n Stack with Even values: ";
	while (pop(num, ST2))
	{
		cout << num << " ";
	}
}

I have this example

bad example has global variables and its not even named consistently with the problem.
for some unholy reason push and pop are not methods but free functions.

Did your professor give you that garbage? Or did you pull it off the web?

The code is getting ahead of yourself anyway. You need to ask your professor to clarify the question. You can't code something until the problem is clear.
Last edited on
yea, unfortunately my professor gave me that. I asked her for clarify the question.
... and she's a professor OMG !!! :thumbdown:
It's probably rehashed old C code with a C++-ism of a reference thrown in just to pretend it's something like C++.
As C++, perhaps:

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
#include <iostream>

class Stack {
public:
	// To be provided if needed
	Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;

	Stack() {}

	~Stack() {
		for (auto t = top; top != nullptr; top = t) {
			t = top->next;
			delete top;
		}
	}

	// adding of an element into stack
	void push(int n) {
		top = new elem(n, top);
	}

	bool pop(int& n) {
		if (top) {
			const auto p {top};

			n = top->key;
			top = top->next;
			delete p;
			return true;
		}

		return false;
	}

private:
	struct elem {
		int key {}; // value of element
		elem* next {}; //pointer to the next element

		elem() {}
		elem(int ky, elem* nxt) : key(ky), next(nxt) {}
	};

	elem* top {};
};

int main()
{
	Stack ST1, ST2;

	std::cout << "Enter number(s), 0 to exit: ";

	for (int num; std::cin >> num && num != 0; )
		if (num % 2)
			ST1.push(num);
		else
			ST2.push(num);

	std::cout << "\nStack with Odd values: ";

	for (int num {}; ST1.pop(num); )
		std::cout << num << " ";

	std::cout << "\nStack with Even values: ";

	for (int num; ST2.pop(num); )
		std::cout << num << " ";
}

So she said : "For example; You have 5 elements in stacks. 80-60-40-20-10.It's 40.This is the 3th element from bottom.
1)You have to change this element with have value A. "A" you enter from keyboard. 2)You have to access this element,but you have to pop all elements over this value 40. 3)You have to delete all elements down 40.
It's A=100 The result will be S=80 (top) 60 middle A=100(bottom)."
so if you change 40 to 100 she wants
80 60 100 20 10 ? is this correct?
the cheap answer is to change the 40 to 100, directly, which is a violation of the stack structure but it is the most efficient way. She does not want that.
she wants you to pop the 80, save it, pop the 60, save it, pop the 40, discard it, push 100, push 60, push 80.

HINT:
this begs recursion... you can use the recursive stack** to save the popped values without a bunch of extra gibberish or a second stack instance or something.
**that is, the computer's call stack, its automatic but you can use it in recursive routines to eliminate storage of data for this type of routine.
Last edited on
Yea she wants to pop all elements over value 40 and delete all elements down 40. But i don't know how to exactly do that.
As a simple way, perhaps:

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
#include <iostream>
#include <initializer_list>

class Stack {
public:
	// To be provided if needed
	Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;

	Stack() {}

	~Stack() {
		for (auto t = top; top != nullptr; top = t) {
			t = top->next;
			delete top;
		}
	}

	Stack(std::initializer_list<int> il) {
		for (const auto& i : il)
			push(i);
	}

	// adding of an element into stack
	void push(int n) {
		top = new elem(n, top);
	}

	bool pop(int& n) {
		if (top) {
			const auto p {top};

			n = top->key;
			top = top->next;
			delete p;
			return true;
		}

		return false;
	}

private:
	struct elem {
		int key {}; // value of element
		elem* next {}; //pointer to the next element

		elem() {}
		elem(int ky, elem* nxt) : key(ky), next(nxt) {}
	};

	elem* top {};
};

int main()
{
	Stack st1 {80, 60, 40, 20, 10};
	Stack st2;

	for (int val {}; st1.pop(val); )
		if (val != 40)
			st2.push(val);
		else {
			st2.push(100);
			break;
		}

	for (int val; st2.pop(val); st1.push(val));

	for (int val; st1.pop(val); )
		std::cout << val << ' ';

	std::cout << '\n';
}

Last edited on
but, its not a hard coded problem, that is just the example.

something then like this (assumes some functions you may not have, write them or directly
access the appropriate info eg top can just be the top pointer's int value.
I think this is the logic? Anyone see an issue? I made assumptions on what to do if the value being replaced is not in the stack at all. (it will put the new item on the bottom and reinsert everything else above it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Stack::replace(int replacee, int replacer)
{
     //if its not empty and its not the right value..
    if (!empty() && top() != replacee) 
     {   
       tmp =pop(); 
       replace(int replacee, int replacer)
       push(tmp);
      }
      else if(!empty()) //implied:  the top is replacee
      {
         tmp = pop(); //discard     //no real need to assign it. 
         push(replacer);
      }   
      else //empty stack. ** empty at this recursion depth!
       push(replacer);
}

Last edited on
Topic archived. No new replies allowed.