I have a problem about Linked Lists. I already know how to create structures and linked list. But now I have to create arbitrary number of linked list which are also be kept in another structure. Which means :
struct list{int x, struct list *next; };
struct parent{int x, struct list *head, struct parent *next;}
And after lists are created when i enter this input for example "123134" linked list should look like :
1 -> 2 -> 3 -> 4
And for example 1 will contain 2->3 list inside of it, 3 will contain 1->4 list inside of it.
I need a starting point and a spark from you. So how can i do this?
Draw your list diagram, which often helps.
Start
|
list1 -> node1 -> node2
|
list2 -> node_a -> node_b -> node_c
|
list3 {empty}
|
list4 -> node_1A
Given a diagram like the above, the lists have two links, one to their own nodes, another to another list. Some objects may need more than one link field.
In your case, draw a diagram. Try inserting a new item. Write down the steps you take (and draw).
If you supply more details in your question, more people will assist.
For an example of a list with nodes containing many lists, see a BTree data structure. Each node contains an array of links to other "subtrees".
Related
I have a set of elements {1,2,3},{2,3,4},{1,2,4},{7,8},{3,4,7,9},{12,16,18,19}, {1,2,4}.
I need to have a data structure which contains the list above appeared only once. If any new list appears and if it matches any of the existing list then I don't want that to be added to the resulting data structure.
For the above example the expected result should be :
{1,2,3},{2,3,4},{1,2,4},{7,8},{3,4,7,9},{12,16,18,19}.
One solution which I am having is to use Trees.
For ex: {1,2,3},{1,2,4}
In the above list I will branch out for value 3 of 1st list and value 4 of 2nd list from the node which has value 2. In this way I can trace the list from the root and find whether the list appears or not.
Root
|
|
1
|
|
------2------
| |
| |
3 4
Please suggest if is there any algorithm to make it faster and in a simple way using C.
You can achieve this using graph data structure.
To do so in c, you can use either of the following methods :
Adjacency Matrix
In this method, you need to maintain a matrix of relationship between your numbers, check out the below image :
Linked list :
In this method, you will maintain a list of connections, i.e if 2 has a connection to 1,3,4 then a list of 1,3,4 will be made with a head value of : 2
These images are taken from : http://simplestcodings.blogspot.in .
And you can find great explaination and example code on the same site at : http://simplestcodings.blogspot.in/2013/09/graphs.html link
Hope it helps.
Is it possible to make an array of arrays in C?
More specifically, is it possible to make a list (array) of adjacency lists (arrays)?
And if so, how?
My textbook uses a list of adjacency lists for Dijkstra's algorithm (and in a lot of other algorithms) instead of using an adjacency matrix, though the book is in pseudocode and even then it makes no reference whatsoever on how to implement the list...
Supposedly, list of adjacency lists are more efficient in a lot of algorithms than adjacency matrices. I thought of using linked lists, but AFAIK it would be the same as using an adjacency matrix, so it would make no point at all to implement it this way...
Did you mean a multidimensional array?
int mdarr[10][20];
Is it possible to make an array of arrays in C?
Yes.
More specifically, is it possible to make a list (array) of adjacency
lists (arrays)?
Yes, it can be implemented using a linked list (or array in your case) of linked lists.
And if so, how?
One list (or array) could maintain nodes, each of which will point to the actual adjacency list as well as contain information about the current point of the graph. This can be implemented creating a struct with the relevant data (one information field and one next pointer, and one pointer to adjacency list)
The second list will be the actual adjacency list of each node. It will be pointed to by the corresponding nodes of the first list. It will contain nodes, each of which will be a graph point connected to the corresponding point in the initial list. It can be implemented similarly, by creating a struct having the relevant data (one field for the next pointer, and one information field).
This is not a multi-dimensional array but is completely different. This will use less space than an adjacency matrix, if your graph is sparse.
As an example, lets take A, B, C, D to be the nodes of an undirected graph such that A is connected to B and C.
Then the lists will be like this :
A -> B -> C -> D
| | |
B A A
|
C
Did you mean a three-dimensional array?.
Yo define an adjacency matrix like this aMat[3][3] (graph of three elements).
So, you could define, for example 4 graphs of three elements each like this: aMatMulti[4][3][3].
given a singly linked list, how to determine head node from a specified node (for e.g say node 4).total # of nodes: 10.Thanks. Logic will do,code is appreciated.
We know given a head node one can perform a forward traverse and determine next node easily.
For this case, use of doubly linked list would be simpler but i was wondering if it is possible to track down head node using singly linked list.Thanks.
Its not possible at all to do that with the kind of singly linked list you've described.
Your question isn't entirely clear. But the only way this is possible is if each of your candidates is the head of a unique list, and one of them is the head that you're after.
For each candidate, traverse the list that starts from it. You will eventually hit the corresponding tail, or you will hit the node in question.
You can also make this work even if your set of candidates are not all heads of unique lists. But you would need logic to detect overlap.
You simply cannot in the singly linked list.
From Wikipedia :
Singly linked lists contain nodes which have a data field as well as a
'next' field, which points to the next node in line of nodes.
You cannot go back to the previous node as you don't have any information about it. If you were given any random node of a singly linked list, the head of your new list will be that node, as you cannot go back any further.
I am creating a student list (linked list) that can add, view and edit student information. I have two fields namely Student Name and Student Grade and I add new students in the list in a way that it is sorted according to the student's grades in descending order.
I have finished doing the add and view portion. The problem is on the edit part because I need to edit the information, then I need to sort it again so that it would be on the proper location of the list.
For example, I have 3 students information arranged according to their grades:
student1 90 -> student2 85 -> student3 80 -> NULL
Then I need to edit student2's grade to 75 so the edited linked list should now be arranged as follows:
student1 90 -> student3 80 -> student2 75 -> NULL
How should I do that? You don't need to give me any code. I just want some advices on how I can implement the edit part of my program. I am thinking of creating a new node (with the edited info), delete the old node and insert the edited node into the list. Is my logic correct? or is there a better way on solving my problem.
You can accomplish your goal by
Removing target node
Editing target node data
Reinsert the node using your existing logic for inserting nodes.
Singly linked list?
Find the node you want to edit, and either keep a pointer to the previous node or write a routine to retrieve the previous node.
Remove your node from the linked list (by setting previous_node->next to thisOne->next)
Make your edits.
Insert the new node in the right place in the list (by traversing the list unti the next node is less than your edited value.
Splice edited into the list ( editedNode->next = nextNode; current->next = editedNode)
With doubly linked lists you can just use the "other"/back/up link to find the previous node
Basically your idea is correct except that I wouldn't create a new node. What I would do would be:
Identify if value has increased or decreased (and update node).
Unlink node from the list.
Search forwards or backwards (depending on increase or decrease of grade) for correct location.
Link node into new location.
Note that indexing the list into an array, etc. may give a quicker search than a linear traversal. If you already have such a mechanism it may be quicker to use that when finding the location to re-insert the node.
You could do a function that edits a specified node. Scan the list until you find that node and then directly edit it. Of course you will use a pointer. For the sorting part, say you have n nodes, compare every node i with the nodes after it and swap them if the one you are comparing with is larger:
for every node n1 in list
for every remaining node n2 in list
if n2->grade > n1->grade
swap 'em
you can swap them copying their memory, so you won't need to change any pointer.
Not that I have time to discuss this properly to reach a conclusion and adapt my code because the phase one (of three) of a school project is in 24hrs, but at least I need to know if I did the correct decision.
I'm using linked lists and here's my structures:
typedef struct sCity {
int cityID;
char *cityName;
struct sCityLink *links;
struct sCity *next;
} nCity, *City;
typedef struct sCityLink {
City cityLinkParent;
City cityLinkTo;
struct sCityLink *next;
} nCityLink, *CityLink;
Basically, I have lots of cities and those cities are linked all together, like a graph. For instance, A, B, C, D and E they are inserted in this order into the structure City. Then, I connect A to B, C and D, B to C, D, E, C to D and E and D to E.
Now, let's say I need to go to city E. This is the last one in the linked list and it takes time to traverse the linked list all the way. Maybe not on this example with 5 cities but in the real app I'm supposed to support like 10,000 cities at least. But the shortest route is from A (which is the starting point) from C to E (or it could be A-D-E or A-B-E, doesn't matter).
Do my structures allow me to find the shortest route from A to E without traversing the whole linked list one by one? If not, what I'm doing wrong?
If yes, how can I do that? I don't have a clue how can I find such a path...
There are two separate issues - one, you probably want to find a City pointer for a city ID (eg. "E"). You cannot do that in less than linear time with your structures; if you need it faster, use a hashtable or binary search tree.
Two, you want to find a path between two given cities. For this you'd probably use the BFS algorithm, for which your data structure is just fine. Note that BFS takes O(V+E) time where V and E are the vertex and edge count of the induced subgraph whose vertices' distance from the start vertex is not greater than the distance from start to end vertex. Which means in the worst case, it takes more time than traversing the list of cities.
You can use an algorithm called Breadth-First Search (BFS). You need to implement a "color" flag on each node to use it. Note that this algorithm only works if your edges are unweighted -- if 2 cities are connected, then they are of equal distance.
If the edges have weight (which it does not look like they do), you need something like Dijkstra's Algorithm or A*.