C++ implementation of stack using array - c++

I have studied the algorithm from Introduction to Algorithm and then
I have written this code. But in my output another value is showing for index 0. and when I use pop function it display 1 instead of 3
#include <iostream>
int top;
void initialise_top(){
top = -1;
}
bool stack_empty(int a[]){
if(top == -1)
return true;
else
return false;
}
void push(int a[], int x, int s){
if(top < s - 1){
top = top + 1;
a[top] = x;
}
else
std::cout << "overflow" << "\n";
}
int pop(int a[]){
if (stack_empty(a) == true)
std::cout << "Underflow" << "\n";
else{
--top;
return a[top+1];
}
}
void display(int a[]){
for(int i = 0;i <= top; i++){
std::cout << a[i] << " ";
}
}
int main()
{
int arr[7];
push(arr,15,7);
push(arr,6,7);
push(arr,2,7);
push(arr,9,7);
push(arr,17,7);
push(arr,3,7);
display(arr);
std::cout << "\n";
int out = pop(arr);
std::cout << pop << "\n";
return 0;
}
Here is the snapshot of the output
enter image description here

In your implementatiton you have "initialise_top()" function.
void initialise_top(){
top=-1;
}
But you don't call it in main function. If you don't call it you can't initialize "top" variable and "top" variable will hold garbage value.
You can read details in here :
Default variable value
And also in theese lines you have some mistakes:
int out=pop(arr);
std::cout<<pop<<"\n";
you must print "out" variable :
std::cout << out << "\n";
You can look to corrected code for your implementation in here :
https://repl.it/JaOd/0

I have this stack array code in C. You can use it as your guide in implementing it to C++.
#include <stdio.h>
#include <stdlib.h>
void push(void);
void pop(void);
int a[5];
int top = -1;
int counter = 0;
int choice;
main() {
do{
printf("*********************************************\nSTACK\nPress the
corresponding button you desire.\n\nPress 1 to push a number to
stack.\nPress 2 to display the current stack.\nPress 3 to pop the current
stack.\nPress 0 to exit.\n\n");
scanf("%d", &choice);
if(choice == 0){
choice = 0;
}
else if(choice == 1){
push();
}
else if(choice == 2){
int i;
printf("Current Stack:\n");
for(i = 0;i <= 4;i++){
printf("%d", a[i]);
}
printf("\n\n");
}
else if(choice == 3){
pop();
}
}while(choice != 0);
}
void push(){
if(top <= 3){
int input;
printf("Enter number to push: ");
scanf("%d", &input);
top = top + 1;
a[top] = input;
int i;
printf("Current Stack:\n");
for(i = 0;i <= 4;i++){
printf("%d", a[i]);
}
printf("\n\n");
}else{
printf("Out of Bounds\n\n");
exit(0);
}
}
void pop(){
if(top >= 0){
printf("You just popped: ");
printf("%d \n\n", a[top]);
a[top] = 0;
printf("Current Stack:\n");
int i;
for(i = 0;i <= 4;i++){
printf("%d", a[i]);
}
printf("\n\n");
top = top - 1;
}else{
printf("Out of Bounds\n\n");
exit(0);
}
}

#include <iostream>
int top;
void initialise_top(){
top=-1;}
bool stack_empty(int a[]){
if(top==-1)
return true;
else
return false;
}
void push(int a[],int x,int s){
if(top<s-1){
top=top+1;
a[top]=x;
}
else
std::cout<<"overflow"<<"\n";
}
int pop(int a[]){
if (stack_empty(a)==true)
std::cout<<"Underflow"<<"\n";
else{
--top;
return a[top+1];
}
}
void display(int a[]){
for(int i=0;i<=top;i++){
std::cout<<a[i]<<" ";
}
}
int main()
{
**initialise_top();**//this statement initialises top=-1
int arr[7];
//std::cout<<stack_empty(arr)<<"\n";
push(arr,15,7);
push(arr,6,7);
push(arr,2,7);
push(arr,9,7);
push(arr,17,7);
push(arr,3,7);
display(arr);
std::cout<<"\n";
int out=pop(arr);
std::cout<<**out**<<"\n";
return 0;
}
1.In your program the value of top=1 when the first element 15 is inserted. due
to this another value is shown for index 0.
So to have top=0, call the function initialise_top(); in main function.
2.To display 3 instead of 1 use
std::cout<<out<<"\n";
Modifications in the program are bold.

I have tried to improve my code. Please tell me if can improve.
#include <iostream>
#define max 1000
class Stack{
int top;
public:
int a[max];
Stack(){
top=-1;
}
bool stack_empty();
void push(int x);
int pop();
void display();
};
bool Stack::stack_empty(){
if(top==-1)
return true;
else
return false;
}
void Stack::push(int x){
int s=max-1;
if(top<s){
top=top+1;
a[top]=x;
}
else
std::cout<<"overflow"<<"\n";
}
int Stack::pop(){
if (stack_empty()==true)
std::cout<<"Underflow"<<"\n";
else{
--top;
return a[top+1];
}
}
void Stack::display(){
for(int i=0;i<=top;i++){
std::cout<<a[i]<<" ";
}
}
int main()
{
Stack stack1;
stack1.push(15);
stack1.push(6);
stack1.push(2);
stack1.push(9);
stack1.push(3);
stack1.display();
std::cout<<"\n";
std::cout<<stack1.pop()<<"\n";
stack1.display();
return 0;
}

Related

Tried making a hash table, can't map all keys, also program crashes

I have to make a program with a hash table that maps single random characters into the table. The program kind of works but sometimes it crashes, also it doesn't map every element. Some of them just won't get inside the table and there are always spare spaces in the table. I don't know what to do to solve these 2 problems. I used 3 versions of open adressing and each of them causes the same 2 problems. Sorry for my bad English. Thank you in advance.
Edited. Of course, I forgot about dynamic allocation. But the problem isn't solved.
#include <time.h>
#include <string>
#include <cstdlib>
using namespace std;
int Liniowo(int i, int proby, int rozmiar) // (open adressing, Linear probing)
{
if(i+proby<rozmiar)
return i+proby;
else
{
return -1;
}
}
int Kwadratowo(int i, int proby, int rozmiar) // (open adressing, Quadratic probing)
{
if (i+proby*proby<rozmiar)
return i+proby*proby;
else
{
return -1;
}
}
int Podwojnie(int i, int proby, int rozmiar, char klucz) // (open adressing, Double hashing)
{
if (i*(klucz*(701%klucz)-klucz%13)<rozmiar&&i*(klucz*(701%klucz)-klucz%13)>0)
return i*(klucz*(701%klucz)-klucz%13);
else
{
return -1;
}
}
int modularnie(char c,int rozmiar) // modular
{
return c%rozmiar;
}
void dodaj(char *tab,int max, char c) // add an element
{
int i=modularnie(c, max);
if (tab[i]== '\0')
tab[i]=c;
else
{
int u=0;
int h;
while (tab[i]!= '\0'&&h!=-1)
{
u++;
// h=Kwadratowo(i, u, max);
h=Podwojnie(i,u,max,c);
}
if (h!=-1)
tab[h]=c;
else
cout << "no niestety, nie udalo sie wstawic " <<endl; //"I couldn't map the element"
}
}
int wyszukaj(char *tab,int max, char c) // search an element
{
int i=modularnie(c, max);
int j=i;
if (tab[i]== '\0')
return -1;
while (tab[i]==c)
{
i=(i+1)%max;
if((i==j)||(tab[i]== '\0'))
return -1;
}
return i;
}
int usun(char *tab,int max, char c) // remove an element
{
int r,j,i=wyszukaj(tab,max,c);
j=i;
if (i==-1)
return -1;
tab[i]= '\0';
while (tab[(++i)%max]!= '\0')
{
i%=max;
r=modularnie(tab[i],max);
if (((i<r)&&(r<=j)) || ((r<=j)&&(j<i)) || ((j<i)&&(i<r)))
{
tab[j]=tab[i];
tab[i]= '\0';
j=i;
continue;
}
}
return 0;
}
int main()
{
srand( time( NULL ) );
int ile;
cout << "podaj wielkosc tablicy: "; //"Type the size of the table"
cin >> ile;
char* tab; // EDITED
tab=new char(ile);
for (int n=0; n<ile; n++)
{
tab[n]= '\0';
}
char e;
for (int i=0; i<ile; i++)
{
e='!'+rand()%127;
dodaj(tab, ile, e);
}
for(int j=0; j<ile; j++)
{
cout << j << ", " << tab[j] << endl;
}
return 0;
}

Change Karnaugh Map simplifier from SOP to POS result

It's my program to simplify Karnough map. I need to change it from SOP to POS for university project. I changed already signs from + to * and from * to +. All i have to do now is to make it read zero-s from text file instead of one's. i tried everything to solve that problem, but i have no idea. I have got two txt files, one with input and one with output. At this moment i need to put in input 0's instead of 1's and in the other side to, to make it work. Please help me, my life depends of that
#include <iostream>
#include <fstream>
#include <cmath>
#include <windows.h>
using namespace std;
//?????
int pow2(int n) // ??2?????,2^n
{
int result=1
;
while(n>0)
result*=2,n--;
return result;
}
int combination(int n,int r) // ?????C_n^r,? n! / ((n-r)!*(r)!)
{
int fm=1,fz=1;
for(int i=1;i<=r;i++,n--)
{
fm*=i;
fz*=n;
}
return fz/fm;
}
void d2b(int d,char* b,int n) // ???????,??????d???????????b?
{
for(n--;n>=0;n--)
{
b[n]='0'+d%2;
d/=2;
}
}
int ispair(char* a1,char* a2,int n) // ????????????????
{
int x,y=0;
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
x=i,y++;
if(y==1) return x;
else return-1;
}
bool issame(char* a1,char* a2,int n) // ????
{
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
return 0;
return 1;
}
int left1(char* a,int N) // ???????????,??????1,??????
{
for(int i=0;i<N;i++)
if(a[i]=='1')
return i;
return -1;
}
void copy(char* a1,char* a2,int n) // ??????
{
for(int i=0;i<n;i++)
a2[i]=a1[i];
}
bool isinside(int x,char* a,int n) // ???x????????a?
{
for(n--;n>=0;n--)
{
if(a[n]!='x' && a[n]!=(char)('0'+x%2))
return 0;
x/=2;
}
return 1;
}
void output(fstream& file,char* a,int n)
{
for(int i=0;i<n;i++)
{
if(i==0)file<<'(';
if(a[i]=='0'||a[i]=='1')
{
if(a[i]=='0') file<<'¬'<<(char)('a'+i);
if(a[i]=='1') file<<(char)('a'+i);
if(a[i+1]=='0'||a[i+1]=='1'||a[i+2]=='0'||a[i+2]=='1'||a[i+3]=='0'||a[i+3]=='1')
{
file<<'+';
}
else{
file<<')';
}
}
else{}
;
}
file<<'*';
}
int count(char* table,char* a,int n,int N) // ??,????????1???????????a?
{
int counter=0;
for(int i=0;i<N;i++)
{
if(table[i]!='1') continue;
if(isinside(i,a,n)) counter++;
}
return counter;
}
void clean(char* table,char* a,int n,int N) // ??????????????????1,???x
{
for(int j=0;j<N;j++)
if(isinside(j,a,n))
table[j]='x';
}
int main()
{
SetConsoleTitle("???-???????????");
system("color 06");
//????
fstream inputFile("input.txt",ios::in); //??????????input.txt
int valNum; //???
inputFile >> valNum; // ???????????
cout<<"\n ????????: "<<valNum;
cout<<"\n ?????????:\n";
int minTermLength=pow2(valNum); // ??????????2^valNum?
char* minTermExpression=new char[minTermLength]; // ???????????????
int lineOff = pow2(ceil(double(valNum)/2));
// ???????
if (inputFile.is_open())
{
for(int i=0;i<minTermLength;i++) // ?????
{
inputFile>>minTermExpression[i];
if(i%lineOff == 0&&(i!=0))
cout<<"\n";
cout<<"\t"<<minTermExpression[i];
}
inputFile.close();
}
// ??????
else{
cout<<"\n ??input.txt??,???????";
return 0;
}
// ?implication????????
char*** implication=new char**[valNum]; // ????
int nonZeroNum=1;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ?????ON???DC?????????
nonZeroNum++;
for(int i=0;i<valNum;i++) // i-??,???????3?,i=0???????,i=1???,i=2???
{
if(pow2(i)>nonZeroNum)break;
int x=pow2(i-1)*combination(nonZeroNum,pow2(i));
implication[i]=new char*[x];
for(int j=0;j<x;j++)
implication[i][j]=new char[valNum];
}
//?????????
int* countNum=new int[valNum+1];
countNum[0]=0;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ???0?
{
d2b(i,implication[0][countNum[0]],valNum); // ??????????implication[0]???
countNum[0]++;
}
int isOptimal=0;
while(countNum[isOptimal]>0) // ??????????
{
countNum[isOptimal+1]=0;
for(int i=0;i<countNum[isOptimal]-1;i++)
for(int j=i+1;j<countNum[isOptimal];j++)
{
int x=ispair(implication[isOptimal][i],implication[isOptimal][j],valNum); // ??????????
if(x==-1) continue;
copy(implication[isOptimal][i],implication[isOptimal+1][countNum[isOptimal+1]],valNum);// ???implication??
implication[isOptimal+1][countNum[isOptimal+1]][x]='x'; // ??????????,?????????x,???????????
countNum[isOptimal+1]++;
}
for(int i=0;i<countNum[isOptimal+1]-1;i++)
for(int j=i+1;j<countNum[isOptimal+1];j++)
if(issame(implication[isOptimal+1][i],implication[isOptimal+1][j],valNum)) // ??????
{
for(int k=j;k<countNum[isOptimal+1]-1;k++)
copy(implication[isOptimal+1][k+1],implication[isOptimal+1][k],valNum);
countNum[isOptimal+1]--;
}
isOptimal++;
}
isOptimal--;
//???????
fstream outputFile("output.txt",ios::out);
outputFile<<"F=";
while(left1(minTermExpression,minTermLength)>=0) //?minTermExpression???1
{
bool flag=0; // ???,????
for(int i=0;i<minTermLength&&flag==0;i++)
{
if(minTermExpression[i]!='1') continue;
int counter=0,recorder;
for(int j=0;j<countNum[isOptimal];j++)
if(isinside(i,implication[isOptimal][j],valNum))
counter++,recorder=j;
if(counter!=1) continue;
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
flag=1;
}
if(flag==1) continue;
int termMaxInclude=0;
int recorder=0;
for(int i=0;i<countNum[isOptimal];i++) // ??????????,???????????1???,??????
if(count(minTermExpression,implication[isOptimal][i],valNum,minTermLength)>termMaxInclude)
termMaxInclude=count(minTermExpression,implication[isOptimal][i],valNum,minTermLength),recorder=i;
if(termMaxInclude==0) {isOptimal--; continue;}
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
}
outputFile.close();
// ?????
outputFile.open("output.txt",ios::in);
char finalExpression[201];
outputFile.getline(finalExpression,200);
outputFile.close();
int termMaxInclude=0;
for(;finalExpression[termMaxInclude]!='\0';termMaxInclude++);
finalExpression[termMaxInclude-1]='\0';
outputFile.open("output.txt",ios::out);
outputFile<<finalExpression;
outputFile.close();
cout<<"\n??????????????";
cout<<"\n ??????,??????!";
cin.get();
return 0;
}
The zeros are simply those not in the ones list.

Recursively reverse a stack

I need to reverse a stack using recursion in C++. I can only use pop, push, and reverseStack, no additional functions such as insertAtBottom which I've found while search stackoverflow and the web.
I've tried:
void Stack::reverseStack(){
if (isEmpty())
return;
else{
int x;
pop(x);
reverseStack();
push(x);
}
}
but this creates a stack exactly the same as the original.
You will need to implement a function to insert an item at the bottom an example is
void Stack::insertAtBottom(int item) {
if(isEmpty())
push(item);
else {
int x;
pop(x);
insertAtBottom(item);
push(x);
}
}
At which point you can implement your reverse as follows
void Stack::reverseStack(){
if (isEmpty())
return;
else{
int x;
pop(x);
reverseStack();
insertAtBottom(x);
}
}
EDIT:
If they need to be in one function, the following is a combination of the two
void Stack::reverseStack(bool reverse=true,int item=0){
if(reverse) {
if (isEmpty())
return;
else{
int x;
pop(x);
reverseStack();
reverseStack(false,x);
}
} else {
if(isEmpty())
push(item);
else {
int x;
pop(x);
reverseStack(false,item);
push(x);
}
}
}
Cheers!
#include <iostream>
#include <stack>
using namespace std;
stack<int> S, R; // S is original stack, R is reversed stack
void reverseStack() {
if(!S.empty()) {
R.push(S.top());
S.pop();
reverseStack();
}
return;
}
int main() {
S.push(1);
S.push(2);
S.push(3);
S.push(4);
S.push(5);
reverseStack();
// Check if R is reversed
while(!R.empty()) {
cout << R.top() << " ";
R.pop();
}
return 0;
}
Hope this helps!
here is a solution to this problem in C language:
#include <stdio.h>
// functions that can be used push(), pop(), and isEmpty()
// the idea is to hold all the values in Function Call Stack until the stack becomes empty.
//When the stack becomes empty, we insert all the held items one by one at the bottom of the stack
// stack-> 1 2 3 4 becomes 4 3 2 1 when we print
#define size 4
int stk[size];
int top = -1;
void insertAtLast(int element);
int isStackFull()
{
if(top == size - 1)
return 1;
return 0;
}
void push(int val)
{
if(isStackFull()==1)
return;
else
//Task 2: Complete the logic
stk[++top] = val;
}
int isStackEmpty()
{
//Task 1: Write logic for isStackEmpty()
if (top==-1)
return 1;
return 0;
}
int pop()
{
if(isStackEmpty()==1)
return -1;
else
//Task 2: Complete the logic
return stk[top--];
}
void reverse(){
if(isStackEmpty()==1){
return;
}
int temp=pop();
reverse();
insertAtLast(temp);
}
void insertAtLast(int element){
if(isStackEmpty()==1){
push(element); //imp
return;
}
int topElements=pop(); //imp
insertAtLast(element); //imp
push(topElements);
}
int main() {
push(4);
push(3);
push(2);
push(1);
for(int i=0;i<size;i++){
printf("%d ", stk[i]);
}
printf("\n");
reverse();
for(int i=0;i<size;i++){
printf("%d ", stk[i]);
}
return 0;
}

C++ Double free or corruption Error when I believe I'm only freeing once

I'm having an issue with the last function in my code that destroys the array, I keep getting Double free or corruption which I think means I'm freeing it up twice but I can't figure out how i'm doing that
Main Code
#include "terrible_dynamic_size_array_unsorted.h"
using namespace std;
int main()
{
int_array arraytest;
init(arraytest);
if(arraytest.count==0)
{
cout<<"Empty array created"<<endl;
}
else
{
cout<<"Error in array creation"<<endl;
}
clear(arraytest);
if(arraytest.count==0)
{
cout<<"Array cleared of data"<<endl;
}
else
{
cout<<"Error in clear function"<<endl;
}
for(unsigned int i=0;i<25;i+=2)
{
if(arraytest.count < arraytest.DEFAULT_CAPACITY)
{
add(arraytest,i);
print(arraytest);
}
else
{
add(arraytest,i);
print(arraytest);
}
}
for(unsigned int i=1;i<25;i+=2)
{
if(arraytest.count < arraytest.DEFAULT_CAPACITY)
{
add(arraytest,i);
print(arraytest);
}
else
{
add(arraytest,i);
print(arraytest);
}
}
if(arraytest.capacity == 2*arraytest.DEFAULT_CAPACITY)
{
cout<<"Resize function works properly"<<endl;
}
else
{
cout<<"Resize not working properly"<<endl;
}
if(contains(arraytest,6))
{
cout<<"Number 6 present in Array"<<endl;
}
else
{
cout<<"Number 6 not in Array Contains not working properly"<<endl;
}
if(contains(arraytest,30))
{
cout<<"Number 30 present in Array Contains not working properly"<<endl;
}
else
{
cout<<"Number 30 not in Array"<<endl;
}
if(remove(arraytest,23) && arraytest.count == 24)
{
cout<<"Number 23 removed from Array"<<endl;
}
else
{
cout << arraytest.count << endl;
cout<<"Number 23 not in Array error in remove"<<endl;
}
if(remove(arraytest,24) && arraytest.count == 23)
{
cout<<"Number 24 removed from Array"<<endl;
}
else
{
cout<<"Number 24 not in Array error in remove"<<endl;
}
if(remove(arraytest,0) && arraytest.count == 22)
{
cout<<"Number 0 removed from Array"<<endl;
}
else
{
cout<<"Number 0 not in Array error in remove"<<endl;
}
if(remove(arraytest,35))
{
cout<<"Error in remove function"<<endl;
}
else
{
cout<<"Number not in Array"<<endl;
}
destr(arraytest);
if(*arraytest.data == 0)
{
cout<<"Array destroyed"<<endl;
}
else
{
cout<<"Error in destroy"<<endl;
}
return 0;
}
Function code
#include "terrible_dynamic_size_array_unsorted.h"
using namespace std;
void init(int_array& arr)
{
arr.count = 0; //set count to 0
arr.capacity = arr.DEFAULT_CAPACITY;
arr.data = new int[arr.capacity];
}
void clear(int_array& arr)
{
destr(arr); //destroys array
init(arr); // initializes array
}
void destr(int_array& arr) //function for destroying array
{
delete[] arr.data;
//*arr.data = 0;
arr.count = 0;
}
void print(const int_array& arr) //prints out the array
{
for (unsigned int i = 0; i < arr.count; ++i)
cout << arr.data[i] << " ";
cout << endl;
}
bool contains(const int_array& arr, const int& target) //
{
unsigned int i;
for (i = 0; i < arr.count; ++i)
{
if (arr.data[i] == target) return true;
//else return false;
}
return false;
}
void resize(int_array& arr) //resizes the array --- WORKING
{
arr.capacity *= 2;
int* new_data = new int[arr.capacity];
for (unsigned int i = 0; i < arr.count; ++i)
{
new_data[i] = arr.data[i];
}
arr.data = new_data;
delete [] arr.data;
}
void add(int_array& arr, const int& payload)
{
if ((arr.count == arr.capacity))
resize(arr);
arr.data[++arr.count] = payload;
}
bool remove(int_array& arr, const int& target)
{
unsigned int i = 0;
if (arr.count == 0)
{
return false;
}
while (i <= arr.count && arr.data[i] != target) {i++;}
if (i > arr.count)
{
return false;
}
arr.data[i] = arr.data[arr.count];
arr.count--;
return true;
}
Header file
#include <iostream>
struct int_array {
int* data;
unsigned int count;
unsigned int capacity;
static const unsigned int DEFAULT_CAPACITY = 20;
};
void init(int_array& arr);
void destr(int_array& arr);
void resize(int_array& arr);
void clear(int_array& arr);
void add(int_array& arr, const int& payload);
bool contains(const int_array& arr, const int& target);
bool remove(int_array& arr, const int& target);
void print(const int_array& arr);
The problem lies with the function void destr(int_array& arr)
Thank you.
arr.data = new_data;
delete [] arr.data;
should be
delete[] arr.data;
arr.data = new_data;
Or, the old array will be leaked and arr.data will point to already freed memory - which will then be illegally freed again by destr.

pop() is not working properly

Here is a c++ code on stack.Ignore the extra code here
#include<iostream>
using namespace std;
class mystack
{
private:
int top;
int size;
int * s;
public:
void initialize()
{
top=-1;
cin>>size;
s=new int[size];
}
~mystack(){delete [] s;}
void push()
{
int x;
if(top==size-1)
cout<<"stack overflow!"<<endl;
else
{
cout<<"Enter element to be pushed:";
cin>>x;
top++;
s[top]=x;
cout<<s[top]<<endl;
}
}
int pop()
{
int p=s[top];
if(top==-1)
return 0;
else
{
top--;
return p;
}
}
int maxsize()
{
return size;
}
int isempty()
{
if(top==-1)
return 0;
else
return 1;
}
void display()
{
int i,p=top;
cout<<s[0]<<endl;
for(i=0;i<=p;i++)
cout<<s[i]<<endl;
}
};
int main()
{
int n,i;
cout<<"Enter no. of stacks:";
cin>>n;
mystack * st=new mystack[n];
for(i=0;i<n;i++)
{
cout<<"Enter size of stack "<<i+1<<":";
st[i].initialize();
}
int c,s;
while(1)
{
cout<<"*****Operations*****"<<endl;
cout<<"1.Push 2.Pop 3.Maxsize 4.isempty 5.Display 6.Quit"<<endl;
cout<<"Enter your choice:";
cin>>c;
if(n>1)
{
cout<<"Operation on which stack:";
cin>>s;
}
else
s=1;
if(c==1)
st[s-1].push();
else if(c==2)
{
if(st[s-1].pop()==0)
cout<<"stack underflow!"<<endl;
else
cout<<st[s-1].pop()<<endl;
}
else if(c==3)
cout<<st[s-1].maxsize()<<endl;
else if(c==4)
{
if(st[s-1].isempty()==0)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if(c==5)
st[s-1].display();
else if(c==6)
break;
else
{
cout<<"Wrong input!"<<endl;
continue;
}
}
return 0;
}
Here accessing pop operation gives the element of top-1.I can't understand why.What should I do?When I do return s[top--] same thing is happening.
Since you haven't gotten back to this, I am going to presume you've already found your logic error.
So here is the one error I found. There may be more, I quit looking ...
In the following code, how many times is pop() being called?
else if(c==2)
{
if(st[s-1].pop()==0)
cout<<"stack underflow!"<<endl;
else
cout<<st[s-1].pop()<<endl;
}