#include <iostream.h> #include<conio.h> #include<stdlib.h> Class stack { int stc
ID: 659682 • Letter: #
Question
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
Class stack
{
int stck[6],i,n;
int top;
public:
stack()
{
top = -1;
}
void push(int x)
{
if(top>5)
{
cout << stack over flow;
return;
}
stk[++top] = x;
cout << inserted: << x;
}
void pop()
{
if(top < 0)
{
cout << stack underflow;
return;
}
cout << deleted: << stk[top--]
}
void disply()
{
if(top < 0)
{
cout << stack empty;
return;
}
for(int i = top; i > 0; i--)
cout << stk[i] << ;
}
void space()
{
for(i = 0; i<n; i++)
while(stk[i]! =i) // swap element stk[i] with st[stk[i]]
DSutil.swap(st, i, stk[i]);
}
};
int main()
{
int ch, i, n;
stack st;
clrsr();
while (1) // check user input
{
cout << 1. Push 2. Pop 3. Display 4. Exit Enter your choice:;
cin >> ch;
switch(ch)
{
case 1: cout << Enter element;
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display(); break;
case 4: exit(0);
}
getch(); // returns character read from keyboard
}
}
In addition to what is specified in the question, carefully analyze the running
time and space used by your implementation.
Explanation / Answer
Answer:
Analyzing time and space used by implementation:
i) Space complexity: Defined stack stk[] size is 06. Every push() and pop() will store and read data from same memory space.
-> So that stk[6] size is 6*2 bytes i.e., 12 ( each int is 02 bytes)
-> When creating
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.