Im writing a program which is being implemented as a singly linked list. I am having trouble with one of the functions.
The funtion will malloc space for a new block and for the key and data areas pointed to from it and return a pointer to new block. It will return null if malloc fails.
Will this work?
static Block *newBlock( int keylength, int vallength )
{
Block *nblock;
Block *v;
Block *k;
nblock = (Block) malloc(sizeof(Block));
v = (Block) malloc(sizeof(vallength));
k = (Block) malloc(sizeof(keylength));
return nblock;
}
All the code related to the function
[code]
#include <stdio.h>
#include <string.h>
/* prototypes, return codes, and typedef for Mapping */
#include "mapss.h"
#define DEBUG 0
typedef struct list_blk {
char *key;
char *data;
struct list_blk *next;
} Block;
typedef struct {
int length;
Block *head;
Block *tail;
} List_header;
/* what a Mappings turn out to be in the implementation-- */
typedef List_header *MapList;
/* prototypes and macros for local functions */
static Block *find( MapList L, const char* key );
static Block *newblock( int keylength, int valuelength);
static void freeblock( Block *bptr );
/* A useful macro: see if ptr is "positioned" at the logical end-of-list,
* the position just after the last real key-val pair on the list.
*/
#define PTR_AT_END(L, ptr) ( ((ptr)->next == ((MapList)L)->tail ))
Mapping makeMapping()
{
MapList L;
L = (MapList)malloc(sizeof(List_header));
if (L == NULL) return NULL; /* malloc failed! */
L->length = 0;
L->head = newblock(0, 0); /* key and data fields never used */
L->tail = newblock(0, 0);
if (L->head && L->tail) {/* malloc succeeded in both calls to newblock */
L->head->next = L->tail;
return (void *)L;
}
else return 0;
}
/* optional, but think about it */
void unmakeMapping(Mapping M)
{
printf("unmakeMapping nyi\n"); /* nyi = not yet implemented */
}
int install( Mapping M, const char *key, const char *data )
{
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
Block *bptr = find( L, key); /* try to find key */
printf("install stub\n");
return INSTALL_FAILED;
}
int lookup( Mapping M, const char *key, char *data ) {
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
printf("lookup stub\n");
return NOT_FOUND;
}
int delete( Mapping M, const char* key) {
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
printf("delete stub\n");
return NOT_FOUND;
}
/* Return the count of Mapping L, = length of underlying list */
int count( Mapping L ) {
printf("count stub\n");
return 0;
}
/********************* local functions *********************/
/* Look for key on L, returning pointer to the block
* -before- the block containing the key. Returns the block
* before the tail if key not found.
*
*/
static Block *find( MapList L, const char* key )
{
printf("find stub\n");
return 0;
}
/* malloc space for a new Block and for the key and data
* areas pointed to from it, return pointer to new Block.
* Return NULL if malloc fails.
*/
static Block *newblock( int keylength, int vallength)
{
printf("newblock stub\n");
return 0;
}
/* Free the space pointed to by bptr - after freeing
* the space reserved for the key and the data areas.
*/
static void freeblock( Block *bptr )
{
printf("freeblock stub\n");
return;
}
[\code]