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
- {
- char name[30],phone[20];
- struct node *next,*prev;
- }node;
- node *head=NULL,*tail=NULL;
- void add_at_first() //ADD_AT_FIRST_FUNCTION///////////////////////////////////////////////////////
- {
- node *n=(node*)malloc(sizeof(node));
- n->next=NULL; n->prev=NULL;
- node *list=head;
- printf("Enter the Name: ");
- scanf(" %[^\n]",n->name);
- printf("Enter the Phone Number: ");
- scanf(" %[^\n]",n->phone);
- printf("\n");
- if(head==NULL){
- head=n;
- tail=n;
- }
- else {
- n->next=head;
- head->prev=n;
- head=n;
- while(list->next!=NULL){
- list=list->next;
- }
- tail=list;
- }
- menu();
- }
- void add_at_last() //ADD_AT_LAST_FUNCTION///////////////////////////////////////////////////////
- {
- node *n=(node*)malloc(sizeof(node));
- n->next=NULL; n->prev=NULL;
- node *list=head;
- printf("Enter the Name: ");
- scanf(" %[^\n]",n->name);
- printf("Enter the Phone Number: ");
- scanf(" %[^\n]",n->phone);
- printf("\n");
- if(head==NULL){
- head=n;
- tail=n;
- }
- else{
- while(list->next!=NULL){
- list=list->next;
- }
- list->next=n;
- n->prev=list;
- tail=n;
- }
- menu();
- }
- void add_at_nth_pos() //ADD_AT_nTH_POSITION_FUNCTION///////////////////////////////////////////
- {
- int pos;
- node *n=(node*)malloc(sizeof(node));
- n->prev=NULL; n->next=NULL;
- node *list=head;
- printf("Enter the Position: ");
- scanf("%d",&pos);
- printf("\n");
- printf("Enter the Name: ");
- scanf(" %[^\n]",n->name);
- printf("Enter the Phone Number: ");
- scanf(" %[^\n]",n->phone);
- printf("\n");
- if(pos==1){
- if(head==NULL){
- head=n;
- tail=n;
- }
- else{
- n->next=head;
- head->prev=n;
- head=n;
- }
- }
- else{
- if(head==NULL){
- head=n;
- tail=n;
- }
- else{
- pos=pos-2;
- while(pos!=0){
- if(list==NULL){
- printf("There is no such Position that you entered.\n\n");
- menu();
- break;
- }
- else{
- list=list->next;
- pos--;}
- }
- if(list->next==NULL){
- list->next=n;
- n->prev=list;
- tail=n;
- }
- else{
- n->next=list->next; n->prev=list; n->next->prev=n;
- }
- }
- }
- printf("Your new data is added successfully\n\n");
- menu();
- }
- void add() //ADD_Function///////////////////////////////////////////////////////////////////////
- {
- int option;
- printf("\t\t\t\t\t1.Insert at First\t\t2.Insert at Last\n\n");
- printf("\t\t\t\t\t\t 3.Insert at n'th Position\n\n");
- printf("Choose an option: ");
- scanf("%d",&option);
- printf("\n\n");
- if(option==1){
- add_at_first();
- }
- else if(option==2){
- add_at_last();
- }
- else if(option==3){
- add_at_nth_pos();
- }
- else{
- printf("You entered a wrong option!! Automatically terminating to menu.\n\n");
- menu();
- }
- }
- void display() //ALL_DATA_DISPLAY_FUNCTION//////////////////////////////////////////////////////////////
- {
- node *list=head;
- while(list!=NULL){
- printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
- list=list->next;
- }
- printf("Reverse Printing:\n\n");
- list=tail;
- while(list!=NULL){
- printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
- list=list->prev;
- }
- menu();
- }
- delete_by_name() //DELETE_BY_NAME/////////////////////////////////////////////////////
- {
- node *list=head; node *temp;
- char testname[30];
- int match=0;
- printf("Enter the name you want to Delete: ");
- scanf(" %[^\n]",testname);
- printf("\n\n");
- printf("The value of difference: %d\n\n",strcmp(list->name,testname));
- if(strcmp(list->name,testname)==0){// if match a head
- if(head->next==NULL){
- free(head);
- head=NULL; tail=NULL;
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- else{
- head=list->next;
- head->prev=NULL;
- free(list);
- list=head;
- while(list->next!=NULL){
- list=list->next;
- }
- tail=list;
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- }
- else{
- while(list->next!=NULL){
- if(strcmp(list->next->name,testname)==0){
- match++;
- break;
- }
- list=list->next;
- }
- if(match==0){
- printf("There is no matching data.\n\n");
- menu();
- }
- else{
- temp=list->next;
- if(temp->next==NULL){
- tail=list;
- free(temp);
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- else{
- temp=list->next;
- list->next=temp->next;
- temp->next->prev=list;
- free(temp);
- while(list->next!=NULL){
- list=list->next;
- }
- tail=list;
- printf("The data is deleted successfully.\n\n");
- }
- }
- }
- }
- delete_by_phone_number() //DELETE_BY_PHONE_NUMBER/////////////////////////////////////////////////////
- {
- node *list=head; node *temp;
- char testphone[30];
- int match=0;
- printf("Enter the Phone Number you want to Delete: ");
- scanf(" %[^\n]",testphone);
- printf("\n\n");
- printf("The value of difference: %d\n\n",strcmp(list->phone,testphone));
- if(strcmp(list->phone,testphone)==0){// if match a head
- if(head->next==NULL){
- free(head);
- head=NULL; tail=NULL;
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- else{
- head=list->next;
- head->prev=NULL;
- free(list);
- list=head;
- while(list->next!=NULL){
- list=list->next;
- }
- tail=list;
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- }
- else{
- while(list->next!=NULL){
- if(strcmp(list->next->phone,testphone)==0){
- match++;
- break;
- }
- list=list->next;
- }
- if(match==0){
- printf("There is no matching data.\n\n");
- menu();
- }
- else{
- temp=list->next;
- if(temp->next==NULL){
- tail=list;
- free(temp);
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- else{
- temp=list->next;
- list->next=temp->next;
- temp->next->prev=list;
- free(temp);
- while(list->next!=NULL){
- list=list->next;
- }
- tail=list;
- printf("The data is deleted successfully.\n\n");
- }
- }
- }
- }
- void delete_by_nth_position() //DELETE_BY_nth_POSTION//////////////////////////////////////////////
- {
- int position,match=0;
- node *list=head; node *temp;
- printf("Enter the Position to delete the data: ");
- scanf("%d",&position);
- printf("\n\n");
- if(position==1){
- if(head->next==NULL){
- free(head);
- head=NULL; tail=NULL;
- menu();
- }
- else{
- head=head->next;
- head->prev=NULL;
- free(list);
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- }
- else{
- position=position-2;
- while(position!=0){
- if(list==NULL){
- printf("There is no such Position that you entered.\n\n");
- menu();
- break;
- }
- else{
- position--;
- }
- }
- temp=list->next;
- if(temp->next==NULL){
- list->next=NULL;
- tail=list;
- free(temp);
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- else{
- temp=list->next; temp->next->prev=list;
- free(temp);
- printf("The data is deleted successfully.\n\n");
- menu();
- }
- }
- }
- void Delete() //DELETE_FUNCTION///////////////////////////////////////////////////////////////////
- {
- int option;
- char name[40];
- printf("\t\t\t\t\t1.Delete by Name\t\t2.Delete by Phone Number\n\n\t\t\t\t\t\t 3.Delete by nth Position.\n\n");
- printf("Choose an option: ");
- scanf("%d",&option);
- printf("\n");
- if(option==1){
- delete_by_name();
- }
- else if(option==2){
- delete_by_phone_number();
- }
- else if(option==3){
- delete_by_nth_position();
- }
- else{
- printf("You entered an wrong option. Automatically terminating to menu\n\n");
- menu();
- }
- }
- void search_by_name() //SEARCH_BY_NAME_FUNCTION////////////////////////////////////////////////
- {
- int match=0;
- node *list=head;
- char testname[30];
- printf("Enter the name you want to search: ");
- scanf(" %[^\n]",testname);
- printf("\n");
- while(list!=NULL){
- if(strcmp(list->name,testname)==0){
- printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
- match++;
- }
- list=list->next;
- }
- if(match==0){
- printf("No match found with %s\n",testname);
- }
- menu();
- }
- void search_by_phone_number() //SEARCH_BY_PHONE_NUMBER_FUNCTION
- {
- int match=0;
- node *list=head;
- char testnumber[30];
- printf("Enter the Phone Number you want to search: ");
- scanf(" %[^\n]",testnumber);
- printf("\n");
- while(list!=NULL){
- if(strcmp(list->phone,testnumber)==0){
- printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
- match++;
- }
- list=list->next;
- }
- if(match==0){
- printf("No match found with %s\n",testnumber);
- }
- menu();
- }
- void search_by_nth_position() //SEARCH_BY_nTH_POSITION/////////////////////////////////////////
- {
- int match=0,position;
- node *list=head;
- printf("Enter the position you want to search: ");
- scanf("%d",&position);
- printf("\n");
- if(position==1){
- printf("Name: %s\nPhone Number: %s\n\n",head->name,head->phone);
- menu();
- }
- else{
- position=position-2;
- while(position!=0){
- if(list==NULL){
- printf("There is no such position that you entered.\n\n");
- menu();
- break;
- }
- list=list->next;
- }
- printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
- menu();
- }
- }
- void search() //SEARCH_FUNCTION/////////////////////////////////////////////////////////////
- {
- int option;
- printf("\t\t\t\t\t1.Search by name\t\t2.Search by phone number\n\n\t\t\t\t\t\t 3.Search Position.\n\n");
- printf("Choose an option: ");
- scanf("%d",&option);
- printf("\n\n");
- if(option==1){
- search_by_name();
- }
- else if(option==2){
- search_by_phone_number();
- }
- else if(option==3){
- search_by_nth_position();
- }
- }
- void menu() //Menu_FUNCTION//////////////////////////////////////////////////////////////////////
- {
- int option;
- printf("\t\t\t\t\t1.Add New Data\t\t2.Delete A Data\n\n");
- printf("\t\t\t\t\t\t 3.End The Program\n\n");
- printf("\t\t\t\t\t4.Search A Data\t\t5.Display all data\n\n");
- printf("Choose an option: ");
- scanf("%d",&option);
- printf("\n\n");
- if(option==1){
- add();
- }
- else if(option==5){
- if(head==NULL){
- printf("There is no data to display.\n\n");
- menu();
- }else{
- display();}
- }
- else if(option==2){
- if(head==NULL)
- {
- printf("There is no data to delete.\n\n");
- menu();
- }
- else{ Delete();}
- }
- else if(option==3){
- printf("Thank you for using the program!!\n");
- }
- else if(option==4){
- if(head==NULL){
- printf("There is no data to search.\n\n");
- menu();
- }
- else{
- search();
- }
- }
- else{
- printf("You entered an wrong option. Automatically terminating to menu\n\n");
- menu();
- }
- return;
- }
- int main()
- {
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement