H
HS-MOON
I'm asking you to help me.
I'm a beginner of studying c++.
I'm trying to make the Singly Linked List(Ordered).
Anyway, I've been debugging all day.
I can't sort it out!!
As you see, I don't know what this is wrong..
When I run this pgm, It can't find the Current Variable.
//
Node *Current = Head;
if (!Head) {
Current = NewNode;
}
//
I think the point Current has Head's Data & Next now.
But It can't find Current variable..
I show you my coding.
If my coding is wrong, please correct it~*
----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;
return true;
}
if (NewNode->Data < Head->Data) // The first one.
{
NewNode->Next = Head;
Head = NewNode;
return true;
}
while (Current != NULL && (NewNode->Data > Current->Data)) // Go to Next
Item.
{
Previous = Current;
Current = Current->Next;
}
if (Current->Data == NewNode->Data) {
cout << "Data Redundancy" << endl;
return false;
}
else // In the middle of the list. // the last one.
{
Previous->Next = NewNode;
NewNode->Next = Current;
return true;
}
}
----------------------------------------------------------
//LinkedList.h
#include <iostream.h>
#include <stdlib.h>
class Node {
friend class List;
private:
Node *Next;
int Data;
public:
Node(int i) {
Data = i;
}
~Node() {}
};
class List {
private:
Node *Head;
public:
List() {
Head = NULL;
}
~List() {}
bool InsertList(Node *NewNode);
bool DeleteList(int Key);
void Display();
};
----------------------------------------------------------
Main.cpp
#include "linked_list.h"
void main() {
int MenuNumber, InsertNumber, DeleteNumber;
Node *DataItem;
List A;
cout << "Linked List" << endl;
cout << "Exit: 0 " << endl;
cout << "Insert: 1" << endl;
cout << "Delete: 2" << endl;
cout << "Display: 3" << endl;
cout << "Select Menu : ";
cin >> MenuNumber;
while (MenuNumber != 0) {
switch (MenuNumber) {
case 1:
cout << "Insert Number: ";
cin >> InsertNumber;
DataItem = new Node(InsertNumber);
if (DataItem == NULL) {
cout << "There is no memory" << endl;
exit(0);
}
if (A.InsertList(DataItem))
cout << "Good Insertion" << endl;
else
cout << "Bad Insertion" << endl;
break;
case 2:
cout << "Delete Number: ";
cin >> DeleteNumber;
if (A.DeleteList(DeleteNumber))
cout << "Good Delete" << endl;
else
cout << "Bad Delete" << endl;
break;
case 3:
A.Display();
break;
default:
cout << "Insert Again." << endl;
break;
}
cout << endl;
cout << "Select Menu Again: ";
cin >> MenuNumber;
}
}
---------------------------------------------------------
Display.cpp
#include "linked_list.h"
void List:
isplay() {
Node *Current = Head;
cout << "List: ";
while (Current != NULL) {
cout << Current->Data << " ";
Current = Current->Next;
}
cout << endl;
}
I'm a beginner of studying c++.
I'm trying to make the Singly Linked List(Ordered).
Anyway, I've been debugging all day.
I can't sort it out!!
As you see, I don't know what this is wrong..
When I run this pgm, It can't find the Current Variable.
//
Node *Current = Head;
if (!Head) {
Current = NewNode;
}
//
I think the point Current has Head's Data & Next now.
But It can't find Current variable..
I show you my coding.
If my coding is wrong, please correct it~*
----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;
return true;
}
if (NewNode->Data < Head->Data) // The first one.
{
NewNode->Next = Head;
Head = NewNode;
return true;
}
while (Current != NULL && (NewNode->Data > Current->Data)) // Go to Next
Item.
{
Previous = Current;
Current = Current->Next;
}
if (Current->Data == NewNode->Data) {
cout << "Data Redundancy" << endl;
return false;
}
else // In the middle of the list. // the last one.
{
Previous->Next = NewNode;
NewNode->Next = Current;
return true;
}
}
----------------------------------------------------------
//LinkedList.h
#include <iostream.h>
#include <stdlib.h>
class Node {
friend class List;
private:
Node *Next;
int Data;
public:
Node(int i) {
Data = i;
}
~Node() {}
};
class List {
private:
Node *Head;
public:
List() {
Head = NULL;
}
~List() {}
bool InsertList(Node *NewNode);
bool DeleteList(int Key);
void Display();
};
----------------------------------------------------------
Main.cpp
#include "linked_list.h"
void main() {
int MenuNumber, InsertNumber, DeleteNumber;
Node *DataItem;
List A;
cout << "Linked List" << endl;
cout << "Exit: 0 " << endl;
cout << "Insert: 1" << endl;
cout << "Delete: 2" << endl;
cout << "Display: 3" << endl;
cout << "Select Menu : ";
cin >> MenuNumber;
while (MenuNumber != 0) {
switch (MenuNumber) {
case 1:
cout << "Insert Number: ";
cin >> InsertNumber;
DataItem = new Node(InsertNumber);
if (DataItem == NULL) {
cout << "There is no memory" << endl;
exit(0);
}
if (A.InsertList(DataItem))
cout << "Good Insertion" << endl;
else
cout << "Bad Insertion" << endl;
break;
case 2:
cout << "Delete Number: ";
cin >> DeleteNumber;
if (A.DeleteList(DeleteNumber))
cout << "Good Delete" << endl;
else
cout << "Bad Delete" << endl;
break;
case 3:
A.Display();
break;
default:
cout << "Insert Again." << endl;
break;
}
cout << endl;
cout << "Select Menu Again: ";
cin >> MenuNumber;
}
}
---------------------------------------------------------
Display.cpp
#include "linked_list.h"
void List:
Node *Current = Head;
cout << "List: ";
while (Current != NULL) {
cout << Current->Data << " ";
Current = Current->Next;
}
cout << endl;
}