Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Phone Book*/
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct node{
- char name[100];
- char number[20];
- char about[1000];
- struct node *next;
- struct node *prev;
- }*start=NULL;
- void sort(struct node *newnode)
- {
- struct node *current,*currentprev,*temp;
- current = start;
- if(start == NULL)
- start = newnode;
- else if(start->next == NULL){
- if(strcmp(newnode,current)>0){
- current->next = newnode;
- newnode -> prev = current;
- }
- else{
- newnode->next = current;
- current->prev = newnode;
- start = newnode;
- }
- }
- else{
- while(current!=NULL){
- if(strcmp(newnode,current)<0)
- break;
- if(current->next==NULL)
- temp=current;
- current = current->next;
- }
- if(current == start){
- newnode->next = current;
- current->prev = newnode;
- start = newnode;
- }
- else if(current == NULL){
- temp->next = newnode;
- newnode->prev = temp;
- }
- else{
- currentprev = current->prev;
- currentprev->next = newnode;
- newnode->prev = currentprev;
- newnode->next = current;
- current->prev = newnode;
- }
- }
- }
- int checkernum(struct node *newnode)
- {
- int i=0;
- struct node *current;
- current=start;
- while(current!=NULL){
- if(strcmp(current->number,newnode->number)==0){
- i++;
- break;
- }
- current=current->next;
- }
- return i;
- }
- int checkername(struct node *newnode)
- {
- int i=0;
- struct node *current;
- current=start;
- while(current!=NULL){
- if(strcmp(current->name,newnode->name)==0){
- i++;
- break;
- }
- current=current->next;
- }
- return i;
- }
- void insert()
- {
- struct node *newnode,*current;
- newnode=(struct node*)malloc(sizeof(struct node));
- newnode->next=NULL;
- newnode->prev=NULL;
- printf("name : ");
- getchar();
- scanf("%[^\n]",newnode->name);
- printf("number: ");
- getchar();
- scanf("%s",newnode->number);
- printf("about : ");
- getchar();
- scanf("%[^\n]",newnode->about);
- int i=checkername(newnode);
- int j=checkernum(newnode);
- if(i==0 && j==0){
- sort(newnode);
- printf("\n*save successful*\n");
- }
- else{
- printf("\nContact Exist, can't save it\n");
- }
- }
- void del(struct node *current)
- {
- if(current == start)
- start = current->next;
- else if(current->next == NULL)
- (current->prev)->next = NULL;
- else{
- struct node *currentprev, *currentnext;
- currentprev = current->prev;
- currentnext = current->next;
- currentprev->next = currentnext;
- currentnext->prev = currentprev;
- }
- }
- void edit(struct node *current)
- {
- del(current);
- char n;
- printf("1. Change name\n");
- printf("2. Skip\n");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- printf("name : ");
- getchar();
- scanf("%[^\n]",current->name);
- }
- printf("1. Change number\n");
- printf("2. Skip\n");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- printf("number: ");
- getchar();
- scanf("%s",current->number);
- }
- printf("1. Change about\n");
- printf("2. Skip\n");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- printf("about : ");
- getchar();
- scanf("%[^\n]",current->about);
- }
- current->next = NULL;
- current->prev = NULL;
- sort(current);
- }
- void byname()
- {
- struct node *current;
- if(start==NULL){
- printf("\nNo Contact saved for search\n");
- return;
- }
- char str[100];
- printf("Enter name for search : ");
- getchar();
- scanf("%[^\n]",str);
- current = start;
- int i=0;
- char n;
- while(current!=NULL){
- if(strcmp(current->name,str)==0){
- printf("\nName found\n");
- printf("name : %s\n",current->name);
- printf("Number : %s\n",current->number);
- printf("about : %s\n",current->about);
- printf("\n");
- i++;
- break;
- }
- current = current->next;
- }
- if(i==0)
- printf("\nNot Found\n");
- else{
- printf("1. Edit\n");
- printf("2. Delete\n");
- printf("0. Home\n");
- printf("Chose an option : ");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- edit(current);
- printf("\n*edit successful*\n");
- }
- else if(n == '2'){
- del(current);
- printf("\n*Delete successful*\n");
- }
- else if(n == '0')
- return;
- }
- }
- void bynumber()
- {
- struct node *current;
- if(start==NULL){
- printf("\nNo Contact saved for search\n");
- return;
- }
- char str[100];
- printf("Enter number for search : ");
- getchar();
- scanf("%[^\n]",str);
- current = start;
- int i=0;
- char n;
- while(current!=NULL){
- if(strcmp(current->number,str)==0){
- printf("\nNumber found\n");
- printf("name : %s\n",current->name);
- printf("Number : %s\n",current->number);
- printf("about : %s\n",current->about);
- printf("\n");
- i++;
- break;
- }
- current = current->next;
- }
- if(i==0)
- printf("\nNot Found\n");
- else{
- printf("1. Edit\n");
- printf("2. Delete\n");
- printf("0. Home\n");
- printf("Chose an option : ");
- getchar();
- scanf("%c",&n);
- if(n == '1')
- edit(current);
- else if(n == '2')
- del(current);
- else if(n == '0')
- return;
- }
- }
- void count()
- {
- struct node *current;
- int count = 0;
- current = start;
- while(current!=NULL){
- count++;
- current = current->next;
- }
- printf("\nTotal contacts %d\n",count);
- }
- void displayall()
- {
- if(start == NULL)
- printf("\nNo contact for display\n");
- else{
- count();
- struct node *current;
- current = start;
- int i=0;
- while(current!=NULL){
- i++;
- printf("\nContact no %d :-\n",i);
- printf("name : %s\n",current->name);
- printf("Number : %s\n",current->number);
- printf("about : %s\n",current->about);
- printf("\n");
- current = current->next;
- }
- }
- printf("\n");
- }
- void home()
- {
- char n;
- printf("\nChose an option : ");
- getchar();
- scanf("%c",&n);
- if(n=='0')
- exit(0);
- else if(n=='1')
- insert();
- else if(n=='2')
- displayall();
- else if(n=='3')
- byname();
- else if(n=='4')
- bynumber();
- else
- return;
- }
- int main()
- {
- char n[100],x[100];
- strcpy(x,"12345");
- printf("\n***PHONE BOOK***\n\n");
- while(1){
- printf("\nEnter PIN: ");
- scanf("%s",n);
- if(strcmp(n,x)!=0){
- printf("Wrong password\n");
- continue;
- }
- printf("Password matched - WELCOME\n");
- while(1){
- printf("\n***HOME***\n\n");
- printf("1. Save a new contact\n");
- printf("2. Show all contact\n");
- printf("3. Search by name\n");
- printf("4. Search by number\n");
- printf("0. exit\n");
- home();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement