A
arnuld
This program compiles fine but has semantic error, I am unable to find it.
I am trying to learn Linked-List implementation in C:
WANTED: Values to be added to the First element.
PROBLEM: it always says first element has values, even when the first element has NULL values
/* A doubly linked list implementation in C. I add some values and then remove some. Each element is a struct and
* we use key to identify the element.
*
* VERISON 1.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
SIZE_KEY = 10,
SIZE_VALUE = 20
};
struct KV_pair
{
char key[SIZE_KEY];
char value[SIZE_VALUE];
struct KV_pair* next;
struct KV_pair* prev;
};
void add_pair(struct KV_pair *const base, char* k, char* v );
struct KV_pair* add_element_to_list(struct KV_pair *const base, struct KV_pair* ps);
struct KV_pair* find_element(struct KV_pair *const base, struct KV_pair* f );
void print_pair(struct KV_pair *const );
int main(void)
{
struct KV_pair* baseElement = malloc( 1 * (sizeof *baseElement));
if( NULL == baseElement )
{
fprintf(stderr, "IN: %s, at %d, malloc() failed\n", __FILE__, __LINE__);
exit( EXIT_FAILURE );
}
memset(baseElement, '\0', sizeof(baseElement));
add_pair(baseElement, "k1", "v1");
print_pair(baseElement);
return 0;
}
void add_pair(struct KV_pair *const base, char* k, char* v )
{
struct KV_pair* s = malloc( 1 * sizeof(*s));
if( NULL == s )
{
fprintf(stderr, "IN: %s, at %d: malloc() failed\n", __FILE__, __LINE__);
}
else if( NULL == base->key )
{
struct KV_pair* temp = base;
printf("List is empty first element to add\n");
strcpy(temp->key, k);
strcpy(temp->value, v);
free(s);
s = NULL;
}
else
{
printf("List already has some elements\n");
strcpy(s->key, k);
strcpy(s->value, v);
if( add_element_to_list(base, s) )
{
printf("Element added\n");
}
else
{
printf("unable to add the element\n");
}
}
}
struct KV_pair* add_element_to_list(struct KV_pair *const base, struct KV_pair* ps)
{
struct KV_pair* b = find_element(base, ps);
if( b )
{
printf("IN: %s, at %d, Element already exists, can not add\n", __FILE__, __LINE__);
return ps;
}
else
{
printf("DUMMY addition, will write this part later\n");
return ps;
}
return NULL;
}
struct KV_pair* find_element(struct KV_pair *const base, struct KV_pair* f )
{
struct KV_pair* b = base;
if( b )
{
for( ; NULL != b; b = b->next )
{
if( 0 == strcmp(b->key, f->key) )
{
return b;
}
}
}
return NULL;
}
void print_pair(struct KV_pair *const base)
{
struct KV_pair* b = base;
for( ; NULL != b; b = b->next )
{
printf("Key = %s\n", b->key);
printf("Value = %s\n", b->value);
printf("----------\n");
}
}
===================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra doubley-LL.c
[arnuld@dune programs]$ ./a.out
List already has some elements
DUMMY addition, will write this part later
Element added
Key =
Value =
I am trying to learn Linked-List implementation in C:
WANTED: Values to be added to the First element.
PROBLEM: it always says first element has values, even when the first element has NULL values
/* A doubly linked list implementation in C. I add some values and then remove some. Each element is a struct and
* we use key to identify the element.
*
* VERISON 1.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
SIZE_KEY = 10,
SIZE_VALUE = 20
};
struct KV_pair
{
char key[SIZE_KEY];
char value[SIZE_VALUE];
struct KV_pair* next;
struct KV_pair* prev;
};
void add_pair(struct KV_pair *const base, char* k, char* v );
struct KV_pair* add_element_to_list(struct KV_pair *const base, struct KV_pair* ps);
struct KV_pair* find_element(struct KV_pair *const base, struct KV_pair* f );
void print_pair(struct KV_pair *const );
int main(void)
{
struct KV_pair* baseElement = malloc( 1 * (sizeof *baseElement));
if( NULL == baseElement )
{
fprintf(stderr, "IN: %s, at %d, malloc() failed\n", __FILE__, __LINE__);
exit( EXIT_FAILURE );
}
memset(baseElement, '\0', sizeof(baseElement));
add_pair(baseElement, "k1", "v1");
print_pair(baseElement);
return 0;
}
void add_pair(struct KV_pair *const base, char* k, char* v )
{
struct KV_pair* s = malloc( 1 * sizeof(*s));
if( NULL == s )
{
fprintf(stderr, "IN: %s, at %d: malloc() failed\n", __FILE__, __LINE__);
}
else if( NULL == base->key )
{
struct KV_pair* temp = base;
printf("List is empty first element to add\n");
strcpy(temp->key, k);
strcpy(temp->value, v);
free(s);
s = NULL;
}
else
{
printf("List already has some elements\n");
strcpy(s->key, k);
strcpy(s->value, v);
if( add_element_to_list(base, s) )
{
printf("Element added\n");
}
else
{
printf("unable to add the element\n");
}
}
}
struct KV_pair* add_element_to_list(struct KV_pair *const base, struct KV_pair* ps)
{
struct KV_pair* b = find_element(base, ps);
if( b )
{
printf("IN: %s, at %d, Element already exists, can not add\n", __FILE__, __LINE__);
return ps;
}
else
{
printf("DUMMY addition, will write this part later\n");
return ps;
}
return NULL;
}
struct KV_pair* find_element(struct KV_pair *const base, struct KV_pair* f )
{
struct KV_pair* b = base;
if( b )
{
for( ; NULL != b; b = b->next )
{
if( 0 == strcmp(b->key, f->key) )
{
return b;
}
}
}
return NULL;
}
void print_pair(struct KV_pair *const base)
{
struct KV_pair* b = base;
for( ; NULL != b; b = b->next )
{
printf("Key = %s\n", b->key);
printf("Value = %s\n", b->value);
printf("----------\n");
}
}
===================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra doubley-LL.c
[arnuld@dune programs]$ ./a.out
List already has some elements
DUMMY addition, will write this part later
Element added
Key =
Value =