HELP!!!!!!!!!!!!!!!!!

Oct 11, 2011 at 4:05am
people I'm reposting my previous topic because I really need your help.!!!!!!!

I have an assignment to read numbers from the text file and assign them into a linked list, and than to output the linked list. But when i run the program there is no any output.

what to do and where is the mistake. I don't know. Please help me, I'm failing!!!!!!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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 (definition given below)
void printList(NodePtr head); // function to print it out (definition given below)

int main(){
  
   int a;
  NodePtr head = NULL;  

  ifstream fin;
  ofstream fout;
  
  fin.open("numbers.in");  
  
  while(!fin.eof()){
    fin>> a;
    NodePtr addHeadNode(NodePtr head, int a);
    
  }
  fin.close();
  
  cout<< "SINGLY LINKED LIST: " << endl;
  void printList(NodePtr head); 
  
  
 return 0; 
}


Function definition for the function which creates a new nodes for the linked list:
1
2
3
4
5
6
7
8
9
NodePtr addHeadNode(NodePtr head, int NewData){
     
  NodePtr NewPtr = new Node;
  
  NewPtr->data = NewData;
  NewPtr->next = head;
  
  return NewPtr;
}


function definition which creates prints out the content of the linked list:

1
2
3
4
5
6
7
8
9
10
void printList(NodePtr head){
  NodePtr p;
  p = head;
  
  while(p != NULL){
    cout<< p->data << endl;
    p=p->next;
  }
}
	
Last edited on Oct 11, 2011 at 4:06am
Oct 11, 2011 at 8:33am
closed account (D80DSL3A)
On lines 26 and 32 you are not calling the functions, you are repeating the function prototypes.
These lines should be:
1
2
addHeadNode(head, a);
printList(head);


If the program still doesn't work try adding code to determine if the file is being read (or use a debugger to step through the program during execution and see what's going on).
1
2
3
4
5
6
7
8
9
10
11
if( fin.open("numbers.in") )
{  
  while(!fin.eof()){
    fin>> a;
    cout << a << endl;// temporary line to echo values read from file to the screen.
    addHeadNode( head, a);    
  }
  fin.close();
}
else
    cout << "Couldn't open file." << endl;
Oct 11, 2011 at 3:22pm
You're not adding items to the list correctly. You need two main nodes. A node pointing to the beginning of the list, and a node pointing to the end of the list. The problem with how you're adding items to the list is that you're adding nodes that point to the first node in the list, and then not giving the program any way to point to those nodes. The "head" node's next pointer is always NULL, which is why your print function isn't printing anything.
Oct 11, 2011 at 4:33pm
closed account (D80DSL3A)
Oops. packetpirate is right! The value of head is not being changed by the function so it will remain = NULL.
Use the return value from the function to re=assign the head value.

Oct 11, 2011 at 4:56pm
No, fun2code... what he needs to do is this.

1. Create a function to add a new node.
2. Pass the first and last nodes, as well as the new data as parameters.
3. Create a temporary node pointer and assign the new data. Set the next pointer to NULL.
4. If the first node's next pointer is NULL, simply assign the memory address of the temporary node to both the first and last nodes.
5. If not, then point the last pointer's next address to the memory address of the temporary pointer, then change the memory address of last to that of the temporary pointer.

The function should be void.

Then, for printing...

1. Pass the first node's pointer to the function, which should be void.
2. Create a temporary pointer that will keep track of where in the list you are. Set its initial value to the first node's location.
3. While the temp pointer is not NULL, print the content of the temp pointer's data variable, then set the location of the temp pointer to temp->next.

That should do it.
Oct 11, 2011 at 5:42pm
Thank you Very very much!!

Will it be possible if I create to main nodes within the function? (well than they will be temporary ones). because by the assignment I can pass only one parameter into function which is head pointer.
Oct 11, 2011 at 5:50pm
Then create another structure containing both nodes and pass that. ;D

If your professor thinks you can create a linked list with a single node, I'd love to see his implementation...
Oct 11, 2011 at 6:10pm
Many thanks again for your help lads. I've tried o do everything as you said, but I still get an empty output :(

this is how i changed the input:
1
2
3
4
5
6
fin.open("le13.in");  
  
 while(fin>>NewData){
    addHeadNode(head, NewData);
  }
   fin.close(); 


This is how I changed addheddnode function following your instructions. though i declared new nodes inside the function because according the assignment i can have only 2 parameters in addhead node function: head and newData.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NodePtr addHeadNode(NodePtr head, int NewData){
   Node *nPtr;
  
nPtr  = new Node();
nPtr->data = NewData;
nPtr->next = NULL;

NodePtr p = head;

if(p == NULL) head = nPtr;
else
{
while(p->next != NULL) p = p->next;
p->next = nPtr;
}


And this is the print function. But the output is still EMPTY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printList(NodePtr head){
  Node *nowPtr;
  nowPtr = head;
  
  if(nowPtr != 0){
    cout<< "with data" << endl;
  
  while(nowPtr->next != NULL){
    cout<< nowPtr->data << endl;
    nowPtr=nowPtr->next;
  }
  }
  else{
    cout<< "empty" << endl;
  }
}
Oct 11, 2011 at 6:23pm
he he Yes. You just don't know how much I'd love to see that implementation too
Oct 11, 2011 at 6:31pm
closed account (D80DSL3A)
Hi packetpirate.

A link list can be made with a single node*. A pointer to the head node is adequate, especially if the desired operation for adding nodes is to push_front. Karajics function name addHeadNode suggests to me that he is trying to add nodes to the front of the list.
A pointer to the tail of the list is useful if a push_back operation is desired but is not absolutely necessary. Although inefficient, one can reach the tail node by iterating to it from the head.

I was trying to correct the problem with adding to the front of the list.

I could give a complete implementation here but I think it would be bad for me to do that because this is a homework assignment for Karajic. I'd be happy to PM you an example if you wish. I've written many examples of various types of linked lists (they make a fun puzzle).

A couple of problems I see with your list of steps:
2. Pass the first and last nodes, as well as the new data as parameters.

Perhaps mention that they should be passed by reference so the values of the pointers may be changed.

3. Create a temporary node pointer and assign the new data. Set the next pointer to NULL.

This is OK but a node should be allocated to the pointer before any assignments are made.

EDIT: This may all be for naught though, as it appears that Karajic has received the desired help (a complete solution was posted) in his duplicate thread on this subject.
http://www.cplusplus.com/forum/general/52230/#msg283554
Last edited on Oct 11, 2011 at 6:36pm
Oct 11, 2011 at 6:52pm
Hi people I received a solution with one node
have a look: http://www.cplusplus.com/forum/general/52230/
Oct 11, 2011 at 6:56pm
Many thanks to everyone!!!!
It seems like it finally runs!

Will think now how to copy the content of that singly linked list to a doubly one. But it's already a different story.

Thanks for your help lads!!!!
Topic archived. No new replies allowed.