Thread: Insert node and delete problem <HURRY!!> <pls HELP>>

  1. #1
    Registered User tom_mk's Avatar
    Join Date
    Feb 2002
    Posts
    3

    Unhappy Insert node and delete problem <HURRY!!> <pls HELP>>

    Hi, everybody
    i've got someproblem about my c++
    i'm in a very hurry, pls help!!!!

    question1
    =======================================
    i tried to insert the new node alphabetically
    for example, if the first data's<name> node is "bbb" , and the new node data <name> is "AAA" , "AAA" will be insert before 'bbb" , meanwhile , if the new one data is " ccc" , "ccc" will be inserted after "bbb", but the problem is, there is only one node happen in the list whihch is the first node that i input only!!!
    pls help!!!

    question2
    =======================================
    in this situation, there is only one node in my link list
    but after i tried to del one node by using del function in my program, there is an error happen, i don't understand about that error, the error is not the error while compile
    but it is porblem while i used the function during the program
    -----------------------------------------------------------------------------
    this is my source
    ----------------------------------------------------------------------------
    // ************************************************** *
    // ** Assignment 2 < comp 114 > **
    // ** ----------------------------------------------**
    // ** Student ID: 319090 **
    // ** Name: CHottanapund Tumnoon **
    // ** Instructor: Ken Chan **
    // ** ----------------------------------------------**
    // ** Date: Febuary 15,2002 **
    // ************************************************** *

    #include <stdlib.h>
    #include <iostream>
    #include <conio.h>
    #define True 1
    #include <string>
    #include <windows.h> // for clear screen

    using namespace std;

    struct node
    {
    string name; // name of the passenger
    string agnumb; // password for reservation
    string height; // the height of the passenger
    string cost; // cost of ticket
    string airname; // airline name
    string des; // destination
    string fnum; // flight number
    string aname; // name of airline
    string dday; // departure day of origin
    string aday; // arrival day of destination
    node *link; // pointer to the next node
    };

    class menu
    {
    public:

    menu(); // constructor funciton < for initialize the private members >

    void mainmenu();
    void newname();
    void listall();
    void clrscr();
    void del();
    void check();

    private:

    };

    // declariung the pointer for the operation in this program
    // --------------------------------------------------------
    node *ptrfirst, *ptrthis, *ptrnew, *ptrlast, *previous, *cursor;
    // ptrfirst = for the pointer points to the first node
    // ptrthis = for the cursor that is movable to the whole link lists
    // ptrlast = for the pointer points to the last node
    // previous = for the cursor that point to the node before the cursor
    // ptrnew = for the new node that is going to be inserted

    node* list_search(node* ptrfirst,string target);


    void main()
    {
    ptrfirst = ptrthis = ptrnew = ptrlast = NULL;

    menu call;
    call.mainmenu();
    }

    // constructor function
    menu::menu() // for initialize the variable
    {

    }

    void menu::mainmenu()
    {
    menu newn,list,clear,del_,check_;
    int ch;

    ptrfirst = ptrthis = ptrnew = ptrlast = NULL;

    while(True)
    {
    clear.clrscr();

    cout << "\n Welcome to Tom airline"
    << "\n ************************************* "
    << "\n Type '1' to enter data"
    << "\n Type '2' to enter see list"
    << "\n Type '3' to cancel the reservation"
    << "\n Type '4' to check for the reservation"
    << "\n Type '8' to quit";
    cout << "\n ************************************* ";
    cout << "\n Choose >> ";
    cin >> ch;

    // ch = getch();

    switch(ch)
    {
    case 1: clear.clrscr();
    newn.newname();
    break;

    case 2: clear.clrscr();
    list.listall();
    break;

    case 3: clear.clrscr();
    del_.del();
    break;

    case 4: clear.clrscr();
    check_.check();
    break;

    case 8: exit(0);

    default : cout<<"\n Wrong choice entered";
    }
    }

    }

    void menu::newname() // function for inserting the new passenger
    {
    // int i;
    string c1,c2;
    ptrnew = new node;

    if(ptrfirst==NULL)
    {
    ptrfirst = ptrlast = ptrnew;

    cout<<"\n\n Enter name: ";
    cin >>ptrlast->name;

    cout<<"\n Enter Password: ";
    cin >>ptrlast->agnumb;

    cout<<"\n Enter height: ";
    cin >>ptrlast->height;

    cout<<"\n Enter cost: ";
    cin >>ptrlast->cost;

    cout<<"\n Enter Name of airline: ";
    cin >>ptrlast->aname;

    cout<<"\n Enter destination: ";
    cin>>ptrlast->des;

    cout<<"\n Enter Flight number: ";
    cin>>ptrlast->fnum;

    cout<<"\n Enter Departure day of origin: ";
    cin>>ptrlast->dday;

    cout<<"\n Enter Arrival Day: ";
    cin>>ptrlast->aday;

    ptrlast->link=NULL;

    }
    else if(ptrfirst!=NULL)
    {
    // ptrlast->link = ptrnew;

    cout<<"\n\n Enter name: ";
    cin >>ptrnew ->name;

    cout<<"\n Enter Password: ";
    cin >>ptrnew ->agnumb;

    cout<<"\n Enter height: ";
    cin >>ptrnew ->height;

    cout<<"\n Enter cost: ";
    cin >>ptrnew ->cost;

    cout<<"\n Enter Name of airline: ";
    cin >>ptrnew ->aname;

    cout<<"\n Enter destination: ";
    cin>>ptrnew ->des;

    cout<<"\n Enter Flight number: ";
    cin>>ptrnew ->fnum;

    cout<<"\n Enter Departure day of origin: ";
    cin>>ptrnew ->dday;

    cout<<"\n Enter Arrival Day: ";
    cin>>ptrnew ->aday;

    for(cursor = ptrfirst; ((cursor != NULL)||(cursor->name > ptrnew->name)); cursor = cursor->link)
    {
    if(cursor->name < ptrnew ->name)
    {
    ptrnew = new node;
    //cursor->link = ptrnew->link;
    cout << " aaaaa\n";
    cursor->link = ptrnew;
    ptrnew->link = cursor->link;
    // cursor = ptrnew;
    break;

    }
    break;
    }

    for(cursor = ptrfirst; ((cursor != NULL)||(cursor->name < ptrnew->name)); cursor = cursor->link)
    {

    if(cursor->name > ptrnew ->name)
    {
    ptrnew = new node;
    // ptrnew = ptrfirst;
    // ptrnew->link = ptrfirst->link;
    cursor->link = ptrnew;
    // ptrnew->link = cursor;
    break;

    }
    break;
    }
    }


    ptrlast->link=NULL;

    menu newn,list,clear,del_,check_;
    int ch;

    while(True)
    {


    cout << "\n Welcome to Tom airline"
    << "\n ************************************* "
    << "\n Type '1' to re-enter data"
    << "\n Type '2' to enter see list"
    << "\n Type '3' to cancel the reservation"
    << "\n Type '4' to check for the reservation"
    << "\n Type '8' to quit";
    cout << "\n ************************************* ";
    cout << "\n Choose >> ";
    cin >> ch;

    switch(ch)
    {
    case 1: clear.clrscr();
    newn.newname();
    break;

    case 2: clear.clrscr();
    list.listall();
    break;

    case 3: clear.clrscr();
    del_.del();
    break;

    case 4: clear.clrscr();
    check_.check();
    break;

    case 8: exit(0);

    default : cout<<"\n Wrong choice entered";
    }
    }


    }

    void menu::listall() // function for view the passenger database
    {
    cout << "\n\n Welcome to Tom's airline database!";
    cout << "\n ----------------------------------------------";

    if(ptrfirst == NULL)
    {

    cout << "\n Empty list";
    cout << "\n ++++++++++++++++++++++++++++++++++++++++++++++";
    }
    else
    {
    ptrthis=ptrfirst;

    do
    {
    // cout << "\n\n Welcome to Tom's airline database!";
    // cout << "\n ----------------------------------------------";
    cout<<"\n\n Name: "<< ptrthis->name
    <<"\n Password: "<< ptrthis->agnumb
    <<"\n Airline: "<< ptrthis->aname
    <<"\n Destination: "<< ptrthis->des;
    cout << "\n ++++++++++++++++++++++++++++++++++++++++++++++";
    ptrthis = ptrthis->link;
    }
    while (ptrthis != NULL);
    }

    // cout << "\n Press anykey to return to the main menu";
    // getch();

    menu newn,list,clear,del_,check_;
    int ch;

    while(True)
    {


    cout << "\n Welcome to Tom airline"
    << "\n ************************************* "
    << "\n Type '1' to enter data"
    << "\n Type '2' to enter re - see list"
    << "\n Type '3' to cancel the reservation"
    << "\n Type '4' to check for the reservation"
    << "\n Type '8' to quit";
    cout << "\n ************************************* ";
    cout << "\n Choose >> ";
    cin >> ch;

    switch(ch)
    {
    case 1: clear.clrscr();
    newn.newname();
    break;

    case 2: clear.clrscr();
    list.listall();
    break;

    case 3: clear.clrscr();
    del_.del();
    break;

    case 4: clear.clrscr();
    check_.check();
    break;

    case 8: exit(0);

    default : cout<<"\n Wrong choice entered";
    }
    }
    }

    void menu::del() // function for cancel the reservation
    {
    string cname;//,cname2[20];
    // int cnum;
    node *remove;
    remove = new node;

    cout << " Cancel the reservation \n";
    cout << " --------------------------\n";
    cout << " Name of the passenger: ";
    cin >> cname;

    // cout << list_search(ptrfirst,cname);

    if(ptrfirst == NULL)
    {
    cout << " ----------------------------------------------\n";
    cout << " There is no " << cname <<"'s reservation in our database! ";
    }
    else
    {
    for(cursor=ptrfirst ; cursor!=NULL ; cursor = cursor->link)
    {
    // cname2 = ptrthis->name;
    // cnum = strcmp(cname,ptrthis->name);


    if(cname == cursor->name)
    {
    if(cursor == ptrfirst)
    {
    remove = ptrfirst;
    ptrfirst = ptrfirst->link;
    delete remove;

    cout << " ----------------------------------------------\n";
    cout << " " << cname << "'s reservation has been canceled!\n";
    }
    else
    {
    previous = (previous->link = cursor);
    remove = previous->link;
    previous->link = remove->link;
    delete remove;
    }
    }
    else
    {
    cout << " ----------------------------------------------\n";
    cout << " else's \n";
    cout << " ----------------------------------------------\n";
    cout << " There is no " << cname <<"'s reservation in our database! ";
    break;
    }
    }

    }
    cout << "\n ++++++++++++++++++++++++++++++++++++++++++++++";
    // cout << "\n Please press anykey to return to the main menu";
    // getch();

    menu newn,list,clear,del_,check_;
    int ch;

    while(True)
    {


    cout << "\n Welcome to Tom airline"
    << "\n ************************************* "
    << "\n Type '1' to enter data"
    << "\n Type '2' to enter see list"
    << "\n Type '3' to cancel more the reservation"
    << "\n Type '4' to check for the reservation"
    << "\n Type '8' to quit";
    cout << "\n ************************************* ";
    cout << "\n Choose >> ";
    cin >> ch;

    switch(ch)
    {
    case 1: clear.clrscr();
    newn.newname();
    break;

    case 2: clear.clrscr();
    list.listall();
    break;

    case 3: clear.clrscr();
    del_.del();
    break;

    case 4: clear.clrscr();
    check_.check();
    break;

    case 8: exit(0);

    default : cout<<"\n Wrong choice entered";
    }
    }
    }

    void menu::check()
    // funciton for checking weather the ticket has been booked by
    // xxx < name of the passenger > or not
    {
    string ccname;

    cout << " Check the reservation \n";
    cout << " --------------------------\n";
    cout << " Name of the passenger: ";
    cin >> ccname;

    if(ptrfirst == NULL)
    {
    cout << " ----------------------------------------------\n";
    cout << " There is no reservation's data in our database! ";
    }

    for(ptrthis=ptrfirst;ptrthis!=NULL;ptrthis = ptrthis->link)
    {
    if(ccname == ptrthis->name)
    {
    cout << " ----------------------------------------------\n";
    cout << " " << ccname << "'s reserved the seat with us already!\n";
    }
    }

    cout << "\n ++++++++++++++++++++++++++++++++++++++++++++++";
    // cout << "\n Please press anykey to return to the main menu";
    // getch();

    menu newn,list,clear,del_,check_;
    int ch;

    while(True)
    {


    cout << "\n Welcome to Tom airline"
    << "\n ************************************* "
    << "\n Type '1' to enter data"
    << "\n Type '2' to enter see list"
    << "\n Type '3' to cancel the reservation"
    << "\n Type '4' to re-check for the reservation"
    << "\n Type '8' to quit";
    cout << "\n ************************************* ";
    cout << "\n Choose >> ";
    cin >> ch;

    switch(ch)
    {
    case 1: clear.clrscr();
    newn.newname();
    break;

    case 2: clear.clrscr();
    list.listall();
    break;

    case 3: clear.clrscr();
    del_.del();
    break;

    case 4: clear.clrscr();
    check_.check();
    break;

    case 8: exit(0);

    default : cout<<"\n Wrong choice entered";
    }
    }
    }

    void menu::clrscr() // clear screen function
    {

    HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hndl, &csbi);
    DWORD written;
    DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y +
    csbi.dwCursorPosition.X + 1;
    COORD curhome = {0,0};

    FillConsoleOutputCharacter(hndl, ' ', N, curhome, &written);
    csbi.srWindow.Bottom -= csbi.srWindow.Top;
    csbi.srWindow.Top = 0;
    SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
    SetConsoleCursorPosition(hndl, curhome);

    }

    /*node* list_search(node* ptrfirst,string target)
    {
    node *cursor, *pre;

    for(cursor = ptrfirst; cursor != NULL; cursor = cursor->link)
    {
    // pre = pre->link = target;
    if(target == cursor->name)
    {
    //cursor = pre->link;
    //pre = pre->link;
    // pre->link = cursor;

    return cursor;
    // return pre;
    }

    }
    return NULL;
    }
    */

    --------------------------------------------------------------------------------
    Thank you everybody
    TOM

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    use the [ CODE ] [ /CODE ] tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM