Problem with insertion function in linked list (pointers)

This is a discussion on Problem with insertion function in linked list (pointers) within the C Programming forums, part of the General Programming Boards category; Hi, i have a weird problem... Please see my code below: Code: void insert(struct record *start) { char name[30],name1[30],flag=' '; ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Question Problem with insertion function in linked list (pointers)

    Hi, i have a weird problem... Please see my code below:

    Code:
    void insert(struct record *start) 
    {
    	char name[30],name1[30],flag=' ';
    	struct record *temp,*curr, *prev;
    	curr=start;  
    	printf("\n Enter name to insert :");
    	gets(name);fflush(stdin);
    	printf("\n Enter the name before which to insert:");
    	gets(name1);fflush(stdin);
    	
    	while(curr->next != NULL && flag == ' ')  
    	{       
                         if(strcmp(curr->name,name1)== 0) //PROBLEM WITH THIS SECTION,TRYING TO ADD BEFORE FIRST NODE
                         {
                                           flag='*';
                                           temp=(struct record *) malloc(sizeof(struct record));
                                           strcpy(temp->name,name);
                                           temp->next = curr;
                                           curr = temp;
                                           printf("\n Insertion done !");
                                           getch();
                                           break;
                         }
              
    		else if(strcmp(curr->next->name,name1)== 0) // insert before second note , third, fourth ,etc
    		{	
                                        flag='*';
    			temp=(struct record *) malloc(sizeof(struct record));
                                        prev  = curr;
    			strcpy(temp->name,name);
    			temp->next = prev->next;
    			prev->next =temp;
    			printf("\n Insertion done !");
    			break;
    		}
    		else
    			curr=curr->next;
    	}
    	       if(flag==' ')
    		printf("\n Record not found !");
    		getch();
    }
    This code user can choose determine where to insert the name according what user type. i can insert the name before second node, but, i cant insert it BEFORE THE FIRST NODE. That means , if i insert the new node before my first node, then the new node will became my first note. My code looks fine but doesn't work, any solution?
    Last edited by kekewong; 10-08-2009 at 10:23 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your code doesn't look fine at all. It is impossible to write an insert-into-a-linked-list function with the prototype
    Code:
    void insert(struct record *)
    You can have
    Code:
    struct record * insert(struct record *)
    (where you return the new head of the list) or you can have
    Code:
    void insert(struct record **)
    (where the address of the head of the list is passed in so that the head of the list can be modified).

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    hmm ok ..
    Code:
                                           temp->next = curr;
                                           curr = temp;
    Does the code above is correct ? replacing the first node as temp.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Think for ten seconds. If you have this function:
    Code:
    int this_doesnt_work(int x) {
        x = 10;
    }
    do you expect the value passed in to change in your original function? No.

    So when you have
    Code:
    void insert(struct record *start)
    it is not physically possible to change the value of start. It cannot be done.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Where is the first node or head node can u tell how u want to add the nodes ??

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    hmm.. TabStop thanks for the solution, but it still doesn't works. Anywhere went wrong???

    lol, sorry i still learning pointers . Lecturer just pass me the question and call us to fix the codes =.= so ... i let you guys see the full code that the lecturer want us to correct it (i have correct half way ):

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct record
    {
    	char name[30];
    	struct record *next;
    };
    
    void main()
    {
    	int choice;
    	char ans;
    
        struct record *insert(struct record *), *append(struct record *);  //prototype
         void view(struct record *);//prototype
    	struct record *del(struct record *); //prototype
    	struct record *start;
    
    	
    	start=(struct record *)malloc(sizeof(struct record));
    	start->next=NULL;
        
    	do
    	{
    		system("cls");
    		printf("\n\n 1. Insert");
    		printf("\n 2. Deletion");
    		printf("\n 3. View");	
    		printf("\n 4. Append");
    		printf("\n 5. EXIT");
    		printf("\n\n Choice:");
    		scanf("%d",&choice);fflush(stdin);
    
    		switch(choice)
    		{
    		case 1:
    			insert(start);
    			break;
    		case 2:
    			del(start);
    			break;
    		case 3:
    			view(start);
    			break;
    		case 4:
    			append(start);
    			break;
    		case 5:
    			 exit(0);
    		default:
    			printf("\n Invalid Choice ");
    		}
    	}while(choice!=99);
    }
    
    
     struct record *insert( struct record *start) 
    {
    	char name[30],name1[30],flag=' ';
    	struct record *temp,*curr, *prev ,*list;
    	curr=start;  
    	printf("\n Enter name to insert :");
    	gets(name);fflush(stdin);
    	printf("\n Enter the name before which to insert:");
    	gets(name1);fflush(stdin);
    	
    	while(curr != NULL && flag == ' ')  
    	{       
                            if(strcmp(curr->name,name1)== 0)
                                  {
                                     flag='*';
                                     temp=(struct record *) malloc(sizeof(struct record));
                           	         strcpy(temp->name,name);
                                     temp->next = curr;
                                     curr = temp;
                                     printf("\n Insertion done !");
                                     getch();
             	                     return start;
                          	        break;
                                   }
              
    		else if(strcmp(curr->next->name,name1)== 0)
    		            {	flag='*';
                			temp=(struct record *) malloc(sizeof(struct record));
                                                    prev  = curr;
                			strcpy(temp->name,name);
                			temp->next = prev->next;
                			prev->next =temp;
                			 getch();
                			printf("\n Insertion done !");
                			return start;
                			break;
    		              }
    		else
    			curr=curr->next;
    	}
    	if(flag==' ')
    		printf("\n Record not found !");
    		getch();
    }
    
    
    struct record *  del(struct record *start)  // delete haven correct it yet
    {
    	char name;
    	struct record *curr;
    	printf("\n Enter the name to delete:");
    	gets(name);fflush(stdin);
    	curr=start;
    	if(strcmp(curr->name,name)==0)
    		{
    			if(start==NULL)
    				{
    				start=(struct record *) malloc(sizeof(struct record));
    				strcpy(start->name,"   ");
    				start->next=NULL;
    				}
    			printf("\n\n\t Deletion successful!");
    		}
    	while(curr)
    	{
    		
    		if(strcmp(curr->next->name,name)==0)
    		{
    			curr->next=curr->next->next;
    			printf("\n Deletion successful !");
    		}
    		else
    			curr=curr->next;
    	}
    	return start;
    }
    
    view(struct record *start)
    {
    	struct record *curr;
    	curr=start;
    	while(curr != NULL)
    	{
    		printf("\n %s \n ",curr->name);
    		curr=curr->next;
    		getch();
    	}
    	printf("\n\n\t No more records !");
    
    }
    
     struct record *append(struct record *start)
    {
    		struct record *curr;
    		char name[30];
    		static int flag;
    		printf("\n Enter the name :");
    		gets(name);fflush(stdin);
    	
    		if(flag==0 || strcmp(start->name," ")==0)
    		{
    			strcpy(start->name,name);
    			start->next=NULL;
    			flag =1 ;
    			getch();
    		}
    		else
    		{
                
    			curr=start;
    			while(curr != NULL)
    			{
                           if( curr->next == NULL)
                           {
        			            curr->next=(struct record *) malloc(sizeof(struct record));
                                curr=curr->next;
        			            strcpy(curr->name,name);
        			            curr->next=NULL;
        			            curr = curr->next ;
                            }
                            else
                            {curr = curr->next ; }
            }
    		}
    		printf("\n\n\t Append successful!");
    		return start;
    	getch();
    }
    Can guide me where is the insertion went wrong? Still cannot insert before the first node @.@
    Last edited by kekewong; 10-09-2009 at 02:43 AM.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    LOL, finally i fix it !!!! XD

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct record
    {
    	char name[30];
    	struct record *next;
    };
    
    void main()
    {
    	int choice;
    	char ans;
    
        struct record *insert(struct record *), *append(struct record *);  //prototype\
         void view(struct record *);
    	struct record *del(struct record *); //prototype
    	struct record *start;
    
    	
    	start=(struct record *)malloc(sizeof(struct record));
    	start->next=NULL;
        
    	do
    	{
    		system("cls");
    		printf("\n\n 1. Insert");
    		printf("\n 2. Deletion");
    		printf("\n 3. View");	
    		printf("\n 4. Append");
    		printf("\n 5. EXIT");
    		printf("\n\n Choice:");
    		scanf("%d",&choice);fflush(stdin);
    
    		switch(choice)
    		{
    		case 1:
    			start = insert(start);
    			break;
    		case 2:
    			del(start);
    			break;
    		case 3:
    			view(start);
    			break;
    		case 4:
    			start = append(start);
    			break;
    		case 5:
    			 exit(0);
    		default:
    			printf("\n Invalid Choice ");
    		}
    	}while(choice!=99);
    }
    
    
     struct record *insert( struct record *start) 
    {
    	char name[30],name1[30],flag=' ';
    	struct record *temp,*curr, *prev;
    	curr=start;  
    	printf("\n Enter name to insert :");
    	gets(name);fflush(stdin);
    	printf("\n Enter the name before which to insert:");
    	gets(name1);fflush(stdin);
    	
    	while(curr != NULL && flag == ' ')  
    	{       
              if(strcmp(curr->name,name1)== 0)
              {
                             flag='*';
                             temp=(struct record *) malloc(sizeof(struct record));
                           	 strcpy(temp->name,name);
                             temp->next = curr;
                             curr = temp;
                             printf("\nInsertion done !");
                              getch();
             	             return curr;
                          	 break;
              }
              
    		else if(strcmp(curr->next->name,name1)== 0)
    		            {	flag='*';
                			temp=(struct record *) malloc(sizeof(struct record));
                            prev  = curr;
                			strcpy(temp->name,name);
                			temp->next = prev->next;
                			prev->next =temp;
                			 getch();
                			printf("\n Insertion done !");
                				return start;
                			break;
    		}
    		else
    			curr=curr->next;
    	}
    	if(flag==' ')
    		printf("\n Record not found !");
    		getch();
    }
    
    
    struct record *  del(struct record *start) // HAVEN FIX DELETE FUNCTION
    {
    	char name;
    	struct record *curr;
    	printf("\n Enter the name to delete:");
    	gets(name);fflush(stdin);
    	curr=start;
    	if(strcmp(curr->name,name)==0)
    		{
    			if(start==NULL)
    				{
    				start=(struct record *) malloc(sizeof(struct record));
    				strcpy(start->name,"   ");
    				start->next=NULL;
    				}
    			printf("\n\n\t Deletion successful!");
    		}
    	while(curr)
    	{
    		
    		if(strcmp(curr->next->name,name)==0)
    		{
    			curr->next=curr->next->next;
    			printf("\n Deletion successful !");
    		}
    		else
    			curr=curr->next;
    	}
    	return start;
    }
    
    view(struct record *start)
    {
    	struct record *curr;
    	curr=start;
    	while(curr != NULL)
    	{
    		printf("\n %s \n ",curr->name);
    		curr=curr->next;
    		getch();
    	}
    	printf("\n\n\t No more records !");
    
    }
    
     struct record *append(struct record *start)
    {
    		struct record *curr;
    		char name[30];
    		static int flag;
    		printf("\n Enter the name :");
    		gets(name);fflush(stdin);
    	
    		if(flag==0 || strcmp(start->name," ")==0)
    		{
    			strcpy(start->name,name);
    			start->next=NULL;
    			flag =1 ;
    			getch();
    		}
    		else
    		{
                
    			curr=start;
    			while(curr != NULL)
    			{
                           if( curr->next == NULL)
                           {
        			            curr->next=(struct record *) malloc(sizeof(struct record));
                                curr=curr->next;
        			            strcpy(curr->name,name);
        			            curr->next=NULL;
        			            curr = curr->next ;
                            }
                            else
                            {curr = curr->next ; }
            }
    		}
    		printf("\n\n\t Append successful!");
    		return start;
    	getch();
    }
    But i still got problem , refering to tabstop says, i also can use
    Code:
    void insert(struct record **)
    Can teach me how to write? i am kind of confusing of two "**" in the same time @.@ ,

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    LOL, finally i fix it !!!! XD

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct record
    {
    	char name[30];
    	struct record *next;
    };
    
    void main()
    {
    	int choice;
    	char ans;
    
        struct record *insert(struct record *), *append(struct record *);  //prototype\
         void view(struct record *);
    	struct record *del(struct record *); //prototype
    	struct record *start;
    
    	
    	start=(struct record *)malloc(sizeof(struct record));
    	start->next=NULL;
        
    	do
    	{
    		system("cls");
    		printf("\n\n 1. Insert");
    		printf("\n 2. Deletion");
    		printf("\n 3. View");	
    		printf("\n 4. Append");
    		printf("\n 5. EXIT");
    		printf("\n\n Choice:");
    		scanf("%d",&choice);fflush(stdin);
    
    		switch(choice)
    		{
    		case 1:
    			start = insert(start);
    			break;
    		case 2:
    			del(start);
    			break;
    		case 3:
    			view(start);
    			break;
    		case 4:
    			start = append(start);
    			break;
    		case 5:
    			 exit(0);
    		default:
    			printf("\n Invalid Choice ");
    		}
    	}while(choice!=99);
    }
    
    
     struct record *insert( struct record *start) 
    {
    	char name[30],name1[30],flag=' ';
    	struct record *temp,*curr, *prev;
    	curr=start;  
    	printf("\n Enter name to insert :");
    	gets(name);fflush(stdin);
    	printf("\n Enter the name before which to insert:");
    	gets(name1);fflush(stdin);
    	
    	while(curr != NULL && flag == ' ')  
    	{       
              if(strcmp(curr->name,name1)== 0)
              {
                             flag='*';
                             temp=(struct record *) malloc(sizeof(struct record));
                           	 strcpy(temp->name,name);
                             temp->next = curr;
                             curr = temp;
                             printf("\nInsertion done !");
                              getch();
             	             return curr;
                          	 break;
              }
              
    		else if(strcmp(curr->next->name,name1)== 0)
    		            {	flag='*';
                			temp=(struct record *) malloc(sizeof(struct record));
                            prev  = curr;
                			strcpy(temp->name,name);
                			temp->next = prev->next;
                			prev->next =temp;
                			 getch();
                			printf("\n Insertion done !");
                				return start;
                			break;
    		}
    		else
    			curr=curr->next;
    	}
    	if(flag==' ')
    		printf("\n Record not found !");
    		getch();
    }
    
    
    struct record *  del(struct record *start) // HAVEN FIX DELETE FUNCTION
    {
    	char name;
    	struct record *curr;
    	printf("\n Enter the name to delete:");
    	gets(name);fflush(stdin);
    	curr=start;
    	if(strcmp(curr->name,name)==0)
    		{
    			if(start==NULL)
    				{
    				start=(struct record *) malloc(sizeof(struct record));
    				strcpy(start->name,"   ");
    				start->next=NULL;
    				}
    			printf("\n\n\t Deletion successful!");
    		}
    	while(curr)
    	{
    		
    		if(strcmp(curr->next->name,name)==0)
    		{
    			curr->next=curr->next->next;
    			printf("\n Deletion successful !");
    		}
    		else
    			curr=curr->next;
    	}
    	return start;
    }
    
    view(struct record *start)
    {
    	struct record *curr;
    	curr=start;
    	while(curr != NULL)
    	{
    		printf("\n %s \n ",curr->name);
    		curr=curr->next;
    		getch();
    	}
    	printf("\n\n\t No more records !");
    
    }
    
     struct record *append(struct record *start)
    {
    		struct record *curr;
    		char name[30];
    		static int flag;
    		printf("\n Enter the name :");
    		gets(name);fflush(stdin);
    	
    		if(flag==0 || strcmp(start->name," ")==0)
    		{
    			strcpy(start->name,name);
    			start->next=NULL;
    			flag =1 ;
    			getch();
    		}
    		else
    		{
                
    			curr=start;
    			while(curr != NULL)
    			{
                           if( curr->next == NULL)
                           {
        			            curr->next=(struct record *) malloc(sizeof(struct record));
                                curr=curr->next;
        			            strcpy(curr->name,name);
        			            curr->next=NULL;
        			            curr = curr->next ;
                            }
                            else
                            {curr = curr->next ; }
            }
    		}
    		printf("\n\n\t Append successful!");
    		return start;
    	getch();
    }
    Thanks alot TabStop
    Last edited by kekewong; 10-09-2009 at 10:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 09:54 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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