C Linked list how can I remove a node - c

So I had a problem with the "String name" I fixed it, now I would like to
add a function that's removes the node by its ID, so I tried to make a function that locates the node with the requested ID, It did not go well because it does not track the ID I entered.
Here is the full code:
#include <stdlib.h>
#include <stdio.h>
typedef struct stringData {
int id;
char *name;
int payment;
struct stringData *next;
} Node;
Node *addEmployee(int id, char *name, int payment) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->id = id;
newNode->name = strdup(name);
newNode->payment = payment;
newNode->next = NULL;
return newNode;
}
void insert(Node **link, Node *newNode) {
newNode->next = *link;
*link = newNode;
}
void removeEmployee(Node *head, int id){
while (head != NULL){
head = head->next;
if(head ->id == id){
printf("The employee exists.");
}
printf("The employee does not exists.");
}
}
void printList(Node *head) {
while (head != NULL) {
printf("----------------------------------\n");
printf("Name: %s\n", head->name);
printf("ID: %d\n", head->id);
printf("Payment: %d\n", head->payment);
printf("----------------------------------\n");
head = head->next;
}
}
int main(void)
{
int id;
char name[42];
int payment;
int input;
Node *head = NULL;
Node *tail = NULL;
Node *n;
n = addEmployee(1, "Dor", 5000);
insert(&head, n);
tail = n;
n = addEmployee(2, "David", 10000);
insert(&head, n);
tail = n;
printList(head);
removeEmployee(head, 2);
while((input != 4)){
printf("\nPlease select 1 option:\n1) Add an employee - type 1\n2) Remove an employee - type 2\n3) Show a list of employees - type 3\n4) Exit - type 4\n");
scanf("%d", &input);
switch(input){
case 1:
printf("\nPlease enter the employee's id: ");
scanf("%d", &id);
printf("\nPlease enter the employee's name: ");
scanf("%41s",name);
printf("\nPlease enter the employee's payment: ");
scanf("%d", &payment);
n = addEmployee(id, name, payment);
insert(&head, n);
tail = n;
break;
case 2:
printf("Please enter the ID of the employee you would like to remove: ");
scanf("%d", &id);
break;
case 3:
printList(head);
break;
case 4:
printf("Good-bye");
break;
default:
printf("This is not an option!");
break;
}
}
return 0;
}
I do not know how to get into it, I'm not that familiar with this subject so It would be very helpful to also see some examples.

Related

Segmentation Fault while working with linked list in C

I am a newbie to c. The question is to implement a Reastuarent Management Software using Linkedlist. A user should be able to enter their order and view their order. A user who pays additional amount for fast delivery should be appeared on top of the order list.
My code, while executing shows no erros but while running it shows segmentation fault when I try to enter the second entry or try to display the list.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct items{
char *item_name;
float cost;
};
struct items array[] = {
{"Pizza",49.9},{"Apples",22.0},{"Oranges",10.5},{"Grapes",3.5},{"Parotta",4.5}
};
struct bill_item{
int bill_no;
char* customer_name;
int order_item_no;
int quantity;
float total;
struct bill_item *next;
};
struct bill_item* head;
int count=0;
void insert_at_start(struct bill_item* node);
void insert_at_end(struct bill_item* node);
void book();
void display_list();
void main(){
int choice;
head = (struct bill_item*)malloc(sizeof(struct bill_item));
head->next = NULL;
while(choice!=3){
printf("\n----------------------------------------------");
printf("\n1.Book\t2.Check Orders\t3.Exit\nEnter option: ");
scanf("%d",&choice);
printf("\n------choice %d",choice);
switch(choice){
case 1: book();
printf("\nItem Purchased!!");
break;
case 2: display_list();
break;
case 3: break;
default: printf("\nEnter the correct option");
break;
}
}
}
void insert_at_start(struct bill_item* node){
if(head->next == NULL){
head->next = node;
}
else{
node->next = head;
head->next = node;
}
}
void insert_at_end(struct bill_item* node){
struct bill_item* ptr = head;
if(head->next==NULL)
head->next = node;
else{
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next = node;
}
}
void book(){
char c;
struct bill_item* node = (struct bill_item*)malloc(sizeof(struct bill_item));
int i=0,choice;
printf("\nMenu");
printf("\n-----------------");
for (i=0;i<5;i++){
printf("\n%d %s : %.2f",i+1,array[i].item_name,array[i].cost);
}
printf("\nEnter choice: ");
scanf("%d",&choice);
printf("\nEnter quantity: ");
scanf("%d",&node->quantity);
node->next = NULL;
count++;
node->bill_no = count;
node->total = array[choice-1].cost*node->quantity;
fflush(stdin);
printf("\nEnter customer name: ");
fgets(node->customer_name,30,stdin);
fflush(stdin);
printf("\nPurchase amount: %.2f \nNeed fast delivery(Extra 100rs will be charges)(y/n)?",node->total);
c = getc(stdin);
fflush(stdin);
if(c=='Y' || c=='y'){
node->total +=100;
printf("\nFast delivery applied\nTotal %.2f",node->total);
insert_at_start(node);
}
else{
printf("\nTotal: %.2f",node->total);
insert_at_end(node);
}
}
void display_list(){
printf("\nBill No\t\tOrder Item\t\tCname\t\tQ\t\tTotal");
struct bill_item* ptr = head;
ptr = ptr->next;
while(ptr->next!=NULL){
printf("\n%d\t\t%d\t\t%s\t\t%d\t\t%.2f",ptr->bill_no,ptr->order_item_no,ptr->customer_name,ptr->quantity,ptr->total);
ptr = ptr->next;
}
}
I have tried to find the errors. But can't point out any. The errror is somewhere at the functions insert_at_end() and insert_at_start()
In display_list(), if head->next is NULL, you try to read the ptr->next without first checking if it's null:
ptr = ptr->next;
while(ptr->next != NULL) {
// ...
}
This will segfault because ptr is null.

making a phonebook system using linked list

i'm creating a phonebook system in c using single linked list it seems that everything is working well but the delete option always giving me an error and i don't know how to fix it so i hope someone can tell what is problem in the code and show me a code that can modify the name or the number in this code
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char *first,*last;
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",&first);
scanf("%s",&last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
}
void del()
{
struct node *pretemp,*temp;
char *f,*l;
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",&f);
scanf("%s",&l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%s ",temp->number);
temp=pretemp->next;
delete pretemp;
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}
this is the error
I had made the following changes in your search and delete function
The first and last buffer in both search and delete function were not allocated memory before using them in scanf. This will cause run-time error
The way you were catching user input for first and last name in scanf was also improper
In search and delete function I modified the string comparison. To compare to string you need use strncmp function. Using == will check the address of first byte.
In search function you were not checking end of list.
In del function I have changed printf("%s ", temp->number) to printf("%d ", temp->number)
void search()
{
struct node *temp;
char first[20], last[20];
temp = head;
printf("Enter name to be searched:\n");
scanf("%s", first);
scanf("%s", last);
while (temp != NULL && ((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0)))
{
temp = temp->next;
}
if (temp != NULL) {
printf("%s\n", temp->firstname);
printf("%s\n", temp->lastname);
printf("%d\n", temp->number);
}
}
void del()
{
struct node *pretemp, *temp;
char first[20], last[20];
temp = head;
pretemp = head->next;
printf("Enter name :");
scanf("%s", first);
printf("Enter Last name:");
scanf("%s", last);
while (temp != NULL) {
if((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0))
{
printf("%s ", temp->firstname);
printf("%s ", temp->lastname);
printf("%d ", temp->number);
temp = pretemp->next;
delete pretemp;
break;
}
else
{
temp = temp->next;
pretemp = pretemp->next;
}
}
}
I made some changes in your code, you can find comment in code bellow.
It is compiled and likend under linux ubuntu 18.04 and it works now.
Basicaly, when you use scanf(" %s ", astr), 'astr' should have enough space to accept input.
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char first[20], last[20]; /* space for input */
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",first); /* you dont need '&' operator for string*/
scanf("%s",last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
}
void del()
{
struct node *pretemp,*temp;
char f[20],l[20]; /* you need a space to store input */
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",f); /* you dont need '&' operator to access a string */
scanf("%s",l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%ld ",temp->number); /* 'number' is long int */
temp=pretemp->next;
free(pretemp); /* 'delete' is c++ operator, not C */
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
} /* missing curly bracket */
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}

My createNewNode function does not work once it is called outside of the loop. What could be the problem?

I am having trouble understanding why the createNewNode function does not work again after being called in the for loop. It works fine when I first want to create the new nodes of the linked lists. However, after I call it again in the switch function, the program just crashes. I would greatly appreciate any help I can get.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
struct Student
{
char first[SIZE];
char last[SIZE];
float score;
char zip[SIZE];
};
struct node
{
struct Student records;
struct node * next;
};
typedef struct node node_t;
//making a global head so it can be accessed everywhere
node_t * head;
//function prototypes
node_t * createNewNode();
node_t * newHead(node_t ** head, node_t * add);
void printList(node_t * head);
void printMenu();
void insertAfterNode(node_t * nodeAfter, node_t * newNode);
void insert(int n);
int main()
{
int numRec,i,userChoice=9;
head = NULL;
node_t * temp;
printf("Please indicate number of records you want to enter (min 5): ");
scanf("%d",&numRec);
getchar();
printf("Please input records in FirstName LastName Score Zipcode format.\n");
for(i = 0; i < numRec; i++)
{
printf("Please input records in for student #%d: ",i+1);
temp = createNewNode();
newHead(&head,temp);
}
while(userChoice != 0)
{
printMenu();
scanf("%d",&userChoice);
switch(userChoice)
{
case 1:
printList(head);
break;
case 2:
numRec++;
insert(numRec);
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
}
return 0;
}
node_t * createNewNode()
{
node_t * result = malloc(sizeof(node_t));
char input[50];
char temp[20];
int i;
fgets(input,50,stdin);
//assigning the values
strcpy(result->records.first, strtok(input , " "));
strcpy(result->records.last, strtok(NULL, " "));
strcpy(temp, strtok(NULL, " "));
strcpy(result->records.zip, strtok(NULL, " "));
result->records.score = atof(temp);
result->next = NULL;
return result;
}
node_t * newHead(node_t ** head, node_t * add)
{
add->next = *head;
*head = add;
return add;
}
void printList(node_t * head)
{
node_t * temp = head;
while(temp != NULL)
{
printf("First Name: %s, Last Name: %s,Score: %0.1f,Zip code: %s\n",temp->records.last,temp->records.first,temp->records.score,temp->records.zip);
temp = temp->next;
}
}
void printMenu()
{
printf("Print records (press 1)\nAdd a new record (press 2)\nDelete record(s) (press 3)\nSearch by zip code (press 4)\nSearch by score range (press 5)\nFind median score (press 7)\nExit the program (press 0)");
}
void insertAfterNode(node_t * nodeAfter, node_t * newNode)
{
//order is important!
newNode->next = nodeAfter->next;
nodeAfter->next = newNode;
}
void insert(int n)
{
node_t * temp1 = createNewNode();
node_t * temp2 = head;
for(int i = 1; i < n-1;i++)
{
temp2 = temp2->next;
}
insertAfterNode(temp2,temp1);
}

C: Read Struct into Link List (from File)

I am new to C programming. I have created a student database to enter student details into a linked list (in the form of structure "phbook") and save linked list onto text file. The part I cannot get to work is to read the text file records onto the linked list. The program runs but does not update the linked list when I close the program then select to "LOAD FROM EXTERNAL FILE". Please can someone help me figure what the problem is. I know everything worked until I coded the "readFile" and "insertFull" function. Friends have told me that the global variable "struct phbook *list = NULL;is causing the problem. Please let me know.
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct phbook{
int number;
char name[20];
int mark;
struct part *next;
};
struct phbook *list = NULL;
struct phbook *find_student(int number);
void insertFull(struct phbook* list, int number, char Name[10],int mark);
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code){
case 1 : insert();
break;
case 2 : search();
break;
break;
case 3 : print();
break;
case 4 :
saveToFile();
break;
case 5 :
readFile();
break;
default: printf("Illegal code\n");
}
printf("\n");
}
}
struct phbook *find_student(int number)
{
struct phbook *p;
for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
if (p != NULL && number == p->number)
return p;
return NULL;
}
void insert(void)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
printf("enter student id");
scanf("%d", &new_node->number);
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
printf("Enter name: ");
scanf("%s", &new_node->name);//readline(new_node->name, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->mark);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
void search(void)
{
int number;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &number);
p = find_student(number);
if (p != NULL){
printf("Name: %s\n", p->name);
printf("Marks: %d\n", p->mark);
}
else
printf("student not found.\n");
}
void print(void)
{
struct phbook *p;
printf("Student_Number Student_Name Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile()
{
FILE* fp;
fp = fopen ("results.txt","w");
struct phbook* curr = list;//he
while(curr!=NULL)
{
fprintf(fp,"%s\n", list->name);
fprintf(fp,"%d\n",curr->number);
fprintf(fp, "%d\n", curr->mark);
curr = curr->next;
}
fclose(fp);
}
void readFile()
{
FILE* fp;
if (!(fp = fopen ("results.txt", "r")))
printf("File NOT Found");
else{
struct phbook *curr;
struct phbook *prev;
char TempName[10];
int TempNumber;
int TempMark;
int done = 0;
int count = 0;
int success;//dummy
curr = list;//sets it to the head (first nde of ll)
if(list == NULL);
printf("List is null\n");
if(curr == NULL)
printf("List is null\n");
while (curr!=NULL)
{
curr = curr->next;
}
while (done == 0)
{
success = fscanf(fp, "%s", TempName);
if (success == 1)
{
success = fscanf(fp, "%d", &TempNumber);
if (success == 1)
{
success = fscanf(fp, "%d", &TempMark);
if(success == 1)
{
insertFull(list,TempNumber,TempName,TempMark);
}
}
}
else
{
done = 1;
}
}
}
}
void insertFull(struct phbook* list, int number, char Name[10],int mark)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
new_node->number = number;
new_node->mark = mark;
strcpy(new_node->name,Name);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
In the procedure
void insertFull(struct phbook* list, int number, char Name[10],int mark)
you are changing your local copy of list, this has no effect on the global phone book list.
You can remove list from the argument lists, thus changing the global variable:
void insertFull(int number, char Name[10],int mark)
this is not strictly good programming, but it is in line with the rest of your code.

Inserting a name and number to the end of linked list

My text file reads like this:
George Washington, 2345678
John Adams, 3456789
Thomas Jefferson, 4567890
James Madison, 0987654
James Monroe, 9876543
John Quincy Adams, 8765432
Andrew Jackson, 7654321
Martin Van Buren, 6543210
Can someone offer insight on how I get my insert function to add the name and ID number to the end of the linked list? When I run the code an select option 1 it skips over the add name and only asks to enter the integer. After that nothing happens.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Creates node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
}*head;
//Create Function Prototypes
void readDataFile ();
void insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char dname);
//Main function
int main()
{
//Declare variables
int i, num, id;
char *name;
char nameDelete [50];
char nameInsert [50];
struct node *n;
//initialize link list
head = NULL;
//Read in file
readDataFile();
//Create list of operations utilized in program
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:\n");
scanf("%[^\n]s", nameInsert);
getchar();
printf("Enter the ID associated with the name: ");
scanf("%d", &id);
insert(nameInsert, id);
break;
case 2:
if (head == NULL)
printf("List is Empty\n");
else
{
printf("Elements in the list are:\n");
}
display(n);
break;
case 3:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the ID number to delete: ");
scanf("%d", &id);
}
if(deleteID(id))
printf("%d deleted successfully \n", id);
else
printf("%d not found in the list\n", id);
break;
case 4:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter name to delete: ");
gets(nameDelete);
}
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
//Define the functions
//Function to read in the data file
void readDataFile()
{
//Initialize the link the list
struct node *temp;
temp = malloc(sizeof(struct node));
temp->next = NULL;
head = temp;
//Open up the file
FILE *fp;
fp = fopen("AssignmentOneInput.txt", "r");
//Use memset function to copy the characters in string
char string[100];
memset(string, 0, 100);
//Create while loop scan in the contents of the file
while(fgets(string, 100, fp) != NULL)
{
sscanf(string, "%20[^,], %d", temp->name, &temp->id);
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
}
fclose(fp);
}
//Function to insert a name and ID number
void insert(char *inName, char *inID)
{
//Create temporary and helper nodes
struct node *temp, *helper;
//Allocate the memory for the temporary node
temp = (struct node *)malloc(sizeof(struct node));
//Convert character into integer value
int intID = atoi(inID);
//Set the data in the node
strcpy(temp->name, inName);
temp->id = intID;
temp->next = NULL;
//Create new node and add to it
helper = (struct node *)head;
while(helper->next != NULL)
{
helper = helper->next;
}
helper->next = temp;
helper = temp;
helper->next = NULL;
}
Once you enter 1 in selection option the standard input has both 1 and newline character, which you are expecting immediately in the next scanf statement.
change the scanf("%[^\n]s", nameInsert); into scanf("%s", nameInsert);
or use getchar() immediately after getting selection option.

Resources