calcdude84se wrote:
Can't it just be

Code:
void Delete(NodePtr* head) {
  NodePtr temp = *head;
  NodePtr prevPtr;
  while(temp != NULL) {
    prevPtr = temp;
    temp = temp->next;
    free(prevPtr);
  }
}

No need to add one more layer?


Nope, the extra layer is required.

EDIT: typedef'ing the pointer away is just confusing everyone (my apologies). Here's the new code with the *'s back ("NodePtr" is now "node_t *" which is "struct node *"):


Code:
#include <stdio.h>
#include <stdlib.h> /* Now we can use macros like NULL and EXIT_SUCCESS */

typedef struct node {
   int data;
   struct node *next;
} node_t; /* Now we can use the moniker "node_t" instead of "struct node" everywhere */

/* Function prototypes: */
void DisplayList(node_t * head);
void Insert(node_t * * head, int value);
void whatever(char **out);
void someotherfunc();
void Delete(node_t ** head);

int main() {
   node_t * head = NULL;
   Insert(&head, 10); /* Update head of list whenever you do something to the list */
   Insert(&head, 20);
   Insert(&head, 30);
   Delete(&head);

   DisplayList(head);
   return EXIT_SUCCESS;
}

void DisplayList(node_t * head) {
   node_t * temp;
   for(temp = head; temp != NULL; temp = temp->next) { /* Make sure to check if node is NULL; trying to dereference a null pointer is very bad */
      printf("%d\n",temp->data);
   }
}

void Insert(node_t ** head, int value) {
   node_t * newnode = (node_t *)malloc(sizeof(*newnode)); /* Create a new node */
   newnode->data = value;
   newnode->next = *head;
   *head = newnode;
}

void Delete(node_t ** head) {
  node_t ** temp = head; /* You want to make a copy of head to traverse the list */
  node_t * prevPtr; /* Pointer to a node; notice there is only 1 asterisk */
  while(temp != NULL) {
    prevPtr = *temp; /* Dereference temp b/c it is a "double pointer" */
    *temp = (*temp)->next;
    free(prevPtr); /* Same here */
  }
}
Ultimate Dev'r wrote:
These are both good points, but as far as this being "useless, ugly, and not standard practice", take a look at pg. 146 of K&R; it typedefs a pointer the same way in their linked list examples as I have.


Your counter to my "it's useless, ugly, and not standard practice" is a reference to a book written about a version of C nobody codes in anymore where return types of functions was assumed to be ints, and there were no void functions at all? Razz

(yes, I know K&R has been updated to ANSI C)
Actually, everyone still uses K&R, at least in undergraduate and graduate CS and engineering academia. Smile At least in my experience in two top schools as a student and instructor respectively.
KermMartian wrote:
Actually, everyone still uses K&R, at least in undergraduate and graduate CS and engineering academia. Smile At least in my experience in two top schools as a student and instructor respectively.


No one uses the first edition of K&R, hence the joke.
KermMartian wrote:
Actually, everyone still uses K&R, at least in undergraduate and graduate CS and engineering academia. Smile At least in my experience in two top schools as a student and instructor respectively.
I never touched K&R.
KermMartian wrote:
Actually, everyone still uses K&R, at least in undergraduate and graduate CS and engineering academia. Smile At least in my experience in two top schools as a student and instructor respectively.


*facepalm*

The first edition of K&R was about K&R C, which nobody codes in as everyone has switched to ANSI C. Jeez Kerm, way to fail at knowing the history of C Razz
I know full-well the history of C and what K&R C looks like; hell, I had to write lexing and parsing in my compiler to understand K&R-style function declarations. Sad I thought we were talking about the book in general instead of the specific non-ANSI coding style of the first edition, though. Rolling Eyes
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement