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,...
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...
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...
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...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT