Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following code is supposed to replace a node that contains a single character with several a linked list of nodes converted from a string:

node *replaceChar(node *head, char key, char *str)
{
    node *nhead = head;

    if (head == NULL)
        return nhead;

    if (str == NULL || strcmp(str, "") == 0)
    {
        if (head->data == key)
            {
            deleteN(head, key);
            }
        head->next = replaceChar(head->next, key, str);
    }

    if (head->data == key)
    {

        node* temp = head;
        node* tail = temp->next;

        head = temp->next;
        free(temp);

        head = stringToList_replace(str, tail);

    }

    head->next = replaceChar(head->next, key, str);

    return nhead;
}

stringToList_replace function takes a string and converts it to a linked list, then returns the tail of that linked list:

node *stringToList_replace(char *str, node* tail)
{
    node *head = malloc(sizeof(node));
    int i;

    if (str == NULL || strcmp(str, "") == 0)
        return NULL;

    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] != '\0')
        {
            head->data = str[i];

            if (str[i+1] != '\0')
            {
                head->next = malloc(sizeof(node));
                head = head->next;
            }
        }
    }

    head->next = tail;

    return head;
}

Finally, deleteN finds all instances of a value (key) in a linked list and deletes them.

node* deleteN(node* head, char key)
{
  if (head == NULL)
    return NULL;

  node* tail = deleteN(head->next, key);

  if (head->data == key)
  {
    free(head);
    return tail;
  }

  else
  {
    head->next = tail;
    return head;
  }

}

I also have a print function in my code that prints the linked list. The problem with my code is that if I delete a value from the list and then try to replace another value, some of the replaced value gets cut off.

For example:

initial linked list:

[E]->[l]->[d]->[e]->[r]->[NULL]

called deleteN(head, e) to delete all instances of 'e':

[l]->[d]->[r]->[NULL]

called replaceChar(node, r, scrolls) to replace all instances of 'r' with 'scrolls':

[l]->[d]->[r]->[o]->[l]->[l]->[s]->[NULL]

The above should be:

[l]->[d]->[s]->[c]->[r]->[o]->[l]->[l]->[s]

I get the correct output if I just do a replacement without deleting first, or just a deletion, or even a replacement before a deletion. However, every time I do a deletion and then a replacement, the output gets cut off. Any ideas?

share|improve this question

1 Answer 1

Call replaceChar(node, r, test), and look at what the output is. In

if (head->data == key)
    {

your skipping your initial node.

share|improve this answer
    
Could you elaborate? Also notice that the first two nodes are getting cut off, which is the same number of nodes that I deleted. Is this a coincidence? –  Karim Elsheikh Jul 10 '13 at 21:23
    
Can you tell me what gets printed when you call replaceChar(node, r, scxolls) ? –  TheoretiCAL Jul 10 '13 at 21:48
    
If i delete e first, ldxolls is printed. If i don't, eldescxolls is printed. I want to be able to delete e and then replace so that it prints ldscxolls not ldxolls. –  Karim Elsheikh Jul 10 '13 at 22:00
    
What happens when you use a more basic non recursive delete, just check the nodes, if the key is data, set the previous node's next to the current nodes's next and free the current node, catching the case where its the first /last node. Then retest it, if the problem still occurs you know its most likely in replace, if not you know its in delete. –  TheoretiCAL Jul 10 '13 at 23:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.