what is wrong with my code?i cant figure it out.
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX 50
struct Node
{
char judul[MAX];
char pengarang[MAX];
int harga;
Node* nextPtr;
};
Node* makeNode(char judul[], char pengarang[], int harga)
{
Node* newNodePtr = (Node*)malloc(sizeof(Node));
if(newNodePtr == NULL)
{
cout << "Out of Memory" << endl;
exit(1);
}
else
{
newNodePtr->judul = judul;
newNodePtr->pengarang = pengarang;
newNodePtr->harga = harga;
newNodePtr->nextPtr = NULL;
}
return newNodePtr;
}
void insertNode(char judul[],char pengarang[],int harga)
{
Node* headPtr = NULL;
Node* rearPtr = NULL;
Node* newNodePtr = NULL;
Node* myPtr = NULL;
Node* newPtr = NULL;
Node* prevPtr = NULL;
if(headPtr == NULL)
{
headPtr= makeNode(judul,pengarang,harga);
rearPtr = headPtr;
myPtr = headPtr;
}
else if(judul <= headPtr->judul && pengarang <= headPtr->pengarang)
{
myPtr = headPtr;
headPtr = makeNode(judul,pengarang,harga);
headPtr->nextPtr = myPtr;
}
else if(judul >= rearPtr->judul && pengarang >= rearPtr->pengarang)
{
myPtr = rearPtr;
rearPtr = makeNode(judul,pengarang,harga);
myPtr->nextPtr = rearPtr;
}
else
{
newPtr = makeNode(judul,pengarang,harga);
myPtr = headPtr;
while(judul >= myPtr->judul)
{
prevPtr = myPtr;
myPtr = myPtr->nextPtr;
}
prevPtr->nextPtr = newPtr;
newPtr->nextPtr = myPtr;
}
}
void deleteNode(char judul[])
{
Node* headPtr = NULL;
Node* rearPtr = NULL;
Node* newNodePtr = NULL;
Node* myPtr = NULL;
Node* newPtr = NULL;
Node* prevPtr = NULL;
if(headPtr->judul == judul)
{
myPtr=headPtr;
headPtr=headPtr->nextPtr;
cout << "Data " << judul << " telah dihapus" << endl;
free(myPtr);
}
else
{
myPtr = headPtr->nextPtr;
prevPtr = headPtr;
while(myPtr != NULL && myPtr->judul != judul) //?
{
myPtr=myPtr->nextPtr;
prevPtr=prevPtr->nextPtr;
}
if(myPtr==NULL)
{
cout << "Element not found" << endl << endl;
}
else
{
prevPtr->nextPtr=myPtr->nextPtr;
cout << "Data " << judul << " telah dihapus" << endl;
free(myPtr);
}
}
}
void display()
{
Node* headPtr = NULL;
Node* rearPtr = NULL;
Node* newNodePtr = NULL;
Node* myPtr = NULL;
Node* newPtr = NULL;
Node* prevPtr = NULL;
cout << endl << endl;
cout << "Data Anda: " << endl;
cout << "Judul" << "\t" << "Pengarang" << "\t" << "Harga" << endl ;
myPtr = headPtr;
while (myPtr != NULL)
{
cout << " " << myPtr->judul << "\t" ;
cout << " " << myPtr->pengarang << "\t" ;
cout << "\t" << myPtr->harga ;
myPtr = myPtr->nextPtr;
cout << endl;
}
cout << endl;
}
void main()
{
char judul[MAX];
char pengarang[MAX];
int harga;
int choice ;
while(choice != 0)
{
cout << "Insert your choice : " << endl;
cout << "(1) Insert " << "\t" << " (3) Display " << endl;
cout << "(2) Delete " << "\t" << " (0) Exit " << endl;
cout << "Your choice : ";
cin >> choice;
cout << endl;
if(choice == 1) //insert
{
cout << "Masukkan Judul : ";
cin >> judul;
cout << "Masukkan Nama Pengarang : ";
cin >> pengarang;
cout << "Masukkan Harga : ";
cin >> harga;
insertNode(judul,pengarang,harga);
display();
}
if(choice == 2) //delete
{
cout << "Delete Data : ";
cin >> judul;
deleteNode(judul);
display();
}
if(choice == 3) //display
{
display();
}
if(choice == 4) //Exit
{
cout << "Thank You" << endl;
}
}
}
i want to type in more than 1 char. but i can't find the way to solve it.
fyi, i'm using visual studio C++ 6 :)
thanks, any helps will be appreciated