Question
Hi, so I have a list of nodes 1,2,3,4 and I want to create a function that deletes the node that points to null. In
Hi, so I have a list of nodes 1,2,3,4 and I want to create a function that deletes the node that points to null. In this case, I want to delete 4. As a result, I should get an output of 1,2,3. Please provide comments, Thanks.
Here are my codes:
SOURCE FILE:
#include
using namespace std;
#include "Linkedlist.h"
int main()
{
IntegerLinkedlist mylist;
int n;
//int d;
int value;
cout << "Enter the number of integers that will be stored: ";
cin >> n;
cout << " Enter " << n << " integers" << endl;
for (int i = 0; i < n; ++i)
{
cin >> value;
mylist.addFront(value);
}
cout << " Output: " << endl;
mylist.print();
//mylist.removefront();
//cout << "Which value do you want to remove? ";
//cin >> d;
//mylist.rotate();
//mylist.remove(d);
//mylist.print();
mylist.removeRear();
mylist.print();
//cout << " Maximum Value: " << mylist.getMax() << endl;
//cout << " Second Largest: " << mylist.getmax_2() << endl;
//cout << "Maximum value:" << endl;
system("pause");
return 0;
}
HEADER FILE:
#include
#ifndef Linkedlist_h
#define Linkedlist_h
using namespace std;
class IntegerLinkedlist {
public:
void addFront(int n);
void print();
//void remove(int d);
//int getmax_2();
//int getMax();
void removeRear();
//void removefront();
private:
struct Node {
Node* next;
int value;
};
Node* head;
Node* tail;
};
void IntegerLinkedlist::addFront(int n)
{
head == NULL;
Node * temp = new Node;
temp->value = n;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
while (temp->next != NULL)
temp = temp->next;
}
temp->next = head;
head = temp;
}
void IntegerLinkedlist::print()
{
Node* curr = head;
while (curr != tail)
{
cout << curr->value << endl;
curr = curr->next;
}
cout << endl;
}
/*
void IntegerLinkedlist::remove(int d)
{
Node* p = head;
//Node* q = head->next;
Node* q;
if (p->value == d)
{
head = head->next; // move to the next node
delete(p); // then delete the node
}
else
{
while (p->next->value != d)
{
if (p->next == NULL)
break;
p = p->next;
}
if (p->next == NULL)
{
cout << "No element" << endl;
}
else
{
q = p->next;
p->next=q->next; // aka this also means p->next->next
delete(q);
}
}
}
*/
/*
int IntegerLinkedlist::getMax()
{
Node* traverse = head;
int max = traverse->value;
while (traverse != tail)
{
if (traverse->value > max)
{
max = traverse->value;
}
traverse = traverse->next;
}
return max;
}
*/
/*
int IntegerLinkedlist::getmax_2()
{
Node* traverse = head;
int max = traverse->value;
int max2 = traverse->value;
int temp;
while (traverse != tail)
{
if (traverse->value > max)
{
max = traverse->value;
}
traverse = traverse->next;
}
}
*/
/*
void IntegerLinkedlist::removefront()
{
Node* p = head->next;
Node* q = head;
if (q->value !=NULL)
{
head = p;
delete(q);
}
//cout << "The list is empty" << endl;
} */
void IntegerLinkedlist::removeRear() // Help!!!!
{
Node* p = head;
Node* q = head->next;
while (q->next == NULL)
{
p->next = NULL;
delete(q);
}
q = q->next;
p = p->next;
}
#endif // !Linkedlist_h
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started