![]() |
|
#1
|
|||
|
|||
Linked ListHi,
I'm not a programmer, just a computer science student. However maybe a good samaritan will help me out, having a little trouble. My goal is this: 1. Create an empty singly-linked dynamic list LIST represented by a global pointer. 2. In a loop (referred to as main loop) prompt the user to enter a word. (must be less than 80 characters) 3. If word entered is quit (regardless of letter case (ie. QuiT, quiT etc.), main loop is terminated. If not, word is stored in LIST in lexicographic order using procedure InsertIntoList(word) and main loop is repeated. Any help would be really appreciated! all I've got so far is this: CPP / C++ / C Code:
Last edited by LuciWiz : 06-Oct-2004 at 00:37.
Reason: Please insert your c code between [c] [/c] tags
|
|
#2
|
|||
|
|||
|
Hi,
you are on right track. The data type can be char array (static storage) or char pointer (dynamic storage) to save the word. Choice is yours. To add a node to list, create a new node and points previous node's next pointer to the current node and current node's next pointer to NULL. So steps to build your function InsertIntoList(word) would be Make head node of the list if list is empty. Store word in it. Point head node's next to NULL. Do not touch head node if list is not empty. Keep head node's address in a new pointer variable called "lastnode" (or anything of your choice) of type LIST. Prepare a new node in a similar way as head node and just keep pointing lastnode's next to this new node and this node's next to NULL. Also,make this new node as lastnode everytime. Ofcourse,this is one logic. You can check the if the word entered is "quit" or not using string manipulation functions available in C. Its called strcasecmp. It does a case insensitive comparison of two strings and returns 0 if they are equal. Check man strcasecmp on any unix machine to check its syntax and related help. Pls see the linked list tutorial in "C/C++ Source Code and Tutorials" in this forum.Its explained very well there. Good Luck. |
|
#3
|
|||
|
|||
|
Hi,
Thank you for the advice. I was thinking to ensure that the word is less than 80 characters (that is one of the conditions) I could just do this: CPP / C++ / C Code:
then after the word is stored as a string called word I would check for 'quit' and go from there. Does that make sense? Quote:
Last edited by LuciWiz : 06-Oct-2004 at 23:09.
Reason: Please insert your code between [c] [/c] tags
|
|
#4
|
|||
|
|||
|
Hi,
scanf won't help in checking the length of the word. What you need to do is define some temp word variable as char array of size say 100 (to accomodate for word size 80+1 end-of-line character) in your main program. After the word is entered and stored in temp word variable, you can use strlen() function in C to measure the length of the word entered. strlen() will read count the total number of characters in the string suuplied till it hits '\0'. You can try following to check length of the word and if the word is "quit" or not by using following if loop. CPP / C++ / C Code:
If the tempword has passed the above two tests ,i.e. word length is less than 80 and word is not "quit", then you can store tempword in you actual linked list node data variable that you are building. Use "man strlen" to checkout help for strlen() function. |
|
#5
|
|||
|
|||
I'm having some serious problemsThe linked list must be in lexicographic order, so new entries must be checked for their proper place. I'm having a serious problem knowing how to create a loop to go through the list and change which node it is looking at. I'd really appreciate some advice. Also it is a requirement that I make a deep copy of any new word, not just point somethng to it. This is what I have right now:
CPP / C++ / C Code:
Last edited by LuciWiz : 06-Oct-2004 at 23:14.
Reason: Please insert your C code between [c] [/c] tags
|
|
#6
|
|||
|
|||
|
First of all, In C, you can not have function definition inside another function. You can not define your function InsertintoInsertIntoList() inside main(). It should be moved outside main(), either before or after.
Secondly,about lexicographic order. You need to add a string to the list by comparing it with the last inserted string. Following is the camparison criteria. String1 preceeds String2 lexicographically if, first character of string1 prceeds ( or less than) first character of string 2. So if current string is lexicographically preceeds the last string then you need to swap current string with last string. Following is a small function which determines , out of the two strings passed which string lexicographically preceeds the other one. CPP / C++ / C Code:
where is boolean is my custom enum data type defined as CPP / C++ / C Code:
You can also return 1 for TRUE and 0 for FALSE if you are not comfortable with enum data types. Also, by deep copy, if you mean saving new word to "data" then yes ofcourse, you need to do that. Your node holds actual data and a "pointer" to the location of next node. I am just giving you the logic of add function. You need to fill in the code. CPP / C++ / C Code:
Good luck. |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Merge sort on a linked list | Temujin_12 | C++ Forum | 1 | 06-Mar-2008 20:33 |
| Insert problem in linked list with two function code | Kay Chan | C++ Forum | 1 | 03-Sep-2004 09:52 |
| Question about linked list and queue | Kay Chan | C++ Forum | 7 | 02-Sep-2004 09:27 |
| help on linked lists any1????? | nick4 | C Programming Language | 1 | 17-May-2004 09:32 |
| [include] list1.h -- Linked list class | dsmith | C Programming Language | 2 | 04-May-2004 09:42 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The