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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:45+00:00 2026-05-26T05:46:45+00:00

So, I decided to look back at some data structures to keep myself sharp

  • 0

So, I decided to look back at some data structures to keep myself sharp 😉

I started to implement a hash table, when I remembered that I needed linked lists for the buckets so that I can avoid hash collisions. So I started my linked list…

I implemented all of the functional methods of my linked list class (add, get, remove, etc) and then I decided that I wanted to try something that I hadn’t tried before. Overloading the array index operators so that my linked list indexes can be retrieved or assigned as if the linked list was an array.

I got the retrieval part working no problem:

template <class T>
T LinkedList<T>::operator[](const int &i) {
    return get(i);
}

The get function returns the data of the associated node, not the node itself…the setter should behave where the value supplied gets stored to the data attribute of node at the given index…my vision is that the user will not ever have to touch the ListNode class.

My end goal is that I can have a smart LinkedList that will behave like so:

LinkedList<int> list;

list[0] = 1;    //Allocates memory for 0th node and stores 1 as the data
list[3] = 2;    //Allocates memory for 1th,2th,and 3th nodes and sets 2 as the data
                //Unassigned nodes should use default constructors on data

int a = list[3];  //Sets a to 2
cout << list[0] << endl;  //prints 1

The getter works fine, but I am having trouble on the setter. Assume the set function with all index error checking and memory allocation is done, as it is. Any help would be appreciated. If it is impossible, then please let me know before I spend more time on it. Thanks.

  • 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-26T05:46:46+00:00Added an answer on May 26, 2026 at 5:46 am

    it looks like you want to return nodes by reference:

    template <typename T>
    class LinkedList {
    ...
      T& operator[](const size_t& i) { return this->get(i); }
      const T& operator[](const size_t& i) const { return this->get(i); }
    ...
    };
    

    (also assumes LinkedList::get() returns references)

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

Sidebar

Related Questions

I'm newbie to mvc, do decided to look through some site examples. Started from
I decided to teach myself assembly language. I have realized that my program will
A few years back, I started learning Ruby/Rails from some beginner's guide for Rails.
I decided to install APC to speed up the site that I work for.
Ive decided that I really dont like microsoft and their ways. Please could you
Lately i decided to take a look at Java so i am still pretty
We decided to use mongodb for some web application (instead of mysql) but want
I have a search page that uses jQuery to populate a table with search
Just some days ago I started looking into a unit test framework called check,
I have some PL/SQL code that I think might have a memory leak. Everytime

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.