0% found this document useful (0 votes)
89 views10 pages

Phonebook

How to create a phonebook in C programming code is uploaded. This code utilizes a linked list representation and file operations to create the .csv file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views10 pages

Phonebook

How to create a phonebook in C programming code is uploaded. This code utilizes a linked list representation and file operations to create the .csv file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

/*

Write a program to implement a phonebook using linked list


1. First Name
2. Last Name
3. Phone Number
The input for the phonebook is taken from the users(minimum 10 entries) and
stored in the link list in alphabetical order.
Next, create a .csv file and store the details in phonebook.
*/

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

int count=0;
struct node
{
char firstname[20];
char lastname[20];
uint64_t number;
struct node *next;
};

struct node *start=NULL;

struct node *getnode()


{
return((struct node *)malloc(sizeof(struct node)));
}

void display(struct node *start)


{
struct node *temp;
temp=start;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%llu\n",temp->number);
temp=temp->next;
}
}

void sort()
{
struct node *temp,*pretemp;
char p[100];
int a,i;
temp=start;
pretemp=start->next;
for(i=0;i<count;i++)
{

temp=start;
pretemp=start->next;
while(temp->next!=NULL)
{
if(strcmp(pretemp->lastname,temp->lastname)<0)
{
strcpy(p,temp->lastname);
strcpy(temp->lastname,pretemp->lastname);
strcpy(pretemp->lastname,p);
strcpy(p,temp->firstname);
strcpy(temp->firstname,pretemp->firstname);
strcpy(pretemp->firstname,p);
a=temp->number;
temp->number=pretemp->number;
pretemp->number=a;
}
else if (strcmp(pretemp->lastname,temp->lastname)==0)
{
if(strcmp(pretemp->firstname,temp->firstname)<0)
{
strcpy(p,temp->lastname);
strcpy(temp->lastname,pretemp->lastname);
strcpy(pretemp->lastname,p);
strcpy(p,temp->firstname);
strcpy(temp->firstname,pretemp->firstname);
strcpy(pretemp->firstname,p);
a=temp->number;
temp->number=pretemp->number;
pretemp->number=a;
}
}
temp=temp->next;
pretemp=pretemp->next;
}
}
}

void insert()
{
struct node *temp,*nn;
nn=getnode();
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;

}
printf("Enter First name:\n");
scanf("%s",&nn->firstname);
printf("Enter Last name:\n");
scanf("%s",&nn->lastname);
printf("Enter number:\n");
scanf("%llu",&nn->number);
temp->next=nn;
nn->next=NULL;
display(start);
count++;
}

struct node *create()


{
struct node *temp,*nn;
if(start!=NULL)
insert();
else
{
nn=getnode();
start=nn;
temp=start;
printf("Enter First name:\n");
scanf("%s",&nn->firstname);
printf("Enter Last name:\n");
scanf("%s",&nn->lastname);
printf("Enter number:\n");
scanf("%llu",&nn->number);
nn->next=NULL;
display(start);
}
count++;
}
void search()
{
struct node *temp;
char f[50],l[50];
temp=start;
printf("Enter name to be searched:\n");
scanf("%s",f);
scanf("%s",l);
while((strcmp(temp->firstname,f)&&strcmp(temp->lastname,l))!=0)
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%llu\n",temp->number);
}

void del()
{
struct node *pretemp,*temp;
char f[50],l[50];
temp=start;
pretemp=start->next;
printf("Enter first name:");
scanf("%s",f);
printf("Enter last name:");
scanf("%s",l);
if((strcmp(temp->firstname,f)&&strcmp(temp->lastname,l))==0)
{
start=temp->next;
free(temp);
}
else
{
while((strcmp(pretemp->firstname,f)&&strcmp(pretemp->lastname,l))!=0)
{
temp=temp->next;
pretemp=pretemp->next;
}

temp->next=pretemp->next;
free(pretemp);
}

void fileop()
{
struct node *temp=start;
int choice;
FILE *f;
char string[50];
printf("1. Read the details stored\n");
printf("2. Write to the file\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
f=fopen("Phonebook.txt","r");
printf("---Phonebook---\n");
while(fgets(string,50,f)!=NULL)
{
printf("%s\t\n",string);
}
fclose(f);
break;
case 2:
f=fopen("Phonebook.txt","w");
printf("Data is now being entered into the text file\n");
while(temp!=NULL)
{
fprintf(f,"%s,%s,%llu\n",temp->lastname,temp->firstname,temp-
>number);
temp=temp->next;
}
printf("Done!\n");
fclose(f);
break;
default:
printf("Invalid!\n");
break;
}
}

int main()
{
int op,ch;
do{
printf("-------Welcome--------\n ");
printf("1.Create\n2.Insert\n3.Display\n4.Search\n5.Sort\n6.Delete\n7.File
creation\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: insert();
break;
case 3: display(start);
break;
case 4:search();
break;
case 5:sort();
break;
case 6:del();
break;
case 7:fileop();
break;
}
printf("Do you want to quit ? 1 for yes / 0 for no:");
scanf("%d",&op);
}while(op!=0);
return 0;
}

/*
OUTPUT:
roschlynn@Roschlynn-Asus:/mnt/c/Users/ROSCHLYNN D'SOUZA/Documents/DSA_LAB$ gcc
15_SEIT_Roschlynn_A4.c
roschlynn@Roschlynn-Asus:/mnt/c/Users/ROSCHLYNN D'SOUZA/Documents/DSA_LAB$ ./a.out
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:1
Enter First name:
Ivan
Enter Last name:
Dsouza
Enter number:
9326164463
Ivan
Dsouza
9326164463
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:2
Enter First name:
Deepak
Enter Last name:
Bhandare
Enter number:
899714551
Ivan
Dsouza
9326164463
Deepak
Bhandare
899714551
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:2
Enter First name:
Roschlynn
Enter Last name:
Dsouza
Enter number:
8104045917
Ivan
Dsouza
9326164463
Deepak
Bhandare
899714551
Roschlynn
Dsouza
8104045917
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:2
Enter First name:
Nitin
Enter Last name:
Kak
Enter number:
9869291003
Ivan
Dsouza
9326164463
Deepak
Bhandare
899714551
Roschlynn
Dsouza
8104045917
Nitin
Kak
9869291003
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:2
Enter First name:
Vardhan
Enter Last name:
Sharma
Enter number:
876554512
Ivan
Dsouza
9326164463
Deepak
Bhandare
899714551
Roschlynn
Dsouza
8104045917
Nitin
Kak
9869291003
Vardhan
Sharma
876554512
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:3
Ivan
Dsouza
9326164463
Deepak
Bhandare
899714551
Roschlynn
Dsouza
8104045917
Nitin
Kak
9869291003
Vardhan
Sharma
876554512
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:4
Enter name to be searched:
Ivan
Dsouza
Ivan
Dsouza
9326164463
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:5
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:3
Deepak
Bhandare
899714551
Ivan
Dsouza
736229871
Roschlynn
Dsouza
8104045917
Nitin
Kak
9869291003
Vardhan
Sharma
876554512
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:7
1. Read the details stored
2. Write to the file
Enter your choice:2
Data is now being entered into the text file
Done!
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:7
1. Read the details stored
2. Write to the file
Enter your choice:1
---Phonebook---
Bhandare,Deepak,899714551

Dsouza,Ivan,736229871

Dsouza,Roschlynn,8104045917

Kak,Nitin,9869291003

Sharma,Vardhan,876554512

Do you want to quit ? 1 for yes / 0 for no:1


-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:6
Enter first name:Nitin
Enter last name:Kak
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:3
Deepak
Bhandare
899714551
Ivan
Dsouza
736229871
Roschlynn
Dsouza
8104045917
Vardhan
Sharma
876554512
Do you want to quit ? 1 for yes / 0 for no:1
-------Welcome--------
1.Create
2.Insert
3.Display
4.Search
5.Sort
6.Delete
7.File creation
Enter your choice:0
Do you want to quit ? 1 for yes / 0 for no:0
roschlynn@Roschlynn-Asus:/mnt/c/Users/ROSCHLYNN D'SOUZA/Documents/DSA_LAB$

.csv file:-
To create a .csv file, create a copy of the .txt file, save it on the desktop and
change the .txt extension to .csv
*/

You might also like