Sorting Words w/ Linked List & Skipping Punctuation - Help Needed

  • Thread starter brad sue
  • Start date
  • Tags
    List
In summary, the conversation discusses different approaches for sorting words in alphabetical order using a linked list. The first approach involves reading all words into a linked list and then sorting them. The second approach is to use a bucket method where words are sorted into appropriate buckets based on their first letter, and then all buckets are combined. The third approach is similar to the second one but involves using a tree structure. The conversation also addresses how to skip punctuation while inputting words, with the suggested method being "%[^,!.]s".
  • #1
brad sue
281
0
Hi,
I have two problems:
please can someone give me an algorithm that would help me to understand how to put words ( read from a file) in alphabetical order using a linked list. I just don't get it!

Also, The words that I need to input is a real text.
but There is punctuation (",", "!",".") that I need to skip in order to continue inputting words.
I know that I can do
Code:
 fscanf(fptr,"  [^,], ", words)
to skip the comma. but how can I do all the 3 punctuations in once?

Thank you for your help
B.
 
Technology news on Phys.org
  • #2
1) There are a lot of ways you can do this.

a) You can read everything into a linked list and then sort
b) You can use a bucket approach. Stick words in an appropriate bucket based on the first letter of the word, then sort each bucket and combine.
c) You can use a tree approach. Same as bucket approach, but you can have a linked list of sub-buckets. You'll be sorting on the fly with this method. All you have to do at the end is combine.

Which one do you want to do?

2) Isn't it just:

"%[^,!.]s"

You're modifing the string to exlude ',', '!', and '.'
 
Last edited:
  • #3


Hi B,
Sorting words in alphabetical order using a linked list can be achieved through a simple algorithm. Here are the steps:

1. Create an empty linked list to store the words.
2. Read the words from the file one by one.
3. For each word, check if it is already present in the linked list. If yes, then ignore it. If no, then insert it in the correct position according to its alphabetical order.
4. Once all the words are inserted, the linked list will contain all the words in alphabetical order.

To handle the punctuation, you can use a conditional statement within your loop where you are reading the words. For example, if the current character is a punctuation mark, then skip it and continue reading the next character until you encounter a letter or a number. Once you have the complete word without any punctuation, you can insert it in the linked list using the above algorithm.

I hope this helps. Good luck with your project!
 

1. How does a linked list help with sorting words?

A linked list is a data structure that allows for efficient insertion and deletion of elements. In the context of sorting words, a linked list can be used to store the words in a specific order, making it easier to rearrange and sort them.

2. How do you handle punctuation in a linked list when sorting words?

Punctuation can be skipped or ignored when sorting words with a linked list. This can be done by implementing a function that checks for punctuation marks and excludes them from the sorting process.

3. Can a linked list be used to sort words in alphabetical order?

Yes, a linked list can be used to sort words in alphabetical order. This can be achieved by comparing the words and rearranging them in the linked list according to their alphabetical order.

4. What is the advantage of using a linked list over other data structures for sorting words?

Linked lists have a dynamic size, meaning that they can grow or shrink as needed. This makes them more efficient for sorting words compared to static data structures such as arrays. Additionally, linked lists allow for easy insertion and deletion of elements, which is important when sorting words.

5. Are there any specific algorithms that can be used with a linked list to sort words?

Yes, there are several popular sorting algorithms that can be implemented with a linked list. Some examples include insertion sort, selection sort, and merge sort. The choice of algorithm depends on factors such as the size of the input and the efficiency of the algorithm.

Similar threads

  • Programming and Computer Science
Replies
21
Views
528
  • Programming and Computer Science
Replies
1
Views
348
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
20
Views
5K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top