Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by list.h ( 16 years ago )
#ifndef LIST_H
#define LIST_H

#include "defines.h"
#include "typedefs.h"

template <class Type> unsigned short int listAdd(Type **root, char *data, unsigned int hash)
{
 if(!(*root))
 {
  (*root)=(Type *)malloc(sizeof(Type));
  (*root)->data=(char*)malloc((strlen(data)+1)*sizeof(char));
  if (!(*root)->data)
   return NO_MEM;

  strcpy((*root)->data,data);
  (*root)->hash=hash;
  (*root)->next=NULL;
  return SUCCESS;
 }
 else
 {
  Type *current=(*root);
  while(current->next)
  {
   if (current->hash==hash)
    if (strcmp(current->data,data)==0)
     return ALREADY_IN;
   current=current->next;
  }

  if (current->hash==hash)
   if (strcmp(current->data,data)==0)
    return ALREADY_IN;

  current->next=(Type*)malloc(sizeof(Type));
  if(!current->next)
   return NO_MEM;
  current=current->next;

  current->data=(char*)malloc((strlen(data)+1)*sizeof(char));
  if( !current->data )
   return NO_MEM;

  strcpy(current->data,data);
  current->hash=hash;
  current->next=NULL;
  return SUCCESS;
 }
}


template <class Type> unsigned short int isInListByHash(Type *root, unsigned int hash)
{
 while(root)
 {
  if (root->hash==hash)
   return SUCCESS;
  root=root->next;
 }

 return NOT_FOUND;
}

template <class Type> unsigned short int listDel(Type **root, char *data, unsigned int hash)
{
 hashTableElement *current=*root;
 if(!current)
  return NOT_FOUND;

 if (current->hash==hash)
 {
  if(strcmp(current->data,data)==0)
  {
   current=current->next;
   free((*root)->data);
   free((*root));
   (*root)=current;
   return SUCCESS;
  }
 }

 while(current->next)
 {
  if(current->next->hash==hash)
   if(strcmp(current->next->data,data)==0)
   {
    hashTableElement *tmp=current->next;
    current->next=tmp->next;
    free(tmp->data);
    free(tmp);

    return SUCCESS;
   }
  current=current->next;
 }

 return NOT_FOUND;
}

template <class Type> char* listFind(Type *root, char *data, unsigned int hash)
{
 while(root)
 {
  if(root->hash==hash)
   if(strcmp(root->data,data)==0)
    return root->data;
  root=root->next;
 }
 return NULL;
}

#endif

 

Revise this Paste

Parent: 19085
Children: 19087
Your Name: Code Language: