I got
Expression must be a modifiable lvalue
in
rear->getNextNode() = &node;
Here is code:
using namespace std;
#include<iostream>
#include<string>
class Node
{
string name;
Node* next;
int arrivedTime;
int runningTime;
char state='R';
public:
Node(char* name,int arrivedTime,int runningTime):name(name),arrivedTime(arrivedTime),runningTime(runningTime){}
void printState()
{
cout << "name=" << name << " " << endl;
}
void execute()
{
runningTime--;
printState();
}
bool whetherArrive()
{
if (arrivedTime > 0)
{
return false;
}
else
{
return true;
}
}
void downArrivedTime()
{
arrivedTime--;
}
Node* getNextNode()
{
return next;
}
};
class Queue
{
public:
Node* head;
Node* rear;
Node* p;
void insert(Node &node)
{
if (head == NULL)
{
head = &node;
rear = &node;
p = &node;
}
else
{
rear->getNextNode() = &node; //here hint Expression must be a modifiable lvalue
}
}
};
int main()
{
cout << "input: process-count" << endl;
int processCount;
cin >> processCount;
for (int i = 0; i < processCount; i++)
{
cout << "input:process-name arrivedTime runningTime" << endl;
char name[20];
int arrivedTime;
int runningTime;
cin >> name >> arrivedTime >> runningTime;
Node node(name, arrivedTime, runningTime);
}
}
rear->getNextNode() return a pointer to Node, and then set the point &node. What's wrong here?
As in the error, to make this compile:
rear->getNextNode() = &node;
getNextNode() must return lvalue so you need to modify singature to:
Node*& getNextNode()
^
Related
I'm trying to implement a stack using a linked list and class template. I'm not getting any compiler errors but my logic may be wrong, kind of lost. I had a working program in just a single file only using a struct so I had difficulties with translating it over using multiple files and template classes. I will also include my single file cpp below, hopefully it helps. Any help will be greatly appreciated. I have a header.h file, functions.cpp and main. cpp.
Header.h
#define STACK_H
#include <iostream>
using namespace std;
//to implement a stack using linked list
template<class T>
class node{
public:
T data;
node<T>*next;
};
template<class T>
class stack{
private:
node<T> *item;
node<T> *top;
public:
stack(); // constructor
void push( node<T> *); // to insert an item to the stack
void pop(); // to remove an item from the stack
void display(); // to display the stack elements on screen
node<T> *newnode(int );
};
#include "functions.cpp"
#endif
functions.cpp
#include <iostream>
#include "header.h"
#ifndef FUNCTIONS
#define FUNCTIONS
using namespace std;
template<class T>
stack <T> :: stack(){
node<T> *top = NULL;
}
template<class T>
void stack <T> :: push(node<T> * q){
if (top == NULL)
top = q;
else
{
q->next = top;
top = q;
}
}
template<class T>
void stack <T> :: pop(){
if (top == NULL) {
cout << "Stack is empty";
}
else {
cout << "Popped element is " << top->data;
item = top;
top = top->next;
delete(item);
}
}
template<class T>
void stack <T> :: display(){
node<T> *q;
q = top;
if (top == NULL) {
cout << "Stack is empty!!";
}
else {
while (q != NULL)
{
cout << q->data << " ";
q = q->next;
}
}
}
template<class T>
node<T> * stack <T> :: newnode(int x)
{
item = new node<T>;
item->data = x;
item->next = NULL;
return(item);
}
#endif
main.cpp
#include<iostream>
#include "header.h"
using namespace std;
int main()
{
int ch, x;
stack <int> myStack;
node<int> *nptr;
do
{
cout << "\n\n1.Push\n2.Pop\n3.Print Stack\n4.Exit";
cout << "\nPlease enter a function(1-4):";
cin >> ch;
if (ch == 1)
{
cout << "\nEnter data:";
cin >> x;
nptr = myStack.newnode(x);
myStack.push( nptr);
}
else if (ch == 2)
{
myStack.pop();
}
else if (ch == 3)
{
myStack.display();
}
else cout << "\nInvalid Entry";
} while (ch != 4);
return 0;
}
Single file working program
struct nodeType
{
int data;
nodeType *next;
};
nodeType *top = NULL;
nodeType *p;
nodeType* newnode(int x)
{
p = new nodeType;
p->data = x;
p->next = NULL;
return(p);
}
void push(nodeType *q)
{
if (top == NULL)
top = q;
else
{
q->next = top;
top = q;
}
}
void pop() {
if (top == NULL) {
cout << "Stack is empty";
}
else {
cout << "Popped element is " << top->data;
p = top;
top = top->next;
delete(p);
}
}
void printStack()
{
nodeType *q;
q = top;
if (top == NULL) {
cout << "Stack is empty!!";
}
else {
while (q != NULL)
{
cout << q->data << " ";
q = q->next;
}
}
}
int main()
{
int ch, x;
nodeType *nptr;
do
{
cout << "\n\n1.Push\n2.Pop\n3.Print Stack\n4.Exit";
cout << "\nPlease enter a function(1-4):";
cin >> ch;
if (ch == 1)
{
cout << "\nEnter data:";
cin >> x;
nptr = newnode(x);
push(nptr);
}
else if (ch == 2)
{
pop();
}
else if (ch == 3)
{
printStack();
}
else cout << "\nInvalid Entry";
} while (ch != 4);
return 0;
}
i have an assigment where i have to work with singly linked list using oop. I got stuck mostly on the sortedInsert function. I am assuming it will be some mistake with using pointers but I can't figure it out. And the program crashes after entering the user input in the for loop in initializeList function it might be because of the sortedInsert function but maybe it's something else.
Can someone help me figure out where am I getting it worng?
Here is the code:
#include <iostream>
using namespace std;
class Item{
private:
int m_value;
Item* m_next;
public:
Item(int val){
m_value = val;
m_next = NULL;
};
int getValue(){ return this->m_value; };
Item* getNext() { return this->m_next; };
void setValue(int val){ m_value = val; };
void setNext(Item* nxt){ m_next = nxt; };
};
class IntList{
private:
Item* m_front = NULL;
Item* m_end;
int m_size = 0;
void bump_up(int m_size){ m_size++; };
void bump_down(int m_size){ m_size--; };
public:
IntList(){ m_front = NULL; };
Item* getFirst(){ return this->m_front; };
//int getValueFirst(){ return this->firstItem->getValue(); };
~IntList();
void sortedInsert(Item* first, int val){
Item* newItem = new Item(val);
Item* current;
if(first == NULL || (first)->getValue() >= newItem->getValue()){
newItem->setNext(first);
first = newItem;
}
else{
current = first;
while(current->getNext() != NULL && current->getNext()->getValue() < newItem->getValue()){
current = current->getNext();
}
newItem->setNext(current->getNext());
current->setNext(newItem);
}
bump_up(m_size);
}
//print
void display(){
Item* p = m_front;
cout << "(" << m_size << ")";
while(p != NULL){
cout << p->getValue() << " ";
p = p->getNext();
}
cout << endl;
}
void initializeList(IntList* list){
int x, i;
cout << "Insert list size: ";
cin >> x;
cout << "Insert " << x << " numbers, which will be put in the list:";
for(i = 0; i <= x; i++){
int a;
cin >> a;
list->sortedInsert(list->getFirst(), a);
}
//cout << endl;
}
int main()
{
IntList* list = new IntList();
initializeList(list);
list->display();
return 0;
}
Being a beginner I tried this single linked list program to accept and display first to last elements.I can't figure out what is wrong.After you run it the program stops responding after taking in the first element. I am not very familiar with the language and am new to pointer concept. This was an assignment work.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
public:
node* head;
node* last;
node* n;
node* p;
int x;
char ch;
void input()
{
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
the big mistake already pointed out is the absence of an initialization by a constructor. Also I suggest to move some data member to private, and make some of them local.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
private:
node* head;
node* last;
public:
alpha() {
head = NULL;
last = NULL;
}
void input()
{
int x;
char ch;
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
node* n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
node* p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
As someone suggested, please implement a destructor ~alpha(), in order to avoid leaks of node instances.
When trying to add a new Node to the liked list it gives me a Segmentation fault. Can someone tell me what is wrong in the implementation of the addBook() function. I am not sure if it's the implementation of the function that is wrong, or the way that I've declared the classes.
class Reservation {
public:
int getID();
string getResevNum();
void setId(int x);
void setReseNum(string y);
private:
int ID;
string reservedNumber;
};
class ReservationCollection {
public:
ReservationCollection();
~ReservationCollection();
int getUserId(int &id);
string getUserBook(string &bookCall);
void findReservation();
void display();
void addBook(int id, string book);
void RemoveBook();
void ShutDown();
private:
struct Node {
Reservation *data;
Node *next;
};
Node *head;
};
ReservationCollection::ReservationCollection() {
Node *head = new Node;
head->next = NULL;
}
ReservationCollection::~ReservationCollection() {
}
void ReservationCollection::addBook(int id, string book){
Node *tmp = new Node;
tmp->data->setId(id);
tmp->data->setReseNum(book);
tmp->next = head->next;
head->next = tmp;
cout <<"Good\n";
}
int Reservation::getID(){
return ID;
}
string Reservation::getResevNum(){
return reservedNumber;
}
void Reservation::setId(int x){
ID = x;
}
void Reservation::setReseNum(string y){
reservedNumber = y;
}
int ReservationCollection::getUserId(int &id){
cout << "Enter Id number " << endl;
cin >> id;
return id;
}
string ReservationCollection::getUserBook(string &bookCall){
cout << "Enter book reservatin " << endl;
cin >> bookCall;
return bookCall;
}
int main()
{
int ID;
string BookNum;
char cmd;
do {
cout << "Enter command: ";
cin >> cmd;
ReservationCollection list;
if (cmd == 'A' || cmd == 'a'){
list.getUserId(ID);
list.getUserBook(BookNum);
list.addBook(ID, BookNum);
}
else if (cmd == 'S' || cmd == 's'){
cout << " list";
}
} while (cmd != 'Q' || cmd == 'q');
}
Wish I could just comment this:
All instances of the line Node *tmp = new Node; should read Node *tmp = new Node();
Don't use pointers if you don't have to. In this example Node has to be a pointer, because the list may have zero nodes, or 1000 nodes, so we need pointers to dynamically allocate memory on demand.
But (Reservation)data does not have to be a pointer. Each node always has one Reservation member.
If you do declare it as pointer then you must allocate it, and free it when it is no longer needed.
head should be initialized to NULL, because the single linked-list has no nodes when it is initialized. The first node inserted becomes the head.
#include <iostream>
#include <string>
using namespace std;
class Reservation
{
private:
int ID;
string reservedNumber;
public:
int getID() { return ID; }
string getResevNum() { return reservedNumber; }
void setId(int x) { ID = x; }
void setReseNum(string y) { reservedNumber = y; }
};
class ReservationCollection
{
public:
struct Node
{
Reservation data;
Node *next;
};
ReservationCollection();
~ReservationCollection();
void addBook(int id, string book);
Node* getHead() { return head; }
private:
Node *head;
};
ReservationCollection::ReservationCollection()
{
head = NULL;
}
ReservationCollection::~ReservationCollection()
{
Node *p = head;
while (p)
{
Node *next = p->next;
cout << "delete: " << p->data.getID() << ", " << p->data.getResevNum() << endl;
delete p;
p = next;
}
}
void ReservationCollection::addBook(int id, string book)
{
Node *node = new Node;
node->data.setId(id);
node->data.setReseNum(book);
//this element is the last element
node->next = NULL;
if (!head)
{
//first element inserted
head = node;
head->next = NULL;
}
else
{
//find the previous node in the list
Node *prev = head;
while (prev->next)
prev = prev->next;
//this node is after previous node
prev->next = node;
}
cout << "Good\n";
}
int main()
{
ReservationCollection list;
list.addBook(0, "Book0");
list.addBook(1, "Book1");
list.addBook(2, "Book2");
list.addBook(3, "Book3");
ReservationCollection::Node *p = list.getHead();
while (p)
{
ReservationCollection::Node *next = p->next;
cout << p->data.getID() << ", " << p->data.getResevNum() << endl;
p = next;
}
}
Creating a Node wont create the structure that Reservation *data; tries to point to, and so you try to access non initialized memory.
You need to initialize data:
tmp->data = new Reservation();
I'm trying to make program that reads from a file and adds to a binary tree, but when I try to compile I get an error:
"Error 1 'treePersons::display' : cannot convert parameter 1 from 'Node *' to 'Person *[]'"
The error appears in the call to display() in main()
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Person{
int social;
int birthday;
string first;
string last;
string state;
double balance;
Person();
Person(int s, int b, string f, string l, string t, double a)
{
social = s;
birthday = b;
first = f;
last = l;
state = t;
balance = a;
}
};
struct Node{
Person data;
Node *left;
Node *right;
Node();
Node(Person x){
data = x;
left = NULL;
right = NULL;
}
};
class treePersons
{
protected:
public:
Node *root;
treePersons(){
root = NULL;
}
int fileName(Person *data[])
{
ifstream fin;
fin.open ("dbfile1.txt");
if (!fin.is_open())
cout << "File not found" << endl;
int i;
for(i = 0; i<100; i++)
while(fin.good())
{
fin >> data[i]->social >> data[i]->birthday >> data[i]->first >> data[i]->last >> data[i]->state >> data[i]->balance;
i++;
}
return i;
}
void add(Person *data[], Node*root)
{
int i = fileName(data);
if(root == NULL)
{
root = new Node();
}
for(int l = 0; l<i; l++)
{
if(data[i]->last == root->data.last)
{
if(data[i]->first != root->data.first)
{
if(data[i]->first < root->data.first)
{
add(data, root->left);
}
else if(data[i]->first > root->data.first)
{
add(data, root->right);
}
else if(data[i]->last == root->data.last && data[i]->first == root ->data.first)
{
cout << "already exists" << endl;
}
else if(data[i]->first < root->data.first)
{
add(data, root->left);
}
else if(data[i]->first > root->data.first)
{
add(data, root->right);
}
}
}
}
}
void printAlphabetically(Node *root)
{
if (root != NULL)
{
printAlphabetically(root->left);
cout << root->data.last << endl;
printAlphabetically(root->right);
}
return;
}
void display(Person *data[],Node *root)
{
add(data,root);
printAlphabetically(root);
};
};
struct State{
string state;
Person data;
State* left;
State * right;
State();
State(Person x)
{
data = x;
left = NULL;
right = NULL;
}
};
class treeState{
protected:
State *root;
public:
treeState()
{
root = NULL;
}
};
void main(){
treePersons T;
T.display(T.root->data,T.root);
}
It's very simple to see what's wrong with your code. You have the following:
treePersons T;
T.display(T.root->data, T.root);
Let's have a look at what a treePersons is:
class treePersons
{
Node *root;
...
};
It contains a single member: a Node. A Node is:
struct Node
{
Person data;
Node *left;
Node *right;
...
};
Your treePersons::display() function has the following signature:
void display(Person *data[], Node *root)
And you are passing a t.root->data (a Person) and t.root (a Node*)
The problem is you are attempting to pass a Person as a Person*[] which just isn't going to happen. There's no way to make that Person into a Person[], and you probably meant to make display take a Person* pointer, which will allow you pass a single Person or a container of Person: void display(Person* data, Node* root);
Of course, doing so will lead you down a big trail of problems as #R Sahu pointed out in the comments (most of your functions take a Person*[]. The solution here is to rethink what you are doing, and as #R Sahu suggests start much smaller and build up your program from there.
Consider also using std::vector when you need containers, and std::unique_ptr or std::shared_ptr where you require pointers (otherwise just use objects!). Also read (really read) the compiler output. It's telling you what the problem is, you just need to read.