Ian Richard
Ian Richard

Reputation: 525

Passing array of linked lists into functions

I am working on a waiting list that utilizes an array of linked lists. I have used linked lists before but can't figure out how to pass the array into functions. I have started out with declaring arrays of head pointers and tail pointers. I am getting this error:

warning: incompatible pointer types passing 'struct node *(*)[4]'
      to parameter of type 'struct node ***' [-Wincompatible-pointer-types]
                                add(&head,&tail);
                                          ^~~~~
lab6.c:10:31: note: passing argument to parameter 'tail' here
void add(NODE ***head,NODE ***tail);

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NODE struct node
struct node {
    char name[20];
    int number;
    NODE *next;
};
void add(NODE ***head,NODE ***tail);
void delete(NODE ***head,NODE ***tail);
void show(NODE ***head);

int main() {
    //Initialize
    NODE *head[4]={NULL,NULL,NULL,NULL};
    NODE *tail[4]={NULL,NULL,NULL,NULL};
    int whileLoop=0;
        while(whileLoop==0) {
                int selection;
                printf("Choose an Option: 1)Add new party 2)Remove table 3)Show list 4)Quit: ");
                scanf("%d",&selection);

                switch(selection) {
                        case 1:
                                add(&head,&tail);
                                break;
                        case 2:
                                delete(&head,&tail);
                                break;
                        case 3:
                                show(&head);
                                break;
                        case 4:
                whileLoop=1;
                                break;
                        default:
                                add(&head,&tail);
                }
        }
        return 0;
}
void add(NODE ***head,NODE ***tail) {
    char tempName[20];
    printf("enter a name for the party: ");
    scanf("%s",tempName);
    printf("enter a party size: ");
    int tempSize;
    scanf("%d",&tempSize);
    if (tempSize>0) {
        NODE *ptr=(NODE *)malloc(sizeof(NODE));
        strcpy(ptr->name,tempName);
        ptr->number=tempSize;
        ptr->next=NULL;
        int i;
        if (tempSize>=1 && tempSize<=2) {
            i=0;
        } else if (tempSize>=3 && tempSize<=4) {
            i=1;
        } else if (tempSize>=5 && tempSize<=6) {
            i=2;
        } else {
            i=3;
        }
        if (NULL==*head[i]) {
            *head[i]=*tail[i]=ptr;
        } else {
            (*tail[i])->next=ptr;
            (*tail[i])=ptr;
        }
    } else {
        printf("Valid size not entered");
    }



}
void delete(NODE ***head,NODE ***tail) {


}
void show(NODE ***head) {


}   

Upvotes: 0

Views: 1374

Answers (1)

M Oehm
M Oehm

Reputation: 29126

You have arrays of pointers to node, which will decay into pointers to pointers to node when passed as argument. Changes you make in the arrays' entries (to the heads and tails of the lists) will be permanent.

So the signature for your function is:

void add(NODE **head, NODE **tail);

for this client code:

NODE *head[4] = {NULL, NULL, NULL, NULL};
NODE *tail[4] = {NULL, NULL, NULL, NULL};

add(head, tail);

And inside add, you address the lists' heads as head[i].

Upvotes: 1

Related Questions