简体   繁体   中英

In my Singly linked List implementation why is it even though I allocated memory for the node to be freed, the pointer to the Node isn't NULL?

Using the delete_SLL function I want to delete the head of this singly linked list(head = 4). Although I get the correct output, the var struct Node* "temp" holding the value of the head isn't NULL. What is it about the variable "temp" that the free function not like? Is the node temp not Malloc-ed when setting it equal to the list head?

Source:Deleting a Node

Code:

#include <stdio.h>
#include <stdlib.h>
struct Node{
  int item;
  struct Node* next;
};

struct List{
  struct Node* head;
  struct Node* tail;
};

int SLL_empty(struct List* lst){
  return lst->head == NULL ;
}

//newLst work
struct List newLst(){
  struct List  lst;
  lst.head = NULL;
  lst.tail = NULL;
  return lst;
}


//Inserts a node to the front of the list[WORKS]
void insert_SLL(struct List* lst, int x){
  struct Node* nde = (struct Node*)malloc(sizeof(struct Node));
  nde->next = lst->head;
  nde->item = x;
  if (SLL_empty(lst))
    lst->tail=nde;
  lst->head = nde;
}


//Deletes a given Node
void delete_SLL(struct List* lst, int x){
  struct Node* temp =  (struct Node*)malloc(sizeof(struct Node));;
  temp = lst->head;
  struct Node* prev = NULL;`enter code here`

  //If the head has the key
  if (temp != NULL && temp->item == x){
    lst->head = temp->next;
    temp->next = NULL;
    free(temp);
  }

  // stops once the key is found
  while(temp != NULL && temp->item != x){
    prev = temp;
    temp= temp->next;
  }

  //If not in list
  if (temp == NULL) return;

  //If middle
  if (temp != NULL && temp->item == x){
    prev->next = temp->next;
    temp->next = NULL;
  }

  //if at the end
  if (temp != NULL && temp->item == lst->tail->item){
    lst->tail= prev;
    prev->next = NULL;
  }
  free(temp);
}

int SLL_pop(struct List *list){
 struct Node* nde = list->head;
 int item = nde->item;
  list->head = nde->next;
  free(nde);
  if (SLL_empty(list))
    list->tail = NULL;
  return item;
}

int main(int argc, const char * argv[]) {
  int i;
  struct List list = newLst();
  for (i = 0; i < 5; ++i)
   insert_SLL(&list, i);
//  printf("The length of the linkedLst is: %d\n",SLL_length(&list));

  delete_SLL(&list, 4);
  while ( list.head != NULL )
    printf("Node: %d\n", SLL_pop(&list));

  return 0;
}

The main purpose of free() is to ask the OS take the allocated memory back to the system. You might not be able to "see" that but if you try to access any element at the "temp" afterward, you should get an error.

While the "temp" in the program is only a variable. C doesn't require to, and can't change the given pointer to NULL due to pass-by-value sense. It's the programmer's work to remember that this pointer is no longer valid.
Or you can set it to NULL manually each time you free a pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM