Stacks Help witohut class - urgent

Hello, can anyone help, i will post the code below, but i have been given an assignment to create a stack and implement it showing test plans and debugging, but im very stuck and every tutorial i come across uses classes which im not allowed to do, any help on getting it working would be much appreciated.

thanks
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
#include<iostream>
#include<stdlib.h>
using namespace std;

int stack[5];
int top;


void push (int x)
{
	if (top>5)
	{
		cout<<"your stack has overflowed or is full, returning to a past state"<<endl;
		return;
	}
	stack[++top]=x;
cout<<"inserted2"<<x<<endl;
}

void pop()
{
	if(top<0)
	{
		cout<<"stack is underflowed,restoring last state"<<endl;
	return;
	}
	cout<<"deleted"<<stack[top--];
}

void display()
{
	if (top<0)
	{
		cout<<"The stack is empty"<<endl;
		return;
	}
	for ( int i = top;i>=0;i--)
		cout<<stack[i]<< " ";
}



int main;
{
	int choice;
		int ele;
		stack stk;
		while(1)

	{
		cout<< "Implementation of a stack"<<endl;
	cout<<" The following programme represents a stack, which follows the L.I.f.O system\n"endl;
	cout<< "L.I.F.O means Last in, First Out.\n For example, if you stack dinner plates\nthe last one to be stacked ontop of the stack, will be the first to be used"endl;
	cout<<" From the stack you can: Push - adds to the stack, Pop - Removes from the stack"<<endl;
	cout<<"You can also find out if the stack is full or empty, and have the stack be printed for a visual display"<<endl;
	cout<<"1.push, 2.pop, 3.display, 4. exit"<<endl;
	cout<<" Please enter your choice"<<endl;
	cin>>choice;

	switch(choice)
	{

	case 1: cout<<"eneter the number of elements you wish to have, remember the stack can only hold five elements"<<endl;
		cin>>ele;
		stk.push (ele);
		break;

	case 2: cout<<"you have popped an element out!"<<endl;
		stk.pop();
		break;

	case 3: stk.display()

	case 4: exit(0)
	}
		}
		return (0);
}
if (top>5)
the comparison should be == or >= instead

stack[++top]=x;
this can be evaluated to :
1
2
3
4
top++;

stack[ top ] = x; // error : current pos is skipped, what you want is to store x
                          // in stack[ top ] then increment top 


stack[top--];
remember that arrays start at 0, so to get the current last element, you need
to subtract 1 (in ur case, to pre-decrement top)

if(top<0)
this will only become true if ever top becomes < 0 ( -1 downwards )
you should use == instead

for ( int i = top;i>=0;i--)
initialization should start at top -1

stack stk; stack itself is a variable, it's type is int (array)
Last edited on
thank you :D!
Topic archived. No new replies allowed.