Thread: Problem Creating Linked List in Function

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Stunner View Post
    Nice man, that makes my code work better, but now I get garbage on the end. It is able to print out only one node and then garbage on the end. If I insert more than one entry I get a fatal error. What you said did improve it. Here is the update:
    Yes, you're going past the end of the string when you're incrementing "n++" after the for-loop in the main. So if you find a nul-character in the string, you go past it, and continue until the next nul, which may be overflowing all sorts of other buffers and cause bad thing to happen. See one of my previous posts.

    --
    Mats

  2. #17
    Registered User
    Join Date
    Jul 2007
    Posts
    29
    Matsp, I took both of your suggestions and the first one made it respond nearly exactly the same as Swoopy's version. The second part however made it crash worse than originally.
    n should be incremented no matter what so that it skips over a space if there is one ( there will be one besides NULL).

    But you are right the n++ needs to be moved to after the if else statement. Here is my entire code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
    float num;
    char opr;
    char type;
    node* next;
    };
    
    void printList(const node*);
    //entry* insert(int n, node* h);
    //entry* deleteNode(int n, node* h);
    //entry* PQInit(entry* h, entry* PQh);
    float arrayToFloat(char part[]);
    node* tempArray(node* head, char string[], char part[], int flag, int stringFlag);
    
    int main()
    {
    node *temp = 0, *head = 0;//starts out empty
    node *PQt = 0, *PQh = 0;//Polish queue
    //float fltval;
    int j, n = 0, flag, count = 0, stringFlag = 0;
    char string[80];
    
    	cout << "Enter an algebraic equation in reverse polish notation: \n";
    	cin.getline(string, 80);
    
    	cout << "Output from func arrayToFloat:\n";
    	while(string[n] != '\0')
    	{
    	char part[15] = {'\0'};
    
    		for(j = 0, flag = 0; (string[n] != ' ') && (string[n] != '\0'); j++, n++) 
    		{
    			part[j] = string[n];
    			if(!isdigit(part[j]))//if character
    			{
    				flag = 1;
    			}//if
    		}//for
    		//n++;
    
    		if(string[n] != '\0')//I dont understand what these if elses are checking for...
    		{
    			stringFlag = 1;
    			//n++;
    		}//if
    		else
    			stringFlag = 0;
    
    		n++;
    
    
    		
    		head = tempArray(head, string, part, flag, stringFlag);
    
    
    	}//while
    
    	//PQh = PQInit(head, PQh);
    	cout << "Output from linked list:\n";
    	printList(head);
    
    	//creating PQ stack from initial array
    	while(head != 0)
    	{
    		PQh = new node;
    		if(head->type == 'N')
    		{
    			PQh->num = head->num;
    			PQh->type = 'N';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//if
    		else
    		{
    			PQh->opr = head->opr;
    			PQh->type = 'O';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//else
    
    		head = head->next;
    	}//while
    
    	cout << "Output from PQ: \n";
    
    	printList(PQh);
    
    	return 0;
    }//main
    
    /***************************************/
    //Prints list of Nodes
    
    void printList(const node* pointer)
    {
    	while(pointer != 0)
    	{
    		if(pointer->type == 'N')
    			cout << pointer->num << " ";
    		else
    			cout << pointer->opr << " ";
    
    		pointer = pointer->next;
    	}//while
    	cout << endl;
    
    }//printList
    
    /****************************************/
    //Converts an array of ints to a float value
    
    float arrayToFloat(char part[])
    {
    int L = 0, i, j;
    float val = 0, value[15] = {0};
    float valtab[5] = {1, 10, 100, 1000, 10000};
    //                 1   2   3     4     5
    
    	for(i = 0; part[i]; i++)
    	{
    		value[i] = part[i] - '0';//converting to int from char	
    	}//for
    	
    	L = strlen(part);
    	for(i = 0, j = L - 1; i < L; i++, j--)
    	{
    		val = val + (value[i] * valtab[j]);
    	}//for
    
    	return val;
    }//arrayToFloat
    
    /******************************************/
    //Initializes PQ
    
    node* tempArray(node* head, char string[], char part[], int flag, int stringFlag)
    {
      float fltval;
      node *temp = new node;
    
      if(flag == 0) //if part consists of int
        {
          if(stringFlag == 1)
    	{
    	  fltval = arrayToFloat(part);//converting array based number to float
    	  cout << fltval << endl;
    
    	  temp->num = fltval;
    	  temp->type = 'N';
    	  temp->next = head;
    	}
        }
      else
        {
          if(stringFlag == 0)
    	{
    	  temp->opr = part[0];
    	  temp->type = 'O';
    	  temp->next = head;
    	}
        }
    
      return temp;
    	
    }//tempArray
    Also another thing, while making modifications, I noticed the if else statement:

    Code:
    		if(string[n] != '\0')//I dont understand what these if elses are checking for...
    		{
    			stringFlag = 1;
    			//n++;
    		}//if
    		else
    			stringFlag = 0;
    And it is not making any sense to me whatsoever(yeah I know its sad, I wrote it, need better comments), supposedly it is supposed to detect if it is a number or an operator. If anyone can save me from my stupidity, that would be great.

  3. #18
    Registered User
    Join Date
    Jul 2007
    Posts
    29
    I think that if else statement is the problem, working on it...

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    29
    HAH! That WAS the problem! I'm so dumb, when I converted it, I did it wrong which caused the problems... sorry guys. Matsp, good call n the n++ counter fixed a problem and now it runs like its supposed to:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
    float num;
    char opr;
    char type;
    node* next;
    };
    
    void printList(const node*);
    //entry* insert(int n, node* h);
    //entry* deleteNode(int n, node* h);
    //entry* PQInit(entry* h, entry* PQh);
    float arrayToFloat(char part[]);
    node* tempArray(node* head, char string[], char part[], int flag, int stringFlag);
    
    int main()
    {
    node *temp = 0, *head = 0;//starts out empty
    node *PQt = 0, *PQh = 0;//Polish queue
    //float fltval;
    int j, n = 0, flag, count = 0, stringFlag = 0;
    char string[80];
    
    	cout << "Enter an algebraic equation in reverse polish notation: \n";
    	cin.getline(string, 80);
    
    	cout << "Output from func arrayToFloat:\n";
    	while(string[n] != '\0')
    	{
    	char part[15] = {'\0'};
    
    		for(j = 0, flag = 0; (string[n] != ' ') && (string[n] != '\0'); j++, n++) 
    		{
    			part[j] = string[n];
    			if(!isdigit(part[j]))//if character
    			{
    				flag = 1;
    			}//if
    		}//for
    		if(string[n] != '\0')
    			n++;
    
    		if(flag == 0)
    		{
    			stringFlag = 1;
    		}//if
    		else
    			stringFlag = 0;
    
    
    		
    		head = tempArray(head, string, part, flag, stringFlag);
    
    
    	}//while
    
    	//PQh = PQInit(head, PQh);
    	cout << "Output from linked list:\n";
    	printList(head);
    
    	//creating PQ stack from initial array
    	while(head != 0)
    	{
    		PQh = new node;
    		if(head->type == 'N')
    		{
    			PQh->num = head->num;
    			PQh->type = 'N';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//if
    		else
    		{
    			PQh->opr = head->opr;
    			PQh->type = 'O';
    			PQh->next = PQt;
    			PQt = PQh;
    		}//else
    
    		head = head->next;
    	}//while
    
    	cout << "Output from PQ: \n";
    
    	printList(PQh);
    
    	return 0;
    }//main
    
    /***************************************/
    //Prints list of Nodes
    
    void printList(const node* pointer)
    {
    	while(pointer != 0)
    	{
    		if(pointer->type == 'N')
    			cout << pointer->num << " ";
    		else
    			cout << pointer->opr << " ";
    
    		pointer = pointer->next;
    	}//while
    	cout << endl;
    
    }//printList
    
    /****************************************/
    //Converts an array of ints to a float value
    
    float arrayToFloat(char part[])
    {
    int L = 0, i, j;
    float val = 0, value[15] = {0};
    float valtab[5] = {1, 10, 100, 1000, 10000};
    //                 1   2   3     4     5
    
    	for(i = 0; part[i]; i++)
    	{
    		value[i] = part[i] - '0';//converting to int from char	
    	}//for
    	
    	L = strlen(part);
    	for(i = 0, j = L - 1; i < L; i++, j--)
    	{
    		val = val + (value[i] * valtab[j]);
    	}//for
    
    	return val;
    }//arrayToFloat
    
    /******************************************/
    //Initializes PQ
    
    node* tempArray(node* head, char string[], char part[], int flag, int stringFlag)
    {
      float fltval;
      node *temp = new node;
    
      if(flag == 0) //if part consists of int
      {
          if(stringFlag == 1)
    	  {
    		 fltval = arrayToFloat(part);//converting array based number to float
    		 cout << fltval << endl;
    
    		 temp->num = fltval;
    		 temp->type = 'N';
    		 temp->next = head;
    	  }//if
      }//if
      else
      {
          if(stringFlag == 0)
    	  {
    		temp->opr = part[0];
    		temp->type = 'O';
    		temp->next = head;
    	  }
      }//else
    
      return temp;
    	
    }//tempArray
    Thanks for all your help guys, you guys are awesome. Sorry bout that stupid error.

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Thanks for all your help guys, you guys are awesome. Sorry bout that stupid error.
    Whew, glad you got it working.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(flag == 0)
    		{
    			stringFlag = 1;
    		}//if
    		else
    			stringFlag = 0;
    This code snippet is EXACTLY the same as "stringFlag = !flag". So why not simplify your tempArray function to only use the one flag - not using two different ones that are always the opposite of each other.

    --
    Mats

  7. #22
    Registered User
    Join Date
    Jul 2007
    Posts
    29
    Quote Originally Posted by matsp View Post
    Code:
    if(flag == 0)
    		{
    			stringFlag = 1;
    		}//if
    		else
    			stringFlag = 0;
    This code snippet is EXACTLY the same as "stringFlag = !flag". So why not simplify your tempArray function to only use the one flag - not using two different ones that are always the opposite of each other.

    --
    Mats
    Haha, true, thanks for mentioning that. Long day, not planning on working on it anymore for now. Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM