LInked list Problems, Please help

R

Rylios

I am trying to make a very basic text editor using a linked list,
fstream, sorting...

This is what i have so far
------------------------------------------------------------------------------------------------------------------------------------
The Linked.H --- Header file

#include <iostream>

using namespace std;

struct NodeType
{
char Data;
NodeType *Address;
};

//global variables
NodeType *list;
NodeType *before;
NodeType *here;
NodeType *after;
NodeType *NewNode;

//Prototypes
void AddNode(char item);
void Search(char Sv,bool & found);
void Insert(char item,char Sv);
void Delete(char item);
void Display();

void main()
{
bool found;
char Sv,item;
int input;
list = NULL;
before = NULL;
here = NULL;
after = NULL;
NewNode = NULL;



cout<<" Please Choose from the following menu\t";
cout<<"1. Add a Company\n";
cout<<"2. Delete a Company\n";
cout<<"3. Insert a Company\n";
cout<<"4. Display\n";
cout<<"5. Exit\n";
cin >>input;


switch (input)
{

case 1:
AddNode();
break;

case 2:
Delete();
break;

case 3:
Insert();
break;

case 4:
Display();
break;

default:
cout<<"Invalid option!"<<endl;
break;
}


void AddNode(char item)
{
NewNode= New NodeType; //Creates a new node
NewNode->Data=item; // Places item in the data field
NewNode->Address=list; //Places the address of the next node into the
address field
list=NewNode; //updates the address of the list
}

void Display() // displays the data to the screen
{
here = list;
while(here != NULL)
{
cout<<here->Data<<" ";
here= here->Address;
}
}

void Search(char Sv, bool & found)
{
here = list;
found = false; before = list;
while((here != NULL) && ( !found))
{
if(Sv == here->Data)
{
found=true;
after=here->Address;
}

else
{
before=here;
here=here->Address;
}
}
void Delete(char item)
{
bool found;
Search(item, found);
if(found)
{
if(here==list)
{
list=after;
delete here;
}
else{ before->Address=after;
delete here;
}
else
cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
}
}

void Insert(char item, char Sv)
{
bool found;
Search(Sv,found)
if(found)
{
NewNode = New NodeType; NewNode->Data=item;
here->Address=NewNode;
NewNode->Address=after;
cout<<item<<"added to the list after"<<Sv<<endl;
}
else
cout<<"Cannot Insert"<<Sv<<"not found";


}

-------------------------------------------------------------------------------------------------------------------------------
The readin/display file

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cfloat>
//#include "Linked.h"

using namespace std;


string company;
string inputFileName;

int main ()
{
cout<<"This Program Is Designed To Modify A File(s)";
cout<<"\n Please Enter The Complete File Name\n";
cin>>inputFileName; //takes in the file name specified by the user
ifstream inStream;
ofstream outStream; //establishes connection and check for success
inStream.open(inputFileName.data());
assert(inStream.good());
int count=0;

for(;;)
{
inStream>>company;
if(inStream.eof()) break;
cout<<"The File Contains"<<company<<"\n";
}
inStream.close();
return 0;

}

---------------------------------------------------------------------------------------------------------------------------------

My main problem is i'm getting a message that says local definitions
are illegal
I'm not sure what it could be, i have searched a few forums/sites and
they all state you probably are missing a brace but when i look it
seems fine...

Any help would be greatly appriciated

Thnanks very much....

Ryl
 
A

Alf P. Steinbach

* (e-mail address removed):
I am trying to make a very basic text editor using a linked list,
fstream, sorting...

This is what i have so far
------------------------------------------------------------------------------------------------------------------------------------
The Linked.H --- Header file

#include <iostream>

using namespace std;

Don't put that in a header file.

struct NodeType
{
char Data;
NodeType *Address;
};

What is 'Address'? Use self-descriptive names.


//global variables
NodeType *list;
NodeType *before;
NodeType *here;
NodeType *after;
NodeType *NewNode;

Don't use global variables.

//Prototypes
void AddNode(char item);
void Search(char Sv,bool & found);
void Insert(char item,char Sv);
void Delete(char item);
void Display();


void main()

'main' must have result type 'int'.

Don't put a 'main' function in a header file.

{
bool found;
char Sv,item;
int input;
list = NULL;
before = NULL;
here = NULL;
after = NULL;
NewNode = NULL;



cout<<" Please Choose from the following menu\t";
cout<<"1. Add a Company\n";
cout<<"2. Delete a Company\n";
cout<<"3. Insert a Company\n";
cout<<"4. Display\n";
cout<<"5. Exit\n";
cin >>input;


switch (input)
{

case 1:
AddNode();

Your prototype specifies an argument, in this call there is none.

break;

case 2:
Delete();
break;

case 3:
Insert();
break;

case 4:
Display();
break;

default:
cout<<"Invalid option!"<<endl;
break;
}

You probably want to put that menu thing in a loop.


void AddNode(char item)
{
NewNode= New NodeType; //Creates a new node
NewNode->Data=item; // Places item in the data field
NewNode->Address=list; //Places the address of the next node into the
address field
list=NewNode; //updates the address of the list
}

void Display() // displays the data to the screen
{
here = list;
while(here != NULL)
{
cout<<here->Data<<" ";
here= here->Address;
}
}

void Search(char Sv, bool & found)
{
here = list;
found = false; before = list;
while((here != NULL) && ( !found))
{
if(Sv == here->Data)
{
found=true;
after=here->Address;
}

else
{
before=here;
here=here->Address;
}
}

Aren't you missing something here?

void Delete(char item)
{
bool found;
Search(item, found);
if(found)
{
if(here==list)
{
list=after;
delete here;
}
else{ before->Address=after;
delete here;

Aren't you missing something here?




Aren't you missing something here?
 
W

Will

I dislike when other's replay to an issue without addressing the
problem, but the following is just FYI...

Unless this example is for an educational institution that is forcing
you to learn the fundamentals of linked lists, or you are running on an
embedded environment where your binary size is an issue... I would
recommend that you use STL lists or other container classes in leu of
creating your own. STL is easy to use has may features and is fully
tested. Using STL allows you to pay more attention to your business
logic and less on the implimentation of your linked list.

- Will
 
A

Alf P. Steinbach

* Will:
I dislike when other's replay to an issue without addressing the
problem, but the following is just FYI...

Please read up on quoting, Will.

And also on netiquette in general.

Since I was the only one answering I assume you're talking about my
posting; there's no truth in your allegation.

Unless this example is for an educational institution that is forcing
you to learn the fundamentals of linked lists,

That is a reasonable assumption.
 
M

m_schellens

He was actually refering to himself.
I dislike ..., *but* the following ...
Regards,
Marc
 
J

John Carson

He was actually refering to himself.
I dislike ..., *but* the following ...
Regards,
Marc

You are probably right, but in that case it was a curious choice to reply to
Alf rather than to the OP.
 
A

Amedeo Verita

I am trying to make a very basic text editor using a linked list,
fstream, sorting...

This is what i have so far
------------------------------------------------------------------------------------------------------------------------------------
The Linked.H --- Header file

#include <iostream>

using namespace std;

struct NodeType
{
char Data;
NodeType *Address;
};

//global variables
NodeType *list;
NodeType *before;
NodeType *here;
NodeType *after;
NodeType *NewNode;

//Prototypes
void AddNode(char item);
void Search(char Sv,bool & found);
void Insert(char item,char Sv);
void Delete(char item);
void Display();

void main()
{
bool found;
char Sv,item;
int input;
list = NULL;
before = NULL;
here = NULL;
after = NULL;
NewNode = NULL;



cout<<" Please Choose from the following menu\t";
cout<<"1. Add a Company\n";
cout<<"2. Delete a Company\n";
cout<<"3. Insert a Company\n";
cout<<"4. Display\n";
cout<<"5. Exit\n";
cin >>input;


switch (input)
{

case 1:
AddNode();
break;

case 2:
Delete();
break;

case 3:
Insert();
break;

case 4:
Display();
break;

default:
cout<<"Invalid option!"<<endl;
break;
}


void AddNode(char item)
{
NewNode= New NodeType; //Creates a new node
NewNode->Data=item; // Places item in the data field
NewNode->Address=list; //Places the address of the next node into the
address field
list=NewNode; //updates the address of the list
}

void Display() // displays the data to the screen
{
here = list;
while(here != NULL)
{
cout<<here->Data<<" ";
here= here->Address;
}
}

void Search(char Sv, bool & found)
{
here = list;
found = false; before = list;
while((here != NULL) && ( !found))
{
if(Sv == here->Data)
{
found=true;
after=here->Address;
}

else
{
before=here;
here=here->Address;
}
}
void Delete(char item)
{
bool found;
Search(item, found);
if(found)
{
if(here==list)
{
list=after;
delete here;
}
else{ before->Address=after;
delete here;
}
else
cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
}
}

void Insert(char item, char Sv)
{
bool found;
Search(Sv,found)
if(found)
{
NewNode = New NodeType; NewNode->Data=item;
here->Address=NewNode;
NewNode->Address=after;
cout<<item<<"added to the list after"<<Sv<<endl;
}
else
cout<<"Cannot Insert"<<Sv<<"not found";


}

-------------------------------------------------------------------------------------------------------------------------------
The readin/display file

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cfloat>
//#include "Linked.h"

using namespace std;


string company;
string inputFileName;

int main ()
{
cout<<"This Program Is Designed To Modify A File(s)";
cout<<"\n Please Enter The Complete File Name\n";
cin>>inputFileName; //takes in the file name specified by the user
ifstream inStream;
ofstream outStream; //establishes connection and check for success
inStream.open(inputFileName.data());
assert(inStream.good());
int count=0;

for(;;)
{
inStream>>company;
if(inStream.eof()) break;
cout<<"The File Contains"<<company<<"\n";
}
inStream.close();
return 0;

}

---------------------------------------------------------------------------------------------------------------------------------

My main problem is i'm getting a message that says local definitions
are illegal
I'm not sure what it could be, i have searched a few forums/sites and
they all state you probably are missing a brace but when i look it
seems fine...

Any help would be greatly appriciated

Thnanks very much....

Ryl

It seems you're defining functions inside the scope of function main().
For instance: AddNode is declared globally (which is correct off course),
but you try to define it inside main's scope. This is illegal.
Some compilers, though, offer the possibility to define local functions as
an extension to the standard.

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,258
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top