Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:07:55+00:00 2026-06-06T05:07:55+00:00

I’m making a program on the HUFFMAN ALGORITHM where I have one class of

  • 0

I’m making a program on the HUFFMAN ALGORITHM where I have one class of nodes (hnode) to contain the symbol (name) and its frequency of occurence (freq); and other class (pq) which is used to form the tree to get the code for the symbols. I have written only the relevant parts of the classes. Now when I try to run this program it always gets stuck in the while loop (in main()) when it enters the while loop second time. I’ve tried everything but still couldn’t figure out why… Can somebody please help to make this code run !!

#define NPTR hnode*
class pq;
class hnode
{
    string name;
    int freq;
    friend class pq;

    public:

    NPTR phnode;
    void show () 
    { cout << "name = " << name << endl << ", freq= " << freq <<endl ; }

    hnode (string x, int fr): name(x), freq(fr)
    {
        phnode = this;
    }

    friend hnode operator + (hnode p, hnode q)
    {
        string s = q.name + p.name;
        int f = q.freq + p.freq ;
        hnode foo (s,f);
        return foo;
    }
};

class pq /// ascending priority queue
{
    struct node
    {
    NPTR data;
    node* next;
    node (NPTR p) : data(p), next(0) {}
    };
    public:
    int count;
    node* getnode (NPTR p) { return new node(p); }
    node* listptr;
    void place (NPTR );
    NPTR mindelete();
    pq() : listptr(0), count(0) {}
};

void pq::place (NPTR p)
{
    if(count == 0)
    {
        listptr = getnode(p);
    }

    else
    {
        node* foo = listptr, *bar = NULL;
        while( (foo!= NULL) && (p->freq >= foo->data->freq) )
        {
            bar = foo;
            foo = foo->next;
        }
        node* ptr = getnode(p);
        ptr->next = bar->next;
        bar->next = ptr;
    }
    ++count;
}

NPTR pq::mindelete()
{
    if(count==0) { cout<<"invalid\n"; exit(1);}
    NPTR val = listptr->data;
    listptr = listptr->next;
    --count;
    return val;
}

void main ()
{
    pq list;
    for ( int i=0; i<3; ++i)
    {
        string s;
        int f;
        cin>>s>>f;
        NPTR p = new hnode(s,f);
        list.place(p);
    }
    while(list.count>1)
    {
        NPTR p1 = list.mindelete();
        NPTR p2 = list.mindelete();
        hnode po = (*p1)+(*p2); // overloaded operator
        NPTR p = &po;
        p->show();
        list.place(p);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:07:57+00:00Added an answer on June 6, 2026 at 5:07 am

    Your code’s creating a locally scoped hnode po over here:

    while(list.count > 1)
    {
        NPTR p1 = list.mindelete();
        NPTR p2 = list.mindelete();
        hnode po = (*p1)+(*p2); // overloaded operator
        NPTR p = &po;
        p->show();
        list.place(p);
    }
    

    Then you’re passing that around by address and you’re making pq::node hold onto that. Sounds like a really bad idea since hnode po goes out of scope at the end of your while loop on every iteration.

    In general, you want to be using smart points and RAII for automated memory management rather than new’ing and delete’ing all over the place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.