Nodes not getting deleted, causing failure

Warning: Long, and probably difficult to follow.

Page 1 of 1

4 Replies - 418 Views - Last Post: 15 December 2009 - 05:18 PM Rate Topic: -----

#1 Fireblaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-November 09

Nodes not getting deleted, causing failure

Posted 15 December 2009 - 01:28 AM

Hello.
So, my final assignment for my C++ class: Take a regular equation, input it one character or value at a time, then move it through several functions.
Flowchart:
-Prepares a doubly linked list
-Sets left paran
-Reads in equation piece by piece - if piece is an operator (+, -, *, /, (, ) ) then this gets set to oper of that struct portion; otherwise, it gets set to value.
-Goes to head of equation
-Finds a right paran, sets RP to it
-Goes backward until left is found, sets LP to it
-Sends it into functions which multiply, divide, add, and subtract whenever those things are found
-Deletes parans
-If there are any more parans, runs from step 2.

Here's the code:
#include<iostream>
#include<string>
using namespace std;

struct node
{
  node* prev;
  char oper;
  int value;
  node* next;
	node()
	{
		next = prev = NULL;
	oper = 0;
	value = 0;
	}
};

void printlist(node* h);
void multiplier (node* l, node* r);
void adder (node* l, node* r);
void paran (node* l, node* r);

void main()
{
  char str[10];

  node* head = new node;
  node* tail = head;
  node* temp, *RP, *LP;
  head->prev = 0;
  int loopend = 0, check = 0, overallcheck = 2;

  tail->value = 0;
  //Sets first node to zero
  tail->next = new node;
  tail->next->prev=tail;
  tail=tail->next;
  tail->oper = '(';
  //Sets next node to left paranthesis, for the calculating
  tail->next = new node;
  tail->next->prev=tail;
  tail=tail->next;

  cout << "Enter a number or operator";
  cin >> str;

  while(str[0] != 's')
  {
	if(str[0] == '+'  || str[0] == '-' || str[0] == '*' || str[0] == '/' || str[0] == '(' || str[0] == ')')
	{
	  tail->oper = str[0];
	  tail->value = 0;
	}
	else
	{
	  tail->value = atoi(str);
	  tail->oper = 0;
	}
	tail->next = new node;
	tail->next->prev=tail;
	tail = tail->next;
	cout << "Enter a number or operator";
	cin >> str;
  }
  //Inputs expression into dll

  tail->oper = ')';
  //Sets a right paranthesis to help calculate
  tail->next = new node;
  tail->next->prev=tail;
  tail = tail->next;
  tail->value = 0;
  //Sets last node as zero to finish list

  printlist(head);


  //Giant line of crazy calculations starts here
  while(overallcheck != 0)
  {
  temp = head;
  do
  {
  if(temp->oper == ')')
  {
	RP = temp;
	loopend = 1;
  }
  else
	temp = temp->next;
  }while(loopend != 1);
  loopend = 0;
  temp = RP;
  do
  {
	if(temp->oper == '(')
	{
	  LP = temp;
	  loopend = 1;
	}
	else
	  temp = temp->prev;
  }while(loopend != 1);
  //Finds next set of parantheses to operate with

  loopend = 0;
  while(loopend != 1)
  {
	for(temp = LP; temp; temp = temp->next)
	  if(temp->oper == '*' || temp->oper == '/')
		check++;
	//Checks if there are any '*'s or '/'s in the dll.  If there are, runs multiplier function.  If not, ends the loop.
	if(check > 0)
	{
	  multiplier(LP, RP);
	  check = 0;
	}
	else
	  loopend = 1;
  };
  //Runs multiplier

  loopend = 0;
  while(loopend != 1)
  {
	for(temp = LP; temp; temp = temp->next)
	  if(temp->oper == '+' || temp->oper == '-')
		check++;
	//Checks if there are any '+'s or '-'s in the dll.  If there are, runs multiplier function.  If not, ends the loop.
	if(check > 0)
	{
	  adder(LP, RP);
	  check = 0;
	}
	else
	  loopend = 1;
  };
  //Runs adder

	loopend = 0;
	overallcheck = 0;
  paran(LP, RP);
  //Deletes parantheses which were operated on

  for(temp = head; temp; temp = temp->next)
	if(temp->oper == '(' || temp->oper == ')')
	  overallcheck++;
  //Checks if there are any more parantheses; if not, loop ends and prints out the answer.
	}
  printlist(head);

  system("pause");
}

void printlist(node *h)
{
  for(node* p = h; p; p = p->next)
  {
	if (p->value != 0 && p->value != -842150451)
	  cout << p->value << " ";
	if (p->oper != 0 && p->oper != 'Í')
	  cout << p->oper << " ";
  }
	cout << endl;
}

void multiplier (node* l, node* r)
{
  int loopend = 0;
  node *temp, *temp2;
  temp = l;
  temp2 = l;
  do
  {
  if(temp->oper == '*')
  {
	temp = temp->prev;
	temp2 = temp2->next;
	temp->value = temp->value * temp2->value;
	temp->next = temp->next->next->next;
	temp2 = temp2->next;
	temp2->prev = temp2->prev->prev->prev;
	loopend = 1;
	//If a '*' is present, gets the values before and after, multiplies them, and deletes via pointing to two later and two before
  }
  if(temp->oper == '/')
  {
	temp = temp->prev;
	temp2 = temp2->next;
	temp->value = temp->value / temp2->value;
	temp->next = temp->next->next->next;
	temp2 = temp2->next;
	temp2->prev = temp2->prev->prev->prev;
	loopend = 1;
	//If a '/' is present, gets the values before and after, divides them, and deletes via pointing to two later and two before
  }
  else
  {
	temp = temp->next;
	temp2 = temp2->next;
  }
  }while(loopend != 1);
}

void adder (node* l, node* r)
{
  int loopend = 0;
  node *temp, *temp2;
  temp = l;
  temp2 = l;
  do
  {
  if(temp->oper == '+')
  {
	temp = temp->prev;
	temp2 = temp2->next;
	temp->value = temp->value + temp2->value;
	temp->next = temp->next->next->next;
	temp2 = temp2->next;
	temp2->prev = temp2->prev->prev->prev;
	loopend = 1;
	//If a '+' is present, gets the values before and after, adds them, and deletes via pointing to two later and two before
  }
  if(temp->oper == '-')
  {
	temp = temp->prev;
	temp2 = temp2->next;
	temp->value = temp->value - temp2->value;
	temp->next = temp->next->next->next;
	temp2 = temp2->next;
	temp2->prev = temp2->prev->prev->prev;
	loopend = 1;
	//If a '-' is present, gets the values before and after, subtracts them, and deletes via pointing to two later and two before
  }
  else
  {
	temp = temp->next;
	temp2 = temp2->next;
  }
  }while(loopend != 1);
}

void paran (node* l, node* r)
{
  node *temp, *temp2;

  l = l->next;
  l->prev = l->prev->prev;
  l = l->prev;
  l->next = l->next->next;
  //Sets list from LP to move forward, remove the prev behind it via changing the link, then 
  //moving to the prev and deleting the next node through the same method
  r = r->prev;
  r->next = r->next->next;
  r = r->next;
  r->prev = r->prev->prev;
  //Sets list from RP to move backward, remove the next ahead of it via changing the link, then 
  //moving to the next and deleting the previous node through the same method
}


The problem is, when I use parantheses in the equation, apart from those set at the beginning, the right ones don't get deleted. I can't figure out why.

I thought that changing this:
for(temp = LP; temp; temp = temp->next)

to this:
for(temp = LP; temp = RP; temp = temp->next)

would fix it, but that gets the code stuck in a neverending loop by immediately setting temp to RP and never reaching that point via temp = temp->next.

Figuring out that part would be helpful as well.

If it's too long, aggravating, or confusing, that's fine. It hardly makes any sense to me, and I wrote the dang thing.
Anyways, any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Nodes not getting deleted, causing failure

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: Nodes not getting deleted, causing failure

Posted 15 December 2009 - 05:42 AM

This is wrong:
for(temp = LP; temp = RP; temp = temp->next)

look again at what you're doing there, particularly the loop control statement (the middle statement).

Don't know if it fixes your problem, but there it is.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Nodes not getting deleted, causing failure

Posted 15 December 2009 - 05:50 AM

Here's some added commentary and assistance from the compiler.

That problem at line 162 looks like it might well bite you hard somewhere or other.

Always compile with display of warnings enabled.
They can help you in so many ways.

DIC.cpp:162:36: warning: multi-character character constant
DIC.cpp:24: error: ‘::main’ must return ‘int’
DIC.cpp: In function ‘void printlist(node*)’:
DIC.cpp:162: warning: comparison is always true due to limited range of data type
DIC.cpp: In function ‘void paran(node*, node*)’:
DIC.cpp:246: warning: unused variable ‘temp’
DIC.cpp:246: warning: unused variable ‘temp2’
Was This Post Helpful? 0
  • +
  • -

#4 Fireblaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-November 09

Re: Nodes not getting deleted, causing failure

Posted 15 December 2009 - 09:44 AM

View PostJackOfAllTrades, on 15 Dec, 2009 - 04:42 AM, said:

This is wrong:
for(temp = LP; temp = RP; temp = temp->next)

look again at what you're doing there, particularly the loop control statement (the middle statement).

Don't know if it fixes your problem, but there it is.

Oh, bleh.
That took me way longer than it should have to figure that one out.
for(temp = LP; temp != RP; temp = temp->next)

That should do it, I think...

View Postjanotte, on 15 Dec, 2009 - 04:50 AM, said:

Here's some added commentary and assistance from the compiler.

That problem at line 162 looks like it might well bite you hard somewhere or other.

Always compile with display of warnings enabled.
They can help you in so many ways.

DIC.cpp:162:36: warning: multi-character character constant
DIC.cpp:24: error: ‘::main’ must return ‘int’
DIC.cpp: In function ‘void printlist(node*)’:
DIC.cpp:162: warning: comparison is always true due to limited range of data type
DIC.cpp: In function ‘void paran(node*, node*)’:
DIC.cpp:246: warning: unused variable ‘temp’
DIC.cpp:246: warning: unused variable ‘temp2’


I always have warnings enabled...I didn't see most of those.
I'm not sure which point the multi-character constant is referring to (don't have a complier atm) but I know the rest.
-My instructor wanted us to use void
-The comparison is supposed to be always true. However, it's not true when it runs into junk data at the end of the list, and that part works fine.
-The last two are just variables I keep fogetting to delete. I don't use them at all. I'll delete them.


EDIT:
Changing the for loop made the program run flawlessly. Thanks a bunch!

This post has been edited by Fireblaze: 15 December 2009 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Nodes not getting deleted, causing failure

Posted 15 December 2009 - 05:18 PM

View PostFireblaze, on 15 Dec, 2009 - 08:44 AM, said:

-My instructor wanted us to use void


You might want to print the info linked to here and anonymously leave a copy on your instructor's desk.

If they are going to take salary as a teacher they shouldn't be passing on bad habits that your classmates will later need to unlearn


"void main()" is wrong. Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1