Linked List issue

hello . am trying to write a linked list but smth is wrong the main.cpp doesn't compile can anybody help me ?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//.h file
#include <assert.h>
#include <string.h>
template <class ListItem>
class InorderList {
// Use L to mean "this List"
public:
int count=0;
   InorderList();
   // Precondition: None
   // Postcondition: L is an empty List
   int insert(const ListElementType & elem);
   // Precondition: None
   // Postcondition: Lpost = Lpre with an instance of elem added to Lpost
   bool first(ListElementType & elem);
   // Precondition: None
   // Postcondition: If the list is empty, none. Otherwise, the variable 
   //    elem contains the first item in L; the "next" item to be returned 
   //    is the second in L.
   // Returns: true if and only if there is at least one element in L.
   bool next(ListElementType & elem);
   // Precondition: The "first" operation has been called at least once.
   // Postcondition: Variable elem contains the next item in L, if there is one,
   //    and the next counter advances by one; if there is no next element, none.
   // Returns: true if and only if there was a next item.
private:
   struct Node; // declaration without definition
   typedef Node *Link; // use declaration of Node
   struct Node { // now we define Node
      ListElementType elem;
      Link next;
      int count
   };
   Link head;
   //Link tail;
   Link current;
};


//----------------insert-------------
template <class ListElementType>
InorderList<ListElementType>::InorderList()
{
   // Initialize an empty List
   head = NULL;
   //tail = NULL;
   current = NULL;
}
//----------------first-------------
template <class ListElementType>
bool InorderList<ListElementType>::first(ListElementType & elem)
{
   // After calling first, current points to first item in list
   
   if (head == NULL)
      return false;
   else {
      elem = head->elem;
      current = head;
      return true;
   }
}

//----------------next-------------
template <class ListElementType>
bool InorderList<ListElementType>::next(ListElementType & elem)
{
   assert(current); 

   // After each call, current always points to the item
   // that next has just returned.
   if (current->next == NULL) // at last element
      return false;
   else {
      current = current->next;
      elem = current->elem;
      return true;
   }
}

//----------------insert-------------
template <class ListElementType>
int InorderList<ListElementType>::insert(ListElementType & elem)
{
   // precondition: list is in order
   Link addedNode = new Node;
   if(!addedNode)
   return -2;
   addedNode->elem = elem;
   // Special case: if the existing list is empty, or if the new data
   // is less than the smallest item in the list, the new node is added
   // to the front of the list
   if (head == NULL || elem < head->elem) {
      
      addedNode->next = head;
      head = addedNode;
      
      /*
      head = addedNode;
      addedNode->next = head;
      */      
   }
 else{
     
      Link pred = head;
      // assertion: pred->elem <= addedNode->elem
      while (pred->next != NULL && 
      	pred->next->elem <= addedNode->elem)
      	{
         // loop invariant: pred->next != 0 && pred->next->elem <= elem
         pred = pred->next;
         
      	}
      
      addedNode->next = pred->next;
      pred->next = addedNode;
      
   }
   return -1 ;
   if (head == NULL || elem == head->elem)
   {count++;
   return (count);
   
}
//main.cpp
#include"InorderList.h"
#include <iostream.h>
#include<string.h>
#include<fstream>
using namespace std ;
int main () 
{ InorderList<string> L;
string word ;
ifstream file ;
file.open ("C:\\name.txt")
if (file.fail())
cout>>"file failed to open ";
while (file.eof())
{file>>word;
L.insert(word);
cout << word ;
}

hello . am trying to write a linked list but smth is wrong the main.cpp doesn't compile can anybody help me ?


If your code doesn't compile, your compiler tells you why.

If you're having trouble interpreting that, then supplying what the compiler tells you to the people who might help you might be a smart thing to do.

Just looking at your poorly indented code, I see an issue with missing braces. For instance, if line 124 is the terminating brace for line 121, where is the terminating brace for the function?
I get an error message saying " in file included from main.cpp"
Topic archived. No new replies allowed.