C++ Priority Queue Class with Linked List - c++

I am having two issues with my c++ code (The test file is below):
I can't seem to figure out why its not breaking out of the while loop, when running, its stuck on the loop "7 versus 325".
So it should go into the next node of temp, which would be null, and then jump into the section where it adds it to the end of the queue. But its just looping and looping.
My second issue is with the the function I have commented out, queue1.back, whenever that is ran, it just errors out and gives what appears to be the address, but the .front() function works just fine.
The Test File I am working with is like this:
89 Alex
325 Rob
72 Joy
91 Bob
using namespace std;
class Person
{
friend class Pqueue;
public:
int priority;
string name;
};
class PQueue
{
friend class Person;
private:
//Structure for my linked list.
typedef struct node {
Person data;
struct node *next;
}Node, *NodePtr;
Node *head, *tail;
public:
//Prototype Functions
PQueue(void); //Initializer function
bool empty(void); //Test if empty
int size(void); //Return size
void enqueue(Person *); //Insert Node
void dequeue(void); //Remove Node
Person* front(void); //Access Next Node
Person* back(void); //Access last node
};
PQueue::PQueue()
{
head = NULL;
tail = NULL;
}
bool PQueue::empty(){
return (head == NULL);
}
void PQueue::enqueue(Person *myPerson){
NodePtr np = (NodePtr) malloc(sizeof(Node));
np->data = *myPerson;
np->next = NULL;
if(empty())
{
cout << "Making into creating the first node, of the linked list" <<endl;
head = np;
tail = np;
}
else { //Queue has more the one node
Node* temp = head;
if(np->data.priority > temp->data.priority) //If the priority is greater then the rest.
{
head = temp; //Saving my head pointer
head->data = np->data; //Assigning new Data to the head pointer
head->next = temp; //Assigning the rest of the linked list back into head.
cout << "Making into creating the first node again, having to reassign." <<endl;
}
else{
//Searching where to place the node.
while(temp->data.priority > np->data.priority) //Searching if the next priority is higher then the passed.
{
cout << "Inside the while loop: " << np->data.priority << " versus "<<temp->data.priority <<endl;
if(temp->next == NULL)
break;
temp = temp->next;
}
if(temp->next == NULL && np->data.priority < temp->data.priority) //Inserting at the end.
{
cout << "Making into creating the last node" <<endl;
tail->next = np;
cout << "Passing the function of creating the last node" <<endl;
}
else //Inserting into the middle of the function.
{
cout << "Inserting in the middle of the queue" <<endl;
np->next = temp->next;
temp->next = np;
}
}
}
}
void PQueue::dequeue(){
if(empty()){
cout << "\nAttempt to remove from an empty list." << endl;
exit(1);
}
Person hold = head->data;
NodePtr temp = head;
head=head->next;
if (head == NULL) tail = NULL;
free(temp);
}
Person* PQueue::front(){
//Person &temp = head->next->data;
//Person &temp = head->data;
Person &temp = head->data;
return &temp;
}
Person* PQueue::back(){
if(empty()){
cout << "\nNo entries in list." << endl;
exit(1);
}
Person &temp = tail->data;
return &temp;
}
int main() {
cout << "Starting main" << endl;
PQueue queue1; //Creating my queue.
cout << "Created Queue" << endl;
Person tempPerson;
ifstream inFile;
inFile.open("/tmp/temp");
cout << "going into while loop" << endl;
while (inFile >> tempPerson.priority >> tempPerson.name){
cout << "The priority is " << tempPerson.priority << " the name is " << tempPerson.name <<endl;
queue1.enqueue(&tempPerson);
}
//Testing Section, trying to get .front and .back to work.
Person *testPerson;
testPerson = queue1.front();
cout << "The TEST priority is " << testPerson->priority << " the TEST name is " << testPerson->name <<endl;
/**
Person *tailPerson;
testPerson = queue1.back();
cout << "The TEST priority is " << tailPerson->priority << " the TEST name is " << tailPerson->name <<endl;
**/
queue1.dequeue();
queue1.dequeue();
queue1.dequeue();
return 0;
}

When you add a new head entry to a non-empty list, you're mistakenly setting the next pointer to point right back at the node it's in, rather than setting it to point at the rest of the linked list like you intended.
head = temp; //Saving my head pointer
head->next = temp; //Assigning the rest of the linked list back into head.

Related

Print a linkedlist

Given the name of a node, this function should search the linked list; if its found inside, then return a pointer that points to that node, otherwise return null. Note: I am certain I have written this function successfully.
// getNode
Node *LinkedList::getNode(string name)
{
Node *temp = head;
while (temp != NULL)
{
if (temp->name.compare(name) == 0)
return temp;
temp = temp->next;
}
return NULL;
}
Given a node, this function prints: teamName(winScore-loseScore) on screen. Examples: UCLA(25-13) or Texas A&M(31-25). Note: I am sure I have written this function successfully.
// printNode
void LinkedList::printNode(Node *node)
{
if (node == NULL)
return;
else {
cout << node->name << "(" << node->winScore;
cout << "-" << node->loseScore << ")";
}
}
Given a team name, this function is supposed to print all the nodes in its adjacency list one-by-one in the following format (NOTE: the following is just one example!) This is where I think I am wrong.
Missouri University beat: New Mexico(52-23), Salisbury (48-31), Virginia (34-9)
void LinkedList::printList(string name)
{
if (head == NULL)
cout << "\n Empty list" << endl;
else {
Node *temp = head;
while (temp != NULL)
{
cout << temp->name << " beat: " << temp->name << endl; // Is this right?
temp = temp->next;
}
}
}
I'm guessing that this is close to what you want:
void LinkedList::printList(string name)
{
// find the node for the name you supply
// (or else I don't understand why 'name' is supplied to this function)
Node *temp = getNode(name);
if (temp) { // node with name found
if (temp->next) { // there's at least one adjacent node
cout << temp->name << " beat: ";
while ((temp = temp->next) != nullptr) {
printNode(temp);
if (temp->next) cout << ", ";
};
cout << "\n";
} else { // no adjacent nodes
cout << temp->name << " did not beat anyone\n";
}
}
}

sort strings in alphabetical order with linked lists in c++

I am doing an assignment of linked list of strings. Everything seems to work fine until I get to the sorting part. I am using classes as well. I already have 2 functions. The first one minimum(string) where it returns the string that would come first in alphabetical order.
This functions works fine. I also have a function remove(string) where it removes a node containing the given string from the linked list. It returns true if successful, otherwise false (if string was not in the list), in this case I have to erase the string returned by the minimum(string) function. The function I am having trouble is sort().
It wants me to define a StringNode pointer to be the head of a new list (the empty list) then I need a loop where I will call the functions of minimum(string) and remove(string). Inside the loop I also need to insert this node into the proper position in the new list (append it to the end). The old head pointer (now empty) must point to the new list.
I am very confused about this, so far I have this:
void StringList::sort()
{
StringNode *newList = new StringNode;
newList = NULL;
string mini;
bool removed;
while (newList->next != NULL)
{
StringNode *newList2 = new StringNode;
StringNode *p = head;
mini = minimum();
p->data = mini;
p->next = NULL;
newList2 = newList2->next;
newList2->next = p;
removed = remove(mini);
newList = newList2;
}
}
How I understand is: I need to create a new node which will be an empty list meaning newList->next = NULL; then on the loop I need to create another new node, and a pointer that will point to the head of new node inside the loop. I need to make the value given by the minimum be stored in the pointer p and the pointer to point back to the new node.
Any help will be greatly appreciated. Thanks!
Here is the hole program.
// StringList.cpp
#include <iomanip>
#include <iostream>
#include <sstream>
#include "StringList.h"
using namespace std;
//******************************************************************************
// StringList: creates an empty list
//******************************************************************************
StringList::StringList()
{
head = NULL;
}
//******************************************************************************
// StringList: deallocates all the nodes in StringList
//******************************************************************************
StringList::~StringList()
{
StringNode *p;
StringNode *n;
p = head;
while (p != NULL)
{
n = p->next;
delete p;
p = n;
}
}
//******************************************************************************
// count: returns the total number of nodes in the list.
//******************************************************************************
int StringList::count()
{
int count = 0;
StringNode *p;
p = head;
while ( p != NULL )
{
count++;
p = p->next;
}
return count;
}
//******************************************************************************
// add: adds a new node to the beginning of the list.
//******************************************************************************
void StringList::add(string movie)
{
StringNode *newNode = new StringNode;
newNode->data = movie;
newNode->next = head;
head = newNode;
}
//******************************************************************************
// remove: removes a node containing the string from linked list
// returns true if successful or false the string is not in the list
//******************************************************************************
bool StringList::remove(string movie)
{
StringNode *p = head;
StringNode *n = NULL;
while (p != NULL && p->data != movie )
{
n = p;
p = p->next;
if (p == NULL )
{
return false;
}
else if (p->data == movie)
{
n->next = p->next;
delete p;
return true;
}
}
}
//******************************************************************************
//display: Displays the strings in the list.
//******************************************************************************
void StringList::display()
{
StringNode *p;
p = head;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
//******************************************************************************
//minimum: return the string in alphabetical order
//******************************************************************************
string StringList::minimum()
{
StringNode *p = head;
string minimum = p->data;
while (p->next != NULL)
{
p = p->next;
if(minimum > p->data)
{
minimum = p->data;
}
}
return minimum;
}
//******************************************************************************
//sort: will call the minimum function and remove function
//******************************************************************************
void StringList::sort()
{
StringNode* newhead; // create a new head pointer
string mini;
bool removed;
//adding the first node to the new list.
StringNode *newnode = new StringNode;
mini = minimum(); // get the minimum from the existing linked list
newnode->data = mini;
newnode->next = NULL;
newhead=newnode; //add the minimum node to the new list(with the newhead)
StringNode *p = newhead;
while (head != NULL) // loop should run until there's no node left in the original list
{
StringNode *newnode = new StringNode;
mini = minimum(); // get the minimum from the existing linked list
newnode->data = mini;
newnode->next = NULL;
p->next=newnode; //add the minimum node to the new list(with the newhead pointer)
removed = remove(mini);
p=p->next;
}
head=newhead; //finally change the head pointer, so that the head now points to sorted list.
}
// StringList.h
#ifndef STRINGLIST_H_INCLUDED
#define STRINGLIST_H_INCLUDED
#include <string>
using namespace std;
class StringList
{
private:
struct StringNode // the Nodes of the linked list
{
string data; // data is a string
StringNode *next; // points to next node in list
};
StringNode *head; // the head pointer
public:
StringList();
~StringList();
int count();
void add(string);
bool remove(string);
void display();
string minimum();
void sort();
};
#endif // STRINGLIST_H_INCLUDED
//Driver.cpp
#include<iostream>
#include<iomanip>
using namespace std;
#include "StringList.h"
int main()
{
//testing StringList
StringList slist;
string movie1 = "Star Wars";
string movie2 = "Fargo";
string movie3 = "Back to the Future";
string movie4 = "Titanic";
// Testing add/display/count
cout << "Testing add/display/count: " << endl;
cout << "count is: " << slist.count() << endl;
slist.add(movie1);
slist.display();
cout << "count is: " << slist.count() << endl;
slist.add(movie2);
slist.display();
cout << "count is: " << slist.count() << endl;
slist.add(movie3);
slist.add(movie4);
slist.display();
cout << "count is: " << slist.count() << endl;
// Testing remove
cout << endl;
cout << "Testing remove: " << endl;
bool delResult;
delResult = slist.remove(movie4);
cout << "remove result movie4 = " << boolalpha << delResult << endl;
delResult = slist.remove(movie3);
cout << "remove result movie3 = " << boolalpha << delResult << endl;
delResult = slist.remove("Not There");
cout << "remove result Not There = " << boolalpha << delResult << endl;
cout << "display after remove: " << endl;
slist.display();
cout << "count is: " << slist.count() << endl;
//Testing minimum
cout << endl;
cout << "Testing minimum: " << endl;
cout << "Test minimum 1: " << endl;
slist.display();
cout << "minimum: " << boolalpha << slist.minimum() << endl;
cout << "Test minimum 2: " << endl;
slist.add(movie4);
slist.display();
cout << "minimum: " << boolalpha << slist.minimum() << endl;
cout << "Test minimum 3: " << endl;
slist.add(movie3);
slist.display();
cout << "minimum: " << boolalpha << slist.minimum() << endl;
//Testing sort and display
cout << endl;
cout << "Testing sort/display: " << endl;
slist.sort();
slist.display();
cout << endl;
cout << "Testing sort/display after add: " << endl;
slist.add("Jurassic Park");
slist.display();
cout << "now sorted: " << endl;
slist.sort();
slist.display();
}
your remove function has small fault. You didn't take care of the possibility of removing the first node.
bool StringList::remove(string movie)
{
StringNode *p = head;
StringNode *n = NULL;
if(p->data==movie) //added this condition
{
head=head->next;
delete p;
return true;
}
while (p != NULL && p->data != movie )
{
n = p;
p = p->next;
if (p == NULL )
{
return false;
}
else if (p->data == movie)
{
n->next = p->next;
delete p;
return true;
}
}
}
final sort function. The objective is to sort the existing list by using the minimum and remove functions and get the new sorted list. You need to get minimum from the existing list and keep adding them at the end of the new list. And finally change the head pointer to point it to the new list.
The while loop should run until the existing list becomes empty.
void StringList::sort()
{
string mini;
bool removed;
//adding the first node to the new list.
StringNode *newnode1 = new StringNode;
mini = minimum(); // get the minimum from the existing linked list
newnode1->data = mini;
newnode1->next = NULL;
removed =remove(mini);
StringNode *p = newnode1;
while (head != NULL) // the loop should run until the original list is empty
{
StringNode *newnode = new StringNode;
mini = minimum(); // get the minimum from the existing linked list
newnode->data = mini;
newnode->next = NULL;
p->next=newnode; //add the minimum node to the new list
removed = remove(mini);
p=p->next;
}
head=newnode1; //finally change the head pointer, so that the head now points to sorted list.
}

C++ Need help accessing values inside of linked list

class Node {
public:
Node();
void setNext(Node*);
private:
void* item;
Node* next;
};
void Node::setNext(Node* n)
{
next = n;
}
class List {
public:
List();
void addFirst(void*);
void reset();
void* getCurItem();
private:
Node* head;
Node* current;
};
void List::addFirst(void* obj)
{
Node *newNode = new Node(obj);
newNode->setNext(head);
head = newNode;
}
void List::reset()
{
current = head;
}
void* List::getCurItem()
{
return current->getItem();
}
Polynomial::Polynomial(std::ifstream& input){
List *polyCo = new List();
List *polyEx = new List();
bool exit = false;
double coefficient=0;
int exponent=0;
while(!exit && input.good()){
input >> coefficient;
if(coefficient != -9999.99){
input >> exponent;
cout << "Exponent before: " << exponent << endl;
cout << "Coefficient before: " << coefficient << endl;
int *ExPtr = &exponent;
double *CoPtr = &coefficient;
cout << "Exponent: " << *(ExPtr) << endl;
cout << "Coefficient: " << *(CoPtr) << endl;
polyCo->addFirst(ExPtr);
polyEx->addFirst(CoPtr);
cout << polyCo->getCurItem() << endl; //SEG FAULT
}
}
polyEx->reset();
polyCo->reset();
}
I am reading numbers from a file into two separate linked lists. I am getting a segmentation fault when attempting to access the value in either of the linked list.
First, I'm going to outline how I think my addFirst() functions works, because I could just be implementing it incorrectly. When addFirst() is called, a new node is created, and the data being read in is set equal to the member of the new node. The existing node in the list is pushed forward, and the new node created becomes the head. The two nodes are also linked.
There could be something else entirely wrong besides my implementation of the function, but at least it is a good place to start, along with the source of the segmentation fault.

Can not display stacks or Push

So I have a project for my C++ class and we are asked to create a library program using Stacks and Doubly Linked Lists. Now we cannot use the stack header file so everything from push, pop, display, etc. has to created by us.
I have the push function working, but the display function I cannot seem to get it to display more than just the lastNode stuct. When it tries to access previousNode, it crashes. Any ideas on what I'm doing wrong?
Also I can not get the pop function to work either. The program crashes as well when it runs.
void pushBook(BookStack *bkStack, BookNode *bkNode)
{
if(isEmpty(bkStack))
{
bkStack->lastNode = bkNode;
bkStack->lastNode->previousNode = NULL;
}
else
{
bkStack->lastNode->previousNode = bkStack->lastNode;
bkStack->lastNode = bkNode;
}
}
BookNode *popBook(BookStack *bkStack)
{
BookNode *temp = new BookNode;
if(isEmpty(bkStack))
{
temp = bkStack->lastNode;
return temp;
}
else if(bkStack->lastNode->previousNode == NULL)
{
temp = bkStack->lastNode;
bkStack->lastNode = NULL;
return temp;
}else
{
temp = bkStack->lastNode->previousNode;
bkStack->lastNode->previousNode = NULL;
bkStack->lastNode = temp;
return temp;
}
}
void displayStackElements(BookStack *bkStack)
{
BookNode *nodePtr = new BookNode;
nodePtr = bkStack->lastNode;
if(isEmpty(bkStack))
{
cout << "Book stack is empty." << endl;
}
else
{
while(nodePtr != NULL)
{
cout << "\nBook Name: " << nodePtr->bk.name << endl;
cout << "Author Name: " << nodePtr->bk.authorName << endl;
cout << "Page Numbers: " << nodePtr->bk.pagesNumber << endl;
cout << "ISBN: " << nodePtr->bk.isbn << endl;
cout << endl;
nodePtr = bkStack->lastNode->previousNode;
}
}
}
bool isEmpty(BookStack *bkStack)
{
bool status;
if(bkStack->lastNode == NULL)
status = true;
else
status = false;
return status;
}
void addBook(BookStack *bkStack)
{
BookNode *bkNode = new BookNode;
cout << "\nEnter the book name: ";
getline(cin, bkNode->bk.name);
cout << "Enter the Author's Name: ";
getline(cin, bkNode->bk.authorName);
cout << "Enter the ISBN: ";
cin >> bkNode->bk.isbn;
cout << "Enter the page numbers: ";
cin >> bkNode->bk.pagesNumber;
pushBook(bkStack, bkNode);
}
void removeBook(BookStack *bkStack)
{
BookNode *removedNode = new BookNode;
removedNode = popBook(bkStack);
if(removedNode == NULL)
{
cout << "\nNo books to remove." << endl;
}else
{
cout << endl << removedNode->bk.name << " was removed." << endl;
}
}
(edit) Sorry about that here's the structs from the main header file
struct Book
{
int isbn;
string name;
string authorName;
int pagesNumber;
};
struct BookNode
{
Book bk;
BookNode *previousNode;
};
struct BookStack
{
BookNode *lastNode = NULL;
};
I haven't done c++ in a while so if any of the statement below are wrong, tell me so !
Here what I see :
In displayStackElements:
nodePtr = bkStack->lastNode->previousNode; should be nodePtr = nodePtr->previousNode; otherwise, you will always display the previousnode of lastnode of bkStack in an infinite loop !
I don't know why you do this everywhere:
BookNode *nodePtr = new BookNode;
nodePtr = bkStack->lastNode;
You don't need a new BookNode if you are assigning it right after it ! You are creating a new BookNode in memory and then overwrite the pointer. This is a memory leak. You can simply do this:
BookNode *nodePtr = bkStack->lastNode;
In your pop function, the last else code should be this. You want to pop out the lastnode, not the previous node of the current last node.
temp = bkStack->lastNode->previousNode;
BookNode *lastNode = bkStack->lastNode
bkStack->lastNode->previousNode = NULL;
bkStack->lastNode = temp;
return lastNode;
In your push function, you are setting the previousNode of last node but after that, you were overwriting the lastNode with the node passed in parameter. Your list became broken because the lastNode->previousNode will always be emtpy. The else statement should be this.
bkNode->previousNode = bkStack->lastNode;
bkStack->lastNode = bkNode;
As you don't show the BookNode class definition or its constructor, I can only assume that you forget to clear the nodes previousNode pointer, which means that after you add the second second node then bkStack->lastNode->previousNode will be an uninitialized pointer.
Dereferencing uninitialized pointers leads to undefined behavior.

Templated Linked List giving wrong output when asked to print a node

I'm creating a database for school records for a project. I have a Student, Faculty, and Administrator class that all inherit things from a Person class. When I add the different objects to a Node, the information is stored in that Node (I see it via the debugger), however when I go to print a Node, I get
00266A88
instead of
Full Name: Reed
M Number: 999
Email:
and so on.
I'm just not sure what is causing the problem. Here is my method to print a node from the list:
template <typename T>
void TemplatedList<T>::printSpecific(int m_Number)
{
Node * Current = Head;
//If there is nothing in the list but the dummy head node, then return because there's nothing to print
if(Head->next == NULL)
{
cout << "Cannot print (M" << m_Number << "), NOT found!" << endl;
return;
}
else
Current = Current->next;
// While Current->next isn't equal to NULL, go through the list and see if the M-Numbers match. If they do, print the student and return
while(Current->next != NULL)
{
if(m_Number == Current->data->getM_Number())
{
cout << Current->data;
return;
}
else
{
Current = Current->next;
}
}
if(Current->next == NULL)
{
if(m_Number == Current->data->getM_Number())
{
cout << Current->data;
return;
}
else
{
cout << "Cannot print (M" <<m_Number << "), NOT found!" << endl;
return;
}
}
}
Here is the function to add one of the of the objects to the list:
template<typename T>
void TemplatedList<T>::addTemplatedList(T newAddition)
{
//Points to current node we're using
Node* Current = Head;
//Points to the node previous in the list to the current
Node* Previous = Head;
//Creates a new Node
Node* newNode = new Node;
//Assigns new Student information to new Node
newNode->data = newAddition;
// Check to see if the Head is only thing in the list. If it is, just place the new Node directly after the Head
if (Head->next == NULL)
{
Head->next = newNode;
newNode->next = NULL;
return;
}
else
{
while (Current->next != NULL)
{
if (newAddition->getM_Number() < Current->next->data->getM_Number())
{
newNode->next = Current->next;
Previous->next = newNode;
return;
}
else if (newAddition->getM_Number() == Current->next->data->getM_Number())
{
cout << "Person with M Number " << newAddition->getM_Number() << " not added because they are already in database." << endl;
delete newNode;
return;
}
Current = Current->next;
Previous = Previous->next;
}
if (Current->next == NULL)
{
Current->next = newNode;
newNode->next = NULL;
}
}
}
And finally here is how I'm calling the add function and creating a new object:
if (inputArray[0] == "A")
{
cout << "Adding Administrator: " << endl <<"\tFull Name:\t" << inputArray[1] << endl;
cout << "\tM Number:\t" << inputArray[2] << endl << "\tEmail Addr:\t" << inputArray[3] << endl << "\tTitle:\t " << inputArray[4] << endl;
Administrator *newAdmin = new Administrator;
istringstream stream (inputArray[2]);
int number;
stream >> number;
newAdmin->setAdmin(inputArray, number);
templatedList.addTemplatedList(newAdmin);
}
I would really appreciate and help that I can get because I'm just not sure what's happening or why it's giving me that incorrect output.
It looks like Node::data is a pointer to Administrator in this example. So when you do
cout << Current->data;
it merely outputs the pointer value. Assuming that you have implemented operator<< for the Administrator class, all you need to do is dereference:
cout << *Current->data;