I'm having trouble getting my linked list to print out, I think the problem is that the head is not linked to the rest of list which is why it only prints the head when I use the print function, but I don't know how to fix it.....
Code:
#include <stdio.h>
#include <stdlib.h>
#include "Lab8.h"
struct node *head = NULL;
struct node *last = NULL;
struct node *temp;
struct node *add()
{
int input = 1;
head = malloc(sizeof(struct node));
last = malloc(sizeof(struct node));
while(input != -1)
{
printf("\nPlease input a dight 0-9(-1 to Stop): ");
scanf("%d", &input);
if(input == -1)
{
printf("head ");
printf("%d\n", head->value);
printf("temp ");
printf("%d\n", temp->value);
printf("last ");
printf("%d\n", last->value);
return(head);
}
temp = malloc(sizeof(struct node));
temp->value = input;
temp->next = NULL;
printf("head ");
printf("%d\n", head->value);
printf("temp ");
printf("%d\n", temp->value);
printf("last ");
printf("%d\n", last->value);
if(head->value == 0)
{
head = temp;
printf("if1\n");
}
else
{
printf("else1\n");
last->next = temp;
last = temp;
}
}
return(head);
}
void print(struct node *list)
{
while(list != NULL)
{
printf("%d\n", list->value);
list = list->next;
}
}
void _delete(int v)
{
}
struct node *find(int v)
{
return 0;
}
void menu()
{
char pick[3];
int FindThisNum;
int DeleteThisNum;
struct node *ptr;
printf("\nLink List Options Menu\n");
printf("(A)dd Items\n");
printf("(P)rint Items\n");
printf("(D)elete an Item\n");
printf("(F)ind an Item\n");
printf("(Q)uit\n\n");
printf("Select one Option: ");
scanf("%s", pick);
char choice = pick[0];
switch(choice)
{
case 'A':
case 'a':
{
ptr = add();
break;
}
case 'P':
case 'p':
{
print(head);
break;
}
case 'D':
case 'd':
{
printf("Which digit would you like to Delete: ");
scanf("%d", &DeleteThisNum);
_delete(DeleteThisNum);
break;
}
case 'F':
case 'f':
{
printf("Which digit would you like to Find: ");
scanf("%d", &FindThisNum);
find(FindThisNum);
break;
}
case 'Q':
case 'q':
{
printf("Goodbye!\n");
exit(1);
break;
}
default:
{
printf("Your entry was not valid, Please try again");
}
}
menu();
}
int main()
{
menu();
return 0;
}