Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct node //STRUCTURE///////////////////////////////////////////////////
- {
- char name[30];
- char phone[20];
- struct node *next;
- } node;
- node *head=NULL;
- int Add() // ADD FUNCTION////////////////////////////////////////////////
- {
- int pos,option;
- printf("\t\t\t\t1.Add at First Position\t\t2.Add at Last Position\n");
- printf("\t\t\t\t\t\t 3.Add at n'th Position\n\n");
- scanf("%d",&option);
- if(option==1)
- {
- node *n=(node*)malloc(sizeof(node)); //ADD___AT___FIRST///////////////////////////////////
- n->next=NULL;
- printf("Enter The Name:\n\n");
- scanf(" %[^\n]",n->name);
- printf("Enter The Phone Number:\n\n");
- scanf(" %[^\n]",n->phone);
- n->next=head;
- head=n;
- menu();
- }
- else if(option==2) //ADD__AT__LAST//////////////////////////////////////////////////
- {
- node *n=(node*)malloc(sizeof(node));
- n->next=NULL;
- printf("\t\t\t\t\t\tEnter The Name:\n\n");
- scanf(" %[^\n]",n->name);
- printf("\t\t\t\t\t\tEnter The Phone Number:\n\n");
- scanf(" %[^\n]",n->phone);
- node *list=head;
- if(list==NULL)
- {
- head=n;
- printf("\t\t\t\t\t\ Successfully Added to the last Position\n\n");
- menu();
- }
- else
- {
- while(list->next!=NULL)
- {
- list=list->next;
- }
- list->next=n;
- menu();
- }
- }
- else if(option==3) //ADD__AT__nth__POSITION////////////////////////////////////////////////////
- {
- printf("\t\t\t\t\t\tEnter the position:\n\n");
- scanf("%d",&pos);
- node *n=(node*)malloc(sizeof(node));
- n->next=NULL;
- printf("\t\t\t\t\t\tEnter The Name:\n\n");
- scanf(" %[^\n]",n->name);
- printf("\t\t\t\t\t\tEnter The Phone Number:\n\n");
- scanf(" %[^\n]",n->phone);
- node *list=head;
- if(pos==1)
- {
- n->next=head;
- head=list;
- menu();
- }
- else if(pos%1==0)
- {
- pos=pos-2;
- while(pos!=0 && list->next!=NULL)
- {
- list=list->next;
- }
- n->next=list->next;
- list->next=n;
- menu();
- }
- }
- else
- {
- printf("\t\t\t\t\t\tYou have entered a wrong command, Follow the steps again\n\n");
- Add();
- }
- }
- int Delete() //DELETE__FUNCTION///////////////////////////////////////////////////////////
- {
- int option,pos;
- char test[30];
- node *list,*temp;
- printf("\t\t\t\t\t\t1.Delete by Position\t\t2.Delete by Matching Name\n\n");
- scanf("%d",&option);
- if(option==1) //DELETE__BY__POSITION/////////////////////////////////////////////////////
- {
- printf("\t\t\t\t\t\tEnter the Position\n\n");
- scanf("%d",&pos);
- if(pos==1)
- {
- temp=head;
- head=head->next;
- free(temp);
- printf("\t\t\t\t\t\tSuccessfully Deleted the Data\a\n");
- menu();
- }
- else
- {
- pos=pos-2;
- list=head;
- while(pos!=0 && list->next!=NULL)
- {
- list=list->next;
- pos--;
- if(list==NULL){
- printf("There is No %dth Position\n\n",pos);
- menu(); }
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
- menu();
- }
- menu();
- }
- else if(option==2) //DELETE__BY__Name/////////////////////////////////////////////////
- {
- printf("\t\t\t\t\t\tEnter the Name you want to Delete\n\n");
- scanf(" %[^\n]",test);
- list=head;
- if(strcmp(test,list->name)==0)
- {
- head=list->next;
- free(list);
- printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
- menu();
- }
- else
- {
- list=head;
- while(strcmp(list->next->name,test)!=0 && list!=NULL)
- {
- list=list->next;
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
- menu();
- }
- }
- else
- {
- printf("\t\t\t\t\t\tYou have entered a wrong command, Follow the steps again\a\n\n");
- Delete();
- }
- }
- int display() //DISPLAY__FUNCTION/////////////////////////////////////////////
- {
- int count=0;
- node *list=head;
- while(list!=NULL)
- {
- count++;
- printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
- list=list->next;
- }
- menu();
- }
- int search() //////Search_FUNCTON////////////////////////////////////
- {
- char test[30];
- int option,count=0;
- node *list=head;
- printf("\t\t\t\t1.Search by name\t\t2.Search by Phone number\n");
- scanf("%d",&option);
- if (option==1){ //////SEARCH_BY_NAME////////////////////////////////////////////
- printf("\t\t\t\t\t\tEnter the name\n\n");
- scanf(" %[^\n]",test);
- while(list!=NULL){
- if(strcmp(test,list->name)==0){
- printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
- count++;
- }
- list=list->next;
- }
- if(count==0){
- printf("\t\t\t\t\t\tNot Found Your Data\a\n\n");
- menu();
- }
- menu();
- }
- if(option==2){ // Search_BY_PHONE_NUMBER////////////////////////////////////////////////////
- printf("Enter The Phone Number:\n\n");
- scanf(" %[^\n]",test);
- while(list!=NULL){
- if(strcmp(test,list->phone)==0){
- printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
- count++;
- }
- list=list->next;
- }
- if(count==0){
- printf("\t\t\t\t\t\tNot Found Your Data\a\n\n");
- menu();
- }
- menu();
- }
- else{
- printf("\t\t\t\t\t\tYou have Entered wrong command, Automatically terminated to Menu\n\n");
- menu();
- }
- menu();
- }
- int menu()
- {
- int n;
- printf("\n\n");
- printf("\t\t\t\t1.Add a new Contact\t\t2.Delete an Contact\n");
- printf("\t\t\t\t3.Search an Contact\t\t4.Display all Contact\n");
- printf("\t\t\t\t\t\t 5.End The Program\n\n\n\n");
- scanf("%d",&n);
- if(n==1)
- {
- Add();
- }
- else if(n==2)
- {
- Delete();
- }
- else if(n==3){
- search();
- }
- else if(n==4)
- {
- display();
- }
- else if(n==5)
- {
- exit(0);
- }
- else
- {
- printf("\t\t\t\t\t\tYou have Entered wrong command, Automatically terminated to Menu\n\n");
- menu();
- }
- }
- int main()
- {
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement