Design: .tail
is not needed
Present function set has no benefit from the tail
member. It is removable. Even delete_last()
walks down the list.
Design: Global variables not needed
This is a major change, so will assume this code is using the simple one linked-list as an exercise.
Bug: Wrong allocation size
Code allocates enough for a pointer rather than enough for the struct
.
Cast not needed. Better to allocate to the de-referenced object than type.
// node temp=(node)malloc(sizeof(node));
node temp = malloc(sizeof *node);
Function declarations say nothing about signature
void foo();
allows later calls like foo(1), foo("hello", 5.0)
to pass undetected as errors.
// void delete_front();
// void count();
// void display();
// void delete_last();
void delete_front(void);
void count(void);
void display(void);
void delete_last(void);
Inconsistent spacing
Use an auto-formatter - life is too short for manual formatting.
Lack of error checking
No checking if scanf("%d",&choice);
succeeded, nor malloc()
.
Unneeded code
temp=NULL;
serves nor purpose in node_add(int x)
Bug: de-referencing of NULL
delete_front()
does not check if head == NULL
before head->link
. tail
is not updated to NULL
when last link deleted.
Bug: de-referencing of NULL
delete_last()
does not check if p->link == NULL
before p->link->link
. Also expect head
to be updated to NULL
if last node deleted.
Prompts use lower case rather than sentence case.
Unless you are E. E. Cummings, use sentence case for prompts.
// printf("\n please enter a choice:");
printf("\n Please enter a choice:");
Insure output is seen before input
Due to various buffer modes of stdout
, best to flush output before input, especially if the prior output does not in with a '\n'
.
printf("\n please enter a choice:");
fflush(stdout); // add
scanf("%d",&choice);