Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file into a linked list 2

Status
Not open for further replies.

Piecey

Programmer
May 23, 2001
1
AU
Hi,

Could someone please show how to read a text file specified in a command line (ie: <executable name> <file name> )into a dynamically linked list. Each node in the list will store one char.

I know how to read the command line arguments, open the file and write the file to standard output using fgetc. I just dont know how to create a dynamically linked list that adds a new node to the end for each char in the text file. All my (numerous) attempts have failed to work.

Any help would be appreciated. I will post/email what code I have written if so desired.

Piece (piecemaker@start.com.au)
 
Havent worked with a linked list recently but it should look something like this

struct node
{
char data;
node* next;
// node* prev; // if double linked
};

// here is one way
// inserting like a stack
void insert(node* head, char c)
{
node* n = new node;
n->next = head;
n->data = c;
head = n;
}

// another way

void insert(node* pos, char c)
{
// pos is where you want to insert it
node* n = new node;
n->next = pos->next;
pos->next = n;
n->data = c;
}

I hope this helps

Matt
 
Hi, see the thread created my. The subject is 'Loading the text file into a linked list'. I have the same type of question in this forum, but while answering I unable to find that, so I created new thread and posted my program.

I think that program suit to your problem.

Regards
Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top