4 Replies - 987 Views - Last Post: 15 November 2007 - 09:43 PM Rate Topic: -----

#1 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Sorting a linked list

Posted 14 November 2007 - 06:21 PM

I am writing code to read from a file and sort the contents using linked list, the file is read, but the list is not sorted and I encounter a message from windows saying that it encountered a problem and it needs to close. Can anyone help me find the bug, Thanks everybody.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include"Songnode.h"
using namespace std;

void songinsert(songptr& head, songptr newnode);//insert a song into the node
void printAll(Songnode *head);//print sorted list into the screen

int main(){
	songptr head;
	Songnode *nodeptr, *shadow;
	ifstream infile;
	string str;
	head=NULL;
	shadow=NULL;
	nodeptr=head;//we can delete this, is not needed, make sure we delete *nodeptr
	infile.open("songs.txt");
	if(infile.fail()){
		cout<<"Could not open file"<<endl;
		exit(1);
	}//end if
	//constructor will call all the necessary functions to remove punctuations and spaces
	while(getline(infile, str)){
		songinsert(head, new Songnode(str));  
	}
	printAll(head);

	return 0;
}

void printAll(Songnode *head){
	while(head!=NULL){
		head->printLine();
		head=head->getlink();
	}
}

//code to insert nodes into the linked list
void songinsert(songptr& head, songptr newnode){
	songptr shadow, nodeptr;
	shadow=NULL;
	nodeptr=head;
	if(head==NULL){//will set out first node
		head=newnode;
		return;
	}//end head=NULL if
	while(nodeptr!=NULL){

		if(*newnode>*nodeptr){
			newnode->setlink(nodeptr->getlink());
			//nodeptr->setlink(newnode);
			
			if(shadow==NULL){
				head=newnode;
			}//end if
			else{
				shadow=newnode;
			}//end else
			return;
			shadow=nodeptr;
			nodeptr=nodeptr->getlink();
		}//end comparison if
		if(shadow==NULL){//
			head=newnode;
		}//end if 
		else{
			shadow->setlink(newnode);
			newnode->setlink(NULL);
		}//end else


		if(*newnode<*nodeptr){
			newnode->setlink(nodeptr);
			if(shadow==NULL){
				head=newnode;
			}//end if
			else{
				shadow=newnode;
			}//end else
			return;
			shadow=nodeptr;
			nodeptr=nodeptr->getlink();
		}//end comparison if
		if(shadow==NULL){
			head=newnode;
		}//end if
		else{
			shadow->setlink(newnode);
			newnode->setlink(NULL);
		}//end else
	}//end while
}//end songinsert



This is my header file
#ifndef SONGNODE_H//avoid multiple loads of songnode class
#define SONGNODE_H//avoid multiple loads of songnode class

#include<iostream>
#include<fstream>//to read our file
#include<string>//Very useful class that will help us remove punctuation
#include<cstdlib>//for exit
using namespace std;

class Songnode{
public:
	Songnode(){}//Default Constructor
	Songnode(string s){getsong(s);}//constructor 
	Songnode* getlink() const{return linksong;}
	void getsong(string s);//get song from file
	void setsong(string thesong){line=thesong;}
	void setlink(Songnode *pointer){linksong=pointer;}
	friend bool operator < (const Songnode &first, const Songnode &second);
	friend bool operator > (const Songnode &first, const Songnode &second);
	string removePunct();//remove useless punctuation
	string removeMultSp();//remove multiple spaces
	string removeMultDash();//remove multiple dashes
	string switchArt();//will switche position of artist and songs
	void removeTheA();//Remove The, A, An from string and switch them
	void swapLastName();
	void printLine();//print line to the screen
	const string getline()const{return line;}

private:
	string line;//our songs
	string composer;
	string songname;
	Songnode *linksong;//our next song
};
typedef Songnode* songptr;//songptr will now hold Songnode pointer variables
#endif//End Songnode class#



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a linked list

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Sorting a linked list

Posted 14 November 2007 - 07:09 PM

I'm in the middle of something, so I can't really look at your problem in too much depth. However, this is a frequent problem with constructing linked lists, and it usually means that you're trying to dereference a null pointer. So take a run through your code and make sure that you're only referencing/dereferencing valid data/objects.

I'll try to take a closer look at this in a bit, but until then, I hope that helps :)

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Re: Sorting a linked list

Posted 14 November 2007 - 11:59 PM

View Postjjhaag, on 14 Nov, 2007 - 07:09 PM, said:

I'm in the middle of something, so I can't really look at your problem in too much depth. However, this is a frequent problem with constructing linked lists, and it usually means that you're trying to dereference a null pointer. So take a run through your code and make sure that you're only referencing/dereferencing valid data/objects.

I'll try to take a closer look at this in a bit, but until then, I hope that helps :)

-jjh


Thanks, I'm trying to locate the problem but I still can't find, any help is appreciated
Was This Post Helpful? 0
  • +
  • -

#4 Jbstate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 07

Re: Sorting a linked list

Posted 15 November 2007 - 04:09 PM

Look at your if statements, it looks like you keep assigning head to the newnode, as for the error statement, I don't know but someone here will help you, I'm sure
Was This Post Helpful? 0
  • +
  • -

#5 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Re: Sorting a linked list

Posted 15 November 2007 - 09:43 PM

View PostJbstate, on 15 Nov, 2007 - 04:09 PM, said:

Look at your if statements, it looks like you keep assigning head to the newnode, as for the error statement, I don't know but someone here will help you, I'm sure


Yes, I tried that, but it's still not working =(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1