I need to make it so the user can put in words until they hit enter 'xxx'. this part is already done, but then i need the list to always be in alphabetical order. I cant figure out how to do this part. I have to make it in alphabetical order in the function that adds a new node, not the function that prints the nodes to the screen. this is because it is for a class and is required.
Node header file:
struct Node
{
string word;
struct Node *next;
};
The function prototype header file:
Node *add_node(Node *list, const string &s);
Node *del_node(Node *list, const string &s);
void deallocate(Node *list);
void print(Node *list);
The code that gets compiled:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "Node.h"
#include "funcs.h"
int main()
{
struct Node *list = 0; // list is a pointer to struct Node
cout << "please enter a few words (xxx to terminate list):\n";
string s; // s is a string object
while (cin >> s) // read a string into s
{
if (s == "xxx")
{
break; // terminate loop when string is equal to "xxx"
}
// add s to list in alphabetical order
list = add_node(list, s);
cout << "\nlist:\n";
print(list);
cout << '\n';
}
cout << "\nhere is the list:\n";
print(list);
cout << '\n';
cout << "please enter word words to delete from the list (xxx to terminate):\n";
while (cin >> s)
{
if (s == "xxx")
{
break; // terminate loop when string is equal to "xxx"
}
// delete first node containing string s
//list = del_node(list, s);
cout << "\nlist:\n";
print(list);
cout << '\n';
}
cout << "\nthe final list:\n";
print(list);
cout << '\n';
// deallocate the linked list
cout << "\ndeallocating the list:\n";
deallocate(list);
cout << "\nall done!\n";
return 0;
}
The code with the functions:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "Node.h"
Node *add_node(Node *list, const string &s)
{
struct Node *n = new struct Node;
n->word = s; // copy string s to word
n->next = list;
// add node n to the list
// the list should always be in ascending alphabetical order
list = n;
return list; // returning pointer to beginning of the list
}
Node *del_node(Node *list, const string &s)
{
// delete node in the list that contains s
// the list should always be in ascending alphabetical order
// if s does not appear in the list, there is nothing to do
// if s appears multiple times in the list, delete the first occurrence
Node *lastp = 0;
Node *p = list;
for (; p; p = p->next)
{
if (p->word == s)
{
lastp->next = p->next;
delete p;
break;
}
lastp = p;
}
return list; // returning pointer to beginning of the list
}
void deallocate(Node *list)
{
for (struct Node *p = list; p;)
{
struct Node *tmp = p; // remember current pointer
p = p->next; // advance p to the next node
delete tmp; // deallocate tmp
// OK to print pointers tmp and p
cout << "deallocated\t" << tmp << "\tnext is\t" << p << '\n';
}
}
void print(Node *list)
{
for (struct Node *p = list; p; p = p->next)
{
cout << p << '\t' << setw(8) << p->word
<< '\t' << "next:" << '\t' << p->next << '\n';
}
}
All the code probably isnt required to answer the question but i figured i'd include it.
Try the following function add_node shown in the demonstrative program below. Take into account that you need to define one more function that will free all allocated memory in the list.
#include <iostream>
#include <string>
struct Node
{
std::string word;
struct Node *next;
};
Node *add_node( struct Node *list, const std::string &s )
{
struct Node *prev = nullptr;
struct Node *current = list;
while ( current && !( s < current->word ) )
{
prev = current;
current = current->next;
}
if ( prev == nullptr )
{
list = new Node { s, list };
}
else
{
prev->next = new Node { s, prev->next };
}
return list;
}
void print_list( const struct Node *list )
{
for ( ; list != nullptr; list = list->next ) std::cout << list->word << ' ';
}
int main()
{
struct Node *list = nullptr;
// for ( const std::string &s : { "B", "X", "A", "C", "F", "G" } )
for ( const char *s : { "B", "X", "A", "C", "F", "G" } )
{
list = add_node( list, s );
}
print_list( list );
std::cout << std::endl;
return 0;
}
The output is
A B C F G X
Related
I created this program that should check the string entered by user in form of characters using doubly linked lists in C++, however I got stuck at the last point in which I should compare the original word with the reversed one to see if the two words are palindrome or not, how to store the content of function display() and reverse() to a string variable so that I can return the value and compare them?
Also, reverse() function doesn't display the reversed word
This is my code:
#include <iostream>
using namespace std;
class Storage {
public:
char lett;
Storage* next;
Storage* prev;
};
void push(char lett1, Storage** head) {
Storage* n = new Storage();
n->lett = lett1;
n->next = NULL;
if (*head == NULL) {
*head = n;
}
else {
n->next = *head;
*head = n;
}
}
void display(Storage* head, int no) {
Storage* s = head;
while (head != NULL) {
int i = 0;
cout << head->lett;
s = head;
head = head->next;
}
}
void reverse(Storage* tail) {
Storage* t = tail;
// Storage* original= tail;
while (t != NULL) {
cout << t->lett;
t = t->prev;
}
}
/*
string checkPalindrome() {
string check;
if ()
check == "Yes";
else
check == "No";
return check;
}
*/
int main() {
Storage* head = NULL; Storage* tail = NULL;;
char lett;
int size;
string result;
cout << ":: Palindrome Program ::\n" << endl;
cout << "Enter total character: ";
cin >> size;
cout << "Enter character: ";
for (int i=0; i < size; i++) {
cin >> lett;
push(lett, &head);
}
cout << "Your word: ";
display(head, size); //compare content of this
cout << "\nReversed word: ";
reverse(tail); // with this
/*
result = checkPalindrome();
cout << "Palindrome: " << result << endl;
*/
return 0;
}
You have some bugs in your code. First of all my tip is that you need to make a class/struct which will hold the head and tail of your list. For example:
class DLList{
public:
NODE *head;
NODE *tail;
};
Also, as you can see you should have a class for your list nodes, and every node should have a pointer to the next node, and to the node before. Don't forget to make the first node previous pointer to point to NULL, and also the last nodes next pointer. Some other things I noticed is that you forgot to deallocate the dynamic/heap memory. Fix that with using 'free' or consider using smart pointers, so you don't have any memory leaks. At the end, try to avoid using namespace std;. It is considered a bad habit, due to bad performance. Hope it helped you. Here is the not optimized code snippet.
#include <iostream>
using namespace std;
class Storage {
public:
char lett;
Storage* next;
Storage* prev;
};
void push(char lett1, Storage** head, Storage **tail) {
Storage* n = new Storage();
n->lett = lett1;
n->next = NULL;
n->prev = NULL;
if (*head == NULL) {
*head = n;
*tail = n;
}
else {
n->next = *head;
(* head)->prev = n;
*head = n;
}
}
std::string display(Storage* head) {
Storage* s = head;
std::string org = "";
while (s != NULL) {
org += s->lett;
s = s->next;
}
return org;
}
std::string reverse(Storage* tail) {
Storage* t = tail;
std::string rev = "";
// Storage* original= tail;
while (t != NULL) {
rev += t->lett;
t = t->prev;
}
return rev;
}
bool checkPalindrome(Storage* head, Storage* tail) {
return display(head) == reverse(tail);
}
int main() {
Storage* head = NULL; Storage* tail = NULL;;
char lett;
int size;
cout << ":: Palindrome Program ::\n" << endl;
cout << "Enter total character: ";
cin >> size;
cout << "Enter character: ";
for (int i = 0; i < size; i++) {
cin >> lett;
push(lett, &head,&tail);
}
cout << "Your word: ";
cout<<display(head)<<endl; //compare content of this
cout << "\nReversed word: ";
cout<<reverse(tail)<<endl; // with this
cout << "\nPalindrome: " << checkPalindrome(head, tail) << endl;
return 0;
}
If you want to build a string with the characters in the linked list, you can use the std::string::operator+= to concatenate the single characters together.
For instance, considering your display function:
void display(Storage* head, int no) {
Storage* s = head;
while (head != NULL) {
int i = 0;
cout << head->lett;
s = head;
head = head->next;
}
}
instead of using cout << head->lett to print a single character, just concatenate that character to the result string using string::operator+=:
// Assume: std::string result
result += head->lett;
You could write a function that takes the linked list of characters as input, and returns a std::string, along these lines:
std::string ToString(const Storage* head) {
std::string result;
// For each node in the linked list
while (...) {
// Append current node's character to the result string
result += currentNode->lett;
}
return result;
}
I am making a program that reads a .CSV file and loads it into binary search trees to sort its content, since I managed to load the .CSV and implement the tree however the content of the file is:
1, Peter, 230151515
5, Merrick, 25551561
7, Lucky, 4301616199
2, luis, 2589191919
16, Alfredo, 25891919
8, Linda, 129616919
I am using the first data of each row as a key and with the code that I carry, it orders it correctly, however I want it to show the rest of the row, not just the key, someone could tell me how I could do that, to show all the data of each key ordered.
output:
1
2
5
7
8
16
What I want to print would be something like:
1 Peter 230151515
2 Luis 2589191919
5 Merrick 25551561
7 Lucky 4301616199
8 Linda 129616919
16 Alfredo 25891919
Someone to help me correct my mistake please.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node {
int key;
string name;
int num;
struct node *left, *right;
};
vector<node> persons;
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
//cout<<"\t";
inorder(root->left);
printf("\t%d\n", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
struct node * minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
}
int main()
{
struct node *root = NULL;
ifstream fin("data.txt");
if (!fin)
{
cout << "File not open\n";
return 1;
}
string line;
const char delim = ',';
while (getline(fin, line))
{
istringstream ss(line);
node person;
ss >> person.key; ss.ignore(10, delim);
getline(ss, person.name,delim);
ss >> person.num;
if (ss)
persons.push_back(person);
}
for (unsigned int i=0; i< persons.size(); i++)
{
cout << setw(5) << persons[i].key<< setw(20) << persons[i].name<< setw(15) << persons[i].num << '\n';
root = insert(root, persons[i].key);
insert(root, persons[i].key);
}
cout << "\n\nInorder:\n";
// cout<<node.name;
inorder(root);
/*/cout<<"\nDelete 50\n";
root = deleteNode(root, 50);
cout<<"Inorder traversal of the modified tree \n";
inorder(root);/*/
/*
insert(root, 80);*/
return 0;
}
When you are printing out the key, you can also print out the other information of each node. Here's a solution using cout:
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
std::cout << root->key << " " << root->name << " " << root->num << "\n";
inorder(root->right);
}
}
There were some main problems and some other problems. The main problems were that you didn't store and print all data. I did the following:
Clean up includes
Remove using namespace std;
Rename struct node to Node
Add a struct for Person next to the struct for Node
Add a constructor for Person to Node
Make insert a method
Use smart pointers. One problem of dynamic memory allocation is that you have to clean up but you didn't
#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
#include <vector>
struct Person {
int key;
std::string name;
int num;
};
struct Node : Person {
Node(const Person &person) : Person(person) {}
std::unique_ptr<Node> left, right;
void insert(const Person &person);
};
void Node::insert(const Person &person) {
/* recur down the tree */
if (key > person.key) {
if (left)
left->insert(person);
else
left = std::make_unique<Node>(person);
} else if (key < person.key) {
if (right)
right->insert(person);
else
right = std::make_unique<Node>(person);
}
}
std::vector<Person> persons;
void inorder(Node *root) {
if (root) {
// cout<<"\t";
inorder(root->left.get());
std::cout << '\t' << root->key << ' ' << root->name << ' ' << root->num << '\n';
inorder(root->right.get());
}
}
Node *minValueNode(Node *node) {
Node *current = node;
/* loop down to find the leftmost leaf */
while (current && current->left) current = current->left.get();
return current;
}
int main() {
std::unique_ptr<Node> root;
std::ifstream fin("data.txt");
if (!fin) {
std::cout << "File not open\n";
return 1;
}
std::string line;
const char delim = ',';
while (std::getline(fin, line)) {
std::istringstream ss(line);
Person person;
ss >> person.key;
ss.ignore(10, delim);
std::getline(ss, person.name, delim);
ss >> person.num;
if (ss) persons.push_back(person);
}
for (unsigned int i = 0; i < persons.size(); i++) {
std::cout << std::setw(5) << persons[i].key << std::setw(20)
<< persons[i].name << std::setw(15) << persons[i].num << '\n';
if (!root) root = std::make_unique<Node>(persons[i]);
else root->insert(persons[i]);
}
std::cout << "\n\nInorder:\n";
// cout<<node.name;
inorder(root.get());
/*/cout<<"\nDelete 50\n";
root = deleteNode(root, 50);
cout<<"Inorder traversal of the modified tree \n";
inorder(root);/*/
/*
insert(root, 80);*/
return 0;
}
#include "node.h"
#include <iostream>
// List class
class List
{
node *head; // head is an object that stores the address of the first node
public:
// constructor that initializes every list to null
List()
{
head = NULL;
}
// prtototype of the list member functions
void Print();
void Insert(float sal, int en);
void Delete(float sal, int en);
};
//linklist.h above
#include "linklist.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/**
* Append a node to the linked list
*/
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(NULL);
// Create a temp pointer
node *tmp = head;
if ( tmp != NULL )
{
// Nodes already present in the list
// Parse to end of list
/*while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}*/
// Point the last node to the new node
tmp->setNext(head);
}
else
{
// First node in the list
head = newNode;
}
}
/**
* Delete a node from the list
*/
void List::Delete(float salary, int data)
{
// Create a temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL )
{
delete tmp;
head = NULL;
}
else
{
// Parse thru the nodes
node *prev;
do
{
if ( tmp->Epnum() == data && tmp->Salary()== salary )
break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->setNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
/**
* Print the contents of the list
*/
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL )
{
cout << tmp->Salary() + tmp->Epnum();
cout << " --> ";
cout << "NULL" << endl;
}
else
{
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
//linlist.cpp above
#include <iostream>
#include <cstdlib>
#include "linklist.h"
using namespace std;
void menu(List &);
int main()
{
// New list
List list;
menu(list);
return 0;
}
void menu(List &list)
{ char choice;
int item;
float salary;
do{
system("CLS"); // use #include <cstdlib>
cout << "\t\t\tMain Menu\n\n";
cout << "\tInsert{A}\n";
cout << "\tDelete\n";
cout << "\tPrint{P}\n";
cout << "\tExit\n";
cout << "\t\t What? ";cin >>choice;
choice = toupper(choice);
cin.ignore();
switch (choice)
{ case 'A':
cout << "Enter Employee numbers to insert and salary : "; cin >> item; cin>>salary;
list.Insert(salary, item);
cout << item<< " Inserted \n"; cin.get();
break;
/*case 'D':
cout << "Enter Item to Delete : "; cin >> item;
list.Delete(item);
cout << item<< " Deleted\n";cin.get();
break;*/
case 'P':
list.Print();cin.get();
break;
}
}while (choice != 'E');
}
//main.cpp above
//node.h
//#ifndef NODE_H
#define NODE_H
//node class
class node {
int epnum;
float salary;
node* next;
public:
node()
{} //null constructor
//stores argument passed in func.
void SetData(float _salary, int _epnum){
salary = _salary;
epnum = _epnum;
}
//stores in next the address of the next node
void setNext (node* anext){
next = anext;
}
//returns epnum stored
int Epnum(){
return epnum;
}
float Salary(){
return salary;}
//returns addres of next node
node* Next(){
return next;
}
};
//node.h above
I need to create a linked list that inserts a node at the front of the list as a program and of course print it out. I am unable to insert the node at the front of the list for some reason and I run into an infinte loop while trying to print it. It does something but I do not know exactly what. Please help.
In List::Insert, you have:
node *tmp = head;
followed by
tmp->next = head;
Hence, you have circular link in the object. Its next points to itself. This leads to infinite loop in the print function.
What you need is very simple:
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(head);
head = newNode;
}
In your insert method when you have 1 element already present tmp is set to head, then tmp->setNext(head); will create a reference to itself. This is the reason of infinite loop in your print method. Try the following insert code instead.
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(head);
head = newNode;
}
I would also note that in you print method there is no corner case for the list with 1 element. Your loop will perfectly handle this case. You will get the same result if you omit One node in the list branch.
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(NULL);
//set the newNode next to point to head
newNode->setNext(head);
//set the new head as the newNode
head = newNode;
// Create a temp pointer
//node *tmp = head;
/*if ( tmp != NULL )
{
// Nodes already present in the list
// Parse to end of list
/*while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->setNext(head);
}
else
{
// First node in the list
head = newNode;
}*/
}
/**
* Delete a node from the list
*/
void List::Delete(float salary, int data)
{
// Create a temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL )
{
delete tmp;
head = NULL;
}
else
{
// Parse thru the nodes
node *prev;
do
{
if ( tmp->Epnum() == data && tmp->Salary()== salary )
break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->setNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
/**
* Print the contents of the list
*/
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL )
{
cout << tmp->Salary() + tmp->Epnum();
cout << " --> ";
cout << "NULL" << endl;
}
else
{
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
nvm i was tired when i was writting this code and just realized what I did wrong.
I am checking data of head node in constructor and its ok, but when i am checking it in the function then its gone. Why is it such a ghost this pointer ?
header for queue
#ifndef QUEUE_H
#define QUEUE_H
#include "Node.h"
#include "LinkedList.h"
class Queue
{
public:
Queue();
~Queue();
void addTail(Node* newNode);
Node* deleteHead();
int isEmpty();
void PrintQueue(char* outName);
protected:
private:
Node* head;
Node* tail;
LinkedList* myList;
};
#endif // QUEUE_H
queue class
#include "Queue.h"
Queue::Queue()
{
//constructor
myList = new LinkedList();
head = myList->getHead();
std::cout<<" head here is " << head->getData()<<std::endl; //
std::cout<<" next after head is " << head->getNext()->getData()<<std::endl; //
tail = myList->getTail();
}
void Queue::addTail(Node* newNode)
{
std::cout<<"inserting before tail (queue)"<<std::endl;
std::cout<<"new node is " << newNode->getData() <<std::endl;
std::cout<<" head here is " << myList->getHead()->getData() <<std::endl;
myList->insertLast(newNode);
}
Node* Queue::deleteHead()
{
if(isEmpty()){
std::cout<<"Queue is empty. Cannot remove anything anymore. Add more data to the queue!"<<std::endl;
return head;
}
return myList->removeFirst();
}
int Queue::isEmpty()
{
return myList->isEmpty();
}
void Queue::PrintQueue(char* outName)
{
Node* cur = head->getNext(); //store current node to keep track. Set it to one after the head
std::ofstream outfile;
outfile.open(outName, std::ios::app);
outfile << "Printing the values of the queue: ";
std::cout<<"Printing the values of the queue: "<<std::endl;
while(cur != tail)
{
std::cout<< cur->getData()<<", "<<std::endl; //print to the console
outfile << cur->getData() <<", "; //print to file
}
outfile.close();
}
Queue::~Queue()
{
//destructor
delete myList;
}
Main function
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
//#include "Stack.cpp"
#include "LinkedList.h"
//#include "LinkedList.cpp"
#include "Node.h"
//#include "Node.cpp"
#include "HashTable.h"
//#include "HashTable.cpp"
using namespace std;
int main( int argc, char* argv[] )
{
//specifying the in and out files
char* inFileName = argv[1];
char* outFileName = argv[2];
std::fstream infile (inFileName) ; // input file
//open the input file and find largest integer
int num;
int largest = 0;
Stack * myStack = new Stack();
char buffer[33]; //create a buffer for using with itoa
std::ofstream outfile;
outfile.open(outFileName);
if ( !infile.is_open() ) //check if input file is open
cout<<"Could not open file\n";
else {
while (infile >> num) { // read file int by int and check if current is not the largest
if(num > largest) largest = num;
Node *newNode= new Node(itoa(num,buffer,10));
myStack->push(newNode);
}
}
std::cout<< std::endl;
infile.close(); //close files that you read to avoid memory leaks
myStack->PrintStack(outFileName);
HashTable* hashBrown = new HashTable();
int currentDigit = 0;
int currentTable = 0;
int numOfDig =0; //stores number of digits of the largest number
string maxNum = itoa(largest,buffer,10);
numOfDig = maxNum.length();
std::cout<< "Num of digits " << numOfDig << std::endl;
Node* current;
while(!myStack->isEmpty())
{
current = myStack->pop();
std::cout<< "pop from stack element " << current->getData() << std::endl;
string str = current->getData();
int index = atoi(&str.back());
std::cout<< "insert at index " << index << std::endl;
std::cout<< "inserting data: "<< current->getData()<< " at index:" << index << std::endl;
hashBrown->myQueues[index].addTail(current);
}
hashBrown->printHashTable(outFileName);
delete myStack;
delete hashBrown;
outfile.close();
std::cout<< "finishing program " << std::endl;
return 0;
}
Linked List
#include "LinkedList.h"
#include "Node.h"
LinkedList::LinkedList()
{
//constructor
head = new Node("head"); //dummy variable
tail = new Node("tail"); //dummy variable
head->setNext(tail);
}
void LinkedList::insertNode(Node* newNode, Node *position)
{
newNode->setNext(position->getNext()); // set its pointer to position
position->setNext(newNode);
}
void LinkedList::insertFirst(Node* newNode)
{
std::cout<<"head here is "<< head->getData() <<std::endl; //
insertNode(newNode, head);
}
void LinkedList::insertLast(Node* newNode)
{
std::cout<<"inserting before tail (LinkedList)"<<std::endl; //
Node* cur = head;
std::cout<<"outside the loop "<< cur->getData() <<std::endl; //
std::cout<<"current node is "<< cur->getData() <<std::endl; //
while(cur->getNext() != tail) //iterate until you reach one before tail
{
std::cout<<"current node is "<< cur->getData() <<std::endl; //
cur = cur->getNext();
}
std::cout<<"inserting before tail"<<std::endl; //
insertNode(newNode, cur); //insert at the end before the dummy tail
}
Node* LinkedList::removeFirst()
{
if(isEmpty())
{
return head;
}
Node* result = head->getNext(); //store pointer to Node that you need to return
head->setNext(result->getNext());
return result;
}
Node* LinkedList::getTail()
{
return tail;
}
Node* LinkedList::getHead()
{
return head;
}
int LinkedList::isEmpty()
{
return head->getNext() == tail;
}
std::string LinkedList::printList(){
Node *current = head;
std::string str;
//append pieces of string to create new line
while(current != tail){
str.append (" --> (");
str.append ( current->getData());
str.append (",");
str.append (current->getNext()->getData());
str.append (")") ;
current = current->getNext();
}
std::cout<<str<<std::endl; //display new
return str; //return string containing next line that will be written to a file in main
}
LinkedList::~LinkedList()
{
//destructor
Node *current = head;
while( current->getNext() != tail ) { //go through whole linked list and delete each node to free memory
Node* next = current->getNext();
delete current;
current = next;
}
delete current;
delete tail;
}
So everything works fine until it tries to access the head or its data in the addTail() function of Queue.
You can use gdb to debug your program, find the bugs and fix them. If the program cored, you can use backtrace to see what caused the core.
Set breakpoint at the lines you think program behavior wired, you can also let the program run step by step.
Run the program and print variable value to see whether the variable value is expected. If the variable is a pointer, you can also print the content it referenced.
A brief gdb tutorial
I have the following code but it gives an error on the line - e = list.first();(towards the end of the code). It says that it cannot convert Node to char can someone tell me how to get return the first value from the linked list in the variable e.
Thanks for all the help:)
//
// main.cpp
// cprogram1
//
#include <iostream>
using namespace std;
class Node {
char data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
char Data() { return data; };
Node* Next() { return next; };
};
// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Print();
void Append(int data);
void Delete(int data);
Node * First() const;
};
/**
* Print the contents of the list
*/
void List::Print() {
// Temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
/**
* Append a node to the linked list
*/
void List::Append(int data) {
// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
// Create a temp pointer
Node *tmp = head;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}
/**
* Delete a node from the list
*/
void List::Delete(int data) {
// Create a temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->SetNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
Node * List::First() const {
Node *tmp = head;
return head;
}
int main ()
{
char c;
int t = 0;
char e;
List list;
while(t==0)
{
cout << "Please enter your command";
cin >> c;
if(c=='c')
{
cout << "You will need to enter 6 letters, one after the other";
cout << "Please enter the first letter";
cin >> e;
list.Append(e);
cout << "Please enter the second letter";
cin >> e;
list.Append(e);
cout << "Please enter the third letter";
cin >> e;
list.Append(e);
cout << "Please enter the fourth letter";
cin >> e;
list.Append(e);
cout << "Please enter the fifth letter";
cin >> e;
list.Append(e);
cout << "Please enter the sixth letter";
cin >> e;
list.Append(e);
list.Print();
list.Delete('b');
list.Print();
e = list.First();
}
}
}
Lets take a look to your program : tested here :
#include <iostream>
using namespace std;
class Node {
char data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
char Data() { return data; };
Node* Next() { return next; };
};
// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Print();
void Append(int data);
void Delete(int data);
Node * First() const;
};
/**
* Print the contents of the list
*/
void List::Print() {
// Temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
/**
* Append a node to the linked list
*/
void List::Append(int data) {
// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
// Create a temp pointer
Node *tmp = head;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}
/**
* Delete a node from the list
*/
void List::Delete(int data) {
// Create a temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->SetNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
Node * List::First() const {
Node *tmp = head;
return head;
}
int main ()
{
char c;
int t = 0;
char e;
List list;
while(t==0)
{
cout << "Please enter your command";
c = 'c';
//cin >> c;
if(c=='c')
{
cout << "You will need to enter 6 letters, one after the other";
cout << "Please enter the first letter";
list.Append('e');
cout << "Please enter the second letter";
//cin >> e;
list.Append('a');
cout << "Please enter the third letter";
//cin >> e;
list.Append('b');
cout << "Please enter the fourth letter";
//cin >> e;
list.Append('f');
cout << "Please enter the fifth letter";
//cin >> e;
list.Append('h');
cout << "Please enter the sixth letter";
//cin >> e;
list.Append(e);
list.Print();
list.Delete('b');
list.Print();
e = list.First()->Data();
t=1;
}
}
}
As you can see, e = list.First() return a pointer to a Node, not a char. You have implemented a function that return a char which is Data(). Thus, you should use e = list.First()->Data();.
Also, your loop is infinite because t is always 0. I just put t=1 to stop your loop and see the result. (if you use this code, don't forget to uncomment the cin and remove c = 'c')
Hope that helps you.
When I run your code I get the error:
main.cpp: error: assigning to 'char' from incompatible type 'Node *'
e = list.First();
^ ~~~~~~~~~~~~
which I think explains it.
Try this:
e = list.First()->Data();