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 9199503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:32:54+00:00 2026-06-17T22:32:54+00:00

I am attempting to write my own template queue class to learn how to

  • 0

I am attempting to write my own template queue class to learn how to use templates. I see that this type of question is asked often, and I have read many of the responses but I still do not see what I am doing wrong.

template <class type>
struct Node{
    type data;
    Node *next;
};

template <class type>
class LinkedListQueue{
public:
    LinkedListQueue();
    void push(type new_data);
    void pop();
    type front();
    void print();

private:
    Node<type> *head;
    Node<type> *tail;
};

template <class type>
LinkedListQueue<type>::LinkedListQueue(){
    this->head = NULL;
    this->tail = NULL;
}

template <class type>
void LinkedListQueue<type>::push(type new_data){
    Node<type> *newNode;
    newNode->data = new_data;
    newNode->next = NULL;

    if(this->head == NULL){
        this->head = newNode;
        this->tail = newNode;
    }else{
        this->tail->next = newNode;
        this->tail = newNode;
    }
}

template <class type>
void LinkedListQueue<type>::pop(){
    if(this->head != NULL){
        this->head = this->head->next;
        if(this->head == NULL){
            this->tail == NULL;
        }
    }else{
    cout << "Queue is Empty" << endl;
    }
}

template <class type>
type LinkedListQueue<type>::front(){
    return(this->head->data);
}

int main() {
    LinkedListQueue<int> newQueue;
    newQueue.push(5);
    newQueue.push(4);
    cout << newQueue.front() << endl;
    newQueue.pop();
    cout << newQueue.front() << endl;
}

I am having trouble determining where the problem is. If I comment off the pop and last front call, the first front() call outputs correctly. However, uncommenting pop and front breaks everything. When I try to debug pop() it seems like there is only one Node in the list.

Any help would be greatly appreciated.

  • 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-17T22:32:55+00:00Added an answer on June 17, 2026 at 10:32 pm

    You’re not allocating your new node. just storing the data.

    Node<type> *newNode; //<=== indeterminate pointer
    newNode->data = new_data;
    newNode->next = NULL;
    

    One way or another, you’re always pushing a new element in, so allocate it, set it up, then figure out where it goes. Also, create a constructor for your node that takes a const-ref of the data, and sets the next to NULL. This makes your code significantly more straight-forward (and actually more efficient):

    Node<type> *newNode = new Node<type>(new_data);
    

    With the Node template becoming:

    template <class type>
    struct Node
    {
        Node(const type& value) 
            : data(value), next(NULL) {};
        type data;
        Node *next;
    };
    

    Finally, your pop() isn’t deleting the node, just mucking with the pointers. you may want to address that as well.

    template <class type>
    void LinkedListQueue<type>::pop()
    {
        if(this->head != NULL)
        {
            Node<type>* victim = this->head;
            this->head = this->head->next;
            if(this->head == NULL)
                this->tail == NULL;
            delete victim; // <=== note: deleting node.
        }
        else
        {
            cout << "Queue is Empty" << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write my own string class (so I can learn more)
I'm currently attempting to write my own program that mirrors the pmap command, specifically
I am attempting to learn Java Programming on my own (without classes/teachers/tutors/etc.) so this
I am attempting to write my own button object in JavaScript that can be
I am curretnly attempting to write a script in python that allows me to
I am attempting to write a method that has an optional EventHandler Paramater. it
I have been attempting to write a VBA Script that can parse out other
I'm attempting to write an extractor(s) for use in matching against a multiple parameter
I am attempting to write a time management tool with wxPython that is ideally
I am trying to write my own control template for a TabItem Header, and

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.