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

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




help! about displaying..

 
Reply to this topicStart new topic

help! about displaying.., in linked list

bengt23648
1 Oct, 2007 - 05:34 AM
Post #1

New D.I.C Head
*

Joined: 30 Jun, 2007
Posts: 37


My Contributions
hi guys! i have this program that adds, sorts and searches a linked list.. and i have this problem:

when I append:

Name: Dog
Age: 33
Phone: 3230021

Name: dog
Age: 22
Phone: 6523388

Name: Dog
Age: 33
Phone: 3230021

when i use the binarySearchName function, it would just display the first item that it sees

it just displays:

Name: dog
Age: 22
Phone: 6523388

coz it's the first item that it sees (in the middle of the list)

I want to display all the items that is equal to my search key.. (i've used strcmpi)

how can i do it? please help...

main
CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<c:\binary.h>

void main()
{
    clrscr();
    int a, age, m, n, o, loc, ag;
    char c, d, ch, name[30], phone[20], itemName[30], keyPhone[20];

    gotoxy((80-17)/2,23);
    cout<<"How Many Entries?  ";
    cin>>a;

    LinkedList *list=new LinkedList();
    clrscr();
    for(int i=0; i<a; i++)
    {
        gotoxy((80-14)/2,5);
        cout<<"NEW ENTRY MENU";
        gotoxy(30,7);
        cout<<"Name:";
        gotoxy(43,7);
        gets(name);
        gotoxy(30,8);
        cout<<"Age:\t\t";
        gotoxy(43,8);
        cin>>age;
        gotoxy(30,9);
        cout<<"Phone No.:\t";
        gotoxy(43,9);
        gets(phone);
        list->appendNode(name,age,phone,i);
        clrscr();
    }

    menu:
    clrscr();
    gotoxy((80-9)/2,8);
    cout<<"MAIN MENU";
    gotoxy((80-21)/2,10);
    cout<<"[A] Sort Entries";
    gotoxy((80-21)/2,11);
    cout<<"[B] Search";
    gotoxy((80-21)/2,12);
    cout<<"[C] Quit";
    gotoxy((80-16)/2,24);
    cout<<"Input Choice:";
    gotoxy(47,24);
    cin>>c;
    if((c=='c')||(c=='C'))
        exit(0);

            else if((c=='a')||(c=='A'))
            {
                clrscr();
                char k;
                char r;
                gotoxy((80-9)/2,5);
                cout<<"SORT MENU";
                gotoxy((80-20)/2,8);
                cout<<"Search by:";
                gotoxy((80-20)/2,10);
                cout<<"[A] Name";
                gotoxy((80-20)/2,11);
                cout<<"[B] Age";
                gotoxy((80-20)/2,12);
                cout<<"[C] Phone Number";
                gotoxy((80-20)/2,13);
                cout<<"[D] Return To Main";
                gotoxy((80-16)/2,23);
                cout<<"Input Choice:";
                gotoxy(47,23);
                cin>>k;

                if ((k=='a')||(k=='A'))
                {
                    list->selection();
                    list->displayNodes();
                    gotoxy((80-20)/2,23);
                    cout<<"Search Again? [y/n]";
                    gotoxy(51,23);
                    cin>>r;
                    if ((r=='y')||(r=='Y'))
                    goto search;
                    else
                    goto menu;
                }
                else if ((k=='b')||(k=='c'))
                {
                    list->selectionSortAge();
                    list->displayNodes();
                    gotoxy((80-20)/2,23);
                    cout<<"Search Again? [y/n]";
                    gotoxy(51,23);
                    cin>>r;
                    if ((r=='y')||(r=='Y'))
                    goto search;
                    else
                    goto menu;
                }
                else if ((k=='c')||(k=='c'))
                {
                    list->selectionSortPhone();
                    list->displayNodes();
                    gotoxy((80-20)/2,23);
                    cout<<"Search Again? [y/n]";
                    gotoxy(51,23);
                    cin>>r;
                    if ((r=='y')||(r=='Y'))
                    goto search;
                    else
                    goto menu;
                }
                else if ((k=='d')||(k=='D'))
                {
                    goto menu;
                }
                getch();
                goto menu;
            }

            else if ((c=='b')||(c=='B'))
            {
                search:
                clrscr();
                gotoxy((80-11)/2,5);
                cout<<"SEARCH MENU";
                gotoxy((80-20)/2,8);
                cout<<"Search by:";
                gotoxy((80-20)/2,10);
                cout<<"[A] Name";
                gotoxy((80-20)/2,11);
                cout<<"[B] Age";
                gotoxy((80-20)/2,12);
                cout<<"[C] Phone Number";
                gotoxy((80-20)/2,13);
                cout<<"[D] Return To Main";
                gotoxy((80-16)/2,23);
                cout<<"Input Choice:";
                gotoxy(47,23);
                cin>>ch;

                if ((ch=='d')||(ch=='D'))
                    goto menu;

                else if ((ch=='a')||(ch=='A'))
                {
                    clrscr();
                    char o;
                    gotoxy((80-14)/2,5);
                    cout<<"SEARCH BY NAME";
                    gotoxy((80-15)/2,8);
                    cout<<"Name:";
                    gotoxy(39,8);
                    gets(itemName);
                    list->selection();
                    list->displayNodes();
                    getch();
                    o = list->binarySearchName(itemName,a);
                    if ((o=='y')||(o=='Y'))
                    goto search;
                    else
                    goto menu;
                }

                else if ((ch=='b')||(ch=='B'))
                {
                    clrscr();
                    char bb;
                    gotoxy((80-13)/2,5);
                    cout<<"SEARCH BY AGE";
                    gotoxy((80-15)/2,8);
                    cout<<"Age:";
                    gotoxy(38,8);
                    cin>>ag;
                    list->selectionSortAge();
                    list->displayNodes();
                    getch();
                    bb = list->binarySearchAge(ag,a);
                    if ((bb=='y')||(bb=='Y'))
                    goto search;
                    else
                    goto menu;
                }

                else if ((ch=='c')||(ch=='C'))
                {
                    clrscr();
                    char bb;
                    gotoxy((80-26)/2,5);
                    cout<<"SEARCH BY TELEPHONE NUMBER";
                    gotoxy((80-15)/2,8);
                    cout<<"Phone:";
                    gotoxy(38,8);
                    gets(keyPhone);
                    list->selectionSortPhone();
                    list->displayNodes();
                    getch();
                    bb = list->binarySearchPhone(keyPhone,a);
                    if ((bb=='y')||(bb=='Y'))
                    goto search;
                    else
                    goto menu;
                }
            }
}


.h
CODE
typedef struct Node
{
    struct Node (char a[],int b,char c[],int i)
    {
        strcpy(name,a);
        age=b;
        index=i;
        strcpy(phone,c);
        previous =NULL;
        next= NULL;
    }
    char name[30];
    int age;
    int pos;
    int index;
    char phone[20];
    struct Node*previous;
    struct Node*next;
}
NODE;

class LinkedList
{
    protected:
        NODE*front;
        NODE*back;
        int size;
    public:
        LinkedList();
        ~LinkedList();
        void appendNode(char[],int,char[], int);
        void displayNodes();
        void destroyList();
        void selection();
        void selectionSortAge();
        void selectionSortPhone();
        char binarySearchName(char[], int);
        char binarySearchAge(int, int);
        char binarySearchPhone(char[], int);
}

LinkedList::LinkedList()
{
    front=NULL;
    back=NULL;
    size=0;
}

LinkedList::~LinkedList()
{
    destroyList();
}


void LinkedList::appendNode(char name[], int age,char phone[], int index)
{
    NODE*n=new NODE(name,age,phone,index);
    if(back==NULL)
    {
        back=n;
        front= n;
    }
    else
    {
        back->next=n;
        n->previous=back;
        back=n;
        n->pos=index;
    }
}
void LinkedList::displayNodes()
{
    clrscr();
    int i=3;
    gotoxy(1,1);
    cout<<"NAME";
    gotoxy(35,1);
    cout<<"AGE";
    gotoxy(50,1);
    cout<<"PHONE NUMBER";
    cout<<"\n\n";
    NODE*temp=front;
    while(temp!=NULL)
    {
        gotoxy(1,i);
        cout<<""<<temp->name;
        gotoxy(35,i);
        cout<<""<<temp->age;
        gotoxy(50,i);
        cout<<""<<temp->phone;
        cout<<"\n";
        temp=temp->next;
        i++;
    }
    cout<<endl;
}


void LinkedList::selection()
{
    NODE *temp1=front;
    NODE *temp2=front;
    NODE *temp4=front;
    while(temp1!=NULL)
    {
        while(temp4!=NULL)
        {
            if(strcmpi(temp2->name,temp4->name)>0)
            {
                temp2=temp4;
            }
            temp4=temp4->next;
        }

        if(temp1!=temp2)
        {
            {
                NODE *t1=temp1->previous;
                NODE *t2=temp1->next;
                NODE *t3=temp2->previous;
                NODE *t4=temp2->next;
                NODE *temp;

                if(t1==NULL)
                {
                    temp2->previous=NULL;
                    front=temp2;
                }
                else
                {
                    temp2->previous=t1;
                    t1->next=temp2;
                }

                if(t2==temp2)
                {
                    temp2->next=temp1;
                    temp1->previous=temp2;
                }
                else
                {
                    temp2->next=t2;
                    t2->previous=temp2;
                    t3->next=temp1;
                    temp1->previous=t3;
                }

                if(t4==NULL)
                {
                    temp1->next=NULL;
                    back=temp1;
                }
                else
                {
                    temp1->next=t4;
                    t4->previous=temp1;
                }
            }
            NODE *temp=temp1;
            temp1=temp2;
            temp2=temp;
        }
        temp1=temp1->next;
        temp2=temp1;
        temp4=temp1;
    }
}

void LinkedList::destroyList()
{
    NODE*temp=back;
    while(temp!=NULL)
    {
        NODE*temp2=temp;
        temp=temp->previous;
        delete temp2;
    }
    back=NULL;
    front=NULL;
}

void LinkedList::selectionSortAge()
{
    NODE *temp1=front;
    NODE *temp2=front;
    NODE *temp4=front;
    while(temp1!=NULL)
    {
        while(temp4!=NULL)
        {
            if(temp2->age>temp4->age)
            {
                temp2=temp4;
            }
                temp4=temp4->next;
        }
        if(temp1!=temp2)
        {
            NODE *t1=temp1->previous;
            NODE *t2=temp1->next;
            NODE *t3=temp2->previous;
            NODE *t4=temp2->next;
            NODE *temp;

            if(t1==NULL)
            {
                temp2->previous=NULL;
                front=temp2;
            }
            else
            {
                temp2->previous=t1;
                t1->next=temp2;
            }

            if(t2==temp2)
            {
                temp2->next=temp1;
                temp1->previous=temp2;
            }
            else
            {
                temp2->next=t2;
                t2->previous=temp2;
                t3->next=temp1;
                temp1->previous=t3;
            }

            if(t4==NULL)
            {
                temp1->next=NULL;
                back=temp1;
            }
            else
            {
                temp1->next=t4;
                t4->previous=temp1;
            }
            NODE *te=temp1;
            temp1=temp2;
            temp2=te;
        }
        temp1=temp1->next;
        temp2=temp1;
        temp4=temp1;
    }
}

void LinkedList::selectionSortPhone()
{
    NODE *temp1=front;
    NODE *temp2=front;
    NODE *temp4=front;
    while(temp1!=NULL)
    {
        while(temp4!=NULL)
        {
            if((strcmpi(temp2->phone,temp4->phone))>0)
            {
                temp2=temp4;
            }
            temp4=temp4->next;
        }

        if(temp1!=temp2)
        {
            NODE *t1=temp1->previous;
            NODE *t2=temp1->next;
            NODE *t3=temp2->previous;
            NODE *t4=temp2->next;
            NODE *temp;

            if(t1==NULL)
            {
                temp2->previous=NULL;
                front=temp2;
            }
            else
            {
                temp2->previous=t1;
                t1->next=temp2;
            }

            if(t2==temp2)
            {
                temp2->next=temp1;
                temp1->previous=temp2;
            }
            else
            {
                temp2->next=t2;
                t2->previous=temp2;
                t3->next=temp1;
                temp1->previous=t3;
            }

            if(t4==NULL)
            {
                temp1->next=NULL;
                back=temp1;
            }
            else
            {
                temp1->next=t4;
                t4->previous=temp1;
            }
            NODE *te=temp1;
            temp1=temp2;
            temp2=te;
        }
        temp1=temp1->next;
        temp2=temp1;
        temp4=temp1;
    }
}

char LinkedList::binarySearchName(char name[], int size)
{
    Node *ptr1=front,*ptr2,*ptr;
    int beg=1,end=size,mid,i;
    char in;

    for(ptr2=ptr1; ptr2->next!=NULL; ptr2=ptr2->next);

    while(beg <= end)
    {
        mid=(beg+end)/2;
        for(ptr = ptr1, i = beg; i < mid; ptr = ptr->next, i++);

        if(strcmpi(name,ptr->name)<0)
        {
            ptr2=ptr->previous;
            end=mid-1;
        }
        else if(strcmpi(name,ptr->name)>0)
        {
            ptr1=ptr->next;
            beg=mid+1;
        }
        else if(strcmpi(name,ptr->name)==0)
        {
            gotoxy((80-26)/2,10);
            cout<<name<<" found at the position "<<mid;
            gotoxy((80-20)/2,13);
            cout<<"Name: "<<ptr->name;
            gotoxy((80-20)/2,14);
            cout<<"Age: "<<ptr->age;
            gotoxy((80-20)/2,15);
            cout<<"Phone: "<<ptr->phone;
            gotoxy((80-20)/2,23);
            cout<<"Search Again? [y/n]";
            gotoxy(51,23);
            cin>>in;
            return in;
        }
    }
    gotoxy((80-25)/2,15);
    cout<<name<<" not found in the list.";
    gotoxy((80-20)/2,23);
    cout<<"Search Again? [y/n]";
    gotoxy(51,23);
    cin>>in;
    return in;
}

char LinkedList::binarySearchAge(int age, int size)
{
    Node *ptr1=front,*ptr2,*ptr;
    int beg=1,end=size,mid,i;
    char in;

    for(ptr2=ptr1; ptr2->next!=NULL; ptr2=ptr2->next);

    while(beg <= end)
    {
        mid=(beg+end)/2;
        for(ptr = ptr1, i = beg; i < mid; ptr = ptr->next, i++);

        if(age < ptr->age)
        {
            ptr2=ptr->previous;
            end=mid-1;
        }
        else if(age > ptr->age)
        {
            ptr1=ptr->next;
            beg=mid+1;
        }
        else if(age==ptr->age)
        {
            gotoxy((80-26)/2,10);
            cout<<age<<" found at the position "<<mid;
            gotoxy((80-20)/2,13);
            cout<<"Name: "<<ptr->name;
            gotoxy((80-20)/2,14);
            cout<<"Age: "<<ptr->age;
            gotoxy((80-20)/2,15);
            cout<<"Phone: "<<ptr->phone;
            gotoxy((80-20)/2,23);
            cout<<"Search Again? [y/n]";
            gotoxy(51,23);
            cin>>in;
            return in;
        }
    }
    gotoxy((80-14)/2,15);
    cout<<age<<" NOT FOUND!";
    gotoxy((80-20)/2,23);
    cout<<"Search Again? [y/n]";
    gotoxy(51,23);
    cin>>in;
    return in;
}

char LinkedList::binarySearchPhone(char phone[], int size)
{
    Node *ptr1=front,*ptr2,*ptr;
    int beg=1,end=size,mid,i;
    char in;

    for(ptr2=ptr1; ptr2->next!=NULL; ptr2=ptr2->next);

    while(beg <= end)
    {
        mid=(beg+end)/2;
        for(ptr = ptr1, i = beg; i < mid; ptr = ptr->next, i++);

        if(strcmp(phone,ptr->phone)<0)
        {
            ptr2=ptr->previous;
            end=mid-1;
        }
        else if(strcmp(phone,ptr->phone)>0)
        {
            ptr1=ptr->next;
            beg=mid+1;
        }
        else if(strcmp(phone,ptr->phone)==0)
        {
            gotoxy((80-26)/2,10);
            cout<<phone<<" found at the position "<<mid;
            gotoxy((80-20)/2,13);
            cout<<"Name: "<<ptr->name;
            gotoxy((80-20)/2,14);
            cout<<"Age: "<<ptr->age;
            gotoxy((80-20)/2,15);
            cout<<"Phone: "<<ptr->phone;
            gotoxy((80-20)/2,23);
            cout<<"Search Again? [y/n]";
            gotoxy(51,23);
            cin>>in;
            return in;
        }
    }
    gotoxy((80-15)/2,15);
    cout<<phone<<" not found in the list.";
    gotoxy((80-20)/2,23);
    cout<<"Search Again? [y/n]";
    gotoxy(51,23);
    cin>>in;
    return in;
}



please help me...
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Help! About Displaying..
2 Oct, 2007 - 03:08 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you binarySearchName has a return statement embedded in the last else-if control block. when the first matching entry is found, the return statement will cause the function to exit and return the value received from the cin statement.

if you are trying to display all of the values matching the search criterion, remove that return statement.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:01PM

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