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!

Linklist problem 1

Status
Not open for further replies.

edmorize

Programmer
May 22, 2001
5
AU
I am a newbie of c programming.. I have no idea how to implement a linklist program in c that can read the data from a text file and how can I distinguish whether it is a command like (INSERT, DELETE) or it is an item such as apple, orange and also how can I separate the " ; " that along with each item? Anything is appreciated!!!

Implement a linked list which maintains lexical order of the string data elements. Your program will be driven by an input file (text) which gives the required commands and arguments in the form described below.

Textfile:
INSERT apple; INSERT orange; INSERT pear; INSERT mango; INSERT pawpaw; INSERT banana; INSERT grape; DELETE mango; DELETE apple; INSERT strawberry; INSERT peach; DELETE grape; SNAPSHOT*; INSERT mango; SNAPSHOT; INSERT cherry; QUIT

*SNAPSHOT is a dump of the data values in the list.
 
Ok So getting ahead.
First f all open the file.
Now use two strings say, cmd and item;
now u know that the first one has to be a command.
Read one character at a time and store it in command till u get a space now at the next location store '\0'. So now you have the command. If the command is not 'SNAPSHOT' read until u encounter a ';' and store that in item else call a function called display() for displaying all the items read in linked list.
now call a function depending upon the type of command and passing the argument item.
And hence u r ready to perform the req. function.
Hope this helps if u want help with the source code post me ur effort and if there is any discrepancies. I'll try to work that out.

Happy Programming.
s-) SwapSawe.
 
Refer to thread205-85720
smaniraja has written it specifically for u.
There is no lack of good people in world, the problem is in locating them. Check out this thread.
Regards,
|-0 SwapSawe.
 
Thx a lot !!!! U are such one of those good people that I can tell!!!! I was worried how can I make it work b4, but u jst gave me a helpful hand... thx!!! I wonder how can I be a good programmer as well as u do.. If I can, I will help others as well as u do. Any recommendation that u can suggest me to do, such as what kind of book that u used to refer to, etc...
Thanks again!
All the Best!!!
edmorize
 
The program is running fine, but it doesn't maintain lexical order of the string data elements on the screen and in the result file. And also, if I do not enter 2 strings after the file name (e.g linklist file.dat ) it got the memory error(0x0040347b, 0x000000") that cannot be read ...
Thxs for yr advise....
 
Hi,

Yes you are right, go to the main() in the program and change the if statement [ if( c <= 1 ) to if(c <= 2). Now if ur not give two string the program will Indicate it to you and will stop.

If u want to add strings in lexical order then you have to make changes in the read_to_node() function. Add the string in the appropriate place but the execution speed of the program will increase. If you want to spee up the replace the Single linked list to Binary tree.

Regards
Maniraja S
 
Thxs for advice... the program that actually doesn't distinguish whether it is a command or item and separate &quot;;&quot; ... as the result that it eventually makes is only what it firstly gets from the text file... The result supposes to actually INSERT sth and DELETE sth.... rather than jst read the data from a text file...
I think the result is eventually remains &quot;banana cherry mango orange paww peach pear strawberry &quot; Ain't I right?!
Thxs a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top