Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
For example, if the user enters the following string:
I love C
and then enters:
very much
and then enters the integer 1, the program would output:
I very much love C
But my output lost the "very much" part, left only with "I vlove c"
The problem is the insert() function, I tired my best to get it work like this.
Anyone got some suggestions.


========original code============
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 256

typedef struct list
{
    char data;
    struct list *next;
} List;


List *create(char *str);
List *insert(List *head, int node, char *str);

int getNode(int length);
void display(List *head);
void removeReturn(char *str);

int main(void)
{
    List *head = NULL;
    char str[MAX];
    int node, length;

    printf("Enter the first string: ");
    fgets(str, MAX, stdin);
    removeReturn(str);
    length = strlen(str);
    head = create(str);
  
    printf("Enter the second string: ");
    fgets(str, MAX, stdin);
    removeReturn(str);
    
    node = getNode(length);
    
    printf("The list before insertion:\n");
    display(head);
    
    head = insert(head, node, str);
    
    printf("The list after insertion:\n");
    display(head);
    
    system("pause");
    return 0;
}

int getNode(int length)
{
    int node = -1;
    
    while(node == -1 || node > length)
    {
        printf("Enter the value of insertion point (0 to %d): ", length);
        scanf("%d", &node);
    }
    
    return node;
}

List *create(char *str)
{
    int i;
    List *head = NULL;
    List *current;
    
    for(i = strlen(str); i >= 0 ; i--)
    {
        current = (List *)malloc(sizeof(List));
        current->data = str[i];
        current->next = head;
        head = current;
    }
    
    return head;
}

List *insert(List *head, int node, char *str)
{
    int i;
    List *head2, *toBeInserted;
    
    head2 = (List *)malloc(sizeof(List));
    head2 = head;
    for(i = 0; i < node; i++)
        head2 = head2->next;
        
    toBeInserted = (List *)malloc(sizeof(List));
    toBeInserted = create(str);

    toBeInserted->next = head2->next;
    head2->next = toBeInserted;

    return head;
}

void display(List *head)
{
    while(head)
    {
        printf("%c", head->data);
        head = head->next;
    }
    printf("\n");
}

void removeReturn(char *str)
{
    str[strlen(str)-1] = '\0';
}
Posted
Updated 26-Oct-11 10:29am
v3

In function create() you malloc() a list item whose data field is a single char variable, and you are just copying each character of the input string into that variable, overwriting all except the last. Change it to a char*, use malloc() to allocate sufficient space for the string, and strcpy() to copy the source string into it.

I was going to suggest you switch to C++ and STL, but this may be a school assignment, so I'll leave it for now.
 
Share this answer
 
Comments
Member 8348793 25-Oct-11 16:42pm    
thanks for the reply.
It is an assignment, base on my understanding of the specs "create a linked list of characters from a string entered by the user"
I made the struct char data not char *data, but I might be wrong.
I never used strcpy() before, this will lead me to more problems, but I will give it a try.
Richard MacCutchan 25-Oct-11 16:53pm    
Use the documentation on MSDN here to learn about strcpy(), and revisit often to check that you are doing things the right way. Programming is never easy to start with, but the only way to get good at it is by lots of practice.
The following code in your insert function
C++
head2 = (List *)malloc(sizeof(List));
head2 = head;

and
C++
toBeInserted = (List *)malloc(sizeof(List));
toBeInserted = create(str);

are memory leaks[^], the first line in both instances is not needed.
You are allocating memory that is not release and to which the pointer is overwritten.

You also need to to free the memory allocated for the list at the end of your program.

In your current code you are losing text because of the
C++
toBeInserted->next = head2->next;
statement in your insert function. the variable toBeInserted is the head of the list containing "very much", you need to use the last node of the list in this statement.

Hint: Each node in your list should contain a word and not just one character from the string entered. Otherwise you cannot be using 1 to add the second list to the first one.
 
Share this answer
 
v2
Comments
Member 8348793 25-Oct-11 16:46pm    
thanks for the reply, I see your point, looks like I need to change my struct to char *data, and also a loop to get to the last node. I will give it a try.
You forgot about using malloc() and strcpy() to copy your strings into the data element of your list item. All you are doing is storing a pointer to the original string in str (in your main loop) which you then overwrite with the next call to fgets(). Try stepping through your code with the debugger to see exactly what happens to your variables as your program runs.
 
Share this answer
 
I tried your suggestions, but it just makes my code more complicated, and I couldn't make it to work.
Therefore I went back to my original approach, and found a solution myself.
Here is the modified insert() function

C++
List *insert(List *head, int node, char *str)
{
    int i;
    List *head2, *toBeInserted;
    
    head2 = head;

    for(i = 0; i < node; i++)
        head2 = head2->next;
    
    for(i = strlen(str); i >= 0 ; i--)
    {
        toBeInserted = (List *)malloc(sizeof(List));
        toBeInserted->data = str[i];
        if(node == 0)
        {
            toBeInserted->next = head;
            head = toBeInserted;
        }
        else
        {
            toBeInserted->next = head2->next;
            head2->next = toBeInserted;
        }
    }
    
    return head;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900