So I have a program in which I am trying to return the head_ptr or tail_ptr in the form of an int and I'm not sure how to accomplish this. Code is attached below. Thanks guys!
MAIN
#include <iostream>
#include <cstdlib>
#include "list.h"
using namespace std;
void uppercaseify(string& mystr);
int main() {
List mylist;
string operation;
int data;
while (cin) {
cin >> operation;
uppercaseify(operation);
if (!cin || cin.eof() || operation == "QUIT") {
exit(EXIT_SUCCESS);
}
if (operation == "STATUS") {
cout << mylist.get_list_size() << endl;
}
if (operation == "PRINT") {
mylist.print_list();
}
if (operation == "PUSH") {
cin >> data;
mylist.push(data);
}
if (operation == "POP") {
data = mylist.pop();
cout << mylist.head_ptr<< endl;
}
if (operation == "SEARCH") {
cin >> data;
if (mylist.search(data))
cout << "IN LIST\n";
else
cout << "NOT IN LIST\n";
}
if (operation == "REMOVE") {
cin >> data;
if (mylist.remove(data))
cout << "REMOVED\n";
else
cout << "NOT REMOVED\n";
}
}
return 0;
}
void uppercaseify(string& mystr) {
for (auto& c: mystr)
c = toupper(c);
LIST CLASS
#pragma once
#include <iostream>
#include "node.h"
using namespace std;
class List {
int list_size;
public:
Node *head_ptr; //Holds newest member
Node *tail_ptr; //Holds oldest member
//Output the list
void print_list() {
if (head_ptr == NULL) {
cout << "EMPTY LIST\n";
return;
}
Node *temp_ptr = head_ptr;
int counter = 0;
while (temp_ptr) { //Same as saying while (temp_ptr != NULL)
cout << "Node " << counter++ << ": " << temp_ptr->get_data() << end$
temp_ptr = temp_ptr->get_next();
}
}
//Write these functions
int pop();
void push(int new_data);
bool search(int test_data);
bool remove (int old_data);
};
NODE.H!
#pragma once
//This Node class is fully functional
class Node {
int data; //Holds the data we're keeping track of
Node *next;
public:
Node() {
data = 0;
next = NULL;
}
Node(int new_data, Node *new_next) {
data = new_data;
next = new_next;
}
int get_data() const { return data; }
Node *get_next() const { return next; }
void set_next(Node *new_next) { next = new_next; }
};
ACTUAL FUNCTIONS!
#include <iostream>
#include "list.h"
using namespace std;
//YOU: Write these four functions
//Make sure you handle empty Lists cleanly!
//Deletes the oldest Node inserted, and returns its data value
//The oldest Node inserted should be tail_ptr
int List::pop() {
//List::node *n_tail_ptr;
//n_tail_ptr = tail_prt;
//tail_ptr = tail_ptr->link();// = new List(NULL);
//string tp = &tail_ptr;
int new1; //prototype
int *uno = new int[new1]; //prototype
delete tail_ptr;//delete &tail_ptr;
return ;//List::head_ptr-> data; //tail_ptr;
}
//Adds a new Node to the end of the list
//The newest Node will be held in head_ptr
void List::push(int new_data) {
head_ptr = new Node(1, head_ptr);
//List *newnode = new List(NULL);
}
//Returns if a Node containing the passed-in integer is in the list
//True if such a Node exists, false otherwise
bool List::search(int test_data) {
if(list_size)// == List::data)
return true;
else
return false;
//Removes the newest node containing the passed in integer
//Only remove one Node. Not all of them.
//Return true if any elements removed, false otherwise
//(int old_data)
bool List::remove (int old_data) {
//node *remove_prt;
//remove_ptr = head_ptr;
//head_ptr = head_ptr->link( );
//delete remove_ptr;
return true;
}
Any time you change the head or next pointer, there's a good chance that you need to change tail_ptr also. Actually do you even need the tail pointer? I don't see any methods that need it or even benefit from it.