Originally Posted by
laserlight
This is bad practice:
Code:
struct node{
int id;
char name[50];
struct node *next;
}*head;
You should only define the struct:
Code:
struct node{
int id;
char name[50];
struct node *next;
};
Then declare head to be local to a function, e.g., the main function.
Anyway, your problem is that you declared a pointer to a node, but you did not actually allocate memory for that node, e.g., by calling malloc. I suggest that you write a few functions for manipulating a singly linked list, e.g., write a function to add a node to the head of the linked list, write a function to destroy the linked list, write a function to print the linked list. You might also find it convenient to define another struct for the linked list itself, to hold not only the head pointer, but also a tail pointer.
Thanks for the prompt response, yea that fixed it, wondering how I missed that.
As for the bad practice, you should tell that to my CS 1 teacher, who claims to be doing this for over 15 years.
The program is working, but I'm still unable to print my LinkedList, it says that it's empty.
The program is still a work in progress as I'm trying to build it off my teachers code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LINESIZE 128
struct node
{
int id;
char name[50];
struct node *next;
}*head;
// Function prototypes
void insert(char *t_name, int *t_id);
void display(struct node *r);
int deletebyid(int num);
int deletebyname(char name);
// main function
int main()
{
int i, t_id, num, val;
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
char name[50], t_name[50], input_name[50], line[LINESIZE], *value;
struct node *head = NULL;
FILE * ifp = fopen("AssignmentOneInput.txt", "r");
while ((fgets(line, sizeof(line), ifp)))
{
value = strtok(line, ",");
num = strtok(NULL, "\n");
val = atoi(num);
temp->id = val;
strcpy(temp->name,value);
}
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Delete by ID\n");
printf("4.Delete by name\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d", &i) <= 0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{
switch(i)
{
case 1:
printf("Enter the name to insert : ");
scanf("%s", t_name);
printf("What is the id number for %s?\n");
scanf("%d", &t_id);
insert(t_name, t_id);
break;
case 2:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf(" %s\,%d\n", temp->name, temp->id);
}
break;
case 3:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf("Enter the ID number to delete : ");
scanf("%d",&num);
}
if(deletebyid(num))
{
printf("%d deleted successfully\n",num);
}
else
{
printf("%d not found in the list\n",num);
}
break;
case 4:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf("Enter the name to delete : ");
scanf("%s", name);
}
if(deletebyname(input_name))
{
printf("%s deleted successfully\n",name);
}
else
{
printf("%s not found in the list\n",name);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
fclose(ifp);
return 0;
}
void insert(char *t_name, int *t_id)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
strcpy(temp->name,t_name);
temp->id = t_id;
temp->next = NULL;
}
void display(struct node *r)
{
r = (struct node*)malloc(sizeof(struct node));
r = head;
if(r == NULL)
{
return;
}
while(r != NULL)
{
printf(" %s\,%d\n", r->name, r->id);
r = r->next;
if(r == NULL)
{
printf("found the end of our linked list!");
}
}
printf("\n");
}
int deletebyid(int num)
{
struct node *temp, *prev;
temp = head;
temp = (struct node*)malloc(sizeof(struct node));
prev = (struct node*)malloc(sizeof(struct node));
while(temp != NULL)
{
if(temp->id == num)
{
if(temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
int deletebyname(char input_name)
{
struct node *temp, *prev;
temp = head;
temp = (struct node*)malloc(sizeof(struct node));
prev = (struct node*)malloc(sizeof(struct node));
while(temp != NULL)
{
if(temp->name == input_name)
{
if(temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}