Can someone help me with my code, and maybe fix it and tell me what my issue was
ID: 3872945 • Letter: C
Question
Can someone help me with my code, and maybe fix it and tell me what my issue was?
#include <iostream>
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<<smallPtr->data<<endl;*/
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<<smallPtr->data<<endl;*/
return 0;
}
Explanation / Answer
Hi, you have iplemented the code in a non structural way, Your porgram is a bit messy. Hence i am posting a fresh working and structured program that you can use. The code has each functionality as a fuction and it calls each funtion with a switch case. Hence this code will be quite easier to implement and take it forward. Thank you.
CODE:
#include<iostream.h>
#include<conio.h>
#include<process.h>
/*
SINGLY LINKED LIST
*/
class Node
{
int info;
Node *next;
public :
Node()
{
info = NULL;
next = NULL;
}
void InsertAtEnd();
void InsertAtBeginning();
void Display();
void DeleteNode();
};
Node *head = NULL;
void Node :: InsertAtEnd(void)
{
Node *ptr = new Node();
Node *temp;
cout<<" Enter a number : ";
cin>>ptr->info;
if(head == NULL)
head = ptr;
else
{
for(temp = head ; temp->next != NULL ; temp = temp->next);
temp->next = ptr;
}
}
void Node :: InsertAtBeginning()
{
Node *ptr = new Node();
cout<<" Enter a number : ";
cin>>ptr->info;
if(head == NULL)
head = ptr;
else
{
ptr->next = head;
head = ptr;
}
}
void Node :: DeleteNode()
{
Node *ptr, *temp;
int n, flag = 0;
cout<<" Enter the node which you want to delete : ";
cin>>n;
for(ptr = head ; ptr != NULL ; ptr = ptr->next)
{
if(ptr->info == n)
{
flag = 1;
break;
}
}
if(flag == 0)
{
cout<<" The number "<<n<<" does not exist in the list.";
getch();
return;
}
if(ptr == head) // IF IT IS THE FIRST NODE
head = head->next;
else if(ptr->next == NULL) // IF IT IS THE LAST NODE
{
for(temp = head ; temp->next != ptr ; temp = temp->next);
temp->next = NULL;
}
else // IF IT IS A NODE IN THE MIDDLE
{
for(temp = head ; temp->next != ptr ; temp = temp->next);
temp->next = ptr->next;
}
delete ptr;
}
void Node :: Display()
{
Node *ptr;
if(head == NULL)
{
cout<<" The list is empty. Nothing to display.";
getch();
return;
}
for(ptr = head ; ptr != NULL ; ptr = ptr->next)
cout<<" "<<ptr->info;
}
void main(void)
{
Node obj;
int ch;
while(1)
{
clrscr();
cout<<" MENU";
cout<<" ----";
cout<<" 1. Insert At End";
cout<<" 2. Insert At Beginning";
cout<<" 3. Display";
cout<<" 4. Delete Node";
cout<<" 5. Exit";
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case (1) : obj.InsertAtEnd();
break;
case (2) : obj.InsertAtBeginning();
break;
case (3) : obj.Display();
break;
case (4) : obj.DeleteNode();
break;
case (5) : cout<<" Exiting...";
getch();
exit(0);
default : cout<<" Invalid input.";
}
getch();
}
}
I have solved your question. Please do not forget to give a positive like to the answer. Thank you.