Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C Linked lists in QM
#1
Hi Gintaras, hi all,

i'd like to have some information to mimic linked lists in QM.

I replaced struct by type in QM

type Node int'nombre Node*suivant
type Liste Node*premier
Liste* Head=sub.Init

#sub Init
function'Liste* 
Liste* liste=malloc(sizeof(Liste))
Node* element=malloc(sizeof(Node))
if(liste==0 or element==0) ret
element.nombre=0
element.suivant=0
liste.premier=element
ret liste


I'm quite bothered by two things:

1.  In C, all pointers to struct are initialized to NULL for safety, and this does not exist in QM, so how
convert the QM declaration 

2. are malloc, calloc and free used the same in QM and C? I ask this because i need to know how free memory safely.

Some other questions are in mind but i want to start with those.

Thanks
#2
NULL is 0. malloc etc are the same. QM code can be very similar to C code.
#3
OK for that.

so more code:


typedef struct node {
int val;
struct node * next;
} node_t;

type node int'val node*next

is there a way to have typedef like in code to define node_t? could not find it.....



The second problem is that C linked lists rely heavily on while/do while statements
But there are no "while" in QM, only rep.

void print_list(node_t * head) {
node_t * current = head;

while (current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
}

i did:
#sub AfficherListe
function Liste*P <===no typedef
if(P==0) ret
Node* actuel=P.premier
rep
if(actuel==0)
break
out actuel.nombre
actuel=actuel.suivant

is this correct?
#4
1. The C typedef defines 2 names for the same struct - node and t_node. You don't need 2 names. Your type declaration is correct. When need a typedef:

type node int'val node*next
type node_t :node ;;like C typedef

2. Correct. C while translates to rep+if.
#5
Perfect, thanks (thinks :node is actually node there, might be a typo)

type node_t node

but in this case, not relevant as C uses typedef to shorten
struct node to node_t, and is useless in QM then that don't use struct keyword.


Forum Jump:


Users browsing this thread: 1 Guest(s)