472,096 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

LInked list Problems, Please help

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

Dec 5 '05 #1
6 2348
* Ry****@comcast.net:
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?

}
else
Aren't you missing something here?
cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 5 '05 #2
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

Dec 5 '05 #3
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 5 '05 #4
He was actually refering to himself.
I dislike ..., *but* the following ...
Regards,
Marc

Dec 5 '05 #5
<m_*********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
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.

--
John Carson
Dec 5 '05 #6
Ry****@comcast.net wrote:
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.
Dec 7 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by cpptutor2000 | last post: by
5 posts views Thread by Kalvin Schroder | last post: by
1 post views Thread by paul cooke | last post: by
5 posts views Thread by Simon Mansfield | last post: by
reply views Thread by Bennett F. Dill | last post: by
5 posts views Thread by Irfan Akram | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.