Simple Linked list woes!

This is a discussion on Simple Linked list woes! within the Windows Programming forums, part of the Platform Specific Boards category; I am having difficulty trying to enable the user to 'Back Step' through user supplied values in a dialog box. ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Simple Linked list woes!

    I am having difficulty trying to enable the user to 'Back Step' through user supplied values in a dialog box.
    If for example the user enters the following coordinates:
    1. 100, 100
    2. 120 , 100
    3. 120 , 120
    4. User doesn't enter any values instead user presses 'Back'. This updates the values as I expect and shows coord. 3 {120,120}.
    5. User them presses 'Back' agian So it should display coord. 2 but instead it displays the 3rd coord again. Even though the breakpoint shows that it has
    changed 'conductor' to 'conductor->prev'.

    I've included a picture of the steps to try and clarify.




    Code:
    struct node
    {
    	double x; /* DLO coordinate. */
    	double y; /* DLO coordinate. */
    	double GlassBite; /* Allows custom glass bite per side of polygon. */
    	node *next;
    	node *prev;
    }InputData; /* GLOBAL, All geometric data entered by user. */
    
    
    INT_PTR CALLBACK GetCoordsProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int nLen = '\0';
    	static int counter = 1;
    	LPTSTR pUserInput;
    	bool bFloat = true;
    	char StaticTxt[20], TempTxt[30];
    	int RetValue = 0;
    	if(root == 0)
    	{
    		CreateNextNode();
    		counter = 1;
    	}
    	switch (message)
    	{
            case WM_INITDIALOG:
    			{
    				/* Set static text in dialog box on the fly. */
    				switch (counter)
    				{
    				case 1:
    					sprintf(StaticTxt, "%s", TEXT("1st Coord. Pair"));
    					if(bEdit && conductor != 0)
    					{
    						/* Update user data in edit fields. */
    						hctrl = GetDlgItem(hdlg, IDC_EDITX1);
    						RetValue = sprintf(TempTxt,"%f",conductor->x);
    						SetWindowText(hctrl, TempTxt);
    						hctrl = GetDlgItem(hdlg, IDC_EDITY1);
    						RetValue =sprintf(TempTxt,"%f",conductor->y);
    						SetWindowText(hctrl, TempTxt);
    					}
    					break;
    				case 2:
    					sprintf(StaticTxt, "%s", TEXT("2nd Coord. Pair"));
    					if(bEdit && conductor != 0)
    					{
    						/* Update user data in edit fields. */
    						hctrl = GetDlgItem(hdlg, IDC_EDITX1);
    						RetValue = sprintf(TempTxt,"%f",conductor->x);
    						SetWindowText(hctrl, TempTxt);
    						hctrl = GetDlgItem(hdlg, IDC_EDITY1);
    						RetValue = sprintf(TempTxt,"%f",conductor->y);
    						SetWindowText(hctrl, TempTxt);
    					}
    					break;
    				case 3:
    					sprintf(StaticTxt, "%s", TEXT("3rd Coord. Pair"));
    					if(bEdit && conductor != 0)
    					{
    						/* Update user data in edit fields. */
    						hctrl = GetDlgItem(hdlg, IDC_EDITX1);
    						RetValue = sprintf(TempTxt,"%f",conductor->x);
    						SetWindowText(hctrl, TempTxt);
    						hctrl = GetDlgItem(hdlg, IDC_EDITY1);
    						RetValue = sprintf(TempTxt,"%f",conductor->y);
    						SetWindowText(hctrl, TempTxt);
    					}
    					break;
    				default:
    					sprintf(StaticTxt, "%dth Coord. Pair", counter);
    					if(bEdit && conductor != 0)
    					{
    						/* Update user data in edit fields. */
    						hctrl = GetDlgItem(hdlg, IDC_EDITX1);
    						sprintf(TempTxt,"%f",conductor->x);
    						SetWindowText(hctrl, TempTxt);
    						hctrl = GetDlgItem(hdlg, IDC_EDITY1);
    						sprintf(TempTxt,"%f",conductor->y);
    						SetWindowText(hctrl, TempTxt);
    					}
    					break;
    				}
    				/* Get handles to static control to update as it is initialized */
    				hctrl = GetDlgItem(hdlg, IDC_STATIC1);
    				SetWindowText(hctrl, StaticTxt);
    			}
            return TRUE;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
    				case IDBACK:
    				{
    					if(1 == counter) /* Can't go lower than 1 so beep. */
    					{
    						MessageBeep (0) ;
    					}
    					else
    					{
    						if(conductor->prev != NULL && bEdit) // Check  (root->prev = 0 always) && (first time through bEdit is false)
    						{
    
    							conductor = conductor->prev;
    							MessageBox(hdlg, "This should change conductor to previous node.", "Previous node.", MB_OK);
    
    							/*************************************************************************/
    							// breakpoint here shows that conductor now equals prev node like I want!!!
    							/*************************************************************************/
    
    						}
    						bEdit = true;
    						counter-=1; // This is used to make changes to the static control in the dialog box.
    
    						EndDialog(hdlg, IDBACK);
    					}
    				}
    				break;
    				case IDDONE:
    					bEdit = false;
    					/* Need to verify that first and last points are equal. **
    					** Also need to verify that line segments do not cross. */
    	
    					/* Want it to fall through. */
    				case IDNEXT:
    				{
    					/* xcoord 1 */
    					hctrl = GetDlgItem(hdlg, IDC_EDITX1);  //(hOwner, hDialogEditField)
    					nLen = GetWindowTextLength(hctrl);
    					if(nLen > 0) /* If nLen=0 then there is no reason to do the following block! */
    					{
    						if(0 == conductor->next && counter > 1) /* If the next node is null and not root */
    						{
    							CreateNextNode();
    						}
    						if(pUserInput = (LPTSTR)malloc(nLen + 1)) //if((LPTSTR)pUserInput = (LPTSTR)new char(nLen + 1))
    						{
    							int test = GetDlgItemText(hdlg, IDC_EDITX1, pUserInput, nLen+1);
    							if(bFloat = ValidateFloat(pUserInput, nLen+1))
    							{
    								conductor->x = strtod(pUserInput, NULL);
    							}
    							free(pUserInput);//delete(pUserInput);
    						}
    					}
    					/* ycoord 1 */
    					hctrl = GetDlgItem(hdlg, IDC_EDITY1);
    					nLen = GetWindowTextLength(hctrl);
    					if(nLen > 0) /* If nLen=0 then there is no reason to do the following block! */
    					{
    						if(pUserInput = (LPTSTR)malloc(nLen + 1))
    						{
    							int test = GetDlgItemText(hdlg, IDC_EDITY1, pUserInput, nLen+1);
    							if(bFloat) /* If previous was a float check this one. */
    							{
    								if(bFloat = ValidateFloat(pUserInput, nLen+1))
    								{
    									conductor->y = strtod(pUserInput, NULL);
    									conductor->next = 0;
    								}
    							}
    							free(pUserInput);
    						}
    					}
    
    					if(!bFloat)
    					{
    						MessageBox(hwnd,"Enter a floating point number!", "Error!",MB_OK);
    						break;
    					}
    					else  //(bFloat)
    					{
    						bFileNeedsSaved = true;
    						EndDialog(hwnd, IDOK);
    					}
    					counter+=1;
    					if( (LOWORD(wParam))== IDDONE ) /* check to see if user selected 'Done' */
    					{
    						/* Need to verify that first and last points are equal. **
    						** Also need to verify that line segments do not cross. */
    						//MessageBox(hwnd, "IDDONE", "it works", MB_OK);
    
    						//Translate();
    						CalcMinMax();
    						EndDialog(hdlg, IDDONE);
    						break;
    					}
    					bFileNeedsSaved = true;
                        EndDialog(hdlg, IDNEXT);
    				}
    				break;
                    case IDCANCEL:
    				{
    					MessageBeep (0) ;
                        EndDialog(hdlg, IDCANCEL);
    				}
                    break;
                }
            break;
            default:
            return FALSE;
        }
        return TRUE;
    }
    
    
    void CreateNextNode()
    {
    	if(0 == root)
    	{
    		root = new node;
    		CountNew +=1;
    		//counter = 1;
    		conductor = root;
    		conductor->prev = 0;
    	}
    	if(0 == conductor->next)
    	{
    		temp = conductor;
    		/* Allocate space for variables. */
    		conductor->next = new node;
    		CountNew +=1;
    		conductor = conductor->next;
    		conductor->prev = temp;
    	}
    	conductor->next = 0; /* Set next node to null so nasty ........ doesn't happen. */
    	conductor->x = '\0'; 
    	conductor->y = '\0'; 
    	conductor->GlassBite = '\0';
    	return ;
    }
    Attached Images Attached Images  
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Here is the rest of the picture!
    Attached Images Attached Images  
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One posible issue is conductor is modified before the previous window is initialized.

    Add breaks in every line where you modify conductor.

    Kuphryn

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thank you kuphryn,

    Sometimes I can be such an idiot! Here is the proof:
    Code:
    case WM_PAINT:	// The window needs to be redrawn.
    	{
    		hdc = BeginPaint (hwnd, &ps) ;
    		/* miscellaneous other code */
    		
    
    		/* Just like you said here it is */	
    		conductor=conductor->next;
    
    		EndPaint (hwnd, &ps);
    		return 0;
            }
    I must now hang my head in shame!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I cannot think and type at the same time or else you'll weird characters.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21