| | |
i neeed ur help .. find error in data structure program
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 2
Reputation:
Solved Threads: 0
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 :
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 :
c Syntax (Toggle Plain Text)
#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(); }
Last edited by WaltP; May 2nd, 2007 at 5:23 am. Reason: Removed COLOR tags and Please learn to use CODE tags -- read the announcements at the top of the forum
•
•
•
•
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???
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
Last edited by WaltP; May 2nd, 2007 at 5:30 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Data Structure question (C++)
- Data structure + GUI question. (Java)
- Can't find the error (C)
- Wierd error messages with calculator program (C++)
- Run-time Error when printing Array Contents. (C)
- help me find the error (C++)
- find out the error in my code (Java)
Other Threads in the C++ Forum
- Previous Thread: help me on this question, pls
- Next Thread: Saving Query in Ms Access .MDb File using C++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






