Oct 11, 2011 at 8:59am Oct 11, 2011 at 8:59am UTC
honestly speaking, your code is with too many many bugs, here is a compilabe version:
#include <iostream>
#include <fstream>
using namespace std;
struct Node{
int data;
Node *next;
};
typedef Node* NodePtr;
NodePtr& addHeadNode(NodePtr& head, int NewData); //function inputs the list
void printList(NodePtr& head); // function to print it out
int main(){
int a;
ifstream fin;
ofstream fout;
NodePtr head = NULL;
fin.open("numbers.in");
while(fin>>a) addHeadNode(head,a);
fin.close();
cout<< "SINGLY LINKED LIST: " << endl;
printList(head);
return 0;
}
NodePtr& addHeadNode(NodePtr& head, int NewData)
{
NodePtr NewPtr = new Node();
NewPtr->data = NewData;
NewPtr->next = NULL;
NodePtr p = head;
if(p == NULL) head = NewPtr;
else
{
while(p->next != NULL) p = p->next;
p->next = NewPtr;
}
return head;
}
void printList(NodePtr& head)
{
NodePtr p = head;
while(p != NULL)
{
cout<< p->data << endl;
p = p->next;
}
}
Oct 11, 2011 at 5:35pm Oct 11, 2011 at 5:35pm UTC
Dude you are awesome!!!
It works now!!!!
Many many thanks!!!
By any chance do you know how to copy the content of a singly linked list into a doubly linked list!!!??
And thank you again for your help!!
Oct 11, 2011 at 7:26pm Oct 11, 2011 at 7:26pm UTC
Stop posting full code solutions! These students are never going to learn anything if you keep doing the work for them!
Nov 12, 2011 at 3:25am Nov 12, 2011 at 3:25am UTC
I doubt that!
sometimes complete solutions are fare better easier to understand and to learn from.