patterncMinorCanonical
Singly linked list in C
Viewed 0 times
singlylistlinked
Problem
Here's my version of singly linked list. Any constructive criticism is highly appreciated.
First header file "sll.h":
Next comes "singly.c" containing all functions definitions used by the list:
```
#include "sll.h"
/ returns 1 if node is created, 0 if not /
int add_at_end(NODE **another, int val)
{
while(*another!=NULL)
another=&(*another)->next;
*another=malloc(sizeof(NODE));
if(*another==NULL)
return 0;
(*another)->value=val;
(*another)->next=NULL;
return 1;
}
/ adds not as list was ordered /
int add_at_order(NODE ** another, int val)
{
NODE * new;
while(another!=NULL && (another)->valuenext;
new=malloc(sizeof(NODE));
if(new==NULL)
return 0;
new->value=val;
new->next=(*another);
*another=new;
}
/ counds nodes in the list /
int count_nodes(NODE * another)
{
int count=0;
while(another!=NULL)
{
count++;
another=another->next;
}
return count;
}
/ sets head of list to NULL, frees memory if there was previous list /
void create_sll(NODE **node)
{
NODE * prev;
NODE * nxt;
nxt=*node;
while(nxt!=NULL)
{
prev=nxt;
nxt=prev->next;
printf("Removi
First header file "sll.h":
#ifndef _SLL_H_
#define _SLL_H_
#include
#include
/* declaration of node in Singly linked list */
typedef struct node{
int value;
struct node * next;
}NODE;
/* declaration of head (pointer to node) */
extern NODE * head;
/* declaration for functions working on sll */
/*creating a list, freeing previously used memory */
void create_list(NODE ** head);
/* adding node at the end of sll */
int add_at_end(NODE **another, int val);
/*printing all nodes in list one after another */
void print_list(NODE * another);
/* counting nodes in list */
int count_nodes(NODE * another);
/* locating value in node in sll */
NODE * locate(NODE * another, int val);
/* adding node in growing order of valued */
int add_at_order(NODE **another, int val);
/* remove node with given value */
void remove_node(NODE **another, int val);
#endifNext comes "singly.c" containing all functions definitions used by the list:
```
#include "sll.h"
/ returns 1 if node is created, 0 if not /
int add_at_end(NODE **another, int val)
{
while(*another!=NULL)
another=&(*another)->next;
*another=malloc(sizeof(NODE));
if(*another==NULL)
return 0;
(*another)->value=val;
(*another)->next=NULL;
return 1;
}
/ adds not as list was ordered /
int add_at_order(NODE ** another, int val)
{
NODE * new;
while(another!=NULL && (another)->valuenext;
new=malloc(sizeof(NODE));
if(new==NULL)
return 0;
new->value=val;
new->next=(*another);
*another=new;
}
/ counds nodes in the list /
int count_nodes(NODE * another)
{
int count=0;
while(another!=NULL)
{
count++;
another=another->next;
}
return count;
}
/ sets head of list to NULL, frees memory if there was previous list /
void create_sll(NODE **node)
{
NODE * prev;
NODE * nxt;
nxt=*node;
while(nxt!=NULL)
{
prev=nxt;
nxt=prev->next;
printf("Removi
Solution
Some minor comments.
seems wrong and the function name is awkward.
to be misnamed as it in fact destroys the list supplied.
Where a call parameter is unmodified by a function, the parameter should
marked
Capitalized names are normally used for #define constants, not for types.
Your
with no typedef.
As a personal preference, I would rename the parameter
even just
Braces are often recommended on single-line statements to avoid a class of
errors caused by unthinking editing or macro use:
In
the end there.
In the header you have
It is often best to put
Note that it is normal to make local functions
need external linkage such as
avoids polluting the global name space and improves the possibilities for
optimization.
add_at_order is missing a return value. Also the comment to the functionseems wrong and the function name is awkward.
create_sll is named create_list in the header file. Either way it seemsto be misnamed as it in fact destroys the list supplied.
Where a call parameter is unmodified by a function, the parameter should
marked
const.Capitalized names are normally used for #define constants, not for types.
Your
NODE type might be better as Node. Or just left as struct nodewith no typedef.
As a personal preference, I would rename the parameter
another as node oreven just
n. I find another unsatisfying.Braces are often recommended on single-line statements to avoid a class of
errors caused by unthinking editing or macro use:
while(*another!=NULL) {
another=&(*another)->next;
}In
print_list I would handle the empty list first - it gets rather lost atthe end there.
In the header you have
extern NODE * head; but head is defined local tomain. So the header declaration is redundant.It is often best to put
main last so as to avoid the need for prototypes.Note that it is normal to make local functions
static (those that do notneed external linkage such as
clear_input, print_menu, get_menu). Thisavoids polluting the global name space and improves the possibilities for
optimization.
Code Snippets
while(*another!=NULL) {
another=&(*another)->next;
}Context
StackExchange Code Review Q#31144, answer score: 3
Revisions (0)
No revisions yet.