954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help me with this single linked list

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

anitaNg
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Line 28:

newNodePtr->pengarang = pengarang;

You are only copying the pointer over, not the contents of the string. Use strcpy(newNodePtr->pengarang,pengarang); instead (or better, since you don't know how large your source string is, use strncpy ).

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

wow thanks. it helped so much. :)
but there's one problem. the data didn't sort alphabetically as it used to be. any idea?

anitaNg
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

You reset head pointer to null on line 38. Why? It's guaranteed to follow the path of that first if statement each time, and what if there already is a valid head pointer? (hint pass it into your function) That may not be the problem, but it's one of the first places I would look.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

oh yeah i didn't realize about that, thanks! :)
but still. it didn't sort and the delete function seems not to work.

anitaNg
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

If you don't rework insertNode() like I was saying, of course it's not going to sort. Get the insert procedure correct and the deleting will be trivial. Do you have access to a debugger? (I don't remember how good the one was in VC6). You need to step through your code and watch as the nodes are added. I've given you a pointer, but I'm not going to rewrite it for you. Take another crack at it and post back your new insertNode() function.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

okay i have changed the insertNode() and deleteNode()and overall it seems to workout,thanks to you :) but there's one more problem. when i debug it, and input more than 4 times, the program stops. any idea?

btw, here are the program

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>



#define MAX 50

struct Node
{
	char judul[MAX];
	char pengarang[MAX];
	int harga;
	Node* nextPtr;
};

	Node* headPtr = NULL;
	Node* rearPtr = NULL;
	Node* newNodePtr = NULL;
	Node* myPtr = NULL;
	Node* newPtr = NULL;
	Node* prevPtr = NULL;

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
	{
		strcpy(newNodePtr->judul,judul);		
		strcpy(newNodePtr->pengarang,pengarang);	
		newNodePtr->harga = harga;
		newNodePtr->nextPtr = NULL;
	}
	return newNodePtr;
}

void insertNode(char judul[],char pengarang[],int harga)
{

				if(headPtr == NULL)
				{
					headPtr= makeNode(judul,pengarang,harga);		
					rearPtr = headPtr;
					myPtr = headPtr;
				
				}
				else if(strcmp(judul,headPtr->judul)<=0)	
				{
						myPtr = headPtr;
						headPtr = makeNode(judul,pengarang,harga);						
						headPtr->nextPtr = myPtr;
					
				}
				
				else if(strcmp(judul,rearPtr->judul)>=0)		
				{
					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;																
				}

				cout << endl << endl;
				//display
				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 deleteNode(char judul[])
{
	
			
			if(strcmp(headPtr->judul,judul)==0)			
			{
				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);
				}
				

			}
			//display
			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);
			
		}
						
		if(choice == 2)		//delete
		{
			cout << "Delete Data : ";
			cin >> judul;

			deleteNode(judul);
			
		}
		if(choice == 3)   //display
		{

			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;
		}
		if(choice == 4)   //Exit
		{
			cout << "Thank You" << endl;
		}
	}
	
	
}
anitaNg
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

hey Jonsca, i think i figured it out already. thanks for helping me! :) i really appreciate it :)

anitaNg
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
hey Jonsca, i think i figured it out already. thanks for helping me! :) i really appreciate it :)

No problem. it seems to workout,thanks to you
By and large, you solved it on your own, so give yourself credit too! :)

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: