Sort using a linked list in c

Im implements Sort. The program reads from a text file with ASCII characters. There are two elements per line, separated by spaces. Suppose the input is "ab". This defines the priority relationship between a and b, saying that "a must happen before b".

So if the file

  ab
 dc
 bd

output

  a
 b
 d
 c

I created two linked lists

  • bigList : store unique items ( count to track previous items)
  • smallList : save previous items
  • List item

Summary of my code

  • reads line by line
  • captures two elements per row
  • checks if they are already present, if not inserts them
  • displays the result based on the account number

it actually prints all the elements in the file, for example for my input above

  a
 b
 b
 d
 d
 c

I am new to C programming and please let me know what I'm doing wrong.

+4
source share
1 answer

I don’t know much about the topological genus, but I think I know enough to leave a reasonable comment. Any user should edit this answer.

I see several problems with this implementation. Some are related to C, others are related to the algorithm, so let me go through them one at a time.


Definition of a problem . Topological sorting is really defined as the priority of elements in a directed graph. However, this proposal alone does not fully define the problem. In particular, topological sorting is the priority of graph elements starting at the specified source vertex. As an example, suppose you have the following oriented graph:

 a -> b b -> c c -> a 

If you start at vertex a , your topological ordering should be {a, b, c} . If you start at vertex c , your topological order should be {c, a, b} . Therefore, the definition of a problem does not make sense without an initial vertex. One of the options for such a vertex may be some vertex of the graph that does not have a border to it, i.e. Each falling edge is an outgoing edge.

Another thing to keep in mind is binding . It is not always possible to reach any peak from any other peak. Therefore, it is worth keeping in mind when implementing such algorithms.


Good data structures are the key to good algorithms . If you want to sort objects in a directed graph, it is best to create a data structure with a directed graph, which itself will include the creation of a Node data structure and an Edge data structure. I suggest looking at the adjacency list . Once you have such a data structure, it is a matter of starting the width of the first search on the graph, and you will get your topological priority as a neat consequence.

When implementing the adjacency list, you still need to store all your elements in one place. A linked list is usually not the best way to do this, because it takes a constant time to insert into one (assuming sorting by data), it takes linear time to search through it. Therefore, suboptimal. As @ David RF suggested Red-Black trees and AVL trees is the way to go. However, I would not do with this optimization. As long as you have a sound processing algorithm, you can always improve the storage data structure. In the end, the interface for linked lists and search trees is the same.


The algorithm can be fast, given that you are using the correct algorithm . In practice, I did not deal with topological varieties, so I do not know the slightest difficulties and any problems. But! If you do this with a wide first search using the usual node-edge data structures (note that edges can be implicitly defined in nodes), your search itself will take linear time using a breadth-first search.

I read your algorithm, and I must admit that I do not quite understand your concept of a large list and a small list. Ambiguous names really don't help. Perhaps this makes working with one tiny mistake, hiding somewhere, but it is not too readable. Maybe someone else can comment on your current implementation.

+2
source

All Articles