Telephone Directory C++ Code
Telephone Directory C++ Code
*head; phndir *tail; int show() { if(head==NULL) { cout<<"No data is found "; return 0; } else { phndir *ptr=head; while(ptr->next!=head) { if(ptr==NULL) { break; } cout<<"\n\nName is "<<ptr->name<<"\nPhone no is "<<ptr-> no<<"\nAddress is "<<ptr->address; ptr=ptr->next; } if(ptr!=NULL) cout<<"\n\nName is "<<ptr->name<<"\nPhone no is "<<ptr->no<<"\nA ddress is "<<ptr->address; } return 0; } int insert() { phndir *ptr=new phndir ; cout<<"\nEnter Data of new entry "; cout<<"\nName : "; cin>>ptr->name; cout<<"Phone no: "; cin>>ptr->no; cout<<"Address : "; cin>>ptr->address; phndir *ptr1=new phndir; ptr1=head; if(head==NULL) {
head = ptr; head->next=ptr; head->prev=ptr; return 0; } if(head->next==head) { if(head->name[0]<ptr->name[0]) { head->next=ptr; head->prev=ptr; ptr->next=head; ptr->prev=head; tail=ptr; } else { phndir*temp=ptr; ptr=head; head=temp; head->next=ptr; head->prev=ptr; ptr->next=head; ptr->prev=head; tail=ptr; } return 0; } while(ptr1->next!=head) { if(ptr1->name[0]>ptr->name[0]) { if(ptr1==head) { ptr->next=head; ptr->prev=head->prev; head->prev=ptr; tail->next=ptr; head=ptr; return 0; } ptr->prev=ptr1->prev; ptr->next=ptr1->prev->next; ptr1->prev->next=ptr; ptr1->prev=ptr; return 0; } ptr1=ptr1->next; } if(ptr1==tail) { if(tail->name[0]>ptr->name[0]) { ptr->next=tail->prev->next;
ptr->prev=tail->prev; tail->prev->next=ptr; tail->prev=ptr; } else { phndir *temp1=ptr; ptr->next=tail->next; ptr->prev=tail; tail->next=ptr; head->prev=ptr; ptr=tail; tail=temp1; } } return 0; } int del() { char nam[10]; cout<<"Enter name of the student u want to delete "; cin>>nam; phndir *ptr1=head; if(head->next==head) { head=NULL; return 0; } while(ptr1->next!=head) { if(ptr1->name[0]==nam[0]) { if(head==ptr1) { head=ptr1->next; tail->next=ptr1->next; ptr1->next->prev=tail; return 0; } ptr1->prev->next=ptr1->next; ptr1->next->prev=ptr1->prev; return 0; } ptr1=ptr1->next; } if(ptr1==tail) { if(tail->name[0]==nam[0]) { tail=ptr1->prev; tail->next=ptr1->next; ptr1->next->prev=tail; } } return 0;
} void main() { int list=1; head=NULL; tail=NULL; while(list!=4) { cout<<"\n\n\nPress 1 to show data \nPress 2 to insert data \nPre ss 3 to delete data \nPress 4 to exit "; cin>>list; if(list==1) { show(); if(list==2) insert(); if(list==3) del(); } }