As you can probably tell from the title, I'm a little frustrated with linked lists. I'm working on a homework assignment and it just isn't making sense to me. I've read and read and read on the subject and I've checked out sample code, but I can't for the life of me get to methods in my class to work, let alone make sense. I've talked to my professor and to tutors at school, but I'm no better now than I was 2 weeks ago when the program was assigned.
Here's the gist:
1) I have to create a linked list that will sort in ascending or descending order based on the user's input and it has to be in a class. The list will read in data from a file that will be named at runtime(???). If it is not named it will get a generic filename.
2) I have to create an append method that will place a new node at the end of the list.
3) I have to create an insert method that will place a new node in the correct order (ascending or descending) in the list.
4) I have to create a print method to output the data.
First off, I have no idea what should go in my constructor. Should I prompt the user to enter 'd' or 'a' and then call the constructor based on their input?
Secondly, I can't get the insert method to work to save my life. I keep getting lost in an infinite loop and don't know why.
Anyway, here's my code so far if anyone would like to help.
Thanks...
Here's the gist:
1) I have to create a linked list that will sort in ascending or descending order based on the user's input and it has to be in a class. The list will read in data from a file that will be named at runtime(???). If it is not named it will get a generic filename.
2) I have to create an append method that will place a new node at the end of the list.
3) I have to create an insert method that will place a new node in the correct order (ascending or descending) in the list.
4) I have to create a print method to output the data.
First off, I have no idea what should go in my constructor. Should I prompt the user to enter 'd' or 'a' and then call the constructor based on their input?
Secondly, I can't get the insert method to work to save my life. I keep getting lost in an infinite loop and don't know why.
Anyway, here's my code so far if anyone would like to help.
Thanks...
Code:
//*** MAIN *** #include <iostream> #include <fstream> #include <string> #include "SortedList.h" using namespace std; void fileName(); void main(){ fileName(); /*string order; cout<<"Enter 'A' for ascending or 'D' for descending "; cin>>order; if(order == 'D'){ SortedList list("descending"); } else SortedList list("ascending");*/ cin.clear(); SortedList list("ascending"); list.appendNode(23); list.printList(); } void fileName(){ ifstream infile; cout<<"Please enter a name for your file: "; string x; getline(cin,x); if(x == "\0"){ cout<<"Your filename will be nums.in"; infile.open("nums.in"); } else{ cout<<"Your filename will be "<<x.c_str()<<endl<<endl; infile.open(x.c_str()); } }
Code:
// SortedList.cpp: implementation of the SortedList class. // ////////////////////////////////////////////////////////////////////// #include <iostream> #include <fstream> #include <string> #include "SortedList.h" using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// SortedList::SortedList(string order) { if(order == "descending") cout<<"so you want a descending list"<<endl; else cout<<"so you want an ascending list"<<endl; head = NULL; } SortedList::~SortedList() { } /*void SortedList::appendNode(int inData){ node *Ptr = head; // combining the steps; create a pointer and assign it to head //Ptr = head; node *nodePtr; nodePtr = new node; nodePtr->data = inData; nodePtr->next = NULL; while(Ptr != NULL){ if(Ptr->next == NULL){ Ptr->next = nodePtr; } else//(nodePtr->next != NULL) Ptr = Ptr->next; } }*/ void SortedList::appendNode(int inData){ node *Ptr; node *nodePtr; nodePtr = new node; nodePtr->data=inData; nodePtr->next=NULL; if(head==NULL) head = nodePtr; else{ Ptr= head; while(Ptr->next!=NULL){ Ptr=Ptr->next; } Ptr->next=nodePtr; } /*else{ Ptr = head; while(Ptr->next){ Ptr = Ptr->next; } Ptr->next = nodePtr; } */ } void SortedList::insertNode(int inData){ node *Ptr = head; node *tempPtr = head->next; node *nodePtr; nodePtr = new node; head->next = nodePtr; nodePtr->data = inData; nodePtr->next = NULL; while(Ptr != NULL){ if(Ptr->data > tempPtr->data) { Ptr->next = nodePtr; nodePtr->next = tempPtr; tempPtr = tempPtr->next; } else //(Ptr->data < tempPtr->data) { Ptr = Ptr->next; tempPtr = tempPtr->next; } } } void SortedList::printList(){ node *Ptr; node *nodePtr; Ptr=head; nodePtr = head; while(nodePtr!=NULL){ cout<<nodePtr->data<<endl; nodePtr = nodePtr->next; } } void SortedList::revprintList(){ } void SortedList::graphvizOut(){ } /*void IntList::AppendNode(int data) { node *Temp; node *NewNodePtr; NewNodePtr = new node; NewNodePtr->data=data; NewNodePtr->next=NULL; if(head==NULL) head = NewNodePtr; else{ Temp = head; while(Temp->next){ Temp = Temp->next; } Temp->next = NewNodePtr; } }*/
Code:
// SortedList.h: interface for the SortedList class. // ////////////////////////////////////////////////////////////////////// #include <iostream> #include <fstream> #include <string> using namespace std; #if !defined(AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_) #define AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 typedef int nodetype; struct node{ nodetype data; node*next; node*prev; }; class SortedList { private: node *head; public: SortedList(string); ~SortedList(); void inputFile(); void appendNode(int); void insertNode(int); void printList(); void revprintList(); void graphvizOut(); }; #endif // !defined(AFX_SORTEDLIST_H__960B0846_9203_4427_9B0E_F30C4B0FA28A__INCLUDED_)
Comment