2 Replies - 3243 Views - Last Post: 16 October 2008 - 08:19 AM Rate Topic: -----

#1 riskyricky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-September 08

Contact Manager Linked List Help

Posted 15 October 2008 - 11:56 AM

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:
 /* 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!

Is This A Good Question/Topic? 0
  • +

Replies To: Contact Manager Linked List Help

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: Contact Manager Linked List Help

Posted 16 October 2008 - 07:27 AM

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...st/linklist.htm
http://www.fortuneci...0/linklist.html
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Contact Manager Linked List Help

Posted 16 October 2008 - 08:19 AM

// 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:

// 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 October 2008 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1