Question

In: Computer Science

HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S...

HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S IN ANOTHER CLASS. HERE IS MY CODE FOR MY MAIN AND HEADER FILES.

HEADER FILE:

#ifndef HANOISTACK_H
#define HANOISTACK_H
#include <iostream>
using namespace std;

class HanoiStack { //class
private:
struct Disc{ //linked list for towers
int num;
Disc* next;
};
Disc* head;

public:
HanoiStack(){ //constructor
head = nullptr;
};
//HanoiStack operator+=(const Disc&);
//HanoiStack operator<<(const Disc&);
void push(int); //push function
int pop(); //pop function
void display(int);//prints pegs
};
#endif

MAIN FILE

int main(){
//class objects
HanoiStack peg;
HanoiStack temp1;
HanoiStack temp2;
HanoiStack temp3;
//integer variables

int num, solved, choice, end, removed, count = 0, tower;
//win condition variable
bool won = false;

cout << "Enter the number of disks" << endl;
cin >> num;//enters the number of disks
while (num > 0 && num < 10){ //confirmation loop
cout << "Error wrong entry try again" << endl;
cout << "Enter the number of disks" << endl;
cin >> num;
}
if (num > 0 && num < 10){
solved = pow(2, num)-1; //minimum number of moves required to win
}

for (int i = num; i > 0; i--){
peg.push(temp1); //inserts disks onto tower
}

Solutions

Expert Solution

You need to change the visibility of head from private to public to be able to access it outside the class but that would be a violation of Object-Oriented Programming paradigm. You can modify your class as:

#ifndef HANOISTACK_H
#define HANOISTACK_H
#include <iostream>
using namespace std;

class HanoiStack { //class
private:
    struct Disc{ //linked list for towers
        int num;
        Disc* next;
    };

public:
    Disc* head;
    
    HanoiStack(){ //constructor
        head = nullptr;
    };
    //HanoiStack operator+=(const Disc&);
    //HanoiStack operator<<(const Disc&);
    void push(int); //push function
    int pop(); //pop function
    void display(int);//prints pegs

};
#endif

Now, you can access head of the linked list as:

HanoiStack temp1;
cout << temp.head->num << endl; // to print num at head of the list

Avoid this design though. Another approach could be, bring your struct Disc definition out of the class and keep head of the list private in the class. Now, make a member function of the class Disc* getHead() which would return the head of the linked list. Do changes as follow:

#ifndef HANOISTACK_H
#define HANOISTACK_H
#include <iostream>
using namespace std;

struct Disc{ //linked list for towers
    int num;
    Disc* next;
};

class HanoiStack { //class
private:
    Disc* head;

public:
    HanoiStack(){ //constructor
        head = nullptr;
    };
    Disc* getHead() { return head; }
    
    //HanoiStack operator+=(const Disc&);
    //HanoiStack operator<<(const Disc&);
    void push(int); //push function
    int pop(); //pop function
    void display(int);//prints pegs
};
#endif

Now, to access the head of the linked list you could do:

HanoiStack temp1;
Disc *temp1head = temp1.getHead(); // this will return head of the linked list of temp1
cout << temp1head->num << endl; // to access number at the head

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
I have the function call showGroceries(list); I need to create a new prototype above int main...
I have the function call showGroceries(list); I need to create a new prototype above int main that provides: return type function name parameter(s) type(s) Then copy-and-paste the prototype below int main. Give the parameter a name and then implement the function so that it takes the vector of strings and displays it so that a vector with the values: {"milk", "bread", "corn"} would display as: Grocery list 1. milk 2. bread 3. corn However, if there is nothing in the...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT