Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- current = start;
- if(start == NULL)
- start = newnode;
- else if(start->next == NULL){
- if(current->name[0] <= newnode->name[0]){
- current->next = newnode;
- newnode -> prev = current;
- }
- else{
- newnode->next = current;
- current->prev = newnode;
- start = newnode;
- }
- }
- else{
- while(current->name[0] <= newnode->name[0]){
- if(current->next == NULL)
- break;
- current = current->next;
- }
- if(current == start){
- newnode->next = current;
- current->prev = newnode;
- start = newnode;
- }
- else if(current->next == NULL){
- current->next = newnode;
- newnode->prev = current;
- }
- else{
- struct node *currentprev;
- currentprev = current->prev;
- currentprev->next = newnode;
- newnode->prev = currentprev;
- newnode->next = current;
- current->prev = newnode;
- }
- }
- }
- void insert()
- {
- struct node *newnode,*current,*currentprev;
- 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);
- sort(newnode);
- printf("\n*save successful*\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);
- int n;
- printf("1. Change name\n");
- printf("2. Skip\n");
- scanf("%d",&n);
- if(n == 1){
- printf("name : ");
- getchar();
- scanf("%[^\n]",current->name);
- }
- printf("1. Change number\n");
- printf("2. Skip\n");
- scanf("%d",&n);
- if(n == 1){
- printf("number: ");
- getchar();
- scanf("%s",current->number);
- }
- printf("1. Change about\n");
- printf("2. Skip\n");
- scanf("%d",&n);
- if(n == 1){
- printf("about : ");
- getchar();
- scanf("%[^\n]",current->about);
- }
- current->next = NULL;
- current->prev = NULL;
- sort(current);
- }
- void byname()
- {
- struct node *current;
- char str[100];
- printf("Enter name for search : ");
- getchar();
- scanf("%[^\n]",str);
- current = start;
- int i=0, n;
- while(1){
- 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;
- }
- if(current->next == NULL)
- 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 : ");
- scanf("%d",&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;
- char str[100];
- printf("Enter number for search : ");
- getchar();
- scanf("%[^\n]",str);
- current = start;
- int i=0, n;
- while(1){
- 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;
- }
- if(current->next == NULL)
- 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 : ");
- scanf("%d",&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(1){
- count++;
- if(current->next == NULL)
- break;
- 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,*currentprev;
- current = start;
- int i=0;
- while(1){
- 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");
- if(current->next == NULL)
- break;
- current = current->next;
- }
- }
- printf("\n");
- }
- void home()
- {
- int n;
- printf("\nChose an option : ");
- scanf("%d",&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();
- }
- int main()
- {
- int 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