Well, I have to return the index that it is being added at...is that not right? I just realized when looking at my code that I didn't have the right return statements, because I am supposed to return -1 if it doesn't work and the index if it does work.
Here is my whole code, excluding the other functions:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct customer_data CUSTOMER_DATA;
void printmenu();
//Structure
struct customer_data
{
char last_name[20];
char first_name[20];
int id;
float balance;
CUSTOMER_DATA *next;
};
CUSTOMER_DATA * customer_head = NULL;
CUSTOMER_DATA * customer_tail = NULL;
// Adding a new Node after specifies number of Nodes
int insertAtIndex(char * lastname, char * firstname, int id,
float balance, int index)
{
int i;
struct customer_data *temp,*prev_ptr,*cur_ptr, *p;
cur_ptr=customer_head;
if(index > size()+1 || index < 0)
{
printf("\nInsertion at given location is not possible\n ");
return -1;
}
else
{
for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
{
//while (p->next != cur_ptr)
//{
prev_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
return index;
if( cur_ptr->next == NULL )
break;
// }
}
}
temp=(struct customer_data*) malloc (sizeof (struct customer_data));
strcpy(temp->last_name,lastname);
strcpy(temp->first_name,firstname);
temp->id=id;
temp->balance=balance;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
void printMenu()
{
printf("Choice\t\tAction\n");
printf("------\t\t------\n");
printf("A\t\tAdd Customer\n");
printf("D\t\tDisplay List Size\n");
printf("P\t\tPrint List Elements\n");
printf("R\t\tRemove Customer\n");
printf("S\t\tSearch Customer\n");
printf("Q\t\tQuit\n");
printf("?\t\tDisplay Help\n\n");
return;
}
int main()
{
int i, id, index;
float balance;
char last_name[20], first_name[20];
char choice;
struct customer_data *info;
printMenu();
choice = 'Z';
do
{
printf("\nWhat action would you like to perform?\n");
choice = getchar();
getchar(); //to flush '\n'
choice = toupper(choice);
switch(choice)
{
case 'A':
printf("Please enter a customer to add:\nPlease enter the last name:\n");
getchar(); //to flush '\n'
scanf("%s", &last_name);
printf("Please enter the first name:\n");
getchar(); //to flush '\n'
scanf("%s", &first_name);
printf("Please enter the id (number):\n");
getchar(); //to flush '\n'
scanf("%d", &id);
printf("Please enter the balance:\n");
getchar(); //to flush '\n'
scanf("%f", &balance);
printf("Please enter the index to insert:\n");
getchar(); //to flush '\n'
scanf("%d", &index);
insertAtIndex(last_name, first_name, id, balance, index);
if (insertAtIndex(last_name, first_name, id, balance, index) != -1)
{ printf("The customer is added at %d", index); }
else
{ printf("The customer could not be added."); }
break;
case 'D':
printf("The size of the linked list is %d", size(), "\n");
break;
case 'P':
displayLinkedList();
break;
case 'R':
printf("Please enter an id(number) to remove:\n");
scanf("%d", id);
if(id != -1)
{ printf("Customer was removed\n"); }
else
{ printf("Customer with the id was not found\n"); }
break;
case 'S':
printf("Please enter an id(number) to search:\n");
scanf("%d", id);
if(searchCustomer(id) == NULL)
{ printf("Customer with the id was not found\n"); }
else
{ searchCustomer(id); }
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
printf("Unknown action\n");
break;
}
} while (choice != 'Q');
return 0;
}