0% found this document useful (0 votes)
236 views

Telephone Directory C++ Code

This C++ program implements a doubly linked list to store phone directory data. The list contains name, phone number, and address fields for each entry. Functions are defined to insert new entries, delete existing entries, and display all entries. The insert function adds a new node to the list in sorted order by name, and the delete function removes a node by name. The main loop allows the user to repeatedly show, insert, or delete entries from the phone directory list until exiting.

Uploaded by

Muhammad Awais
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views

Telephone Directory C++ Code

This C++ program implements a doubly linked list to store phone directory data. The list contains name, phone number, and address fields for each entry. Functions are defined to insert new entries, delete existing entries, and display all entries. The insert function adds a new node to the list in sorted order by name, and the delete function removes a node by name. The main loop allows the user to repeatedly show, insert, or delete entries from the phone directory list until exiting.

Uploaded by

Muhammad Awais
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream> using namespace std; struct phndir { char name[20]; char no[12]; char address[20]; phndir *next,*prev; }; phndir

*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(); } }

You might also like