C++ :: Traversing Array Of Lists Of Objects?

Nov 13, 2013

I am working on building a graph of a maze problem using an adjacency list and my own custom class Node.

Each Node has a vNum and jumpAmount integer variable, the vNum is the unique key.

If I have an array of lists of Node objects declared by: Code: forward_list<Node> adjacencyList[10] I skip the 0 row and only use 1-9. It makes more sense logically for my program and Im not worried about wasted memory at this point.

How could I go about traversing and outputting member variables for all the Nodes in each list in my array?

Say my code builds the following adjacency list:

Code:
1 -> { 2, 3, 4, 7 }
2 -> { 1, 3, 5 }
3 -> { 1, 2, 6 }
4 -> { 1, 5, 6, 7 }
5 -> { 2, 4, 8 }
6 -> { 3, 4 }
7 -> { 1, 4, 8 }
8 -> { 5, 7, 9}
9 -> { 8 }

where the values in brackets are the nodes that have an edge with the ith row node. Had to put it in code tags for the thread to be allowed to post.

I am using the forward_list library for my list functions and this is where my problem arises. The cplusplus.com - The C++ Resources Network website iterates through the list using:

Code:
for ( auto it = mylist.begin(); it != mylist.end(); ++it )
std::cout << ' ' << *it;

Obviously this works for ints, doubles, etc, but not objects with member variables. My attempt to replicate this code for my problem looks like:

Code:
for( int i = 1; i < 10; ++i ) {
cout << "Node " << i << " -> { ";
for( Vertex it = adjList[i].begin(); it != adjList[i].end(); ++it )
{
cout << it.getVNum() << ", ";
}}

I have to come up with user defined conversions for this to work and I have never done such a thing. Is there a way to go about doing this to avoid user defined conversions, because if there isnt I feel like attempting to do this specific problem might be a little too difficult for someone who hasnt defined any custom conversions.

Potentially, I could write my own List class and have a next pointer member variable that points to the next object in the list and use that to use a dot reference to a get function to display member variables. This is how I learned linked lists back in my Data Structures class. However I am trying to avoid that for the time being, as that would be a lot more code to implement rather than just figuring out how to use the forward_list library.

View 4 Replies


ADVERTISEMENT

C++ :: Searching For Objects In Linked Lists

Jun 18, 2013

So I have a Class named Subject, wich I used linked list to keep the objects. Now I have another Class called Tests which I should register a test by the Subject class, so I made a search like this:

List obj; //it's the object to access the List class, which manipulates the
//nodes from Subject.

obj.currentPtr=obj.firstPtr;
while (obj.currentPtr!=NULL) {
if(obj.currentPtr->nameSubject==value) //searching if there's a
return obj.currentPtr; //name equal to provided
obj.currentPtr=obj.currentPtr->nextPtr;
}
return 0;

I've made a similar process for Subject. But in this when I access the firstPtr, it's shown to be 0, once I have already the list of Subject and it should not be zero. What's wrong?

View 3 Replies View Related

C++ :: Traversing A Linked List

Mar 30, 2013

When I am traversing a linked list ...i suppose we can do it only with pointers

like p is a head node pointer then

p
p->next
p->next->next
p-next->next->next

so on represents a link...

if i have to pass head node as struct variable itself...can i traverse a link using dot operator??

p and p.next and how to retrieve third and remaining links through this method if we can traverse like this???

View 1 Replies View Related

C++ :: Traversing Linked List (Minions)

Jan 29, 2015

I have been trying to create a linked list of "Minions" where each has x coordinates, y coordinates, wp (weapon power), and a pointer to next minion.

the code:

#include<cstddef>
#include<iostream>
using namespace std;
struct minion{
int x;
int y;
int wp;
minion* next;

[Code] ....

Problem is: I made 3 for loops in main, first one to display the current weapon power which expected to output 10 10 10 (initial set weapon powers), 2nd for loop increases their weapon power (for whatever reason, a level up or something), third loop should display the new increased weapon power.

BUT, what i get is 10 and below it 30. looks like min4's wp only increased. There must be something wrong i am doing while i try to loop through my linked list and i cant figure it out...

View 3 Replies View Related

C++ :: Traversing Doubly Linked List

Feb 26, 2014

I have no problem traversing at both forward and backward but not simultaneously. I can traverse forward, and traverse again forward with no problem. I can also traverse backward and traverse backward again with no problem (take note this is by not exiting the program). Without exiting the program, with the same datas inputed, if i traversed forward, i cannot traverse backward (it only gives me infinite loop of the first data) and vice versa.

#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;

[Code] ....

View 2 Replies View Related

C/C++ :: Traversing A List Comparing To Compare Values?

Apr 22, 2014

I have a method that traverses a list to compare a number from the list to user input from elsewhere in the program. I have tried a few different things and I am probably overlooking something simple but my problem is my if statement is executing the else statement every run no matter if the input matches. This function is to return the location in the list that the num matches. -1 if no match, 0, 1, 2, otherwise depending on the number of nodes.

int NumberList::find(int num)
{
int temp = 0, count = 0;
ListNode *nodePtr;

[Code]....

View 2 Replies View Related

C++ :: Traversing Both Ways In A Doubly Linked List?

Mar 22, 2015

I've been working on a doubly linked list project for my Data Structures course. I've got everything to work so far, except for traversing in both directions. Traversing in the forward direction works fine, but when I try choosing to traverse backwards (starting from the tail of the linked list), it just says that the value is not in the list. That being said, the output looks like this:

This is not true, as you can obviously see, the value 26 is in the list, just two elements to the left of the tail (the value in tail for this particular case, obviously, is 36). The output should have been something like, "The value 26 was found 2 elements from the tail node". Here's the code for the findValue() function:

Code:
template <typename T>
bool LinkedListX<T>::findValue()//bool valueFound {
int traverseDirection = 0;
bool found = false;
int key;

[code]....

View 6 Replies View Related

C :: 3D Array Of Pointers To Linked Lists

Aug 8, 2013

I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements. the 3d array of pointers is declared like this:

Code:

struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS]; and there are linked lists declared like this:
Code: struct particle { /* structure for particles */
double sw[3]; /* square well components */
double hs[3]; /* hard sphere components */
double u[3]; /* unit vector for rotations */
struct particle *link;
};

I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:

Code:

void generate_list(){
int i,j,k,l;
/* determine the number of cells to decompose the simulation box in each of x, y and z directions*/
int(cells_x) = floor(boxX/cell_size);
int(cells_y) = floor(boxY/cell_size);
int(cells_z) = floor(boxZ/cell_size);
/* initialise the array of pointers to NULL */
for (j=0;j<cells_x;j++){

[Code]...

I am getting a pointer type cast error when I compile "assignment from incompatible pointer type",

View 6 Replies View Related

C++ :: Array Based Implementation Of Linked Lists

Jul 19, 2014

I first want to say that i am trying to solve my code without Pointers.

My goal is to..
1. Construct an empty 2D array with a capacity of 25. (list[25][2];)
2. Empty(): test if the stack is empty
3. Push(): add a value to the stack in the (list[i][0] = value;) position and (list[i][1] = previous list[i][0] position)
4. Top(); read the value(list[i][0]) at the top(count) of the stack
5. Pop(); remove the value at the top of the stack (list[i]= 0;)
6. Display(); displays all the elements in the stack going from the top to bottom order. (shows array index, data value, and next array index)

I have hit a road block and don't know what to fix or where to go from here.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
char name[5];
srand(time(0));
//Protoypes
void construction(int table[25][2]);

[Code]...

View 1 Replies View Related

C++ :: Implementing Double Linked Lists As Array Of Pointers

Dec 27, 2012

I am studying/writing/ code for a future Advanced Data Structure class in C++; however I am not suppose to use STL, Templates and etc (the way I just code "kinda" of resembles what I have to use).

My application is suppose to read/analyze all integers contained in a text file of 5-digit integers (10000 - 99999).

For simplicity I am using the following input (or check the attached):

20007 20008 20009
20010 20010
20012 20012
20013
20014 20010
20015
20016 ....

So far, my code is not displaying/printing the lists separated by the first digit of these 5-digits integers. I am expecting it to display/print logically/similar to the following:

Output:

Results for input file numbers.txt:
27 total integers read from file

The 3 unique integers beginning with digit 1 were
18399 17342 19948

The 6 unique integers beginning with digit 3 were
39485 34710 31298 38221 35893 32791

The 4 unique integers beginning with digit 4 were
43928 49238 45678 43210

The 6 unique integers beginning with digit 6 were
64545 62987 66221 61777 66666 65432

The 2 unique integers beginning with digit 8 were
88888 86861

The 1 unique integer beginning with digit 9 was
98765

There were 22 unique 5-digit integers in the file.

The highest unique count in one list was 6 integers.

My code that will follow soon displays/prints only the LAST 5-digits "group" of integers (in this case the 5-digits starting with 3). I am not sure what's wrong with my code; perhaps I am not designing it correctly. May be my calls in it are on the wrong place or I have to write all integers and then traverse it and output it (if that's the case, I am not sure how).

My code follows:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_CELLS = 10;
const int UNIQUE_FIVE_DIGIT = 5;

[Code] .....

View 10 Replies View Related

C :: Free Memory From A Dynamically Allocated Array Of Pointers To Linked Lists

Feb 26, 2013

Having some frustrating issues trying to free memory from a dynamically allocated array of pointers to linked lists. I think the problem is in how I initialize the pointers to NULL. Is there a more elegant way to have the program recognize that the list is empty so it knows to create a head node for the linked list in the function 'add_end_stub_to_array'?

I ran the code through Valgrind and it says that memory is definitely lost from this array.

This is the structure definition.

Code: struct stub_edge {
int loc_id;
int anim_type;
int mkt;
struct stub_edge *next_node;
};

Here is the code snippet from main allocating and deallocating memory to the array.

Code:

struct stub_edge **stub_list = (struct stub_edge **)malloc( sizeof(struct stub_edge *) * 12);
for (i = 0; i < 12; i++)
{
stub_list[i] = (struct stub_edge *)malloc(sizeof(struct stub_edge));
stub_list[i] = NULL;
}
stub_list = add_end_stub_to_array(end_stubs, stub_list);
destroy_end_stub_array(stub_list);

Here the function for adding nodes to the lists by reading through a dynamically allocated 2D array. (The end_stubs array is ordered by month and each linked list represents events occuring within the month).

Code:

struct stub_edge **add_end_stub_to_array(int **end_stubs, struct stub_edge **list)
{
long int i = 0;
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
int break1 = 0;
while(i < num_edges && break1 == 0 && mon < 12)

[Code]...

Here is the function for freeing memory from the list.

Code:

void destroy_end_stub_array(struct stub_edge **list)
{
if(list != NULL)
{
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
for(mon = 0; mon < 12; mon++)

[Code]...

View 1 Replies View Related

C :: Make A Hash Table Array Of Linked Lists / Separate Chaining Technique

Jul 5, 2013

how to create a hash table array and use linked lists inside the elements.

View 5 Replies View Related

C++ :: How To Use Class With Array Of Objects

Oct 12, 2014

class FlashCard {
public:
int a,b;
void PrintCard(void);
int CorrectAnswer(void);
};
void SwapFlashCard(FlashCard &a,FlashCard &b)

[Code]....

Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?

View 7 Replies View Related

C++ :: Using Array Of Pointers To Objects?

Sep 27, 2013

I've created an Array of pointers to objects using:

Person ** A = new person * [arraysize];

When I intend to access a specific person do I have to do this? :

something = A->[i];

and when I want a specific object within my struct do I have to do this? :

something_else = A->[i]->random_int;

View 10 Replies View Related

C++ :: Array Of Class Objects

May 13, 2014

I need an array of class objects but am unsure of how one might accomplish this. I have so far...

//element class driver code
Element Arsenic(lowCeiling, highCeiling);
Element Cadmium(lowCeiling, highCeiling);
Element Chromium(lowCeiling, highCeiling);
Element Copper(lowCeiling, highCeiling);
Element Lead(lowCeiling, highCeiling);
Element Nickel(lowCeiling, highCeiling);
Element Molybdenum(lowCeiling, highCeiling);
Element Mercury(lowCeiling, highCeiling);
Element Selenium(lowCeiling, highCeiling);
Element Zinc(lowCeiling, highCeiling);

Element metal[10] = {Arsenic, Cadmium, Chromium, Copper, Lead, Nickel, Molybdenum, Mercury, Selenium, Zinc};

Could the array be created in this manner or is that illegal code?

View 2 Replies View Related

C++ :: Array Of New Objects - Delite?

Mar 22, 2013

When I make an array of new objects and write delite arraySelector, will that delite all of the array elements or just the first one?

View 9 Replies View Related

C++ :: Array Of Objects And Inheritance

Dec 20, 2013

I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)

class D{
A **a;
int size;
.......
a = new A*[size];
......
};

And i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?

View 5 Replies View Related

C/C++ :: Quicksort For Array Of Objects

Feb 16, 2014

I have a homework requiring me to use quicksort on a pointer array of CDs. They are sorted by the title. I have tried the general solution for integer arrays which sets the pivot at the middle integer but it didn't work/ stopped working when I tried to start sorting the already inputed CDs. The same is happening with the current version (an adaptation of pieces of code):

void swap(CD &a, CD &B)/>
{
CD temp;
temp = a;

[Code]....

View 2 Replies View Related

C++ :: Array Of Pointers To Objects?

Dec 5, 2013

my code:

int OKCount=0;
int WaitingCount=0;
int ReservationCount=0;
Flight::Flight(int capacity, int waitingMax) {
seats=capacity;

[code].....

reservations is a data member in the class flight as:

Reservation **reservations;

OKReservation is a derived class and its abstract base class is Reservation.

My problem is that the reservations array loses its value in other function

View 8 Replies View Related

C/C++ :: Array Of Payroll Objects?

May 20, 2014

Define a PayRoll class that has data members for an employee's hourly pay rate (an integer , representing cents) and number of hours worked (also an integer ). The class provides two member functions, setRate and setHours that assign the value of their parameter to the appropriate data member. The class provides a third member function, getPay, that returns weekly gross pay (in cents), computed as follows: hours times rate for the first 35 hours plus hours times rate times one and a half for any hours past 35.

View 3 Replies View Related

C++ :: Subclass Objects In One Array?

Jul 16, 2012

i want to save similar objects, meaning having same superclass, in an array. Example: I have a superclass vehicle. Class car and truck inherit from vehicle. Now i have a few from both of them an want to put them in an array, vector, set or else. Is this possible in C++ or are there an workarounds? When i remember right, Java and C# offer this feature.

Here again an example:

Code:
class vehicle{
int mMaxSpeed;
} class truck : public vehicle {
int mTrailerLength;
} class car : public vehicle {

[code]....

View 5 Replies View Related

C++ :: Class Admin - Array Of Objects

Jun 26, 2013

What I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries. I'm trying to make array of objects, not sure if it's right

here is the code

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

class Employee {
public:
Employee(string name, short type, int salary)

[Code] .....

View 2 Replies View Related

C++ :: Use Array To Store Class Objects?

Feb 11, 2013

So starting with the Item.h file :

#ifndef ITEMH
#define ITEMH
using namespace std;
class Item

[Code]...

I think I'm getting the wrong idea about strings. I thought I could add data just by using '+=' but apparently not.

View 4 Replies View Related

C++ :: How To Make Array Of Objects For Class

Aug 18, 2013

class A {
Public:
A (int);
A(int,int);
int get (int,int);
};

View 5 Replies View Related

C++ :: Creating Array Of Eight Circle Objects

Nov 4, 2013

Im supposed to create an to array of eight Circle objects initialized with the following radii: 2.5, 4.0, 1.0, 3.0, 6.0, 5.5, 3.5, 2.0. Then use a bubble sort to arrange the objects in ascending order of radius size before displaying the area of each object.

The error I get is "Cannot open include file: 'Circle.h': No such file or directory". Do I have to create a separate file for it?

#include <iostream>
#include <iomanip>
#include "Circle.h"
using namespace std;
class Circle {
public:
Circle()

[Code] ...

View 1 Replies View Related

C++ :: How To Initialize Array Of DirectX9 Objects

Jan 3, 2015

I've got an error saying that there is an access violation at 0x0000040 error no 0xC0000005

I've searched the net, saying the error may be due to an uninitialzed variable.I debugged the code, and found out that an object created from directx D3DXMATRIXA16 is uninitialized.With values ??,??,?? in each element

pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax];

here, pMeshContainer is a variable passed into a function called GenerateSkinnedMesh by pointer

GenerateSkinnedMesh(..., D3DXMESHCONTAINER_DERIVED *pMeshContainer)

Then in turn, GenerateSkinnedMesh is called within a callback from a DirectX9 API called

ID3DXAllocateHierarchy

Code:
class CAllocateHierarchy : public ID3DXAllocateHierarchy {
public:
STDMETHOD( CreateFrame )( THIS_ LPCSTR Name, LPD3DXFRAME *ppNewFrame );
STDMETHOD( CreateMeshContainer )( THIS_
LPCSTR Name,

[code]....

when the method CreateMeshContainer finished execution, the meshContainer is passed back to its parent like this

*ppMeshContainer = pMeshContainer;

The whole meshContainer stuff is stored persistently inside the frame root wrapped within a class called CMesh So in that process, I haven't initialized pBoneMatrices in anyways. But what and where is the best way to initialize an array of DirectX9 objects.

There is a function called

D3DXMatrixIdentity(&...);

But how can I initialize each one of them with this call?

Notice that pMeshContainer->pBoneMatrices does contain a valid address, despite the fact that the elements inside it are never initialized...

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved