Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*C-Project *Phone Book* (doubly link list)*/
- #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("Enter Name : ");
- getchar();
- scanf("%[^\n]",newnode->name);
- printf("Enter Number: ");
- getchar();
- scanf("%s",newnode->number);
- printf("Enter About : ");
- getchar();
- scanf("%[^\n]",newnode->about);
- int i=checkername(newnode);
- int j=checkernum(newnode);
- if(i==0 && j==0){
- sort(newnode);
- system("CLS");
- printf("*save successful*\n");
- char n;
- getchar();
- printf("1. save another contact\n");
- printf("2. view recent save contact\n");
- printf("0. Home\n");
- printf("Chose an option: ");
- scanf("%c",&n);
- if(n=='0'){
- system("CLS");
- return;
- }
- else if(n=='1'){
- system("CLS");
- insert();
- }
- else if(n=='2'){
- system("CLS");
- printf("name: %s\n",newnode->name);
- printf("number: %s\n",newnode->number);
- printf("about: %s\n\n",newnode->about);
- }
- }
- else{
- system("CLS");
- printf("Contact Exist, can't save it\n");
- printf("1. save another contact\n");
- printf("0. Home\n");
- char n;
- getchar();
- printf("Chose an option: ");
- scanf("%c",&n);
- if(n=='0'){
- system("CLS");
- return;
- }
- else if(n=='1'){
- system("CLS");
- insert();
- }
- }
- }
- 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");
- printf("\nChose an option : ");
- getchar();
- scanf("%c",&n);
- system("CLS");
- if(n == '1'){
- printf("edit name : ");
- getchar();
- scanf("%[^\n]",current->name);
- }
- system("CLS");
- printf("1. Change number\n");
- printf("2. Skip\n");
- printf("\nChose an option : ");
- getchar();
- scanf("%c",&n);
- system("CLS");
- if(n == '1'){
- printf("edit number: ");
- getchar();
- scanf("%s",current->number);
- }
- system("CLS");
- printf("1. Change about\n");
- printf("2. Skip\n");
- printf("\nChose an option : ");
- getchar();
- scanf("%c",&n);
- system("CLS");
- if(n == '1'){
- printf("edit about : ");
- getchar();
- scanf("%[^\n]",current->about);
- }
- system("CLS");
- 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){
- system("CLS");
- printf("Name 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){
- system("CLS");
- printf("Contact Name Not Found\n");
- return;
- }
- else{
- printf("1. Edit\n");
- printf("2. Delete\n");
- printf("0. Home\n");
- printf("Chose an option : ");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- system("CLS");
- edit(current);
- system("CLS");
- printf("*edit successful*\n");
- }
- else if(n == '2'){
- system("CLS");
- del(current);
- system("CLS");
- printf("*Delete successful*\n");
- }
- else if(n == '0'){
- system("CLS");
- 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){
- system("CLS");
- printf("Number 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){
- system("CLS");
- printf("Contact Number Not Found\n");
- return;
- }
- else{
- printf("1. Edit\n");
- printf("2. Delete\n");
- printf("0. Home\n");
- printf("Chose an option : ");
- getchar();
- scanf("%c",&n);
- if(n == '1'){
- system("CLS");
- edit(current);
- system("CLS");
- printf("*edit successful*\n");
- }
- else if(n == '2'){
- system("CLS");
- del(current);
- system("CLS");
- printf("*Delete successful*\n");
- }
- else if(n == '0'){
- system("CLS");
- 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 byserial(int i)
- {
- int j=1;
- struct node *current;
- current=start;
- while(j<i){
- current=current->next;
- }
- printf("\n");
- printf("Name : %s\n",current->name);
- printf("Number : %s\n",current->number);
- printf("About : %s\n",current->about);
- printf("\n");
- char n;
- getchar();
- printf("1. Edit\n");
- printf("2. Delete\n");
- printf("0. Home\n");
- printf("Chose an option : ");
- scanf("%c",&n);
- if(n == '1'){
- system("CLS");
- edit(current);
- printf("*edit successful*\n");
- }
- else if(n == '2'){
- system("CLS");
- del(current);
- printf("*Delete successful*\n");
- }
- else if(n == '0'){
- system("CLS");
- return;
- }
- }
- void displayall()
- {
- if(start == NULL){
- printf("No contact for display\n");
- return;
- }
- 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;
- }
- }
- char n;
- getchar();
- printf("Select a contact or press '0' to go home: ");
- scanf("%c",&n);
- int i=n-48;
- if(i==0){
- system("CLS");
- return;
- }
- else{
- system("CLS");
- byserial(i);
- }
- printf("\n");
- }
- void home()
- {
- char n;
- while(1){
- printf("***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");
- printf("\nChose an option : ");
- getchar();
- scanf("%c",&n);
- if(n=='0'){
- system("CLS");
- exit(0);
- }
- else if(n=='1'){
- system("CLS");
- insert();
- }
- else if(n=='2'){
- system("CLS");
- displayall();
- }
- else if(n=='3'){
- system("CLS");
- byname();
- }
- else if(n=='4'){
- system("CLS");
- bynumber();
- }
- else{
- printf("Invalid input\n");
- }
- }
- return;
- }
- int main()
- {
- char n[100],x[100];
- strcpy(x,"password");
- printf("\t\t\t\t\t\t***PHONE BOOK***\n\n");
- while(1){
- printf("\nEnter password: ");
- scanf("%s",n);
- if(strcmp(n,x)!=0){
- printf("Wrong password\n");
- continue;
- }
- system("CLS");
- printf("Password matched - WELCOME\n");
- home();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement