Thread: need some help

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    1

    need some help

    help im breaking my neck over this for a while

    as a fool i am fooling around with linked structures i can create one and do some basic stuff on it like inserting and deleting but
    i have no basic solid idea trying to sort some linked structures in alphabetic order

    it worked until i tried to sort

    # include <iostream.h>
    # include <iomanip.h>
    # include <string.h>

    const int MAXNAME = 80;


    struct TeleType
    {
    char name[MAXNAME];
    TeleType *nextAddr;
    };

    TeleType * findSmallest(TeleType *begin);
    void swap( char *str1[], char *str2[]);
    void display( TeleType *p );
    void insert( TeleType * );
    void insertAfter( TeleType *p, char string[] );
    //void strcopy( char *string1, char *string2 );
    TeleType* findAddr( TeleType *list, char str1[]);
    void del( TeleType *b, TeleType *x);
    void qsort( TeleType *a );

    int main()
    {
    char string1[MAXNAME];
    char string2[MAXNAME];
    char string3[MAXNAME];

    TeleType *list , *current;

    list = new TeleType;

    insert(list);

    display(list);

    cout << "AFTER WHICH WORD WOULD YOU LIKE TO ADD AN EXTRA WORD: ";

    cin.getline(string1, MAXNAME);

    current = findAddr(list, string1);

    cout << "WHICH WORD WOULD YOU LIKE TO ADD: ";
    cin.getline(string2, MAXNAME);

    insertAfter( current, string2 );

    display(list);

    cout << "WHICH WORD WOULD YOU LIKE TO DELETE: ";
    cin.getline(string3, MAXNAME);

    current = findAddr(list, string3);

    del(list, current);

    display(list);

    qsort(list);

    cout << "after sorting: "<< endl;

    display(list);

    return 0;
    }

    void insert( TeleType *begin )
    {
    TeleType *current, *last;

    current=begin;

    cout << "Enter first word (or '.' to end list): ";
    cin.getline(current->name, MAXNAME);

    if (current->name[0] == '.' )
    {
    delete current;
    current = NULL;
    }

    while (current != NULL)
    {
    last = new TeleType;
    cout << "Enter next word (or '.' to end list): ";
    cin.getline(last->name, MAXNAME);
    if (last->name[0] == '.' )
    {
    delete last;
    last = NULL;
    }
    current->nextAddr = last;
    current = current->nextAddr;
    }

    return;
    }


    void insertAfter ( TeleType *p, char string[] )
    {
    TeleType *current, *temp ;

    temp = p->nextAddr;
    current= new TeleType;
    p->nextAddr=current;
    strcpy (current->name, string );
    current->nextAddr = temp;

    return;
    }

    TeleType* findAddr( TeleType *list, char str1[])
    {
    TeleType *current;

    current = list;

    while (current != NULL)
    {
    if( strcmp(str1, current->name) == 0)
    return current;
    current = current->nextAddr;
    }
    return 0;
    }

    void display( TeleType *p)
    {
    while( p!= NULL)
    {
    cout << p->name << " ";
    p=p->nextAddr;
    }
    }

    /*void strcpy(char *string1, char *string2)
    {
    while(*string1++ = *string2++)
    ;
    return;
    } */

    void del(TeleType *b, TeleType *x)
    {
    TeleType *current, *pre;

    current = b;

    while(current != x)
    {
    pre=current;
    current = current->nextAddr;
    }
    pre->nextAddr=current->nextAddr;
    delete current;
    return;
    }

    void sortme( TeleType *a )
    {
    TeleType *current, *smallest;

    current = a;

    cout << "we gaan nu sorteren: "<< endl;

    while( current != 0)
    {
    smallest = findSmallest(current);
    swap(current->name, smallest->name);
    current = current->nextAddr;
    }
    return;
    }

    void swap( char str1[], char str2[])
    {
    char temp[MAXNAME];
    strcpy(temp, str1;
    strcpy(str1, str2);
    strcpy(str2, temp);
    return;
    }

    TeleType * findSmallest(TeleType *begin)
    {
    TeleType *current, *smallest;

    smallest = begin;
    current = smallest->nextAddr;

    while(current != NULL)
    {
    if(strcmp(smallest->name, current->name > 0) )
    smallest->name = current->name;
    current = current->nextAddr;
    }
    return current;
    }





    can anyone give me some advice its not mentioned in the bronson book(a first book of C++) i am reading

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed