Displaying Reversed Linked List - c++

I am trying to reverse a liked list in c++ but unfortunately I am unable to do that. Here is the entire program I am working on. I don't know where the problem is. The problem I think is either
reverse()
or
displayList()
void reverse()
{
struct node *curr = head;
struct node *next = NULL;
struct node *prev = NULL;
while (curr != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
void displayList()
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main(){
createNode(100);
createNode(310);
createNode(150);
createNode(300);
createNode(500);
cout << "Original List" << endl;
displayList();
cout << endl << "Reversed List" << endl;
reverse();
displayList();
return 0;
}
When I comment displayList() when I first called it, it worked. Basically the function runs only one time.

Assuming displayList to be a member function of some List type because there is no local variable head anywhere: You destroy your list (leaking all data) when iterating over it in displayList. Here are some fixes.
void displayList() const
{
node* curr = head;
while (curr != nullptr)
{
std::cout << curr->data << ' ';
curr = curr->next;
}
std::cout << '\n';
}
If you get into the habit of making functions const that are not supposed to change the object, your compiler would have spotted this for you.

Related

How to delete element from list in c++

I am currently making a singly linked list in C++
Now I'm trying to make a function showList that prints the content of the list and if it is empty, prints "Empty list". However, right now it prints the list and "Empty list" every single time. When the list is empty, it prints an empty line and in new line "Empty list"
Here is my current code:
template <typename T>
struct Node {
T data;
Node* next;
};
template <typename T>
void showList(const Node<T>* head){
while (head != nullptr){
std::cout << head->data << " " ;
head = head->next;
}
std::cout << std::endl;
if(head->data = 0){
std::cout << "Empty list"<< std::endl;
}
}
Assuming you mean an empty list is a list where head is nullptr, you could check it explicitly:
void showList(const Node<T>* head) {
if (head == nullptr) {
std::cout << "Empty list"<< std::endl;
return;
}
while (head != nullptr){
std::cout << head->data << " " ;
head = head->next;
}
std::cout << std::endl;
}

read access violation, temp was 0xDDDDDDDD

void deleteBasedOnID(student* head,student* tail,int stu_ID)
{
if (head == NULL)
{
std::cout << "nothing to be deleted";
}
else if (stu_ID == head->stu_ID)
{
//delete from the beginning
temp = head->next;
if (temp == NULL)
{
tail = NULL;
}
else
{
temp->prev = NULL;
}
std::cout << "Deleted ID: " << head->stu_ID << std::endl;
delete head;
head = temp;
}
else
{
//start from second
temp = head;
previous = NULL;
while (stu_ID != temp->stu_ID)
{
previous = temp;
temp = temp->next;
if (temp == NULL)
{
std::cout << "no such ID!" << std::endl;
return;
}
}
previous->next = temp->next;
if (temp->next == NULL)
{
tail = previous;
}
else
{
temp->next->prev = previous;
}
std::cout << "Deleted ID: " << temp->stu_ID << std::endl;
delete temp;
}
}
I have a student struct and global pointers head and tail, I put the said head and tail into deleteBasedOnID head and tail arguments
it works fine for if(head == NULL) or the else block but however whenever I try to delete from the beginning, it works fine internally but after I tried to display the list, error occurred.
This is my display function body
void display()
{
temp = head;
while (temp != NULL)
{
std::cout << "Student ID: " << temp->stu_ID << std::endl;
temp = temp->next;
}
}
main function
int main()
{
head = NULL;
tail = NULL;
temp = NULL;
int id;
std::cout << std::endl;
std::cout << "Enter the id you want to delete: ";
std::cin >> id;
deleteBasedOnID(head, tail, id);
std::cout << "New sorted list" << std::endl;
display();
return 0;
}
In your deleteBasedOnId-function, you change the value for the parameter head, which is a copy of the pointer to head you made in main. The original pointer in main keeps its value, which now points to deleted memory, hence the segfault. You can take a reference to the pointer to head as parameter to solve this problem:
void deleteBasedOnID(student*& head,student*& tail,int stu_ID)

How to use a reference to a pointer in a C++ function?

I'm trying to implement a simple linked list. I need an insert function that takes a value and a node to insert after, so I have this:
#include <iostream>
using namespace std;
typedef struct node node;
struct node {
int data;
node * next = NULL;
};
void insert(node *& head, int value, node *& insert_after_me) {
node n; n.data = value;
node * new_node = &n;
if (insert_after_me == NULL) {
insert_after_me = new_node;
cout << "after assign insert: " << insert_after_me->data << endl;
head = new_node;
cout << "after assign head: " << head->data << endl;
}
}
int main () {
node * head = NULL;
node * new_node = NULL;
insert(head, 1, new_node);
cout << head->data << endl;
cout << new_node->data << endl;
cout << head << endl;
cout << new_node << endl;
}
This is obviously an imcomplete implementation, but when I run it I get the following as output:
after assign insert: 1
after assign head: 1
1
1924530936
0x7fff56177928
0x7fff56177928
This tells me in main, head and new_node are pointing to the same thing, yet they have different values stored in data. Why?
Note: Though it seems to me a nonsensical implementation to make insert take a node to insert after and then change that node, that's a requirement of the project. I can't change how insert is called.

C++ Priority Queue Class with Linked List

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.

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;