Dynamically Initialization of MinStack is not able to do. Why?
#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class Stack
{
int top;
int length;
int *arr;
public:
//Maximum size of Stack
Stack(int len)
{
top = -1;
length=len;
arr=new int[length];
} //constructor
void push(int data);
int pop();
bool isEmpty();
bool isStackFull();
int Size();
void Display();
int getPeek();
};
void Stack::push(int data)
{
if (isStackFull())
{
cout << "Stack Overflow"<<endl;
}
else
{
arr[++top] = data;
}
}
int Stack::pop()
{
if (isEmpty())
{
cout << "Stack Underflow"<<endl;
return 0;
}
else
{
int data = arr[top--];
return data;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
bool Stack::isStackFull()
{
return (top == length-1);
}
int Stack::Size()
{
return (top +1);
}
int Stack::getPeek()
{
return arr[top];
}
void Stack::Display()
{
for(int i=top;i!=-1;i--)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
class SpecialStack: public Stack
{
public:
Stack min1;
SpecialStack(int len):Stack(len)
{
min1= new Stack(len);
}
int pop();
void push(int x);
int getMin();
};
void SpecialStack::push(int x)
{
if(isEmpty()==true)
{
Stack::push(x);
min1.push(x);
}
else
{
Stack::push(x);
int y = min1.pop();
min1.push(y);
if( x < y )
min1.push(x);
else
min1.push(y);
}
}
/* SpecialStack's member method to remove an element from it. This method
removes top element from min stack also. */
int SpecialStack::pop()
{
int x = Stack::pop();
min1.pop();
return x;
}
int SpecialStack::getMin()
{
int x = min1.pop();
min1.push(x);
return x;
}
int main()
{
SpecialStack s(3); //size of stack should be 3
s.push(10);
s.push(20);
s.push(30);
cout<<s.getMin()<<endl;
s.push(5);
cout<<s.getMin();
return 0;
}
Dynamically Initialization of MinStack is not able to do. Why, How to rectify this problem.Please explain.
In the SpecialStack constructor you initialize the base Stack properly, but not the min1 member. They should be handled the same way:
SpecialStack(int len):Stack(len),min1(len)
{ }
When you don't have min1 in the constructors initializer list, the compiler tries do call its default constructor, but the Stack class doesn't have one.
I solved the given problem as shown in the code
But I am getting wrong answer.
I used the Segment tree approach.
node contains max1 (maximum no in given range) and sum(max sum of two no in given range).
Please help.
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
struct node
{
long long int max1;
long long int sum;
};
node constructstutil(int a[],node tree[],int st,int end,int index)
{
if(st==end)
{
tree[index].max1=a[st];
tree[index].sum=a[st];
return tree[index];
}
int mid=(st+end)/2;
node i=constructstutil(a,tree,st,mid,2*index+1);
node j=constructstutil(a,tree,mid+1,end,2*index+2);
int max2=i.max1+j.max1;
if(max2> i.sum&& max2>j.sum)
{
tree[index].max1=max(i.max1,j.max1);
tree[index].sum=max2;
return tree[index];
}
else
{
if(i.sum>j.sum)
{
tree[index].max1=max(i.max1,j.max1);
tree[index].sum= i.sum;
return tree[index];
}
else
{
tree[index].max1=max(i.max1,j.max1);
tree[index].sum=j.sum;
return tree[index];
}
}
}
node* constructst(int a[],int n)
{
int size,k;
k=(int)(ceil(log2(n)));
size=2*(int)pow(2,k)+1;
node* tree=new node[size];
constructstutil(a,tree,0,n-1,0);
return tree;
}
void updatevalueutil(int a[],node tree[],int s,int e,int i,int val,int index)
{
if(i>e||i<s)
return;
if(s==e&& s== i)
{
tree[index].max1=val;
tree[index].sum=val;
return;
}
int mid=(s+e)/2;
updatevalueutil(a,tree,s,mid,i,val,2*index+1);
updatevalueutil(a,tree,mid+1,e,i,val,2*index+2);
int i1=2*index+1;
int i2=2*index+2;
int max2=tree[i1].max1+tree[i2].max1;
if(max2> tree[i1].sum && max2>tree[i2].sum)
{
tree[index].max1=max(tree[i1].max1,tree[i2].max1);
tree[index].sum=max2;
}
else
{
if(tree[i1].sum>tree[i2].sum)
{
tree[index].max1=max(tree[i1].max1,tree[i2].max1);
tree[index].sum= tree[i2].sum;
}
else
{
tree[index].max1=max(tree[i1].max1,tree[i2].max1);
tree[index].sum=tree[i2].sum;
}
}
}
void updatevalue(int a[],int n,node tree[],int i,int val)
{
a[i]=val;
updatevalueutil(a,tree,0,n-1,i,val,0);
}
node queryutil(int a[],node tree[],int qs,int qe,int s,int e,int index)
{
node t;
t.max1=t.sum=0;
if(qs<=s&&qe>=e)
return tree[index];
if(qs>e || qe<s || qs>qe)
return t ;
int mid=(s+e)/2;
node i=queryutil(a,tree,qs,qe,s,mid,2*index+1);
node j=queryutil(a,tree,qs,qe,mid+1,e,2*index+2);
int max2=i.max1+j.max1;
if(max2> i.sum&& max2>j.sum)
{
t.sum=max2;
t.max1=max(i.max1,j.max1);
return t;
}
else
{
if(i.sum>j.sum)
{
t.sum= i.sum;
t.max1=max(i.max1,j.max1);
return t;
}
else
{
t.sum=j.sum;
t.max1=max(i.max1,j.max1);
return t;
}
}
}
long long int query(int a[],int n,node tree[],int qs,int qe)
{
int s=0;
int e=n-1;
node t= queryutil(a,tree,qs,qe,s,e,0);
return t.sum;
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
node* tree=constructst(a,n);
int k;
scanf("%d",&k);
while(k--)
{
char ch;
int index,val;
int qs,qe,ans;
cin>>ch;
if(ch=='U')
{
scanf("%d %d",&index,&val);
updatevalue(a,n,tree,index-1,val);
}
else if (ch=='Q')
{
scanf("%d %d",&qs,&qe);
long long int ans=query(a,n,tree,qs-1,qe-1);
printf("%lld\n",ans);
}
}
}
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;
}
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;
}
This is the skyscraper floors problem from spoj. I m getting SIGABRT when I run the code.Link for the problem is http://www.spoj.com/problems/SCRAPER/. When I run this code on my PC, it runs without any error but spoj's online judge shows SIGABRT error.
#include <iostream>
#include <vector>
using namespace std;
struct floor
{
unsigned int floornum;
bool calldone;
vector <floor*> connection;
floor()
{
floornum=0;
calldone=0;
}
};
struct elevator
{
unsigned int x,y;
elevator()
{
x=y=0;
}
};
struct skyscrapper
{
unsigned int f,e,a,b;
vector <elevator> ele;
vector <floor*> flooraddress; //this is a spoj problem
void read()
{
cin>>f>>e>>a>>b;
elevator *temp;
for(unsigned int i=0;i<e;i++)
{
temp=new elevator;
cin>>(*temp).x>>(*temp).y;
ele.push_back(*temp);
}
for (unsigned int i=0;i<f;i++)
{
floor* tempp=new floor;
(*tempp).floornum=i;
flooraddress.push_back(tempp);
}
}
void allotaddress()
{
unsigned int j,k;
for(unsigned int i=0;i<ele.size();i++)
{
j=ele[i].y;
k=ele[i].x;
while(j<f)
{
if(j!=ele[i].y)
{
(*(flooraddress[j])).connection.push_back(flooraddress[j-k]);
(*(flooraddress[j-k])).connection.push_back(flooraddress[j]);
}
j=j+k;
}
}
}
};
bool flag;
bool traverse(floor* m,int destination)
{
if((*m).calldone==1)
{
return 0;
}
(*m).calldone=1;
if((*m).floornum==destination)
return 1;
if((*m).connection.empty())
return 0;
for(int i=0;i<(*m).connection.size();i++)
{
flag=traverse(((*m).connection[i]),destination);
if(flag==1)
return flag;
}
return 0;
}
int main()
{
int n;
cin>>n;
skyscrapper iit[n];
bool ans[n];
for(int i=0;i<n;i++)
{
iit[i].read();
iit[i].allotaddress();
ans[i]=traverse(iit[i].flooraddress[iit[i].a],iit[i].b);
}
for(int i=0;i<n;i++)
{
if(ans[i]==1)
cout<<"It is possible to move the furniture."<<endl;
else
cout<<"The furniture cannot be moved."<<endl;
}
return 0;
}