Question: Help me, please. I'm creating a phone log system in c language using a stack single linked list, it seems that everything is working well

Help me, please.

I'm creating a phone log system in c language using a stack single linked list, it seems that everything is working well but the delete option always gives me a system break after I put the input. Can someone fix it? The delete option should delete only the top memory input and return to the main after the delete process.

And also can you add or modify in a delete, display, and search option, if there is no phone log history or output memory, print "The Phone Number List is Empty".

#include

#include

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;

printf(" FULL NAME\t|\tPHONE NUMBER ");

while(temp!=NULL)

{

printf(" %s %s\t|\t%d ",temp->firstname,temp->lastname,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: ");

scanf("%s",newnode->firstname);

printf("Enter Last name: ");

scanf("%s",newnode->lastname);

printf("Enter number: ");

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: ");

scanf("%s",newnode->firstname);

printf("Enter Last name: ");

scanf("%s",newnode->lastname);

printf("Enter number: ");

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 (FULL NAME) : ");

scanf("%s",first); /* you dont need '&' operator for string*/

scanf("%s",last);

while((temp->firstname==first)&&(temp->lastname==last))

{

temp=temp->next;

}

printf(" FULL NAME\t|\tPHONE NUMBER ");

printf(" %s %s\t|\t%d ",temp->firstname,temp->lastname,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);

main();

}

else

{

temp=temp->next;

pretemp=pretemp->next;

}

} /* missing curly bracket */

}

int main()

{

int op,ch;

do{

printf(" -------Welcome-------- ");

printf("1.Create 2.Display 3.Delete 4.Search ");

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;

default:

printf (">: Invalid Input! No action being taken! ");

break;

}

printf(" Do you want to quit ? 1 for no / 0 for yes: ");

scanf("%d",&op);

}while(op);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!