//.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)
returnfalse;
else {
elem = head->elem;
current = head;
returntrue;
}
}
//----------------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
returnfalse;
else {
current = current->next;
elem = current->elem;
returntrue;
}
}
//----------------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>
usingnamespace 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?