A
arnuld
I am learning about linked lists. This works fine without any trouble. Any
advice for improvement or you want me to implement some more operations in
order to improve my understanding of linked lists:
/* This C implementation of singly linked list implements 3 operations: add, remove and insert elements at some specific position in
* the list.
*
* VERISON 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct my_struct* add_to_list( struct my_struct *const, const int);
struct my_struct* remove_from_list( struct my_struct *const, const int );
struct my_struct* insert_after_element(struct my_struct *const, const int, const int);
void print_list( const struct my_struct* );
struct my_struct
{
int num;
struct my_struct* next;
};
int main(void)
{
struct my_struct* root_node = malloc( 1 * sizeof(*root_node));
const int i = 1;
const int j = 2;
if( NULL == root_node )
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "main");
return EXIT_FAILURE;
}
else
{
root_node->next = 0;
root_node->next = NULL;
}
add_to_list(root_node, i);
add_to_list(root_node, j);
print_list(root_node);
printf("going to remove element with value = %d\n", i);
remove_from_list(root_node, i);
print_list(root_node);
printf("going to insert element after root_node\n");
insert_after_element(root_node, root_node->num, i);
print_list(root_node);
return 0;
}
struct my_struct* add_to_list(struct my_struct *const base_node, const int i )
{
struct my_struct* s = base_node;
struct my_struct* c = NULL;
struct my_struct* p = malloc( 1 * sizeof(*p));
if( NULL == p )
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "add_to_list");
return NULL;
}
p->num = i;
p->next = NULL;
for( ; s != NULL ; s = s->next ) c = s;
c->next = p;
return c->next;
}
struct my_struct* remove_from_list( struct my_struct *const base_node, const int r )
{
struct my_struct* s = base_node;
struct my_struct* c = NULL;
for( ; (NULL != s) && (s->num != r); s = s->next ) c = s;
if( s )
{
c->next = s->next;
free(s);
return c;
}
return NULL;
}
struct my_struct* insert_after_element(struct my_struct *const base_node, const int after, const int i)
{
struct my_struct* s = base_node;
struct my_struct* n = NULL;
struct my_struct* p = NULL;
for( ; (NULL != s) && (s->num != after ); s = s->next );
if( NULL == s )
{
printf("No element with number match: %d\n", i);
}
else
{
p = malloc( 1 * sizeof(*p));
if( NULL == p )
{
fprintf(stderr, "IN: %s, %s: malloc() failed\n", __FILE__, "insert_after_element");
return NULL;
}
p->num = i;
n = s->next;
s->next = p;
p->next = n;
}
return p;
}
void print_list( const struct my_struct* p )
{
for( ; NULL != p; p = p->next )
{
printf("Num = %d\n", p->num);
}
printf("\n\n");
}
================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra singly-LL.c
[arnuld@dune programs]$ ./a.out
Num = 0
Num = 1
Num = 2
going to remove element with value = 1
Num = 0
Num = 2
going to insert element after root_node
Num = 0
Num = 1
Num = 2
[arnuld@dune programs]$
advice for improvement or you want me to implement some more operations in
order to improve my understanding of linked lists:
/* This C implementation of singly linked list implements 3 operations: add, remove and insert elements at some specific position in
* the list.
*
* VERISON 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct my_struct* add_to_list( struct my_struct *const, const int);
struct my_struct* remove_from_list( struct my_struct *const, const int );
struct my_struct* insert_after_element(struct my_struct *const, const int, const int);
void print_list( const struct my_struct* );
struct my_struct
{
int num;
struct my_struct* next;
};
int main(void)
{
struct my_struct* root_node = malloc( 1 * sizeof(*root_node));
const int i = 1;
const int j = 2;
if( NULL == root_node )
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "main");
return EXIT_FAILURE;
}
else
{
root_node->next = 0;
root_node->next = NULL;
}
add_to_list(root_node, i);
add_to_list(root_node, j);
print_list(root_node);
printf("going to remove element with value = %d\n", i);
remove_from_list(root_node, i);
print_list(root_node);
printf("going to insert element after root_node\n");
insert_after_element(root_node, root_node->num, i);
print_list(root_node);
return 0;
}
struct my_struct* add_to_list(struct my_struct *const base_node, const int i )
{
struct my_struct* s = base_node;
struct my_struct* c = NULL;
struct my_struct* p = malloc( 1 * sizeof(*p));
if( NULL == p )
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "add_to_list");
return NULL;
}
p->num = i;
p->next = NULL;
for( ; s != NULL ; s = s->next ) c = s;
c->next = p;
return c->next;
}
struct my_struct* remove_from_list( struct my_struct *const base_node, const int r )
{
struct my_struct* s = base_node;
struct my_struct* c = NULL;
for( ; (NULL != s) && (s->num != r); s = s->next ) c = s;
if( s )
{
c->next = s->next;
free(s);
return c;
}
return NULL;
}
struct my_struct* insert_after_element(struct my_struct *const base_node, const int after, const int i)
{
struct my_struct* s = base_node;
struct my_struct* n = NULL;
struct my_struct* p = NULL;
for( ; (NULL != s) && (s->num != after ); s = s->next );
if( NULL == s )
{
printf("No element with number match: %d\n", i);
}
else
{
p = malloc( 1 * sizeof(*p));
if( NULL == p )
{
fprintf(stderr, "IN: %s, %s: malloc() failed\n", __FILE__, "insert_after_element");
return NULL;
}
p->num = i;
n = s->next;
s->next = p;
p->next = n;
}
return p;
}
void print_list( const struct my_struct* p )
{
for( ; NULL != p; p = p->next )
{
printf("Num = %d\n", p->num);
}
printf("\n\n");
}
================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra singly-LL.c
[arnuld@dune programs]$ ./a.out
Num = 0
Num = 1
Num = 2
going to remove element with value = 1
Num = 0
Num = 2
going to insert element after root_node
Num = 0
Num = 1
Num = 2
[arnuld@dune programs]$