4
\$\begingroup\$

I'm trying to write a concise linked list implementation in C and so far it looks pretty good. I'd like some pointers (pun) on how I can improve what I've got in terms of naming, consistency, efficiency, and anything else that's relevant.

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
 int data;
 struct node *next;
}node_t;
int length(node_t *head){
 int length = 0;
 for(node_t *cur = head; cur; cur = cur->next)
 length++;
 return length;
}
void printList(node_t *head){
 for(node_t *cur = head; cur; cur = cur->next)
 printf("%d\n", cur->data);
}
void deleteList(node_t **head){
 node_t *current = *head, *next;
 while (current != NULL) {
 next = current->next;
 free(current);
 current = next;
 }
 *head = NULL;
}
void deleteNode(node_t *node, node_t *head){
 node_t *cur = head;
 while (cur->next != node)
 cur = cur->next;
 cur->next = node->next;
 free(node); node=NULL;
}
node_t *getNode(int key, node_t *head){
 node_t *node = head;
 while(node){
 if (node->data == key){ return node; }
 else { node = node->next; }
 }
 return NULL;
}
node_t *insert(int key, node_t *head){
 node_t *cur = head;
 while(cur->next)
 cur = cur->next;
 node_t *new = malloc(sizeof(node_t));
 if (new == NULL){ exit(-1); }
 new->data = key;
 new->next = NULL;
 cur->next = new;
 return new;
}
node_t *createList(int key) {
 node_t *head = malloc(sizeof(node_t));
 if (head == NULL){ exit(-1); }
 head->data = key;
 head->next = NULL;
 return head;
}
int main(){
 node_t *head = createList(1);
 insert(2, head);
 node_t *nodeA = insert(3, head);
 insert(4, head);
 printf("1) Printing list:\n");
 printList(head);
 node_t *nodeB = getNode(4, head);
 deleteNode(nodeA, head);
 deleteNode(nodeB, head);
 printf("2) Printing list:\n");
 printList(head);
 deleteList(&head);
 printf("3) Printing list:\n");
 printList(head);
 return 0;
}
asked Feb 16, 2017 at 0:00
\$\endgroup\$
1
  • \$\begingroup\$ Always use opening and closing brackets {} it can lead to ugly bugs and it doesn't give you any benefit if you don't use them. Also go on a new line after an opening bracket { code is easier to read anf it keeps it cleaner \$\endgroup\$ Commented Feb 16, 2017 at 4:24

1 Answer 1

3
\$\begingroup\$

Bug: can't delete head node

Your delete_node() function doesn't work if the node you want to delete is the head of the list. In fact, the function signature can't work:

void deleteNode(node_t *node, node_t *head);

Since the head pointer can change if you delete it, your signature has to be one of these two instead;

void deleteNode(node_t *node, node_t **head);
node_t *deleteNode(node_t *node, node_t *head);

Insert should be renamed

Since your "insert" function adds to the end of the list, I think it should be called append() instead.

answered Feb 16, 2017 at 0:18
\$\endgroup\$
3
  • \$\begingroup\$ The head node bug I understand, but the insert one I had intended to write an append and prepend in the future. \$\endgroup\$ Commented Feb 16, 2017 at 0:21
  • \$\begingroup\$ @Chirality But isn't insert() already the append() function? \$\endgroup\$ Commented Feb 16, 2017 at 3:30
  • \$\begingroup\$ Sure is. Valid comment, just explaining where I planned on going with it. \$\endgroup\$ Commented Feb 16, 2017 at 4:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.