Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with my code, and maybe fix it and tell me what my issue was? #include using namespace std; class Node{ private:

Can someone help me with my code, and maybe fix it and tell me what my issue was?

#include

using namespace std;

class Node{

private:

// int data;

// Node* next;

public:

Node(int d=0, Node* n = NULL){

data = d;

next = n;

}

int data; // data moved to public

Node* next; // next moved to public

Node* getNext(Node* n);

void setNext(Node* n);

void setFront(Node* f);

int getData(int data);

void setData(int d);

};

class SLL{

private:

Node* front;

public:

SLL(Node* front){}

void insertToFront(int d);

void deleteFront(int d);

void insertToBack(int d);

void deleteBack(int d);

void printList(int d);

bool deleteMiddle(int d);

void insertMiddle(int d);

void printSmallest(int d);

void deleteAllNodes(int d);

};

int Node::getData(int data){

return data;

}

Node* Node::getNext(Node* next){

return next;

}

void Node::setData(int d){

data = d;

}

void Node::setNext(Node* n){

next = n;

}

void SLL::insertToFront(int d){

if(front == NULL){

Node* nn = new Node(d, NULL);

nn = front;

front = nn;

}

else{

Node* nn = new Node(d, NULL);

front = nn;

}

}

void SLL::deleteFront(int d){

if(front == NULL){

return;

}

else if(front -> next == NULL){

delete front;

front = NULL;

}

else{

Node* temp;

temp = front;

front = front ->next;

delete temp;

}

}

void SLL::insertToBack(int d){

Node* nn = new Node(d, NULL);

if (front == NULL){

front = nn;

}

else{

Node* temp = front;

while(temp -> next != NULL){

temp = temp -> next;

}

temp -> next = nn;

}

}

void SLL::deleteBack(int d){

if(front -> next == NULL){

delete front;

front = NULL;

}

else{

Node* nextToEnd = front;

Node* end = front -> next;

while(end -> next != NULL){

nextToEnd = end;

end = end -> next;

}

delete end;

nextToEnd -> next = NULL;

}

}

void SLL::printList(int d){

Node *temp;

if(front == NULL){

return;

}

while(temp != NULL){

temp = front;

cout << temp -> data;

temp = temp -> next;

}

}

bool SLL::deleteMiddle(int d){

Node *temp;

if (front->next == NULL || front == NULL){

return false;

}

temp = front->next;

front -> data = temp -> data;

front -> next = temp -> next;

return true;

}

void SLL::insertMiddle(int d){

int size = 0;

Node* temp = front;

if(front == NULL){

return;

}

else{

while(temp != NULL){

temp = temp -> next;

size++;

}

//size = size / 2;

}

Node* nn = new Node(d, NULL);

temp = front;

for(int i = 1; i < size/2; i++){

temp = temp -> next;

}

nn = temp -> next;

temp -> next = nn;

}

void SLL::printSmallest(int d){

Node *temp;

if(front == NULL){

return;

}

while(temp != NULL && temp < front){

temp = front -> next;

front = temp;

}

}

void SLL::deleteAllNodes(int d){

Node* temp;

if(temp -> data == front -> data){

temp = front;

delete temp;

}

while(temp -> next -> data != NULL){

temp = temp -> next;

Node* curr = temp -> next;

temp -> next = curr -> next;

delete curr;

}

}

int main(){

//reference...

/*SLL* list;

list->insertToFront(11);

list->insertToFront(12);

list->insertToFront(13);

list->printList();

list->deleteAllNodes();

Node* a = new Node(5, NULL);

Node* b = new Node(3, NULL);

Node* c = new Node(4, NULL);

list->insertToFront(a);

list->insertToFront(b);

list->insertToFront(c);

list->printList();

list->deleteMiddle(4);

list->printList();

list->deleteMiddle(6);

list->printList();

list->deleteAllNodes();

list->insertToFront(20);

list->insertToFront(22);

list->insertToFront(21);

list->printList();

Node* smallPtr = NULL;

smallPtr = list->printSmallest(smallPtr);

cout<data<

SLL* list = new SLL(NULL);

list->insertToFront(5);

list->insertToFront(6);

list->insertToFront(10);

Node* a = new Node(5, NULL);

Node* b = new Node(3, NULL);

Node* c = new Node(7, NULL);

list->insertToFront(a);

list->insertToFront(b);

list->insertToFront(c);

list->deleteFront();

list->insertToBack(3);

list->insertToBack(2);

list->deleteBack();

list->printList;

list->deleteMiddle();

list->insertMiddle(5);

list->printSmallest();

list->deleteAllNodes;

list->insertToFront(20);

list->insertToFront(22);

list->insertToFront(21);

list->printList();

Node* smallPtr = NULL;

smallPtr = list->printSmallest(smallPtr);

cout<data<

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

Question How are VEBA assets allocated when a plan terminates?

Answered: 1 week ago

Question

Question May a taxpayer roll over money from an IRA to an HSA?

Answered: 1 week ago

Question

Question What is the doughnut hole in HSA coverage?

Answered: 1 week ago