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

  • Home
  • SEARCH
  • 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 7544053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:35:11+00:00 2026-05-30T08:35:11+00:00

I am trying to format a linked list so that it prints 5 nodes

  • 0

I am trying to format a linked list so that it prints 5 nodes on each line. I am not sure how to do this as I am new to operator overloading. Here is something i attempted but im stuck in a rut and cant seem to grasp the concept

     ostream &operator<<(ostream &os, List &s){ 

     nodeType<Type>* current = s.head; 
     int i = 0; 

 while (current != NULL) //while more data to print 
 { 
 os << current->info << " "; 
 current = current->link; 

 ++i; 

 if (i % 5 == 0) { 
     cout << '\n'; 
     i = 0; 
 } 
 } 

os << '\n'; // print the ending newline 

       return os; 
     }

Rest of Code

list.cpp

List::List()
{
node *head = NULL;
node *precurrent = NULL;
node *current = NULL;
int *temp = 0;
insert = 0;
search = 0;
remove = 0;
}

List::~List()
{
while (head != 0)
    remove();
}

void List::insert(int insert)
{
if (head==null)    \\If there is no list already, create a new head.
{
    temp = new Node;
    temp->data = insert;
    head = temp;
}
else                \\otherwise, insert the new node after current
{
    temp = new Node;
    temp->data = insert;
    temp->next = current->next;
    current->next = temp;
}
}

void List::search(int search)
{
current=head;
while (head->next != 0)  //Cycle through the list, and if the number is found, say so
{
    if (current->data = search)
        cout<<"Number found."<<endl;
    else
        cout<<"Number not found."<<endl;
}
}

void List::remove(int remove)
{
if (head == null)
    cout <<"Error.  No List."<<endl;
else if (head->next == null)
{
    num = head->data;
    delete head;
    head=null;
    current=null;
}
else if (head == current)
{
    temp = head->next;
    num = head->data;
    delete head;
    head=temp;
    current=temp;
}
else
{
    temp = current->next;
    num = current->data;
    delete current;
    precurrent->next = temp;
    current = temp;
}
}

list.h

//CLASS PROVIDED:  list                                 
//
// CONSTRUCTOR for the list class:
//   list()
//     Description:     Constructor will initialize variables
//     Preconditions:   None
//     Postcondition:   int insert = ""
//                      int search = ""
//                      int remove = ""
//   ~list()
//     Description:     Destructor destroys variables
//     Preconditions:   None
//     Postcondition:   variable deleted
//
// MEMBER FUNCTIONS for the list class      
//
//   string insert(int)
//     Description: Inserts an integer into a linked list   
//     Precondition: none
//     Postcondition: function returns Success/Error message.
//
//   string search(int);
//     Description:     Searches for certain linked list member and returns int to set current variable
//     Precondition:    none
//     Postcondition:   function returns int
//
//    string remove(int);
//     Description:     removes linked list member
//     Precondition:    user sends int to be deleted
//     Postcondition:   function returned string sddress
//   
//   void display(void);
//     Description:     displays entire linked list
//     Precondition:    none
//     Postcondition:   function returns screen output
//
//    void quit(void);
//     Description:     closes program
//     Precondition:    none
//     Postcondition:   none
//


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

class list
{     
  public:
//CONSTRUCTOR/DESTRUCTOR---------------------
    list();
    ~list();                                     

//GETS---------------------------------------
    void insert(int);
    string search(int);
    string remove(int);
    void display(void);
    void quit(void);

  private:

        int insert;
        int search;
        int remove;  


};




#endif
  • 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-05-30T08:35:13+00:00Added an answer on May 30, 2026 at 8:35 am

    You need to set current to s.head, not just head, which isn’t defined because this non-member operator overload is (as its name implies) not a member.

    You are also advancing the pointer completely wrong; you should print one info on each iteration like this:

    EDIT: If you want to print 5 per line, then do this:

    int i = 0;
    
    while (current != NULL) //while more data to print
    {
         os << current->info << " ";
         current = current->link;
    
         if (i % 5 == 0) {
             cout << '\n';
             i = 0;
         } else
             ++i;
    }
    
    os << '\n'; // print the ending newline
    

    Also Type is not defined (unless it’s somewhere in code you haven’t posted). If your List is a template, you need to make your operator overload a template as well.

    Please initialise variables instead of declaring them and then assigning to them. This:

    nodeType<Type> *current; //pointer to traverse the list
    current = head; //set current so that it points to the first node
    

    should be

    nodeType<Type>* current = s.head;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to format a table from XML. Lets say I have this line
I'm trying to format large currency numbers like this: NSNumber *testVal = [NSDecimalNumber decimalNumberWithString:
I am trying to format some c# output so that a number always has
I'm trying to format strings in XSLT that needs to be in pascal case
I'm trying to format a date for a given locale new Locale(mk, MK) .
I was trying to implement a small sample of a linked-list data type. I
im trying to format this string into a fixed column style but cant get
I am trying to format a string into regular time not military time. How
I am trying to format xml entries I have so that I can use
im trying this format: $(#<%= hfWidth.UniqueID %>).val($(#drag).attr(offsetWidth)); to fill the hidden field with client-side

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.