Please Re-write this code and make sure its simple and has lot of comments. #inc
ID: 3561833 • Letter: P
Question
Please Re-write this code and make sure its simple and has lot of comments.
#include <cstdlib>
#include<iostream>
using namespace std;
char name[20];
class node
{
public:
int data;
node* next;
};
class PointerList
{
public:
PointerList()
{
top = NULL;
count=0;
}
bool empty()
{
if(count==0)
return true;
else
return false;
}
void insert(int value, int element)
{
node *newelement = new node;
if(top == NULL)
{
newelement->data = element;
newelement->next = NULL;
top = newelement;
count++;
}
else
{
node *temp;
temp = top;
int flag = 0;
while(temp!=NULL) {
if (temp->data == value) {
flag = 1;
break;
}
temp=temp->next;
}
if (flag == 1) {
newelement->data = element;
newelement->next = temp->next;
temp->next = newelement;
count++;
}
else {
cout<<"The Value "<<value<<" not found in list...Insertion failed ";
}
}
}
void remove(int value)
{
node *temp;
temp = top;
node * previous = NULL;
int flag = 0;
while(temp != NULL) {
if (temp->data == value) {
flag = 1;
break;
}
else {
previous = temp;
temp=temp->next;
}
}
if (flag == 0) {
cout<<"The element " <<value<<" could not be found... Deletion failed ";
}
else {
if (previous == NULL) {
node * old = top;
top = top->next;
count--;
delete(old);
}
else {
node * old = previous->next;
previous->next = old->next;
count--;
delete(old);
}
}
}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->next;
}
cout<<" ";
}
private:
node *top;
int count; //head
int stackData;
};
int main() {
PointerList *sl = new PointerList();
sl->insert(0,10);
sl->insert(10,20);
sl->insert(20,30);
sl->insert(20,40);
sl->insert(30,50);
sl->insert(50,60);
sl->insert(5,70);
sl->remove(30);
sl->remove(10);
sl->remove(50);
sl->print();
cin>>name;
return 0;
}
Explanation / Answer
#include <cstdlib>
#include<iostream>
using namespace std;
char name[20];
class node
{
public:
int data;//stores data part of the linked list
node* next;//stores address to the next node of the linked list
};
//the abstract datatype Pointerlist is defined
class PointerList
{
public:
PointerList()
{
top = NULL;//initializes the linked list
count=0;//initializes the counter
}
bool empty()
{
if(count==0)
return true;//checks for the linked list if it's empty or not
else
return false;
}
void insert(int value, int element)
{
node *newelement = new node;
if(top == NULL)
{
newelement->data = element;//if there is no node in linked list this 'if' case initializes the first node
newelement->next = NULL;//address of the next node is set to null as there is no 2nd element
top = newelement;//now top is the 1st element and not null
count++;//counter is increased
}
else
{
node *temp;//if there is atleast 1 node in linked list this case is used
temp = top;
int flag = 0;
while(temp!=NULL) {
if (temp->data == value) {
flag = 1;//the elements are stored till the top doesn't become null
break;
}
temp=temp->next;//the address is set to next node
}
if (flag == 1) {
newelement->data = element;//since temp is not null the 2nd node or more nodes are added
newelement->next = temp->next;
temp->next = newelement;
count++;
}
else {
cout<<"The Value "<<value<<" not found in list...Insertion failed ";//if anything else happens, the node is not added
}
}
}
void remove(int value)
{
node *temp;
temp = top;
node * previous = NULL;
int flag = 0;
while(temp != NULL) {
if (temp->data == value) {
flag = 1;//we check for the linked list if it is not the last node,
break;
}
else {
previous = temp;//then here we remove that node and
temp=temp->next;//set the address to the next node leaving that node which is to be removed
}
}
if (flag == 0) {
cout<<"The element " <<value<<" could not be found... Deletion failed ";//if anything else happens it's not removed
}
else {
if (previous == NULL) {
node * old = top;
top = top->next;// if it's the last node we remove the last node
count--;//counter is decreased
delete(old);//node deleted
}
else {
node * old = previous->next;
previous->next = old->next;
count--;
delete(old);
}
}
}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<<temp->data<<",";//untill the last node is reached
temp=temp->next;//items are printed
}
cout<<" ";
}
private:
node *top;
int count; //head
int stackData;
};
int main() {
PointerList *sl = new PointerList(); //new abstract datatype is initialized
sl->insert(0,10);
sl->insert(10,20);
sl->insert(20,30);
sl->insert(20,40);//values are entered
sl->insert(30,50);
sl->insert(50,60);
sl->insert(5,70);
sl->remove(30);
sl->remove(10);
sl->remove(50);
sl->print();
cin>>name;
return 0;
}