1
Given the TAD LISTA_ENC_NC_ORD, need to implement an operation that takes two lists and returns a list resulting from the concatenation of them, and that cannot have elements with the same value.
Here the TAD:
typedef struct nodo
{
int inf;
struct nodo * next;
}NODO;
typedef NODO * LISTA_ENC_NC_ORD;
It is chained, ordered crescently and has a header node (the node is a NODE too, but its inf field is the number of elements in the list, and its next is the first element).
The operation:
LISTA_ENC_NC_ORD concatenar (LISTA_ENC_NC_ORD l1, LISTA_ENC_NC_ORD l2)
Insert operation:
void ins (LISTA_ENC_NC_ORD l, int val)
{
NODO *novo, *aux;
novo=(NODO *)malloc(sizeof(NODO));
if (!novo)
{
printf ("erro! memoria insuficiente");
exit(2);
}
for (aux=l; aux->next!=NULL && val>(aux->next)->inf; aux=aux->next);
novo->inf = val;
novo->next = aux->next;
aux->next = novo;
l->inf++;
}
I can’t do it at all ^^
What have you tried? What specific problem are you having? With more information it is possible to help without going out doing all the homework for you. :)
– hugomg
Returning to the subject, given that the lists are ordered, I got the impression that the operation you want to implement is more "merge" than for a concatenation. And I must confess that it is very strange this scheme of storing the size of the list in the first element.
– hugomg
my problem is I didn’t know what to do so the list wouldn’t have repeated elements
– poirot
this size storage scheme is so you don’t have to go through the entire list to find the number of elements
– poirot
The reason I find it odd is that, unlike vectors, linked lists don’t need to know their own size to do most things. And storing the number of elements within the list itself makes it super easy for you to make a mistake and treat the number of elements as if it were an element itself. One way to mitigate this problem would be to store the metadata in a second type of data instead of putting it "in-band".
– hugomg
For insertion, query and removal operations I use to know if the position in which the user wants to insert, query or remove is valid.
– poirot
If the list is sorted, it makes no sense for the user to pass a position. And even if you did, inserting things in the middle of a linked list is not efficient because you have to go through the list all the time.
– hugomg
Yes, on an orderly list it doesn’t make any sense at all. The teacher just wanted to complicate.
– poirot