To all the Masters,

Sir can you help me in this problem, the program tells the infix to convert into postfix.
ex.
a+b-b*c+g/s
=ab+bc*-gs/+

My problem is, once you compile the program there is no error message, but if you run it, the output did not show. I know this is a logical error, I'm trying to use a linked list and I'm newbee from this struct type. I can implement the problem using array but I'm exercising myself to use the structure in c++. It is hard for me to understand the function of this struct data type, can you help me in this problem.

The program is not finished, I'm just testing the concept of struct data type.

the file is attached in cpp format.

thank you and more power.

Gaspan, Mark Cristian
BSCS, Student.

Attachments
/*Gaspan, Mark Cristian
  Garrote, Sheryn
  Genio, Precy Lou
  Hersia, Sarah
  Espiritu,
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct node{
	char *data;
	node *next;
}*postfix,*infix,*stack,*ptr1,*temp,*bptr,*delnode,*head,*headpost;

void main(){
	char *dataned[30];
	int i=0,e;
	int lenstr;
	infix=new node;
	postfix=new node;
	stack=new node;
	temp=new node;
	headpost=postfix;
	ptr1=infix;
	head=infix;
clrscr();
	cout<<"Converting Infix to Postfix\n";
	cout<<"Input the Infix:\n\t->";
	do{
		cin>>dataned[i];
		i=i+1;
	}while(dataned[i]==" ");
	for(e=0;e<i;e++){
		ptr1->data=dataned[e];
		ptr1=ptr1->next;
	}
	ptr1=head;
	do{
		if(ptr1->data!=")"||ptr1->data!="("||ptr1->data!="+"|| //Input the variable
			ptr1->data!="-"||ptr1->data!="*"||ptr1->data!="/"|| //to the postfix
			ptr1->data!="^"){
				postfix->data=ptr1->data;
				postfix=postfix->next;
		}else{//Input the operator to the stack
			if(stack==NULL){
				temp->data=ptr1->data;
				temp->next=stack;
				stack=temp;
			}else{
				//comparing the precedence
				if( ((stack->data=="+"||ptr1->data=="-")&&
					(stack->data=="-"||ptr1->data=="+"))||
					((stack->data=="/"||ptr1->data=="*")&&
					(stack->data=="*"||ptr1->data=="/")) ){
						postfix->data=stack->data;
						postfix=postfix->next;
						temp=stack->next;
						delete stack;//delete the node
						stack=temp;

					}else if((stack->data=="/"||stack->data=="*")&&
						(ptr1->data=="+"||ptr1->data=="-")){
						postfix->data=stack->data;
						postfix=postfix->next;
						temp=stack->next;
						delete stack;
						stack=temp;
						temp->data=ptr1->data;
						temp->next=stack;
						stack=temp;

					}else if((stack->data=="+"||stack->data=="-")&&
						(ptr1->data=="*"||ptr1->data=="/")){
							postfix->data=ptr1->data;
							postfix=postfix->next;

					}else if(ptr1->data=="^"&&stack->data!="("){
						stack->data=ptr1->data;
						stack=stack->next;

					}else if(ptr1->data=="("){
						temp->data=ptr1->data;
						temp->next=ptr1;
						ptr1=temp;
					}else if(ptr1->data==")"){
						postfix->data=stack->data;
						bptr=stack->next->next;
						delnode=stack->next;
						delete stack;
						delete delnode;
						stack=bptr;
					}else{
						postfix->data=stack->data;
						postfix=postfix->next;
						delnode=stack->next;
						delete stack;
						stack=delnode;
					}
			}
		}

	}while(ptr1!=NULL);
	cout<<endl;
	for(postfix=headpost;postfix!=NULL;postfix=postfix->next){
		cout<<postfix->data;
	}
getch();
}

Well it's already broken before it's even got 10 lines into the program.

> cin>>dataned;
Each dataned is an UNINITIALISED pointer. You're busily scribbling data all over other peoples memory.

> while(dataned==" ")
This is unlikely to be ever true (pure luck basically), so the loop only happens once (if it even gets there).
Further, since you increment i, you're not even attempting to compare with the input.

> ptr1=ptr1->next;
You only allocate 1 node, so the rest of whatever you're pointing at in your linked list is garbage.

Since this is C++ (I guess), I would think you're supposed to model the idea of 'stack' as a class, with the well known 'push' and 'pop' methods.
That at least would clear away a lot of the complexity from your overly long main() function.

This article has been dead for over six months. Start a new discussion instead.