Delters1 0 Newbie Poster

This is a homework problem. I have 5 modules to encode and decode a txt file using the huffman algorithm along with. I'm having some difficulty with where to start with the decoding module. I have to do the decoding using the functions from binary.h and binary.cpp and my priority queues . Here is the code I have.

//pqueue.h//////
#include <iostream>
#include <stdlib.h>
#include "tree.h"

using namespace std;

typedef int PQPriorityType;


struct PriorityQueue
{
   int* ptr;
   PriorityQueue()
   {
     ptr = NULL;
   }
};
void insert(PriorityQueue& q, PQItemType v, PQPriorityType priority);
void remove(PriorityQueue& q, PQItemType &v, PQPriorityType &priority);
void update(PriorityQueue& q, PQItemType v, PQPriorityType priority);
bool isEmpty(PriorityQueue q);
//////////////////////////////

//pqueue.cpp/////////////////
#include "pqueue.h"
#include <iostream>
#include <stdlib.h>
using namespace std;


struct PQCell
{
  PQItemType     item;
  PQPriorityType priority;
  PQCell*        next;
  PQCell(PQItemType i, PQPriorityType p)
  {
     item = i;
     priority = p;
  }
};


void insertCell(PQCell*& cell, PQItemType v, PQPriorityType priority)
{
  if(cell==NULL||priority<cell->priority)
  {
    PQCell* x=new PQCell(v,priority);
    x->next=cell;
    cell=x;
  }
  else insertCell(cell->next,v,priority);
}


void insert(PriorityQueue& q, PQItemType v, PQPriorityType priority)
{
  insertCell(q.ptr, v, priority);
}


void removeCell(PQCell*& cell, PQItemType &v, PQPriorityType &priority)
{
  v=cell->item;
  priority=cell->priority;
  PQCell* x=cell;
  cell=cell->next;
  delete x;
}

void remove(PriorityQueue& q, PQItemType &v, PQPriorityType &priority)
{
  removeCell(q.ptr,v,priority);
}


void update(PriorityQueue& q, PQItemType v, PQPriorityType priority)
{
  PQItemType sDummy;
  PQPriorityType pDummy;
  PQCell* c=q.ptr;
  PQCell* lastc=NULL;
  while(c!=NULL)
  {
    if(c->item==v)
    {
      if(lastc==NULL)q.ptr=c->next;
      else lastc->next=c->next;
      removeCell(c,sDummy,pDummy);
      insertCell(q.ptr,v,priority);
      break;
    }
    lastc=c;
    c=c->next;
  }
}

bool isEmpty(PriorityQueue q)
{
  return !q.ptr;
}
//////////////////////////////////////

//tree.h//////////////////////////////
# ifndef TREE_H
# define TREE_H
using namespace std;
enum NodeKind {leaf, nonleaf};
  struct Node {
    NodeKind kind;
    char     ch;
    Node*    left;
    Node*    right;

    Node(char c)
    {
      kind = leaf;
      ch   = c;
    }
    Node(Node* L, Node *R)
    {
      kind  = nonleaf;
      left  = L;
      right = R;
    }      
  };
# endif

////////////////////////////////////////////////
//binary.h////////////////////////////////////
#include <cstdio>
using namespace std;
struct BFILE
{
  FILE* file;
  BFILE(FILE* f)
  {
    file = f;
  }
};

BFILE* openBinaryFileWrite(const char* filename);
void   writeBit(BFILE* f, int b);
void   writeByte(BFILE* f, int b);
void   closeBinaryFileWrite(BFILE*& f);
BFILE* openBinaryFileRead(const char* filename);
int    readBit(BFILE* f);
int    readByte(BFILE* f);
void   closeBinaryFileRead(BFILE*& f);
///////////////////////////////////////

//binary.cpp////////////////////////////
BFILE* openBinaryFileWrite(const char* filename)
{
  FILE* f = fopen(filename, "w");
  if(f == NULL) return NULL;
  else return new BFILE(f);
}

/***********************************************
 *               writeBit                      *
 ***********************************************
 * Write bit b into open file f.  b must be    *
 * 0 or 1.                                     *
 ***********************************************/

void writeBit(BFILE* f, int b)
{
  if(b < 0 || b > 1)
  {
    printf("Bad bit %i passed to writeBit\n", b);
    fflush(stdout);
  }
  putc(b + '0', f->file);
}

/***********************************************
 *               writeByte                     *
 ***********************************************
 * Write one byte (8 bits) holding b into open *
 * file f.  It is requied that 0 <= b <= 255.  *
 ***********************************************/

void writeByte(BFILE* f, int b)
{
  int r = b;
  if(r < 0) r = r + 256;
  for(int i = 0; i < 8; i++) 
  {
    putc((r >> 7) + '0', f->file);
    r = (r << 1) & 0xFF;
  }
}

/***********************************************
 *          closeBinaryFileWrite               *
 ***********************************************
 * Close file f.  It should have been opened   *
 * using openBinaryFileWrite.                  *
 ***********************************************/

void closeBinaryFileWrite(BFILE*& f)
{
  if(f != NULL) 
  {
    fclose(f->file);
    delete f;
    f = NULL;
  }
}

/***********************************************
 *          openBinaryFileRead                 *
 ***********************************************
 * Return an open file for reading bits from   *
 * file filename.                              *
 ***********************************************/

BFILE* openBinaryFileRead(const char* filename)
{
  FILE* f = fopen(filename, "r");
  if(f == NULL) return NULL;
  else return new BFILE(f);
}

/***********************************************
 *                 readBit                     *
 ***********************************************
 * Read one bit from file f and return it (0   *
 * or 1).  At end of file, return EOF.         *
 ***********************************************/

int readBit(BFILE* f)
{
  int c = getc(f->file);
  if(c == EOF) return EOF;
  else return c - '0';
}

/***********************************************
 *                 readByte                    *
 ***********************************************
 * Read one byte from file f and return it.    *
 * At end of file, return EOF.                 *
 ***********************************************/

int readByte(BFILE* f)
{
  int r = 0;
  int b;
  for(int i = 0; i < 8; i++) 
  {
    b = readBit(f);
    if(b == EOF) return EOF;
    r = (r << 1) | b;
  }
  return r;
}

/***********************************************
 *          closeBinaryFileRead                *
 ***********************************************
 * Close file f.  It should have been opened   *
 * using openBinaryFileRead.                   *
 ***********************************************/
void closeBinaryFileRead(BFILE*& f)
{
  if(f != NULL) 
  {
    fclose(f->file);
    delete f;
    f = NULL;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.