Single linked list

前端 未结 7 795
無奈伤痛
無奈伤痛 2021-01-23 13:54

I have created a single linked list. Everything works fine.

I just want to know if I have done anything potentially dangerous in my code. The code snippets I am concern

7条回答
  •  长发绾君心
    2021-01-23 14:12

    Agree with the issues raised by goldPseudo and thaggie/Steven.

    In push(), replace strncpy() with strlcpy() to ensure the destination string is always NUL terminated.

    In cleanup(), I'd suggest that you remove the exit(0); statement -- you don't need it. Exiting a programme from within a subroutine is generally not the best thing to do.

    You should take away one lesson from creating your first singly linked list, and that is, singly linked lists are generally not very useful in the real world because:

    • They're too hard to manipulate. Just look at the complexity of your pop() subroutine.
    • Relatively slow because you have to start at the beginning of the list each time you want to retrieve an element from the list.

    You should now attempt to write your first doubly linked list. While doubly linked lists are more complex to implement, they are easier to manipulate (especially when deleting an element) than singly linked lists.

提交回复
热议问题