0

I'm new to C and need your assistance please. I have posted already 2 parts of the big Linked List issues I was having because i didn't want to bombard you all with a big code so i'm doing this in parts. This is a new question though so if you could explain me I would really appreciate it as always.

I have a function on my doubly linked list that is supposed to delete a string that's on my list but I seem to have a problem it's not deleted anything. In fact it get's me stuck and i can't input anything. I would like to paste my code for you to maybe understand better with what i'm dealing with. Love your help!

This is my struct node:

 struct node
 {
 char data[100];
 struct node *previous;  // Points to the previous node
 struct node *next;   // Points out to the next node
 }*head, *last;

This is my function called: delete_from_middle

 char delete_from_middle(char words[99])
 {
  struct node *temp,*var,*temp1;
  temp=head;
  strcpy(temp->data, words);

  while (temp!=NULL)
  {
    if (temp->data == words)
    {
        if (temp->previous==NULL)
        {
            free(temp);
            head=NULL;
            last=NULL;
            return 0;
        }
        else
        {
            var->next=temp1;
            temp1->previous=var;
            free(temp);
            return 0;
        }
    }
    else
    {
        var=temp;
        temp=temp->next;
        temp1=temp->next;
      }
   }
  printf(" Data deleted from list is %s \n", words);
  return 0;
 }

And this is where i assign it on my main

 int main()
 {
  char loc[99];
  char words[99];
  int i, dat;

  head=NULL;

printf("Select the choice of operation on link list");
printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");

while(1)
{
    printf("\n\n Enter the choice of operation you want to do ");
    scanf("%d",&i);

    switch(i)
    {
        case 1:
        {
            printf("Enter a word you want to insert in the 1st node ");
            scanf(" %s",words);

            insert_beginning(words);
            display();
            break;
        }
        case 2:
        {
            printf("Enter a word you want to insert in the last node ");
            scanf(" %s",words);
            insert_end(words);
            display();
            break;
        }
        case 3:
        {
            printf("After which data you want to insert your new data ");
            scanf(" %s",words);

            printf("Enter the data you want to insert in list ");
            scanf(" %s",loc);

            insert_after(words, loc);
            display();
            break;
        }
        case 4:
        {
            delete_from_end();
            display();
            break;
        }
        case 5:
        {
            printf("Enter the value you want to delete");
            scanf(" %s",words);
            delete_from_middle(words);
            display();
            break;
        }

really sorry if the code seems long but i really tried to figure how to do it. Any help? please let me know if i'm missing something or if my question is not correctly asked.

6
  • 2
    debugger would be the best bet Commented Dec 25, 2013 at 19:50
  • An issue which I see is this : if (temp->data == words). What do you mean by this? You should google and learn how to compare two arrays in C. Commented Dec 25, 2013 at 19:53
  • I think that (temp->data == words) is not what you think it does. You want to compare values, instead you are comparing pointers. Commented Dec 25, 2013 at 19:54
  • should i use strcmp then? Commented Dec 25, 2013 at 19:56
  • i want to input a string not an array. Am i making a newbie mistake here? Commented Dec 25, 2013 at 19:58

6 Answers 6

1

Well, the line

if (temp->data == words) {

certainly does not do what you expect it to do: You are comparing pointers, not the strings behind the pointers! Use strcmp() for that.


To be precise: the == operator is written to compare two arrays, but these arrays decay into pointers to their first elements, the code is equivalent to

if (&temp->data[0] == &words[0]) {

But that is probably a lesson you should learn later on, it confuses enough seasoned C programmers...

Sign up to request clarification or add additional context in comments.

Comments

0

You start the function by setting temp to point to head of the list. Then you replace head->data with your search string. Obviously now head->data == words and previous == null so head is deallocated and function returns zero

5 Comments

You don't need to call strcpy. That is just wrong. You compare temp's data to your words like you do. When you find a node that matches, you point previous node's next to temp's next and the reverse for the previous pointers and then you delete temp. If you want to remove all occurrences, you need to keep your current temp location using temp1 and continue from there until you reach end of list
curious question: why is everybody "afraid" in a way of strcpy?
What do you mean afraid? It's not letting you do what you meant to do. Are you joking?
on my previous functions it worked fine but a lot of people didn't recommend me to use it that's why i asked
but i'll see how i can fix the issue with what you explained me thank you by the way appreciate the help
0

Your code is very complex and seems to contain more than one problem. You should probably cut the function into smaller parts to find errors easier.

ex:

struct node *find(char words[99])
{
  struct node *temp;
  temp = head;
  while (temp != NULL)
  {
    if (strcmp(temp, words) == 0)
      return temp;
  }
  return NULL;
}

void deleteNode(struct node *n)
{
  if (n->previous != NULL)
    n->previous->next = n->next;
  else // n is head
    head = n->next;
  if (n->next != NULL)
    n->next->previous = n->previous;
  else
    last = n->previous;
  free(n);
}

char delete_from_middle(char words[99])
{
  struct node *target = find(words);
  if (target != NULL)
    deleteNote(target);
}

Comments

0

There is a problem in your code. Try: Traverse the whole linked list like

for(node temp=head; temp!=null; temp=temp->next)
{
   if(temp->data==words)
   {
        //update links
        temp->previous=temp->next; 
        temp->next->previous=temp->previous->next;
        break;
    }
 }
free (temp); //delete/free node

Comments

0
First in while loop, if condition is always true because of this line -
strcpy(temp->data, words);

Now there are two parts in if -> first ( if(temp->previous == NULL)
)  if there is only a single element in the list then list will set
to null by setting head = NULL and last = NULL;

Second part -> If list has more than one elements then your
operations are 

var->next=temp1;  //This one is wrong cause var is not assigned its only declared
temp1->previous = var; //this one is causing problem  free(temp); you freed the    memory that was allocated for temp but
//forget to delete temp . 
return 0;
//At last your element is not actually deleted and you freed the
//memory that was allocated for that element.



For deleting a specific element simplest code is -> 
char delete_from_middle(char words[99])  
{   
struct node *h;
h = head; 
while ( h! = NULL )   {
    if ( strcmp(h->data, words) == 0)
    {
        if ( h->previous == NULL )
        {
           if( head == last )
             {
               head = NULL;
               last = NULL;
             }
           else
             {
               head = head->next;
               head->previous = NULL;

              }
        }
        else
        { 
          if( h->next!=NULL ) h->next->previous = h->previous;
          h->previous->next = h->next;
        }
      printf(" Data deleted from list is %s \n", words);   
      free(h);
      return 0;
    }
   h = h->next;
 } 
    printf(" Data not found");     
    return 0;  }

2 Comments

If you tell me exactly how do you want to delete your element eg. by comparing to a specific element, by using a specific position...etc Then it would be better to understand.
okay so basically i inputed a few words that we're stored and displayed and I want to have the option to delete one of the words and to type which word i want to delete
0

There are so many problems in the code, while deleting the integrity of the list is not maintained. The following should be the way to delete a node from the list:

void delete_from_middle(char words[99])
{
  struct node *temp;
  temp=head;

  while (temp!=NULL)
  {
    if (strcmp(temp->data,words)==0) //this is the data we are looking for, go and delete this
    {
        if (temp->previous==NULL) //this is the head
        {
            head=temp->next;
            temp->next->previous=NULL;
            free(temp);
            printf(" Data deleted from list is %s \n", words);
            return;
        }
        else if(temp->next==NULL) //this is last node
        {
            temp->previous->next=NULL;
            free(temp);
            printf(" Data deleted from list is %s \n", words);
            return;
        }
        else
        {
            temp->previous->next=temp->next;
            temp->next->previous=temp->previous;
            free(temp);
            printf(" Data deleted from list is %s \n", words);
            return;
        }
    }
    else //this node does not contain the data, go to the next node
    {
        temp=temp->next;
    }
  } 
  //search ended
  printf(" Data not found\n");
  return;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.