hello everyone,, first i wanna tell that i am a new membor and i heard alot about this usefull forum.. and today i want ur help guys...
in this question i dont know the causes of the error : the topic is doubly link list which is soted.. the type of the data is a structure.. i did it but can someone check it out for me???
The Address Book


Develop a program to represent a personal address book. The program should maintain a list of contacts, and each contact has the person’s name, address and telephone number. The list should be implemented as a Doubly Linked List.

Write a menu driven program to allow the user to select one of the following options:

1. Add new contact
2. Delete a contact by name.
3. Search by name.
4. Print all contacts (A-Z).
5. Print all contacts (Z-A)
6. Exit.

The list must be sorted according to persons’ name. The search by name should print all the contacts that have the same name as the entered by the user. The deletion process must be based on the name. Once the contact is found the user should be asked first if he\she is sure about the deletion. If there was more than one contact with same name then only the first occurred name shall be deleted.

the solution :

#include <iostream>
#include <cassert>
#include<cstring>
using namespace std;
struct person
{
string name;
string address;
long phone;
};
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *next;
nodeType<Type> *back;
};
 
 
template <class Type>
class doublyLinkedList
{
friend ostream& operator<<(ostream&,
const doublyLinkedList<Type>&);
public:
const doublyLinkedList<Type>& operator=
(const doublyLinkedList<Type> &);
void initializeList();
bool isEmptyList();
void destroy();
void normalPrint();
void reversePrint();
int length();
Type front();
Type back();
void search(const Type& searchItem);
void insertNode(const Type& insertItem);
void deleteNode(const Type& deleteItem);
doublyLinkedList();
doublyLinkedList(const doublyLinkedList<Type>& otherList);
~doublyLinkedList();
 
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const doublyLinkedList<Type>& otherList);
};
 
template<class Type>
doublyLinkedList<Type>::doublyLinkedList()
{
first= NULL;
last = NULL;
count = 0;
}
template<class Type>
bool doublyLinkedList<Type>::isEmptyList()
{
return(first == NULL);
}
template<class Type>
void doublyLinkedList<Type>::destroy()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
void doublyLinkedList<Type>::initializeList()
{
destroy();
}
template<class Type>
int doublyLinkedList<Type>::length()
{
return count;
}
template<class Type>
ostream& operator<<(ostream& osObject,
const doublyLinkedList<Type>& list)
{
nodeType<Type> *current;
current = list.first;
while(current != NULL)
{
cout<<current->info.name<<" "<<current->info.address<<" "<<current->ifo.phone;
current = current->next;
}
return osObject;
}
template<class Type>
void doublyLinkedList<Type>::normalPrint()
{
nodeType<Type> *current;
current = first;
while(current != NULL)
{
cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone;
current = current->next;
}
}
 
template<class Type>
void doublyLinkedList<Type>::reversePrint()
{
nodeType<Type> *current;
current = last;
while(current != NULL)
{
cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone;
current = current->back;
}
}
template<class Type>
void doublyLinkedList<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
current = first;
while(current != NULL)
if(current->info.name==deleteItem.name)
cout<<current->info.name<<" "<<current->info.address<<" "<<current->info.phone<<endl;
current = current->next;
}
template<class Type>
Type doublyLinkedList<Type>::front()
{
assert(first != NULL);
return first->info;
}
template<class Type>
Type doublyLinkedList<Type>::back()
{
assert(last != NULL);
return last->info;
}
template<class Type>
void doublyLinkedList<Type>::insertNode(const Type& insertItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;
bool found;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info.name==insertItem.name;
newNode->info.address==insertItem.address;
newNode->info.phone == insertItem.phone;
newNode->next = NULL;
newNode->back = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
count++;
}
else
{
found = false;
current = first;
while(current != NULL && current!found)
if(current->info.name==deleteItem.name)
found = true;
else
{
trailCurrent = current;
current = current->next;
}
if(current == first)
{
first->back = newNode;
newNode->next = first;
first = newNode;
count++;
}
else
{
if(current != NULL)
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
newNode->next = current;
current->back = newNode;
}
else
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
last = newNode;
}
count++;
}
}
}
 
template<class Type>
void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if(first == NULL)
cerr<<"Cannot delete from an empty list"<<endl;
else
if(first->info.name == deleteItem.name)
{ int n;
cout<<"If you are sure to delete press 1";
cin>>n;
if (n==1){
current = first;
first = first->next;
if(first != NULL)
first->back = NULL;
else
last = NULL;
count--;
delete current;
}
else
{
found = false;
current = first;
while(current != NULL && !found)
if(current->info.name == deleteItem.name)
found = true;
else
current = current->next;
if(current == NULL)
cout<<"The item to be deleted is not in the list"
<<endl;
else
if(current->info.name==deleteItem.name)
{
trailCurrent = current->back;
trailCurrent->next = current->next;
if(current->next != NULL)
current->next->back = trailCurrent;
if(current == last)
last = trailCurrent;
count--;
delete current;
}
else
cout<<"The item to be deleted is not in list."
<<endl;
}
}
}
template<class Type>
void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>&
otherList)
{
nodeType<Type> *current;
current=otherList.first;
if(first != NULL)
destroyList();
if(otherList.first == NULL)
{
first = NULL;
last = NULL;
count = 0;
}
else
{
while(current !=NULL)
{ insertNode(current->info);
current=currnet->next;
}
}
}
 
template<class Type>
doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>&
otherList)
{
first = NULL;
copyList(otherList);
 
}
template<class Type>
const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=
(const doublyLinkedList<Type> &otherList)
{
if(this != &otherList)
copyList(otherList);
return *this;
}
template<class Type>
doublyLinkedList<Type>::~doublyLinkedList()
{
destroy();
}
void main()
{
doublyLinkedList<person> L;
person p1;
p1.name="noora";
p1.address="house no 55,block no 3232";
p1.phone= 39898988;
L.insertNode(p1);
person p2;
p2.name="mariam Ali";
p2.address="house no 63,block no 126";
p2.phone=39656569;
L.insertNode(p2);
 
person p3;
p3.name="maryiam Hassan";
p3.address="house no 87, block no 555";
p3.phone=39595959;
L.insertNode(p3);
 
person p4;
 
cout<<"Enter Name"<<endl;
cin>>p4.name;
cout<<"Enter address"<<endl;
cin>>p4.address;
cout<<"Enter phone number";
cin>>p4.phone;
L.insertNode(p4);
L.normalPrint();
person p5;
p5.name="mariam";
person p6;
p6.name="noora";
person p7;
p7.name="Fatima";
L.deleteNode(p5);
L.search(p6);
L.search(p7);
L.normalPrint();
L.reversePrint();
}

Recommended Answers

All 2 Replies

in this question i dont know the causes of the error : the topic is doubly link list which is soted.. the type of the data is a structure.. i did it but can someone check it out for me???

Neither do we, since we don't have a clue what type of error you're talking about. Seems the DaniWeb Psychic Plugin isn't working at the moment.

Please be
1) less vague
2) less code (341 lines to find an unknown error as a little much to ask)
3) more proactive -- read the announcements at the top of the forum
4) more helpful -- format your code so it can be understood

thaaaaaaanx for these lovely advices .. sorry... for that...
forgive me ( i am a new member) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.