error while passing values in push operation in stack implementation(Parenthesis matching) - c++

#include <iostream>
#include<string>
using namespace std;
class Stack{
public:
int top;
int size;
string *s;
void isBalanced(Stack *st,string exp);
};
push function is
void push(Stack *st,string x){
if(st->top==st->size-1)
cout<<"Stack full\n";
else{
st->top++;
st->s[st->top]=x;
}
}
pop function
string pop(Stack *st){
string x;
if(st->top==-1)
cout<<"Stack is empty\n";
else{
x=st->s[st->top--];
}
return x;
}
errored function
void Stack::isBalanced(Stack *st,string exp){
for(int i=0;exp[i]!='\0';i++){
if(exp[i]=='('){
push(st,exp[i]);
}else if(exp[i]==')'){
if(top==-1)
cout<<"stack empty\n";
pop(st);
}
}
if(top==-1)
cout<<"Balanced\n";
else
cout<<"Not balanced\n";
}
in main
int main()
{
Stack st;
string expression;
cout<<"ENTER EXPRESSION TO CHECK PARENTHESIS BALANCED OR NOT : ";
cin>>expression;
st.size=expression.length();
st.top=-1;
st.s=new string[st.size];
st.isBalanced(&st,expression);
}
here i was trying to implement parenthesis matching problem using in c++ but in the line below code throws error in function isBalanced please
try
to fix
my problem
push(st,exp[i]);
this line throws error canot covert something... and i cant able to fix it.I had donr all posible ways and cant be rectified so ...

push(st, std::to_string(exp[i])); should fix your problem!

Related

My st.show on stack cannot be display for some reason

My problem on my code is when i run it, it says the error no matching function call to stack::show() which i have. i dont know whats causing the error, did some research that it should be on the public class which already there.
I used switch case 1 is to input 2nd is to show or to display the user inputs which I cant call out using st.show();
private:
int MAX;
int top;
string *arr_q;
public:
Stack (int size)
{
MAX=size;
top=-1;
arr_q=new string[MAX];
}
void push(string subs)
{
if ((top+1)==MAX)
cout << "Stack Overflow..." << endl;
top=top+1;
arr_q[top]=subs;
}
void show(int lim)
{
int i = lim;
cout<<"Stack contains --"<<endl;
for (int ctr=i; ctr>=0; ctr--)
{
if (ctr==i)
cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
else
cout<<"\t\t"<<arr_q[ctr]<<endl;
}
}
};
case 2: {
st.show();
break;
The error is that your show function has a parameter but when you call it there is no parameter.
Seems reasonably likely that show should be written without a parameter. Like this
void show()
{
cout<<"Stack contains --"<<endl;
for (int ctr=top; ctr>=0; ctr--)
{
if (ctr==top)
cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
else
cout<<"\t\t"<<arr_q[ctr]<<endl;
}
}

i can't get an output from this

#include<iostream>
using namespace std;
class stack
{
int size=10;
int stack[size]={0}, value=0, top;
top=size;
public:
void push(int v)
{
if(top==0)
cout<<"\nstack is full\n";
else
{--top;
stack[top]=v;}
}
void pop()
{
if(top==size)
cout<<"\nstack is empty\n";
else
{top++;
stack[top];
stack[top-1]=0;
}
}
void display()
{
if(top==size)
cout<<"\nstack empty\n";
else
{
for(int i=top;i<size-1;i++)
{
cout<<stack[i];
}
}
}
};
int main()
{
stack s;
char t;
int value,ch;
do
{
cout<<"\n1.push\n";
cout<<"\n2.pop\n";
cout<<"\n3.display\n";
cout<<"enter choice:\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the value to be pushed\n";
cin>>value;
s.push(value);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
default:
cout<<"\nwrong choice\n";
}
cout<<"\ndo u want to retry\n";
cin>>t;
}while(t=='y' || t=='Y');
return 0;
}
Simplest fix to errors occurring is changing int size=10; to static const int size=10;.
After this, apart from occurring warning with stack[top]; being empty statement, there is logical error in display loop in for(int i=top;i<size-1;i++) where it should be either for(int i=top;i<size;i++) or for(int i=top;i<=size-1;i++).
As answered by Tomáš Zahradníček, you need to fix a few things to have your code compile (using -std=c++11).
I used for(int i=top; i<size; ++i) in the display method. I also add that your pop method could simply do top++; without overwriting the stack.
Anyways, regarding your problem of nothing being printed on cout : you obviously tried with 1 item pushed in the stack, but not with 2, which would have pointed to faulty line (the for loop).

Binary Search with few modification

While I am trying to compile the code with few modification in binary search recursive function. The program is acting weird. Some time it gives the correct value and some time it goes to infinite loop. Please explain what went wrong with the code. I am using DEV C++ as an IDE.
CODE:
#include<iostream>
#include<sstream>
using namespace std;
//function to compare the two integers
int compare(int low, int high)
{
if (low==high)
return 0;
if (low<high)
return 1;
else
return -1;
}
//Function for binary search using recursion
int *BinarySearch(int *Arr,int Val,int start,int end)
{
int localstart=start;
int localend=end;
int mid=(start+end)/3;
cout<<"MID:"<<mid;
int comp= compare(Val,Arr[mid]);
if(comp==0)
return &(Arr[mid]);
else if (comp>0)
return BinarySearch(Arr,Val,localstart,mid-1);
else
return BinarySearch(Arr,Val,mid+1,localend);
return NULL;
}
main()
{
int *arr;
arr= new int [256];
string str;
getline(cin,str);
stringstream ss;
ss<<str;
int index=0;
while(ss>>arr[index])
{index++;}
//cout<<arr[index-1];
cout<<"Enter Value:";
int value;
cin>>value;
int *final;
final=BinarySearch(arr,value,0,index-1);
if(final!=NULL)
cout<<"Final:"<<*final;
else
cout<<"Not Found";
getchar();
getchar();
return 0;
}
Two ideas:
What should BinarySearch do if Val is not in the array? Trace out what your code does in this case.
(start+end)/3 probably isn't the middle of the current range.

ternary tree giving error

This is simple ternary tree structure . I have written code correctly but while running it says after some time:
Sorry ternary.exe has stopped working.
Can you tell me the cause of this error.
#include<iostream>
#include<string>
using namespace std;
struct tnode{
int data[2];
tnode *ptr[3];
};
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
//for initializing tnode variables as null or null character
void newtree(tnode *&T){
T->data[0]='\0';
T->data[1]='\0';
T->ptr[0]=NULL;
T->ptr[1]=NULL;
T->ptr[2]=NULL;
}
void fillto(tnode *&T,int a){
if(T->data[0]=='\0'){
T->data[0]=a;
}
else if(T->data[0]!='\0'&&T->data[1]=='\0'){
T->data[1]=a;
if(T->data[0]>T->data[1])
swap(T->data[0],T->data[1]);
}
else{
if(a<T->data[0]){
if(T->ptr[0]==NULL){
T->ptr[0]=new(tnode);
newtree(T->ptr[0]);
}
fillto(T->ptr[0],a);
}
else if(a>T->data[1]){
if(T->ptr[2]==NULL){
T->ptr[2]=new(tnode);
newtree(T->ptr[2]);
}
fillto(T->ptr[2],a);
}
else{
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
T->ptr[1]=new(tnode);
}
fillto(T->ptr[1],a);
}
}
}
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c[0];
newtree(T);
E=T;
while(l<=s.length()){
c[0]=s[l];
cout<<atoi(c)<<endl;
fillto(T,atoi(c));
l++;
}
return E;
}
int main(){
string s="5398124";
tnode *T;
T=new(tnode);
T=datatnode(s);
cout<<T->data[0];
return 0;
}
You should remove '=' sign as below
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c;
newtree(T);
E=T;
int a = s.length();
while(l<a){
c=s[l];
cout<<atoi(&c)<<endl;
fillto(T,atoi(&c));
l++;
}
return E;
}
Its difficult to say from your code (as mentally you have to run it in your head). Better to debug it out. Call some debug at key points in your code and try to locate the exact line of code.... this could produce a lot of debug depending how big your data-set is.
At a guess I would say that you probably hit a bad address or somthing like this, that is usually why programs die un-expectedly and immediatly! So I would suggest being very secure on your pointer checking. For example:
void fillto(tnode *&T,int a){
if (T != NULL){
if(T->data[0]=='\0')
{
T->data[0]=a;
}
:
:
}
else
{
printf("Warning: NULL pointer!\n");
}
}
Basically any time you use a pointer that is passed in to a function you should check it is not null. This is generally good code practise and may help you to find your bugs :)
Also int initialisation can just be:
int i = 0;
instead of
int i = '\0';
The fundamental flaw that causes the error is in the 'void fillto(tnode *&T,int a)' function:
...
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
...
}
As the function newtree does not check if the pointer is null, you end up dereferencing a NULL pointer in newtree

Segmentation Fault in Loop's Condition

The following code is to sort a linked list after creating it. The sorting algorithm used is somewhat similar to Bubble Sort. I am checking the two consecutive nodes and swapping them if necessary. I used the debugger which told me that the fault is raised while condition checking for the loops which are used while sorting.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
using namespace std;
struct link_list
{
char value[20];
struct link_list *next;
};
int main()
{
struct link_list *head=NULL;
int i,j;
char input[20];
char ch;
struct link_list *loop_var,*temp2,*prev_node,*temp4=NULL;
temp3=NULL;
do
{
cout<<"\nEnter the string you want to insert";
cin>>input;
cout<<"\nDo you want to continue entering?";
cin>>ch;
if (head==NULL)
{
head=new link_list;
strcpy(head->value,input);
head->next=NULL;
continue;
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next);
temp2=new link_list;
loop_var->next=temp2;
strcpy(temp2->value,input);
temp2->next=NULL;
}while(ch=='y' || ch=='Y');
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value<<"\n";
char arr[20];
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<"\nLoop1";
for (temp4=head;temp4->next!=NULL;temp4=temp4->next)
{
cout<<"\nLoop2";
temp2=temp4;
if (strcmp(temp2->value,temp2->next->value)>0)
{
cout<<"\nSwap Enter";
if (temp2==head && temp2->next->next==NULL)
{
cout<<"\nSpecial1";
temp2->next->next=temp;
temp2->next=NULL;
}
else if (temp2==head)
{
cout<<"\nSpecial2";
head=temp2->next;
temp2->next=head->next;
head->next=temp2;
}
else if (temp2->next->next==NULL)
{
cout<<"\nSpecial3";
prev_node->next=temp2->next;
prev_node->next->next=temp2;
temp2->next=NULL;
}
else
{
cout<<"\nNormal1";
prev_node->next=temp2->next;
temp2->next=prev_node->next->next;
prev_node->next->next=temp2;
cout<<"\nNormal2";
}
}
prev_node=temp4;
cout<<"\nLoop2PreExit";
fflush(stdin);
cout<<"\nLoop2Exit";
}
cout<<"\nLoop1Exit";
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value;
getch();
}
temp2->next->next=temp;
"temp" is not defined anywhere... if your compiler filled in that hole for you, then this is what is causing your loop's condition to segfault.
Also, naming every other variable "temp#" is an easy way to have mistakes like this.