Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,412 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,445 people online right now. Registration is fast and FREE... Join Now!




Contact Manager Linked List Help

 
Reply to this topicStart new topic

Contact Manager Linked List Help, Help With linked list

riskyricky
15 Oct, 2008 - 10:56 AM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 5

I Have created a Contact manager that stores contacts along with other information. I used a fixed size array, and need help changing program into a linked list. I also need help creating a search function. This function would let the user search the contacts by name, phone, address, etc. How would i get the function to search each list.

My program code is:
CODE
/* In this program we create a contact manager to manage three total
contacts, by student ID. Includes phone number GPA and Hours.*/

#include <stdio.h>

enum kind {GPA, HOURS} kind;  

struct student{

        union grade {
                            int hours;
                                    double gpa;} grade;
                int kind;
        int id;

        struct phone{

            int area;
                        int pre;
                        int suf;} phone;        
        };         

int main (int argc, char **argv){

    int entry, i;
    struct student stu[3];

    char gh;

    int num = 0;

do{

    printf("[1] Enter a new Contact \n");
    printf("[2] Display the Contacts \n");
    printf("[3] Quit\n\n");
    printf("Enter 1 - 3: ");
            scanf("%i", &entry);

    switch(entry){

        case 1:
            printf("Enter a Student ID number:\n900");
            scanf("%i", &stu[num].id);
            printf("[G] Enter GPA:\n");
            printf("[H] Enter number of completed Hours\n");
                    scanf(" %c", &gh);
                    switch(gh){
                            case 'G':
                                        stu[num].kind = GPA;
                                                printf("GPA: ");
                                                scanf(" %lf", &stu[num].grade.gpa);
                                    break;
                                case 'H':
                                        stu[num].kind = HOURS;
                                                printf("Hours: ");
                                                scanf(" %i", &stu[num].grade.hours);
                                    break;
                        default:
                            printf("Incorrect entry\n");

            }
                    
                
                        printf("Enter the phone number (###) ###-####\n");
                        scanf(" (%i) %i-%i", &stu[num].phone.area, &stu[num].phone.pre, &stu[num].phone.suf);
            num++;

    break;

        case 2:
    
            for(i = 0; i<num; i++){
                    printf("ID: 900%i\n", stu[i].id);

            if(stu[i].kind == GPA){
                    printf("GPA: %lf\n", stu[i].grade.gpa);
    
            }
            else if(stu[i].kind == HOURS){
                printf("Hours: %i\n", stu[i].grade.hours);
            }
                printf("Phone: (%i) %i-%i\n", stu[i].phone.area, stu[i].phone.pre, stu[i].phone.suf);
            }

            break;
    
        case 3:
        
            printf("Thank You for using MyContactManager\n");
        
            break;

            default:
                    printf("Entry not acceptable\n");
    
}
    
} while(entry < 3);        
        
        return (0);

}


Any help greatly Appreciated. Thanks!


User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Contact Manager Linked List Help
16 Oct, 2008 - 06:27 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Here are some tutorials for a singly linked list (from a Google search on "linked list tutorial"). Adding a search function to this should be fairly trivial. Just make a function that does a loop or recursion, to check one of the members of each struct contained in the list.


http://richardbowles.tripod.com/cpp/linklist/linklist.htm
http://www.fortunecity.com/skyscraper/fals...0/linklist.html
User is offlineProfile CardPM
+Quote Post

David W
RE: Contact Manager Linked List Help
16 Oct, 2008 - 07:19 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
cpp
// template class List //

template< class T >
class List
{
public:
List();
~List();
void addpre( T e );
void append( T e );
void addafter( int c, T e );
void scratch( T e );
void show();
bool has( T e ) const; // this can be a start on your 'search'
int length() const;
T List<T>::max(); // you don't need this
T List<T>::min(); // nor this ...
private:
struct node
{
T data;
node *link;
}*p;
};

template< class T >
List<T>::List()
{
p=NULL;
}

template< class T >
List<T>::~List()
{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}

template< class T >
void List<T>::addpre(T num) // inserts num with type T at front of List
{
node *b;
b = new node;
b->data = num;
b->link = p;
p = b;
}

template< class T >
void List<T>::append(T num) // inserts num with type T at end of List
{
node *end,*tmp;

if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
end = p;
while( end->link != NULL )
end = end->link;

tmp = new node;
tmp->data = num;
tmp->link = NULL;
end->link = tmp;
}
}

template< class T >
void List<T>::addafter( int c, T num) // inserts num with type T after index c
{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
q = q->link;
if( q == NULL )
{
cout << "\nThe highest index allowed is " << i
<< " and that is less than requested index " << c << ".";
cout << "\nAppend y/n ? ";
string reply;
getline( cin, reply );
if( reply[0] == 'n' || reply[0] =='N' ) return;
q = p;
while( q->link != NULL )
q = q->link;

break;
}
}

t = new node;
t->data = num;
t->link = q->link;
q->link = t;
}

template< class T >
void List<T>::scratch( T e )
{
node *q,*r;
q = p;
if( q->data == e )
{
p = q->link;
delete q;
return;
}

r = q;
while( q!=NULL )
{
if( q->data == e )
{
r->link = q->link;
delete q;
return;
}

r = q;
q = q->link;
}
cout<<"\nElement "<< e <<" not Found.";
}

template< class T >
void List<T>::show()
{
node *q;
for( q = p; q != NULL; q = q->link )
cout<<endl<<q->data;
cout << "\n\nPress 'Enter' to continue ... ";
cin.clear();
cin.sync();
cin.get();
}

template <class T>
bool List<T>::has( T e ) const
{
node *q;
q = p;
while(q != NULL)
{
if(q->data == e )
return true;
else
q = q->link;
}
return false;
}

template <class T>
int List<T>::length() const
{
node *q;
int c=0;
for( q=p; q != NULL; q = q->link )
c++;
return c;
}

template <class T>
T List<T>::max()
{
node *q;
q = p;
T max = p->data;

while(q->link != NULL)
{
if( (q->link)->data > max )
max = (q->link)->data;
q = q->link;
}
return max;
}

template <class T>
T List<T>::min()
{
node *q;
q = p;
T min = p->data;

while(q->link != NULL)
{
if( (q->link)->data < min )
min = (q->link)->data;
q = q->link;
}
return min;
}]


Maybe you could use/modify this template class List I just submitted to 'snippets' ... Use it to make a list of your objects like this:

cpp
// define your object structure (i.e the fields in each record)
class studentRec
{
public:
string name; // field 1
// ... // field 2
// ...
};


int main()
{
// create an empty fall term list of records ...
List < studentRec > recFallTerm;
// now you can append, etc, ... to this list
//...
}


This post has been edited by David W: 16 Oct, 2008 - 07:54 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:34PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month