Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.c
* Author: access
*
* Created on September 4, 2017, 2:04 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int char_value;
int int_convert_member;
int int_convert_parameter;
long input_convert;
char user_input_c [3];
char contact_name[24];
const int contact_num_len = 24;
const int contact_name_len = 24;
void* sort_ptr = NULL;
void* find_ptr = NULL;
struct GSM_contact_s {
struct GSM_contact_s *next;
int contact_num[24];
char contact_name[24];
};
/*
* A pointer stack for traversal, addition and location
*/
struct GSM_contact_s *contact_head = NULL;
struct GSM_contact_s *contact_tmp = NULL;
struct GSM_contact_s *contact_ptr = NULL;
struct GSM_contact_s *contact_prev = NULL;
struct GSM_contact_s *contact_new = NULL;
//struct GSM_contact_s *contact_find = NULL;
struct GSM_contact_s *search_arr[256];
struct GSM_contact_s *contact_travers_abc(char* contact_name, int index1);
void *contact_travers_find(const char* contact_name);
void contact_search(const char* contact_name);
void contact_delete(const char* contact_name);
void list_disp(struct GSM_contact_s *ptr);
void list_disp_by_search(struct GSM_contact_s *ptr);
void contact_create();
void clear_name();
void clear_input();
/*
*
*/
int main(int argc, char** argv) {
do {
printf("\n\nWelcome to your address book!! :) \n\n\n");
printf("What would you like to do? \n\n");
printf("1. Add a contact\n");
printf("2. Search for a contact\n");
printf("3. Delete a contact\n");
printf("4. Display all contacts\n");
printf("5. Exit\n\n");
printf("Enter your choice:- ");
fgets(user_input_c,sizeof(user_input_c),stdin);
input_convert = strtol(&user_input_c[0], NULL, 10);
printf("user_input_c = %d\n", user_input_c[0]);
printf("input_convert = %ld\n\n", input_convert);
switch(input_convert){
case(1):
clear_name();
printf("\n\nYou have chosen to add a new contact. \n\nPlease enter a name...\n");
fgets(contact_name,sizeof(contact_name),stdin);
printf("You have entered %s\n\n", contact_name);
contact_create(contact_name, 0);
break;
case(2):
clear_name();
printf("\n\nYou have chosen to find a contact. \n\nPlease enter a name...\n");
fgets(contact_name,sizeof(contact_name),stdin);
printf("\nLooking up name\n");
contact_search(contact_name);
break;
case(3):
clear_name();
printf("\n\nYou have chosen to delete a contact. \n\nPlease enter a name...\n");
fgets(contact_name,sizeof(contact_name),stdin);
printf("\nLooking up name\n");
contact_delete(contact_name);
case(4):
clear_name();
list_disp(contact_head);
break;
case(5):
exit(1);
break;
default:
clear_name();
printf("\n\nWrong entry, try again!!!");
}
} while (input_convert != 5);
return (EXIT_SUCCESS);
}
void clear_name(){
for(int i = 0; i < 24; i++){
contact_name[i] = '\0';
}
}
void contact_delete(const char* contact_name){
contact_ptr = contact_travers_find(contact_name);
if(contact_ptr == NULL){
printf("The contact you were looking for could not be located");
} else {
printf("The contact you searched for has been found\n");
printf("The contact is: %s\n", contact_ptr->contact_name);
printf("Located at address %p\n\n", contact_ptr);
contact_prev->next = contact_ptr->next;
contact_ptr = NULL;
printf("The contact has now been deleted\n Have a nice day!! :) \n");
}
contact_tmp = NULL;
contact_prev = NULL;
}
void contact_search(const char* contact_name){
contact_ptr = contact_travers_find(contact_name);
if(contact_ptr == NULL){
printf("The contact you were looking for could not be located");
} else {
list_disp_by_search(contact_ptr); //
}
contact_tmp = NULL;
contact_prev = NULL;
}
void *contact_travers_find(const char* contact_name){
int name_length = strlen(contact_name);
printf("name length = %d\n", name_length);
for(int i = 0; i < 255; i++){
search_arr[i] = NULL;
}
if (contact_tmp == NULL){
contact_tmp = contact_head;
}
bool name = false;
if(contact_tmp != NULL){
int i = 0;
while(contact_tmp){
name = (strncmp(contact_name, contact_tmp->contact_name, name_length -1) == 0);
//printf("TEST %d\n", i++);
if(!name){
printf("!name\n");
if(contact_tmp->next != NULL){
contact_tmp = contact_tmp->next;
} else {
printf("return search_arr[0] = %p \n", search_arr[0]);
return &search_arr[0]; //<----VRETURNING ADDRESS OF ARRAY MEMBER
}
} else if (name) {
printf("name\n");
search_arr[i] = contact_tmp;
printf("i = %d search_arr[i] = %p \n", i, search_arr[i]);
printf("contact_tmp = %p \n", contact_tmp);
i = ++i;
if(contact_tmp->next != NULL){
contact_tmp = contact_tmp->next;
} else {
printf("return search_arr[0] = %p \n", search_arr[0]);
return &search_arr[0]; // <---- RETURNING ADDRESS OF ARRAY MEMBER
}
}
}
}
}
void contact_create(char* contact_name){
contact_ptr = contact_travers_abc(contact_name, 0);
if(contact_ptr != NULL){
strncpy(contact_ptr->contact_name, contact_name, contact_name_len);
printf("\nNew node created at address: %p \nContact name: %s \n", contact_ptr, contact_ptr->contact_name);
} else {
printf("Error, memory was not allocated\n\n\n");
}
contact_ptr = NULL;
printf ("contact_ptr = NULL \n");
}
struct GSM_contact_s *contact_travers_abc(char* contact_name, int index1){
if(contact_head == NULL){
contact_head = malloc(sizeof(struct GSM_contact_s));
if(contact_head == NULL){
printf("\nMemory Error: Initial head not allocated\n\n");
return NULL;
}
contact_head->next = NULL;
for(int i = 0; i < 24; i++){
contact_head->contact_name[i] = '\0';
contact_head->contact_num[i] = 0;
}
contact_tmp = (contact_head != NULL) ? contact_head : NULL;
if(contact_tmp == NULL){
printf ("\nMemory Error: Initial head not allocated\n\n");
return NULL;
}
return contact_tmp;
} else if (contact_tmp == NULL){
contact_tmp = contact_head;
}
while(contact_tmp){
printf("line 130: contact_tmp != NULL\n");
int_convert_member = contact_tmp->contact_name[index1] < 96 ? contact_tmp->contact_name[index1] + 32 : contact_tmp->contact_name[index1];
int_convert_parameter = contact_name[index1] < 96 ? contact_name[index1] + 32 : contact_name[index1];
printf("\nint_convert_member = %d", int_convert_member);
printf("\nint_convert_parameter = %d\n", int_convert_parameter);
if(int_convert_parameter == int_convert_member){
if ((contact_tmp->contact_name[index1] == '\0') && (contact_name[index1] == '\0')){
if(index1 > 0) {
printf("\n\nContact name already exists!!! \n\n");
return NULL;
}
}
printf("line 140: int_conver_parameter == int_convert_member\n");
sort_ptr = contact_travers_abc(contact_name, ++index1);
} else if (int_convert_parameter < int_convert_member) {
printf("line 145: int_conver_parameter < int_convert_member\n");
if(contact_prev != NULL){
contact_new = malloc(sizeof(struct GSM_contact_s));
if(contact_new == NULL){
printf("new contact memory not allocated");
return NULL;
}
for(int i = 0; i < contact_name_len; i++){
contact_new->contact_name[i] = '\0';
contact_new->contact_num[i] = 0;
}
contact_new->next = contact_prev->next;
contact_prev->next = contact_new;
contact_prev = NULL;
contact_tmp = NULL;
return contact_new;
} else {
printf("line 152: contact_prev = NULL\n");
contact_new = malloc(sizeof(struct GSM_contact_s));
if(contact_new == NULL){
printf("new contact memory not allocated");
return NULL;
}
for(int i = 0; i < contact_name_len; i++){
contact_new->contact_name[i] = '\0';
contact_new->contact_num[i] = 0;
}
contact_new->next = contact_head;
contact_head = contact_new;
contact_prev = NULL;
contact_tmp = NULL;
return contact_new;
}
} else if ((int_convert_parameter != int_convert_member) && (int_convert_parameter > int_convert_member)) {
//if the contact is alphabetically next or later in the list, move on to the next node
contact_prev = contact_tmp;
printf("int_convert_parameter != int_convert_member\n");
//if you get to the end of the list, just add another node
if(contact_tmp->next == NULL){
printf("contact_tmp->next == NULL\n");
contact_new = malloc(sizeof(struct GSM_contact_s));
if(contact_new == NULL){
printf("new contact memory not allocated");
return NULL;
}
for(int i = 0; i < contact_name_len; i++){
contact_new->contact_name[i] = '\0';
contact_new->contact_num[i] = 0;
}
contact_tmp->next = contact_new;
contact_new->next = NULL;
contact_prev = NULL;
contact_tmp = NULL;
return contact_new;
} else {
printf("line 174: contact_tmp != NULL\n");
contact_tmp = contact_tmp->next;
sort_ptr = contact_travers_abc(contact_name, 0);
}
}
} //end while
contact_tmp = NULL;
contact_prev = NULL;
return sort_ptr;
}
void list_disp(struct GSM_contact_s *ptr){
contact_tmp = ptr;
while(contact_tmp != NULL){
printf("Name: %s", contact_tmp->contact_name);
contact_tmp = contact_tmp->next ? contact_tmp->next : NULL;
}
}
//THE BELOW FUNCTION IS WHERE I AM STUMPED
void list_disp_by_search(struct GSM_contact_s *ptr){
for(ptr; ptr != NULL; ptr++){ //<----- CLEARLY THIS IS WRONG
printf(ptr->contact_name);
}
}