Hey everyone, i am sending this message because i have a problem with my program in Linked Lists. All of a sudden i can't print my list. Actually i have my insert function where i add the numbers and it seems to work ok.
I tried so many times to change my function but still nothing. I have my head and tail pointing to NULL (struct Node *head = NULL, struct Node *tail = NULL) outside main(). I have my code and please i will appreciate it so much if you explain me what is the problem here and every time my function print_list() says the list is empty.
Thank youCode:struct Node //My struct { int value; struct Node *next; }; struct Node *head = NULL; struct Node *tail = NULL; void insert(struct Node *, int); void Delete(struct Node *); void empty_list(struct Node *); void print_list(struct Node *); struct Node *reverse(struct Node *); int size(struct Node *); int main() //Start of main() function { int choice, pl; int done = -1; int num, rev; while(done != 0) // I want to have a menu so the user has to choose one of the options { printf(" MENU \n"); printf(" --------------------------- \n"); printf(" 1. Insert an element in the list \n"); printf("\n"); printf(" 2. Delete an element from the list \n"); printf("\n"); printf(" 3. Print the length of the list \n"); printf("\n"); printf(" 4. Print if the list is empty or not \n"); printf("\n"); printf(" 5. Print the elements of the list \n"); printf("\n"); printf(" 6. Reverse list\n"); printf(" \n"); printf(" 7. Exit \n"); printf("\n"); printf(">> Give your choice: "); scanf("%d", &choice); printf("\n"); switch(choice) { case 1: printf("\nGive a number: "); scanf("%d", &num); printf("\n"); insert(&head, num); break; case 2: Delete(&head); break; case 3: pl = size(&head); printf("\nThe list has : %d elements", pl); printf("\n\n"); break; case 4: empty_list(&head); break; case 5: print_list(head); break; case 6: rev = reverse(head); printf("The list in reverce order is: %d ", rev); printf("\n"); printf("\n"); break; case 7: done == 0; break; default: printf("Wrong choice"); printf("\n"); } } return 0; }//end of main() function //From this part and so on i started to create my functions void insert(struct Node *head, int num) //insert function { struct Node *temp1; temp1=(struct Node*)malloc(sizeof(struct Node)); temp1 = head; while(temp1->next!=NULL) temp1 = temp1->next; struct Node *temp; temp = (struct Node*)malloc(sizeof(struct Node); temp->value = num; temp->next = NULL; temp1->next = temp; printf("\nYour number has been inserted in your list \n"); printf("\n\n"); } void Delete(struct Node *head)//Delete function { struct Node *last; last = (struct Node *)malloc(sizeof(struct Node)); last = head; struct Node *previous_last; previous_last = (struct Node *)malloc(sizeof (struct Node)); while(last->next != NULL) { previous_last = last; last = previous_last->next; } previous_last->next = NULL; free(last); printf("The element has been deleted \n"); printf("\n"); } void empty_list(struct Node *head)//empty_list funcion { struct Node *pointer = head; if (pointer == NULL) { printf("The list is empty"); printf("\n"); } else { printf("The list is not empty"); printf("\n"); } } struct Node *reverse(struct Node *head) //reverse function { struct Node *current = NULL; struct Node *temp; struct Node *first; while (first!= NULL) { temp = first; first = first->next; temp->next = current; current =temp; } return current; } void print_list(struct Node *head)//print_list fuction { struct Node *pointer = head; if(pointer==NULL) { printf("The list is empty"); printf("\n"); } else { printf("The list is:"); while(pointer->next != NULL) { ("%d", pointer->value); printf("\n"); pointer = pointer->next; } } } int size(struct Node *head)//size (of the list, how many elements are in the list) function { struct Node *temp = head; int counter = 0; while(temp) { counter = counter + 1; temp = temp->next; } return counter; }