Linked list pointer error

This is a discussion on Linked list pointer error within the C Programming forums, part of the General Programming Boards category; I have a linked list written up, am trying to make the "delete current record" function, for somehow it seem ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Linked list pointer error

    I have a linked list written up, am trying to make the "delete current record" function, for somehow it seem not working properly, I believe it is to do with my point structure, I checked everything in the code, but still can't tell where, can someone kindly point the error, thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <dos.h>
    #include <windows.h>
    
    typedef struct data_type               // declare structure for sine_wave database
    {
            double amp;
            double freq;
            double phase;
            struct data_type *next;
            struct data_type *former;
    } DATA;
    
    void print_data(DATA *item)             // function to print current SINE-WAVE details
    {                                     // to screen
    
    	if (item==NULL)
    	{
    		gotoxy(20,5);
    		printf("                                        ");
    		gotoxy(20,5);
                    printf("       END OF THE SINE WAVE LIST        ");
    	}
    	else
    	{
    		gotoxy(20,5);
    		printf("                                        ");
    		gotoxy(20,5);
                    printf("            SINE WAVE DATA              ");
    
                    gotoxy(25,10); printf("Amplitude:                           ");
                    gotoxy(25,11); printf("Frequency:                       Hz  ");
                    gotoxy(25,12); printf("Phase:                           deg ");
    
                    gotoxy(40,10); printf("%9.2lf", item->amp);
                    gotoxy(40,11); printf("%9.2lf", item->freq);
                    gotoxy(40,12); printf("%9.2lf", item->phase);
    	}
    }
    
    DATA * add_data(DATA *item)              // add a SINE_WAVE to the end of the linked list
    {
            DATA *new_data;
    
    	gotoxy(20,5);
            printf("             Adding a new wave               ");
    
    
            new_data = (DATA *) malloc(sizeof(DATA));
            new_data->next = NULL;
    
    
            gotoxy(25,10); printf("Amplitude:                           ");
            gotoxy(25,11); printf("Frequency:                       Hz  ");
            gotoxy(25,12); printf("Phase:                           deg ");
            
            gotoxy(40,10); scanf("%lf", &new_data->amp);
            gotoxy(40,11); scanf("%lf", &new_data->freq);
            gotoxy(40,12); scanf("%lf", &new_data->phase);
    
    	if (item != NULL)
            {              
                    item->next = new_data;
    	}
    
            return (new_data);
    }
    
    DATA * del_data(DATA *item)             // delete data from list
    {
            DATA *previous, *following;
            if (item!=NULL)                 //ensure item exists to delete
            {
            previous=item->former;        //assign former and next
            following=item->next;        //linksto pointers accordingly
    
            if (previous!=NULL) previous->next=following;            //reroute around
               
            if (following!=NULL) following->former=previous;
    	printf("%d\n",item);
            free(item);                     //release the item's memory
    
            if (previous!=NULL) 
    		item=previous;
            else item=following;
    }
    	return(item);
    }
    
    DATA * edit(DATA *item)              // Edit data in list
    {
    	DATA *new;
    	
    	gotoxy(20,5);
            printf("             editing current wave record              ");
    
          	new = item;
      
            gotoxy(25,10); printf("Amplitude:                           ");
            gotoxy(25,11); printf("Frequency:                       Hz  ");
            gotoxy(25,12); printf("Phase:                           deg ");
            
            gotoxy(40,10); scanf("%lf", &new->amp);
            gotoxy(40,11); scanf("%lf", &new->freq);
            gotoxy(40,12); scanf("%lf", &new->phase);
    
    	
            return (item);
    }
    
    
    DATA * next(DATA *item)              // move to next item in list
    {
    
    	if (item != NULL)
    	{
    		item = item->next;
    	}
    
    	return (item);
    }
    
    
    DATA * find(DATA *item)             // locate a particular item in the list
    {
    		gotoxy(20,5);
    		printf("      Facility not implemented yet!     ");
    
    		Sleep(1000);
    
    		return (item);
    }
    
    
    int main (void)                       // main function
    {
            DATA *first=NULL, *current=NULL, *last=NULL;
    	char response;
    
    	system("cls");
    
    	do                                 // repeat until "q" hit
    	{
    
                    print_data(current);             // print current item in list to screen
    
                    gotoxy(25,19); printf("'d': Delete current inputted data");   // display command options
                    gotoxy(25,18); printf("'a': Adding a new sine wave data");
                    gotoxy(25,20); printf("'f': find");
    		gotoxy(25,21); printf("'n': browse the next record on the database");
    		gotoxy(25,22); printf("'r': return to start of list");
                    gotoxy(25,23); printf("'e': edit current record");
                    gotoxy(25,24); printf("'q': quit the database");
    
    
    		gotoxy(40,25); putch(' ');      // overwrite old command
    		gotoxy(25,25);
    		printf("Enter command: ");      // get new command
    		response = getch();
    		gotoxy(34,40); putch(response); // print new command to screen
    
    		switch (response)               // call relevent function
    		{
                            case 'd': current = del_data(current); break;
                            case 'a': last = add_data(last); break;
                            case 'f': current = find(first); break;
                            case 'n': current = next(current); break;
    			case 'r': current = first; break;
                            case 'e': current = edit(current); break;
    		}
    
    		if (first == NULL) current = first = last;  // assign pointers to first
    									  // item provided in list
    	} while (response != 'q');
    
    	system("cls");
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    ha, problem solved

    I have forgotten to declare the "*former" pointer at the adding function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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