Hello, could anyone here please review this code:

the deleteEntry () function are not well working. I dunno how to solve this.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Node
{
    char firstName [15];
    char lastName [15];
    char Number [15];
    char searchName [15];
    struct Node *next;
} ;

struct Node *Head = NULL;

int printInfoRecieveOption () ;
void clearScreen () ;
void insertEntry ();
void printAll ();
void findEntry (char name []) ;
void deleteEntry () ;


int main ()
{
    int restart, userOption, otherOption, check, findUser;
    char userName [15];

    do{
    userOption = printInfoRecieveOption ();
    if(userOption == 1)
       {
          insertEntry ();
          printf("Contact Added Successfuly!\n\n\n");
       }
    else if(userOption == 2)
       {
          printf("Lookup A Contact\n");
          printf("___________________\n");
          printf("Please Enter The Last Name Of The Contact You Are Trying To Find\n");
          scanf("%s", &userName);
          getchar () ;
          clearScreen () ;
          printf("Contact To Find: %s\n", userName);
          printf("Is This Correct?\n");
          printf("(1) Yes\n");
          printf("(2) No\n");
          scanf("%i", &otherOption);
          getchar () ;
          clearScreen () ;
          if(otherOption > 2 || otherOption < 1)
            check = 3;
          else if(otherOption == 1 || otherOption == 2)
            check = 1;
          while(check == 3)
               {
          printf("Contact To Find: %s\n", userName);
          printf("Is This Correct?\n");
          printf("(1) Yes\n");
          printf("(2) No\n");
          scanf("%i", &otherOption);
          getchar () ;
          clearScreen () ;
          if(otherOption > 2 || otherOption < 1)
            check = 3;
          else if(otherOption == 1 || otherOption == 2)
            check = 1;
              }

           findEntry (userName) ;
       }
    else if(userOption == 3)
       {
          deleteEntry () ;
       }
    else if(userOption == 4)
       {
          printAll ();
       }
    }  while(userOption != 5);
    if(userOption == 5)
    {
      clearScreen () ;
      printf("Goodbye!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      system("pause");
      return 0;
    }

}


void deleteEntry ()
{
   struct Node *curr = Head;
   char name [15], decide;
   int Decide;
   printf("Last Name To Delete\n");
   scanf("%s", &name);
   getchar ();
   while(curr != NULL)
      {
         if(strcmp(name,curr->lastName))
         {
              printf("First Name: %s\n", curr->firstName);
              printf("Last Name: %s\n", curr->lastName);
              printf("Phone Number: %s\n", curr->Number);
              printf("Is This The Person You Would Like To Delete ? <Y/N>\n");
              scanf("%c", &decide);
              getchar ();

              if(decide == 'y' || decide == 'Y')
              Decide = 1;

              if(Decide == 1)
                free(curr);
         }
         curr = curr->next;
      }
}

void findEntry (char name [])
{
    struct Node *current = Head;
    char Name [15];
    int find = 0;

    if (current == NULL)
    {
        printf("----------------------------------------\n");
        printf("There Are No Contacts In The Phonebook\n");
        printf("----------------------------------------\n");
    }
    else
    {
        while (current != NULL)
        {
            strcmp(Name,current->lastName);// , sizeof(current->lastName));
            if(strcmp(name,Name))
            {
            find = 1;
            printf("Phone Number: %s\n", current->Number);
            }
            current = current ->next;
        }
    }

    if(find == 1)
        printf("Contact Was Found\n");
        //printf("Phone Number: %s\n", current->Number);
    else if(find != 1)
        printf("Contact Was Not Found\n");
}







void printAll ()
{
        struct Node *current = Head;

    if (current == NULL)
    {
        printf("----------------------------------------\n");
        printf("There Are No Contacts In The Phonebook\n");
        printf("----------------------------------------\n");
    }
    else
    {
        while (current != NULL)
        {
            printf("| First Name: %s\n", current->firstName);
            printf("| Last Name: %s\n", current->lastName);
            printf("| Phone Number: %s\n\n\n", current->Number);
            current = current ->next;
        }
    }

    printf("\n");

}






void insertEntry ()
{
     char first [15], last [15], number [15];
     int option1, option2, check;

     do{   
     printf("Add A Contact\n");
     printf("_________________\n");
     printf("Please Enter Contacts First Name : ");
           scanf("%s", &first);
           getchar ();
           printf("\n");
     printf("Please Enter Contacts Last Name : ");
           scanf("%s", &last);
           getchar ();
           printf("\n");
     printf("Please Enter Contacts Phone Number (XXX-XXX-XXXX) : ");
           scanf("%s", &number);
           getchar ();
           clearScreen () ;
     printf("The Following Entry Will Be Added :\n");
            printf("First Name: %s\n", first);
            printf("Last Name: %s\n", last);
            printf("Phone Number: %s\n", number);
            printf("________________________\n");
            printf("Add This To The Phonebook ? \n");
            printf("( 1 ) Yes\n");
            printf("( 2 ) No\n");
            scanf("%i", &option1);
            getchar () ;
     clearScreen () ;
     if(option1 == 1 || option1 == 2)
     check = 2;
     else if(option1 > 2 || option1 < 1)
     check = 3;
     while(check == 3)
     {
        printf("ERROR: Please Use Either 1 For Yes or 2 For No\n");
        printf("First Name: %s\n", first);
        printf("Last Name: %s\n", last);
        printf("Phone Number: %s\n", number);
        printf("________________________\n");
        printf("Add This To The Phonebook ? \n");
        printf("( 1 ) Yes\n");
        printf("( 2 ) No\n");
        scanf("%i", &option1);
        getchar () ;
        clearScreen ();
        if(option1< 1 || option1 > 3)
        check = 3;
        else if(option1 == 1 || option1 == 2)
        check = 1; 
     }
} while(option1 == 2);
//---------------------------------------------------------------------------

        struct Node *temp;
    temp = (struct Node *) malloc (sizeof(struct Node));

    if (temp != NULL)
    {
        strncpy(temp->firstName, first, sizeof(temp->firstName));
        strncpy(temp->lastName, last, sizeof(temp->lastName));
        strncpy(temp->Number, number, sizeof(temp->Number));

        if (Head == NULL)
        {
            Head = temp;
            Head -> next = NULL;
        }
        else
        {
            temp->next = Head;
            Head = temp;
        }
    }
}






int printInfoRecieveOption ()
{
    int restart;
    printf("1. Add A Contact\n");
    printf("2. Lookup A Contact\n");
    printf("3. Delete A Contact\n");
    printf("4. Display The Entire Phonebook\n");
    printf("5. Exit The Program\n");
    printf("_____________________________________\n");
    printf("What Would You Like To Do ?\n");
    scanf("%i", &restart);
    getchar () ;
    clearScreen () ;
    while(restart > 5 || restart <1)
    {
       printf("PLEASE USE THE NUMBERS 1 - 5\n");
       printf("1. Add A Contact\n");
       printf("2. Lookup A Contact\n");
       printf("3. Delete A Contact\n");
       printf("4. Display The Entire Phonebook\n");
       printf("5. Exit The Program\n");
       printf("_____________________________________\n");
       printf("What Would You Like To Do ?\n");
       scanf("%i", &restart);
       getchar () ;
       clearScreen () ;
    }

    return restart;
}


void clearScreen ()
{
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");     
}

Recommended Answers

All 4 Replies

Might be because there is no free(...) function defined.

But to be honest, "function are not well working" is a little vague.

The problem in deleteEntry is that you just free the memory for an entry without updating any of the pointers in the list at line 24

void deleteEntry ()
{
   struct Node *curr = Head;
   char name [15], decide;
   int Decide;
   printf("Last Name To Delete\n");
   scanf("%s", &name);
   getchar ();
   while(curr != NULL)
      {
         if(strcmp(name,curr->lastName))
         {
              printf("First Name: %s\n", curr->firstName);
              printf("Last Name: %s\n", curr->lastName);
              printf("Phone Number: %s\n", curr->Number);
              printf("Is This The Person You Would Like To Delete ? <Y/N>\n");
              scanf("%c", &decide);
              getchar ();

              if(decide == 'y' || decide == 'Y')
              Decide = 1;

              if(Decide == 1)
                free(curr);
         }
         curr = curr->next;
      }
}

Worst having done this at line26 you then go on to reference the memory pointed to by curr that has just been deleted.

In a singly linked list of say 5 items; A, B, C, D, E

If you want to delete an item from the middle, C, then you have to update the next pointer in B to point to D. That means that when you find the item you want to delete you have to still have a pointer to the item before that.

You also have to have a special case for the head item because in that case you have to update the head pointer rather than a pointer in the data structure.

The pseudo code might look a little like

If Head != NULL
  If Delete HEAD Item
    deletePtr = Head
    Head = Head->Next
    free deletePtr
  Else
    current = Head
    While(current->Next != NULL)
      If Delete current->Next item
         deletePtr = current->Next
         current->Next = current->Next->Next
         free deletePtr
      EndIf
    Wend
  EndIf
EndIf

Note inparticular that we only every delete an object after we have finsihed using the data it contains.

Line 137 should be strcat not strcmp

In a couple of places scanf("%s", &last); the & is not required, you might even say wrong it should be scanf("%s", last);

//in first:

//line:3:

while(curr != NULL)
      {
       if(strcmp( (name+'\0'),curr->lastName)) 

//in second:
/* you must add '\0' symbol for name 'cause scanf - it is not do this. After you must realocated mermory and copy not delete elements only.*/

@Nikolas9896

Firstly scanf does add '\0' to the end of strings it reads. If you have an implementation that doesn't it is non-conforming and you should get a new compiler.

Secondly (name+'\0') is not valid syntax; operator+ has no meaning for an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.