-1

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

2 Answers2

0

I had made the following changes in your search and delete function

  1. 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
  2. The way you were catching user input for first and last name in scanf was also improper
  3. 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.
  4. In search function you were not checking end of list.
  5. 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;
        }

    }
}
Dark Sorrow
  • 1,681
  • 14
  • 37
  • i tried your changes but still have the same error and the compiler always put a blue line on pretemp = pretemp->next – Houda Salah Dec 26 '19 at 17:58
0

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;
}
risbo
  • 188
  • 7