Skip to main content
edited body
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92
// node temp=(node)malloc(sizeof(node));
node temp = malloc(sizeof *node*temp);
// node temp=(node)malloc(sizeof(node));
node temp = malloc(sizeof *node);
// node temp=(node)malloc(sizeof(node));
node temp = malloc(sizeof *temp);
added 192 characters in body
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92

Design: OK initial attempt at abstraction

main() does not expose struct q_9 members.

Design: .tail is not needed

This is a major change, so will assume this code is using the simple one linked-list as an exercise.

Design: Consider common naming prefix (and avoid type-defining a pointer)

Example:

typedef struct q9 {
  struct q9 *link;
  int data;
} q9_T;

q9_T *q9_newnode(int x);
int   q9_count(const q9_T *node);
void  q9_display(const q9_T *node);
void  q9_add(q9_T *node, int x);
void  q9_delete_front(q9_T *node);
void  q9_delete_last(q9_T *node);
// new ideas
int   q9_pop(q9_T *node);
int   q9_top(const q9_T *node);
int   q9_end(const q9_T *node);  // Potential real use of .tail
bool  q9_empty(const q9_T *node);

Design: .tail is not needed

This is a major change, so will assume this code is using the simple one linked-list as an exercise.

Design: OK initial attempt at abstraction

main() does not expose struct q_9 members.

Design: .tail is not needed

This is a major change, so will assume this code is using the simple one linked-list as an exercise.

Design: Consider common naming prefix (and avoid type-defining a pointer)

Example:

typedef struct q9 {
  struct q9 *link;
  int data;
} q9_T;

q9_T *q9_newnode(int x);
int   q9_count(const q9_T *node);
void  q9_display(const q9_T *node);
void  q9_add(q9_T *node, int x);
void  q9_delete_front(q9_T *node);
void  q9_delete_last(q9_T *node);
// new ideas
int   q9_pop(q9_T *node);
int   q9_top(const q9_T *node);
int   q9_end(const q9_T *node);  // Potential real use of .tail
bool  q9_empty(const q9_T *node);
added 192 characters in body
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92

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);

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.

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);
added 192 characters in body
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92
Loading
Source Link
chux
  • 33.7k
  • 2
  • 38
  • 92
Loading