Concatenating in linked list

This is a discussion on Concatenating in linked list within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> // needed for malloc, free struct node { // a node in the linked list ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    47

    Concatenating in linked list

    Code:
    #include <stdio.h>
    #include <stdlib.h>			// needed for malloc, free
    
    
    struct node {				// a node in the linked list
    	char data;
    	struct node *next;
    };
    
    int menu(void);					// display list of choices, get user's choice
    struct node *addString(struct node *, const char *, int *, struct node *[]); // Add a string (char array) to the list
    void output(struct node *f, int *, struct node *[]); // Output a string based on a users selected index
    void stringLength(struct node *f, int *, struct node *[]); // Output the length of a string based on a users selected index
    struct node *concatenate(struct node *f, int *, int *, int *,  struct node *[]); // concatenate 2 strings together
    struct node *insertChar(struct node *, int *, char *, int *,  struct node *[]); // Insert a character at a given position of a string
    struct node *del(struct node *, int *, int *,  struct node *[]);	// Delete a character at a given position of a string	
    struct node *destroy(struct node *, int *, struct node *[]); // deallocate a user specified list
    struct node *destroyAll(struct node *, struct node *[]); // deallocate all lists
    
    int main(void)
    {
    	struct node *front;		// front points to the front of our list
    	struct node *linkedListStrings[5] = {NULL, NULL, NULL, NULL, NULL}; //an array of 5 struct node pointers 
    	int optionsResponse;	// User input for main menu options
    	int index = 0;				// Index of the first or only string used
    	int index2;				// Index of the second string used
    	int index3;				// Index of the third string used (concatination)
    	int position;			// The posiiton in the string to insert characters
    	char insertedCharacter;	// The users input character to be inserted into the linked list
    	char response[20];	// user input for sub menu options
    	int i;
    	front = NULL;			// initialize list to NULL
    	
    	do				// display menu, do command, quit when user inputs choice 8
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 :  // Adding a new string
    for(i=0;i<5;i++)
    	printf("&#37;d\t%p\n", i, linkedListStrings[i]);
    					printf ("Enter a new string:  ");	
    					scanf("%s", response);
    					printf ("Enter a node to place it in [0-4]: ");
    					scanf("%d", &index);
    					addString(front, response, &index, linkedListStrings);
    					printf("\n");
    for(i=0;i<5;i++)
    	printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 2 : 
    				for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// printing a string from a selected index
    					printf ("Enter the index of the string that you would like to output [0-4]:  ");
    					scanf("%d", &index);
    					output(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 3 : 
    				for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// printing the length of a string from a selected index
    					printf ("Enter the index of the string [0-4]:  ");
    					scanf("%d", &index);
    					stringLength(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 4 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// concatenating two strings together and selecting an index to place the new string
    					printf ("Enter the index of the first string [0-4]:  ");	
    					scanf("%d", &index);
    					printf ("Enter the index of the second string [0-4]:  ");	
    					scanf("%d", &index2);
    					printf ("Enter an index to place the concatenated string [0-4]:  ");	 
    					scanf("%d", &index3);
    					concatenate(front, &index, &index2, &index3, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 5 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// Inserting a character into a position of specifed string
    					printf ("Enter the index of the string [0-4]:  ");	
    					scanf("%d", &index);
    					getchar();
    					printf ("Enter a character to be inserted into the string:  ");	
    					scanf("%c", &insertedCharacter);
    					printf ("\nEnter the position in the string at \nwhich you would like to insert a character:  ");
    					scanf("%d", &position);
    					insertChar(front, &index, &insertedCharacter, &position, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 6 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// Deleting a character from a specified position
    					printf ("Enter the index of the string [0-4]:  ");	
    					scanf("%d", &index);
    					printf ("Enter the position in the string corresponding \nto the character you would like to delete:  ");	
    					scanf("%d", &position);
    					del(front, &index, &position, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 7 :  for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// destroying/deleting a string
    					printf ("Enter the index of the string to be destroyed [0-4]:  ");	
    					scanf("%d", &index);
    					destroy(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break; 
    			case 8 :  for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// destroying/deleting a string
    					destroyAll(front, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break; 
    		}
    	} while(optionsResponse != 9); 
    	destroyAll(front, linkedListStrings);
    }
    
    int menu(void)	// display menu and get user's choice
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("2.\tOutput a string\n");
    	printf("3.\tOutput the length of a string\n");
    	printf("4.\tConcatenate two strings together\n");
    	printf("5.\tInsert a character at a given position of a string\n");
    	printf("6.\tDelete a character at a given position of a string\n");
    	printf("7.\tDestroy a string\n");
    	printf("8.\tDestroy all strings\n");
    	printf("9.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    struct node *addString(struct node *f, const char *response, int *index, struct node *linkedListStrings[])// Add a string (char array) to the list
    { 	
    	int i = 0; // i will act as a counter to print values from the array
    	// create a new node, store response as the datum
    	struct node *temp = (struct node *)malloc(sizeof(struct node)); 
    	struct node *front = f; // front points to the front of our list
    	
    
    	if (linkedListStrings[*index] != NULL) // Tests to make sure a node was not already created at the index specified
    	{
    		printf("Error - A string already exists at location %d", *index);
    	}
    	else
    	{	
    		/*	While the character in the char array is not '\0' iterate through
    			the array and add each value to the node */
    		f = linkedListStrings[*index];
    		while(response[i] != '\0') 
    		{
    			if(f == NULL) // if f is NULL store the value in the first node
    			{
    				front = f = (struct node *)malloc(sizeof(struct node));
    				f->data = response[i++];
    				f->next = NULL;
    			}
    			else if(f->next == NULL) // If f is not null, find the next NULL node and store data to it
    			{
    				temp = (struct node *)malloc(sizeof(struct node));
    				temp->data = response[i++];
    				temp->next = NULL;
    	      
    				f->next = temp;
    			}
    			else // If f is not null and the next node in the list is not null, move to the next node
    			{
    				f = f->next;
    			}
    		}
    			linkedListStrings[*index] = front; // front of the list as it has not been modified
    	}	  
    		return *linkedListStrings; 
    }
    
    
    void output(struct node *f, int *index, struct node *linkedListStrings[]) // Output a string based on a users selected index
    {
    	struct node *front = f;  // front points to the front of our list
    
    	f = linkedListStrings[*index];
    
    	if (f == NULL) // Error checking to prevent the user from selecting a NULL index
    	{
    		printf("A string has not been created for index %d\n", *index);
    	}
    	else
    	{
    		printf("String at index %d is: ", *index);
    		while(f != NULL) // iterate through the list, printing each node's datum
    		{
    			printf("%c", f->data);
    			f = f->next;
    		}
    	}
    
    	printf("\n\n");
    }
    
    
    struct node *concatenate(struct node *f, int *index, int *index2, int *index3, struct node *linkedListStrings[])  // concatenate 2 strings together
    {
    	struct node *front = f; // front points to the front of our list
    	struct node *node1; // points to linkedListStrings[*index]
    	struct node *node2; // points to linkedListStrings[index2]
    	struct node *node3; // points to linkedListStrings[index3]
    	struct node *temp;  // temporary value holder
    	struct node *frontNode3; // Points to the front of node3 (linkedListStrings[index3])
    
    	f = node1 = linkedListStrings[*index];
    	node2 = linkedListStrings[*index2];
    	//frontNode3 = node3 = malloc(sizeof(struct node));
    	frontNode3 = node3 = (struct node *)malloc(sizeof(struct node)); // ADDED
    
    
    	if(linkedListStrings[*index] != NULL) // Make sure that the first user specified linked list is not empty
    	{ 
    		if (linkedListStrings[*index2] != NULL)// Make sure that the second user specified linked list is not empty
    		{
    			if (linkedListStrings[*index3] == NULL) // Make sure that the location to place the concatenated strings is empty
    			{
    				while(f != NULL) // Add linkedListStrings[*index] to the user specified location
    				{
    					node3->data = f->data;
    					//temp = malloc(sizeof(struct node)); 
    					temp = (struct node *)malloc(sizeof(struct node)); // ADDED
    					node3->next = temp;
    					f = f->next;
    					node3 = node3->next;
    				}
    
    				f = node2;
        
    				while(f != NULL) // Add linkedListStrings[index2] at the end of linkedListStrings[*index] of the user specified location
    				{
    					node3->data = f->data;
    					//temp = malloc(sizeof(struct node)); 
    					temp = (struct node *)malloc(sizeof(struct node)); // ADDED
    					f = f->next;
          
    					if(f != NULL)
    					{
    						node3->next = temp;
    						node3 = node3->next;
    					}
    				}
    					linkedListStrings[*index3] = frontNode3; // front of the list 3 as it has not been modified
    			}
    			else // Error - linkedListStrings[index3] != NULL
    			{	
    				printf("A string already exists at location %d \n", *index3);	
    			}
    		}
    		else // Error - linkedListStrings[index2] = NULL
    		{
    			printf("A string has not been created for location %d\n", *index2);
    		}
    	}
    	else // Error - linkedListStrings[*index] = NULL
    	{
    		printf("A string has not been created for location %d\n", *index);
    	}
    	   
    	return *linkedListStrings;
    }
    The bolded section of my code is what I am having trouble on. It seems to work, but when I go to print what is in index 3, after concatenating index 0 and index 1 to index 3, The program crashes. But before it crashes it tells me the result and it is correct. I know my print/output method is correct because when I make a new string, it gives me the correct response, it just wont work for concatenate. What am i doing wrong?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    where do you set the ->next pointer of the last node to NULL in the concatenated list?

    why do you need to pass indexes by pointers?

    why not to write a copy function and call it twice to concat 2 lists?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I was getting warning messages when I didn't have a pointer to the index. That got rid of warning.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    That got rid of warning.
    you choose not the best way to get rid of warnings... passing indexes by value will make code easier to read and it si 100&#37; doable thing
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Code:
     if(f != NULL)
    {
    node3->next = temp;
    node3 = node3->next;
    }
    so if f is null don't set the value the next node which makes it null

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    so if f is null don't set the value the next node which makes it null
    which makes it garbage...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I fixed all of my index pointer warnings a better way, and added 2 other methods that don't seem to be working correctly. I am having trouble deleting characters from a specified position and adding a character to a specified position. I am not sure what I am doing wrong.

    Concatenate seems to work, but it crashes after I try to output the concatenated word. It is able to tell me what my concatenated word is, but immediately crashes after.

    I bolded the methods that do not work correctly

    Code:
    #include <stdio.h>
    #include <stdlib.h>			// needed for malloc, free
    
    
    struct node {				// a node in the linked list
    	char data;
    	struct node *next;
    };
    
    int menu(void);					// display list of choices, get user's choice
    struct node *addString(struct node *, const char *, int *, struct node *[]); // Add a string (char array) to the list
    void output(struct node *f, int *, struct node *[]); // Output a string based on a users selected index
    void stringLength(struct node *f, int *, struct node *[]); // Output the length of a string based on a users selected index
    struct node *concatenate(struct node *f, int *, int *, int *,  struct node *[]); // concatenate 2 strings together
    struct node *insertChar(struct node *, int *, char *, int *,  struct node *[]); // Insert a character at a given position of a string
    struct node *del(struct node *, int *, int *,  struct node *[]);	// Delete a character at a given position of a string	
    struct node *destroy(struct node *, int *, struct node *[]); // deallocate a user specified list
    struct node *destroyAll(struct node *, struct node *[]); // deallocate all lists
    
    int main(void)
    {
    	struct node *front;		// front points to the front of our list
    	struct node *linkedListStrings[5] = {NULL, NULL, NULL, NULL, NULL}; //an array of 5 struct node pointers 
    	int optionsResponse;	// User input for main menu options
    	int index = 0;				// Index of the first or only string used
    	int index2;				// Index of the second string used
    	int index3;				// Index of the third string used (concatination)
    	int position;			// The posiiton in the string to insert characters
    	char insertedCharacter;	// The users input character to be inserted into the linked list
    	char response[20];	// user input for sub menu options
    	int i;
    	front = NULL;			// initialize list to NULL
    	
    	do				// display menu, do command, quit when user inputs choice 8
    	{				
    		optionsResponse = menu();
    		switch(optionsResponse) 
    		{
    			case 1 :  // Adding a new string
    for(i=0;i<5;i++)
    	printf("&#37;d\t%p\n", i, linkedListStrings[i]);
    					printf ("Enter a new string:  ");	
    					scanf("%s", response);
    					printf ("Enter a node to place it in [0-4]: ");
    					scanf("%d", &index);
    					addString(front, response, &index, linkedListStrings);
    					printf("\n");
    for(i=0;i<5;i++)
    	printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 2 : 
    				for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// printing a string from a selected index
    					printf ("Enter the index of the string that you would like to output [0-4]:  ");
    					scanf("%d", &index);
    					output(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 3 : 
    				for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// printing the length of a string from a selected index
    					printf ("Enter the index of the string [0-4]:  ");
    					scanf("%d", &index);
    					stringLength(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 4 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// concatenating two strings together and selecting an index to place the new string
    					printf ("Enter the index of the first string [0-4]:  ");	
    					scanf("%d", &index);
    					printf ("Enter the index of the second string [0-4]:  ");	
    					scanf("%d", &index2);
    					printf ("Enter an index to place the concatenated string [0-4]:  ");	 
    					scanf("%d", &index3);
    					concatenate(front, &index, &index2, &index3, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 5 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// Inserting a character into a position of specifed string
    					printf ("Enter the index of the string [0-4]:  ");	
    					scanf("%d", &index);
    					getchar();
    					printf ("Enter a character to be inserted into the string:  ");	
    					scanf("%c", &insertedCharacter);
    					printf ("\nEnter the position in the string at \nwhich you would like to insert a character:  ");
    					scanf("%d", &position);
    					insertChar(front, &index, &insertedCharacter, &position, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 6 : for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// Deleting a character from a specified position
    					printf ("Enter the index of the string [0-4]:  ");	
    					scanf("%d", &index);
    					printf ("Enter the position in the string corresponding \nto the character you would like to delete:  ");	
    					scanf("%d", &position);
    					del(front, &index, &position, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break;
    			case 7 :  for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// destroying/deleting a string
    					printf ("Enter the index of the string to be destroyed [0-4]:  ");	
    					scanf("%d", &index);
    					destroy(front, &index, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break; 
    			case 8 :  for(i=0;i<5;i++)
    				printf("%d\t%p\n", i, linkedListStrings[i]);
    				// destroying/deleting a string
    					destroyAll(front, linkedListStrings);
    					for(i=0;i<5;i++)
    					printf("%d\t%p\n", i, linkedListStrings[i]);
    					break; 
    		}
    	} while(optionsResponse != 9); 
    	destroyAll(front, linkedListStrings);
    }
    
    int menu(void)	// display menu and get user's choice
    {
    	int temp;
    	printf("Please select from one of the following String menu options: \n\n");
    	printf("1.\tCreate a new string\n");
    	printf("2.\tOutput a string\n");
    	printf("3.\tOutput the length of a string\n");
    	printf("4.\tConcatenate two strings together\n");
    	printf("5.\tInsert a character at a given position of a string\n");
    	printf("6.\tDelete a character at a given position of a string\n");
    	printf("7.\tDestroy a string\n");
    	printf("8.\tDestroy all strings\n");
    	printf("9.\tQuit\n\n");
    	printf("I choose option number: ");
    	scanf("%d", &temp);
    	return temp;
    }
    
    struct node *addString(struct node *f, const char *response, int *index, struct node *linkedListStrings[])// Add a string (char array) to the list
    { 	
    	int i = 0; // i will act as a counter to print values from the array
    	// create a new node, store response as the datum
    	struct node *temp = (struct node *)malloc(sizeof(struct node)); 
    	struct node *front = f; // front points to the front of our list
    	int indexValue;
    	indexValue = *index;
    
    	
    
    	if (linkedListStrings[*index] != NULL) // Tests to make sure a node was not already created at the index specified
    	{
    		printf("Error - A string already exists at location %d", indexValue);
    	}
    	else
    	{	
    		/*	While the character in the char array is not '\0' iterate through
    			the array and add each value to the node */
    		f = linkedListStrings[*index];
    		while(response[i] != '\0') 
    		{
    			if(f == NULL) // if f is NULL store the value in the first node
    			{
    				front = f = (struct node *)malloc(sizeof(struct node));
    				f->data = response[i++];
    				f->next = NULL;
    			}
    			else if(f->next == NULL) // If f is not null, find the next NULL node and store data to it
    			{
    				temp = (struct node *)malloc(sizeof(struct node));
    				temp->data = response[i++];
    				temp->next = NULL;
    	      
    				f->next = temp;
    			}
    			else // If f is not null and the next node in the list is not null, move to the next node
    			{
    				f = f->next;
    			}
    		}
    			linkedListStrings[indexValue] = front; // front of the list as it has not been modified
    	}	  
    		return *linkedListStrings; 
    }
    
    
    void output(struct node *f, int *index, struct node *linkedListStrings[]) // Output a string based on a users selected index
    {
    	struct node *front = f;  // front points to the front of our list
    	int indexValue;
    
    	indexValue = *index;
    
    	f = linkedListStrings[*index];
    
    	if (f == NULL) // Error checking to prevent the user from selecting a NULL index
    	{
    		printf("A string has not been created for index %d\n", indexValue);
    	}
    	else
    	{
    		printf("String at index %d is: ", *index);
    		while(f != NULL) // iterate through the list, printing each node's datum
    		{
    			printf("%c", f->data);
    			f = f->next;
    		}
    	}
    
    	printf("\n\n");
    }
    
    
    struct node *concatenate(struct node *f, int *index, int *index2, int *index3, struct node *linkedListStrings[])  // concatenate 2 strings together
    {
    	struct node *front = f; // front points to the front of our list
    	struct node *node1; // points to linkedListStrings[*index]
    	struct node *node2; // points to linkedListStrings[index2]
    	struct node *node3; // points to linkedListStrings[index3]
    	struct node *temp;  // temporary value holder
    	struct node *frontNode3; // Points to the front of node3 (linkedListStrings[index3])
    	int indexValue, index2Value, index3Value;
    
    	indexValue = *index;
    	index2Value = *index2;
    	index3Value = *index3;
    
    	f = node1 = linkedListStrings[indexValue];
    	node2 = linkedListStrings[index2Value];
    	//frontNode3 = node3 = malloc(sizeof(struct node));
    	frontNode3 = node3 = (struct node *)malloc(sizeof(struct node)); // ADDED
    
    
    	if(linkedListStrings[indexValue] != NULL) // Make sure that the first user specified linked list is not empty
    	{ 
    		if (linkedListStrings[index2Value] != NULL)// Make sure that the second user specified linked list is not empty
    		{
    			if (linkedListStrings[index3Value] == NULL) // Make sure that the location to place the concatenated strings is empty
    			{
    				while(f != NULL) // Add linkedListStrings[*index] to the user specified location
    				{
    					node3->data = f->data;
    					//temp = malloc(sizeof(struct node)); 
    					temp = (struct node *)malloc(sizeof(struct node)); // ADDED
    					node3->next = temp;
    					f = f->next;
    					node3 = node3->next;
    				}
    
    				f = node2;
        
    				while(f != NULL) // Add linkedListStrings[index2] at the end of linkedListStrings[*index] of the user specified location
    				{
    					node3->data = f->data;
    					//temp = malloc(sizeof(struct node)); 
    					temp = (struct node *)malloc(sizeof(struct node)); // ADDED
    					f = f->next;
          
    					if(f != NULL)
    					{
    						node3->next = temp;
    						node3 = node3->next;
    					}
    				}
    					linkedListStrings[index3Value] = frontNode3; // front of the list 3 as it has not been modified
    			}
    			else // Error - linkedListStrings[index3] != NULL
    			{	
    				printf("A string already exists at index %d \n", index3Value);	
    			}
    		}
    		else // Error - linkedListStrings[index2] = NULL
    		{
    			printf("A string has not been created for index %d\n", index2Value);
    		}
    	}
    	else // Error - linkedListStrings[*index] = NULL
    	{
    		printf("A string has not been created for index %d\n", indexValue);
    	}
    	   
    	return *linkedListStrings;
    }
    
    struct node *insertChar(struct node *f, int *index, char *insertedCharacter, int *position, struct node *linkedListStrings[]) // Insert a character at a given position of a string
    {
    	struct node *front = f; // front points to the front of our list
    	struct node *temp;
    	int iter; // Used to iterate through the linked lists nodes
    	int indexValue;
    
    	indexValue = *index;
    	 
    	f = linkedListStrings[*index];
    
    	if(linkedListStrings[*index] != NULL)
    	{
    		/*	Iterate from 1 position from the specified position where
    			we insert the new character	*/
    		for(iter = *position-1; iter > 0; iter--) 
    		{
    			if(f != NULL) // Go to the next node while f is not null
    			{
    				f = f->next;
    			}
    			else // Error checking
    			{
    				printf("Location does not exist!\n");
    				iter = 0;
    			}
    		}
        
    		if(f != NULL)
    		{  
    			temp = malloc(sizeof(struct node));
    			temp->data = *insertedCharacter;
    
    			if(position > 0) // place the new character at its user specified position
    			{  
    				temp->next = f->next;
    				f->next = temp;
    			}
    			else // If position is 0, place the new character at the beginning of the list
    			{
    				front = temp;
    				temp->next = f;  
    			} 
          
    			linkedListStrings[indexValue] = front; // front of the list as it has not been modified
    		}
    	}
    	else // error checking - If the index the user specified was NULL
    	{
    		printf("A string has not been created for location %d\n", indexValue);
    	}
      
    	return *linkedListStrings;
    }
    
    struct node *del(struct node *f, int *index, int *position, struct node *linkedListStrings[]) // Delete a character at a given position of a string	
    {
    	struct node *front = f; // front points to the front of our list
    	struct node *temp;
    	int iter; // Used to iterate through the linked lists nodes
    	int indexValue;
    
    	indexValue = *index;
    
    	f = linkedListStrings[indexValue];
    
    	if(linkedListStrings[indexValue] != NULL)
    	{
    		/*	Iterate from 1 position from the specified position where
    			we delete the character	*/
    		for(iter = *position-1; iter > 0; iter--)
    		{
    			if(f != NULL)
    			{
    				f = f->next; // Go to the next node while f is not null
    			}
    			else // Error checking
    			{
    				printf("Error - No such position.");
    				iter = 0;
    			}
    		}
    
    		if(f != NULL)
    		{      
    			if(position > 0) // delete the character at its user specified position
    			{  
    				temp = f->next;
    				f->next = temp->next;
    				free(temp);
    			}
    			else // if position was 0, destroy the string
    			{
    				front = f->next;
    				free(f);  
    			} 
    			linkedListStrings[indexValue] = front; // front of the list as it has not been modified
    		}
    	}
    	else // Error checking - if(linkedListStrings[*index] = NULL), cannot delete anything
    	{
    		printf("A string has not been created for index %d\n", indexValue);
    	}
    
    	return *linkedListStrings;
    }
    

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    I would start with creating the function
    Code:
    struct node * createNode(void)
    {
    	struct node * p =malloc(sizeof(*p));
    	if(p)
    	{
    		p->data = '\0';
    		p->next = NULL;
    	}
    	return p;
    }
    and use it everywhere you are allocating a new node - this way you'll be sure no garbage values are added to the list
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    My insert character method seems to delete the index of the linked list that I want to manipulate. I don't free anything in it though... Can anyone see anything that I am doing wrong?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    I think it would be very useful for you and for us if you were to write in comments above each function what each function is supposed to do, and what the preconditions and postconditions are.
    When you have 5 pointers as input to a function, you really really need to do this! Is it valid for every one of those pointers to be null? You haven't said otherwise.

    Also, refactor as much as you can to reduce the code length. What vart just mentioned is a brilliant start. Look for more ways to do the same thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I fixed my errors for add a character and delete a character:

    Code:
    f = linkedListStrings[indexValue];
    needed to be:
    Code:
    front = f = linkedListStrings[indexValue];
    for both methods and now they work. My concatenate works on my linux partition but not on my windows xp partition. Unfortunately my professor uses windows.


    for my concatenate method,

    Node *f points to the front of the node originally. index is the index of the first user selected linked list. index2 is the index of the second user selected linked list. index3 is the index to store the concatenated linked lists. linkedListStrings[] is the array of linked lists.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    A lot of your comments are wrong/inconsistent:
    Code:
    	struct node *node1; // points to linkedListStrings[*index]
    	struct node *node2; // points to linkedListStrings[index2]
    I understand the parameters to your concatenate method now you're described them, however you could give the indexes better names so that they need less describing.
    Also, why are they pointers?! They should just be passed as regular ints since you don't modify the int they are pointing to.
    The more you use pointers the more bugs you are likely to have. Do you want lots of bugs?

    Note: This is how it works when you ask for help. We help fix the things that were causing you to create bugs in the first place, not just fix the immediate bug. It's the whole "Give a man a fish and he'll eat for a day; teach a man to fish and he'll eat for life" thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    I do not see where you nullifying the next pointer for the last node... still
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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