I don't normally post in these kinds of forums and prefer to just google my way through problems, but after 3 hours of trying to solve this, I am beat.
I am working on a program that uses a linked list and can add/delete/print out nodes (orbs in this program) but I am stuck on adding them. I can add the first node just fine, but when I go to add another, the program crashes when I try to malloc space to the new node.
I've posted where I'm at so far below. Note that I removed the other functions for the sake of saving space and I cannot even begin to work on them until I can actually get nodes into the list. Any help will be greatly appreciated, thank you.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct list
{
struct orb* head;
};
struct orb
{
char* fname;
char* lname;
int mana;
struct orb *next;
};
struct list* create_list(void);
void insert(struct list* linked_list, int new_mana, char* new_fname,
char* new_lname, int index);
void delete_list(struct list* linked_list);
// Creates a new empty linked list
struct list* create_list(void)
{
struct list* new_list;
new_list = malloc(sizeof(struct list));
new_list->head = NULL;
return new_list;
}
// Insert a new orb
void insert(struct list* linked_list, int new_mana, char* new_fname,
char* new_lname, int index)
{
struct orb* new_orb = malloc(sizeof *new_orb);
struct orb* crnt;
int i;
new_orb->mana = new_mana;
strcpy(new_orb->fname, new_fname);
strcpy(new_orb->lname, new_lname);
if(linked_list->head == NULL || index == 0)
{
new_orb->next = linked_list->head;
linked_list->head = new_orb;
return;
}
crnt = linked_list->head;
for(i = 0; crnt->next != NULL && i != index - 1; i++)
{
crnt = crnt->next;
}
new_orb->next = crnt->next;
crnt->next = new_orb;
}
void delete_list(struct list* linked_list)
{
struct orb* crnt;
struct orb* temp;
crnt = linked_list->head;
while(crnt != NULL)
{
temp = crnt;
crnt = crnt->next;
free(temp);
}
free(linked_list);
}
int main(void)
{
struct list* linked_list;
linked_list = create_list();
int choice, index, tmana;
char tfname[21], tlname[21];
while(choice != 6)
{
printf("Here are your options\n");
printf("1. Insert orb\n");
printf("6. Quit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("What is the first name?\n");
scanf("%s", &tfname);
printf("What is the last name?\n");
scanf("%s", &tlname);
printf("What is the mana of this orb?\n");
scanf("%d", &tmana);
printf("Where should this be inserted?\n");
scanf("%d", &index);
insert(linked_list, tmana, tfname, tlname, index);
break;
}
}
delete_list(linked_list);
system("PAUSE");
return 0;
}