Write a program that immplements a pointer based linked list with the following functions:
a. Construct/ constructor
b.empty
c. dispaly a node/ search
d.display all nodes
e. delate a node
f. delete all nodes
g. insert a node
The program displays a menu of operations below.
string AuthorTitle;
int BookCode;
double Price;
but I'm having a problem about the main program..
could you help me.. please.. I'm just a beginner...
Immplementation
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
class NODE{
public:
string AuthorTitle;
int BookCode;
double Price;
NODE *next;
};
typedef NODE *NodePointer;
NodePointer ptr, first, predptr, newptr, temp, deleteNode;
string in1;
int in2;
double in3;
public:
void book(){
first = new NODE;
first = NULL;
ptr = first;
}
bool Empty(){
return first == NULL;
}
void Insert(){
newptr = new NODE;
predptr = ptr;
cout << "Enter the Author or Title of the Book, Book code, and the price of the Book: ";
cin >> in1 >> in2 >> in3;
newptr -> AuthorTitle = in1;
newptr -> BookCode = in2;
newptr -> Price = in3;
if (ptr != 0){
newptr -> next = predptr -> next;
predptr -> next = newptr;
}
else {
newptr -> next = first;
first = newptr;
}
}
void Delete(){
cout << "Enter the Element that you want to delete[Author or Title and Book Code]: ";
cin >> in1 >> in2;
predptr = first;
while (temp -> AuthorTitle != in1 && temp -> BookCode != in2){
predptr = predptr -> next;
}
if (deleteNode != NULL){
ptr = predptr -> next;
predptr -> next = ptr -> next;
}
else {
ptr = first;
first = ptr -> next;
delete ptr;
}
}
void Display(){
temp = first;
while (temp != NULL){
cout << temp -> AuthorTitle << " " << temp -> BookCode << " " << temp -> Price;
temp = temp -> next;
}
cout << "This is the end of the List!" << endl;
}
void Traverse(){
cout << "Enter The Author or Title, and Book Code of the book: ";
cin >> in1 >> in2;
temp = first;
if(Empty()){
cout << "The List is Empty" << endl;
}
else {
while((temp -> next != NULL) && (temp -> AuthorTitle != in1 && temp -> BookCode));
temp = temp -> next;
}
if (temp -> AuthorTitle == in1 && temp -> BookCode == in2){
cout << "The data is found" << endl;
cout << temp -> AuthorTitle << " " << temp -> BookCode << " " << temp -> next << " " << temp -> Price;
}
else {
cout << "The data was not found" << endl;
}
}
};
Main
#include <iostream>
#include <string>
using namespace std;
int main() {
char option;
Book x;
if(x.Empty()){
cout << "List is not Empty!" << endl;
}
else {
cout << "List is Empty!" << endl;
}
do {
cout << "I - Insert ,D - Dispaly ,N - deleteNode ,S - Search" << endl;
cout << "E - Exit" << endl;
cout << "Enter the letter you want to view";
cout << endl;
cin >> option;
cout << endl;
switch (option){
case 'I': x.Insert();
break;
case 'D': x.Dispaly();
break;
case 'N': x.Delete();
break;
case 'S': x.Traverse();
break;
case 'E':
break;
default:
break;
}
}
while(option != 'E');
return 0;
}
Thnks...