edisonthk
edisonthk

Reputation: 1423

Linked list deletion is removing the wrong node

Please I need someone help. I'm having trouble with my homework.

This homework is very simple. Create a list(1,2,3) and delete the middle number by creating a function delnode. But it must use the function free().

Right now,I have created the list (1,2,3) by using linked list method. I want to delete the number 2 but it doesn't work. It should be comes out with (1,3) but it comes out with (2,3).

#include <stdio.h>
#include <stdlib.h>


struct node{
    int number;
    struct node* next;
};

typedef struct node node;

//prototype function
node* allocateMemory(void);
node* insertNode(node*);
void delnode(node*);

int main(){
    int i,num;
    node* entr = allocateMemory();
    node* p = NULL;

    entr->number = 1;
    entr->next = NULL;

    num = 3;

    for(i=1;i<num;i++){
        if(!p){
            p=insertNode(entr);
        }else{
            p=insertNode(p);
        }

        p->number = i+1;
    }

    while(entr){
        if(entr->number == 2){
            entr->number == NULL;
            break;
        }
        entr=entr->next;
    }

    while(entr){
        printf("%d\n",entr->number);
        entr=entr->next;
    }

    return 0;
}

node* insertNode(node* current){
    node* newNode = allocateMemory();
    current->next = newNode;
    newNode->next = NULL;
    return newNode;
}


void delnode(node* current){
    node* temp = allocateMemory();
    temp = current->next->next;
    free(current->next);
    current = temp;
    free(temp);
    return ;
}

node* allocateMemory(void){
    return (node*)malloc(sizeof(node));
}

Upvotes: 0

Views: 164

Answers (2)

Aesthete
Aesthete

Reputation: 18848

Ok, this function delnode is all kinds of messed up. You don't need to allocate more memory when you're trying to delete a node. You need to link the previous to the next, and delete the current.

| prev |      | current |      | next |
|------|      |---------|      |------|
   /\              X               /\
   ||              X               ||
   |================================|

It should look more like this.

void delnode(node* current)
{
  node* next = current->next;
  // Now we need to find the node previous to this.
  node* prev = entr; // From the start.
  while(prev->next != current) { prev = prev->next; };
  free(current); // delete the current node.
  prev->next = next; // Link the previous node to the next in the list.
}

If you're trying to add three nodes, you need to change your loop.

node* current = entr;
for(i=0;i<3;i++)
{
  int newCount = current->number++; // Increment count.
  current = insertNode(current); // Returns the new node.
  current->number = newCount; // Assign new count.
}

If you want to delete the node with number of 2:

node* idx = entr;
while(idx)
{
  if(idx->number == 2) { delnode(idx); break; } // Delete node and break.
  idx = idx->next; // Else, go to next node.
}

Upvotes: 2

amit
amit

Reputation: 178451

Some issues:

(1) The original list you are creating is [2,3], and not [1,2,3] - your loop iterates for i=1,i=2 - and in each you insert i+1 - resulting in [2,3]
(2) Your deletion is not doing anything:

while(entr){
   if(entr->number == 2){
       entr->number == NULL;
       break;
   }
   entr=entr->next;
}

Note that entr->number == NULL is only a boolean evaluation and not an assignment - since you use operator==

(Note it would fail for operator= as well, because you do not want to assign NULL to the value - what you really want to do is assign the previous node next field.)

P.S.
The terminology for this data structure is a Linked List, not a "Linear List"

Upvotes: 4

Related Questions