Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node{
- int data;
- struct node *next;
- };
- typedef struct node Node;
- typedef Node *List;
- void printList(List head){
- printf("Lista collegata: \n");
- while((head -> next) != NULL){
- printf("%d\n",(head -> data));
- head = (head -> next);
- }
- printf("-----------------\n");
- }
- void headInsert(List *phead, int val){
- List new;
- // abbiamo creato un puntatore al nodo nuovo (Node *new)
- new = malloc(sizeof(Node));
- // malloc crea un puntatore (di tipo void*), conversione implicita.
- // abbiamo allocato sizeof(Node) byte, e il suo puntatore e' new!
- // ORA DOBBIAMO CONTROLLARE IL SUCCESSO DI MALLOC
- if (new != NULL){
- printf("Memory allocation successful!\n");
- (new -> next) = *phead;
- *phead = new;
- (new -> data) = val;
- }
- else
- printf("Unable to allocate dynamically memory!\n");
- }
- void clusterInsert(int n, int ins[n], List head){
- for(int i = 0;i<n;i++)
- headInsert(&head,ins[i]);
- }
- int main(){
- // abbiamo bisogno di un puntatore al primo nodo!
- // List e' &Node
- List head = malloc(sizeof(Node));
- if (head != NULL){
- head -> data = 0;
- head -> next = NULL;
- }
- else
- printf("Head not allocated!\n");
- headInsert(&head,1);
- headInsert(&head,2);
- headInsert(&head,3);
- printList(head);
- searchCascadeDestroy(&head,2);
- printList(head);
- int clust[3] = {4,5,6};
- clusterInsert(3,clust,head);
- printList(head);
- destroy(head);
- printList(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement