how would i go on about adding an item into an array. the catch here, is that instead of adding content normally, i want the added content to be added as a linked list. for example, if i add something into the first slot, and want to add another item similar to the first in the same slot, i would do so implementing a linked list function. i have a linked list header file which will do the adding, but i dont know how to make it work for an array.
any help would be greatly appreciated.

Your intent is a little confusing, it sounds like you want to add multiple items to the same element of an array. Which is not directly possible.

You mentioned using a linked list to do this. This would work, but you would need an array of lists. Each element of the array would then be a list head, and all the lists would have to be the same type. For example, a declaration such as list<int> listArray[5] would produce an array of 5 integer list head elements. From there, you could then us a statement such as listArray[2].push_back(someValue); to add another integer to the end of that particular list (in this case, list 3 of 5).

If you want the "array" itself to be more dynamic, you'll want to consider using a vector instead. A vector allows you to add and remove elements, and will re-allocate its resources (re-size) under certain conditions. The thing you have to be careful about though is if a vector re-allocates, some pointers within the lists could be messed up (depending on how they're built). If you use a vector, I would consider using a vector of pointers to lists. That way, if the vector re-alloactes, it doesn't move the list heads and therefore (in theory) won't break your lists.

Your intent is a little confusing, it sounds like you want to add multiple items to the same element of an array. Which is not directly possible.

Actually, I think if you create a structure, and then create an array of that structure, you can do what the op is suggesting. It's not a linked list though.

struct foo{
int myFirstData;
int mySecondData;
};

int main(){
foo myArray[10];
myArray[0].myFirstData = 1;
myArray[0].mySecondData = 2;
}

Although, I'm not 100% sure of what the Op is asking.

Edit:

I think you can make an array of classes also.

Yes, you can make an array of any defined data type, whether built-in or user-defined. But you can't create, for example, and array of integers and store more than one integer in an element, if you attempt to do so, you'll simply overwrite the element with the new integer value. Hence the reason I said it's not directly possible.

Did you even read the rest of the post?

Yes, you can make an array of any defined data type, whether built-in or user-defined. But you can't create, for example, and array of integers and store more than one integer in an element, if you attempt to do so, you'll simply overwrite the element with the new integer value. Hence the reason I said it's not directly possible.

Did you even read the rest of the post?

Like I said, I didn't really understand what he's looking for.

After rereading it, it kind of sounds like he's looking for an array of linked lists. I'm not 100% sure on syntax and I have no compiler to test it, so I won't bother trying to write it as I would probably just butcher it.

sorry for all the confusion. i admit my description of my problem was shady at best. what i need to do is make an array of lists like Fbody pointed out. i have the code for a linked list i used, and now, i need to implement that into an array. here is what i have so far,

im stuck at the end, where the int main() is located.
thanks

#include <iostream>
#include <string>
using namespace std;




//new class for studentnode
class node
{
      public:  
             string data; 
             node * next; 
             string university;  
              int id;    
              
          
}
   ;


//new class for studentlist
class studentlist
{
private:      
      node * start;
      
      
public:
       //constructor
       studentlist()
       {
           start = NULL;            
       }
       
   
       
       // function to add a new student to list
       void add(string stu, string univ, int id)
       {
           //create a new node
           node * somenode;
           somenode = new node;
           
           //put student in the new node!
           somenode->data = stu;
           somenode->university = univ;
           somenode->id = id;
          
              
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }
       

       //function to get a specified student, if not traverse the list until found.
void getStudent (int id)
{ 
     node* current;
     current = start; 
     while(current!=NULL)
 {   
         if(current->id == id)   
   {    
               cout << "Using the ID, we found the student in the system!! "<< endl;
               cout << "Student Name: " << current->data << endl; 
               cout << "School Attending: " << current->university <<endl; 
              
                 break;  
   }  
                  current = current->next;  
   }
                  }

       

       //display all the items in the list
       void print()
       {
            node * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << "Basic Information Available" << endl;
               cout << current->data << endl;
               cout << current->university << endl;
               cout << current->id << endl;
               
               
               //move current to next node
               current = current->next;               
           }     
       }
};



 



//main program thats ran.
int main()
{
    
    
    
    cout <<"This linked list should display students as they are added" << endl;
    
    
    studentlist listofstudents;
    
    listofstudents.add("Greg" , "A" , 1000);
    listofstudents.add("Eddie" , "U" , 1001);
    listofstudents.add("Hector" , "T" , 1002);
    listofstudents.add("Paul" , "C" , 1003);
    listofstudents.add("Eric" , "U" , 1004);
    
 
    
    //listofstudents.print();
    //displays all names on the list
 
    cout << "Printing out List..." << endl;
       
       cout << " " <<endl;
    listofstudents.print();
       
       cout << " " <<endl;
    
   listofstudents.getStudent(1001);


   // here is the initialization of the studentTable array.
   // what i need to do now, is be able to use the functions 
   // i defined in the studentlist. for example, if i need to add
   // an item to the studentTable array, i would use the add fucntion 
   // i declared "void add(string stu, string univ, int id)" 
	
   studentlist studentTable[10];
  
   // stuck here :/
  
    
    cin.get();
    return 0;
}

here are the exact instructions from the assignment:

"Rather than having one giant linkedlist which can only be searched slowly, break the list of students up into multiple smaller lists, each of which can be searched quickly. To do this, create a data structure that consists of an array of studentlists. When a new student is inserted, the student will be added to one of the lists within the array. When a student is searched for by ID number, your program will only search the studentList that holds the requested student, thereby making search much faster.For example, when inserting a student with ID number 13847, take the id number mod the array size. So, if there are 10 studentLists (a size 10 array), compute the ID mod 10, in this case giving ‘7’. Next, insert the student into the 7th list. If you insert in this fashion, search is easy. Suppose you want to locate the student with ID 98773 and your array is of size 10. You know that you only need to search the 3rd studentList in the array, as your insert method is guaranteed to have put the student into that list."

any ideas would be greatly helpful. thank you

When you use an array's name in combination with the subscript operator '[x]', you have an object that you can interact with.

If we assume that studentTable[10] represents all the "classes" that you teach, studentTable[0] represents one "class", studentTable[1] represents another "class" etc...

What you need to do is select the class you would like to add a student to, then simply call the add method.

//assuming this declaration, we have an array of 10 lists of students "classes"
studentlist studentTable[10];

//to access a specific list, use a subscript
studentTable[0].add("Greg" , "A" , 1000);  //call studentlist::add() for the first list in the array

here are the exact instructions from the assignment:

"Rather than having one giant linkedlist which can only be searched slowly, break the list of students up into multiple smaller lists, each of which can be searched quickly. To do this, create a data structure that consists of an array of studentlists. When a new student is inserted, the student will be added to one of the lists within the array. When a student is searched for by ID number, your program will only search the studentList that holds the requested student, thereby making search much faster.For example, when inserting a student with ID number 13847, take the id number mod the array size. So, if there are 10 studentLists (a size 10 array), compute the ID mod 10, in this case giving ‘7’. Next, insert the student into the 7th list. If you insert in this fashion, search is easy. Suppose you want to locate the student with ID 98773 and your array is of size 10. You know that you only need to search the 3rd studentList in the array, as your insert method is guaranteed to have put the student into that list."

any ideas would be greatly helpful. thank you

That makes it a little more complex, but the concept is still the same. You simply use the modulus operator to generate the array subscript that you need to use instead of looping or arbitrarily selecting one.

thank you very much.
i think i know what to do now.

I may be missing something here given the antihistamine I took an hour ago, but since I've never hesitated to sound foolish in the past I shan't be bashful at this point either.

What size is a list and how would the compiler know how much memory to sequester for an array of lists? List objects should have their own addresses (distinct from the address of the frist node of the list, etc) so an array of pointers to lists seems more intellectually satisfying as I'm thinking about it, though I admit to never having written such a structrue to know whether an array of lists works or not. (Could the name of a user defined object degenerate to the address of the object similar to the name of an array degenerating to the address of the first element of the array eventhough that doesn't happen for the names of native data types?)

I may be missing something here given the antihistamine I took an hour ago, but since I've never hesitated to sound foolish in the past I shan't be bashful at this point either.

What size is a list and how would the compiler know how much memory to sequester for an array of lists? List objects should have their own addresses (distinct from the address of the frist node of the list, etc) so an array of pointers to lists seems more intellectually satisfying as I'm thinking about it, though I admit to never having written such a structrue to know whether an array of lists works or not. (Could the name of a user defined object degenerate to the address of the object similar to the name of an array degenerating to the address of the first element of the array eventhough that doesn't happen for the names of native data types?)

That's basically what it is, an array of pointers. But studentTable[0] will always point to the first node in the list. To navigate that, or any of the other 9 lists in the array, you would have to create a temp node and go through all the next pointers.

The space needed is on a by node basis. As a new node is needed, the space is created.

studentTable[0] points to the first node in it's list so studentTable[0].data will reference that specific node.
studentTable[1] points to the first node in it's list so studentTable[1].data will reference that specific node.
studentTable[2] points to the first node in it's list so studentTable[2].data will reference that specific node.
etc ...

If you want the second node in the list

node foo;
foo = studentTable[0].next; //or studentTable[0] -> next;
//some code here
foo = foo.next; //etc to navigate the list.

If my logic or syntax is off here, please correct me.

I may be missing something here given the antihistamine I took an hour ago, but since I've never hesitated to sound foolish in the past I shan't be bashful at this point either.

What size is a list and how would the compiler know how much memory to sequester for an array of lists? List objects should have their own addresses (distinct from the address of the frist node of the list, etc) so an array of pointers to lists seems more intellectually satisfying as I'm thinking about it, though I admit to never having written such a structrue to know whether an array of lists works or not. (Could the name of a user defined object degenerate to the address of the object similar to the name of an array degenerating to the address of the first element of the array eventhough that doesn't happen for the names of native data types?)

A user-defined data type has a size, just like any built-in type.

For example, if you declare an array, the array's size is (numElements * elementSize). A typical integer uses 4-bytes of information, therefore elementSize == 4. In an array with 5 elements, you would get ((numElements = 5) * (elementSize = 4)) == 20 bytes for an array of 5 ints.

In a list, the first object in the list is referred to as the "head" of the list and each object within the list contains an additional member variable which is a pointer to the next element in the list. When you store a "list" in an array, you are actually storing the "head object" of the list, not the entire list.

//a very simple example from a class a few months ago
struct Link {
  int iData;
  Link* next;  //notice the pointer to another Link
};


class LinkList  //define an object to use as the "head" of the list
{
public:
/* misc member function declarations */
private:
  Link *pFirst;
};

Assume that we have a list definition such that the size of one object in the list is 12-bytes. Keeping in mind that only the "head" of the list is stored in the array, we can determine the size of the array at compile time because each object has a defined size, and the number of objects is known. If the array is intended to have 10 elements, that generates an array size of ((numElements = 10) * (elementSize = 12)) == 120-bytes. If we add an element to the third list in the array, all we do is modify the address the pointer contained in the object found at myArray[2]. The object itself is not within the boundaries of the array, it's floating around out there somewhere on the heap.

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.