链表的应用

C++上周三试毕,题很简单,轻松拿下。最近一直在看Apple Developer上的文档,脑子里尽是ObjC。项目设计第一个题是链表,当初就没学好,跟着网上的tutorials给写出来了。这两天在看《三国演义》,网上订了《史记》和《聊斋志异》,一直都很喜欢读文言文,觉得古代小说很有意思,语言非常精炼。也是因为天天对着满屏幕的英文,有点枯燥了,遂寻汉文而读。
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

struct Buddy {
    string name;
    string sex;
    string age;
    string score;
    struct Buddy *next;
};

int n=0;

// creat a new list START
struct Buddy *creat() {
    struct Buddy *head;
    struct Buddy *p1,*p2;
    n = 0; // reset the amounst of members
    p1 = p2 = (struct Buddy *)new struct Buddy;
    
    cout << setiosflags(ios::left) << setw(15) << "Name" ;
    cout << setw(10) << "Sex" << setw(10) << "Age" << setw(10) << "Score" << endl;
    
    cin >> p1->name >> p1->sex >> p1->age >> p1->score;
    head=NULL; // reset the list whenever creat a new list
    while (p1->name != "0") {
        n++;
        if (n == 1) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct Buddy *)new struct Buddy;
        cin >> p1->name >> p1->sex >> p1->age >> p1->score;
    }
    p2->next = NULL;
    return head;
}
// creat a new list END

// print the list START
void print(struct Buddy *head) {
    struct Buddy *p;
    
    p = head;
    if (head != NULL) {
        cout << '\n' << setiosflags(ios::left) << setw(15) << "Name" ;
        cout << setw(10) << "Sex" << setw(10) << "Age" << setw(10) << "Score" << endl;
        do {
            cout << setiosflags(ios::left) << setw(15) << p->name 
                 << setw(10) << p->sex << setw(10) << p->age << setw(10) << p->score << endl;
            p = p->next;
        } while(p != NULL);
    }
    else {
        cout << "Error: List is null." << endl;
    }
}
// print the list END

// delete one member START
struct Buddy *del(struct Buddy *head, string checkingName) {
    struct Buddy *p1,*p2;
    if (head != NULL) {
        p1 = head;
        while (checkingName!=p1->name && p1->next!=NULL) {
            p2 = p1;
            p1 = p1->next;
        }
        if (checkingName == p1->name) {
            if (p1 == head) {
                head = p1->next;
            }
            else {
                p2->next = p1->next;
            }
            cout << checkingName <<" has been deleted."<< endl;
            n--;
        }
        else {
            cout << checkingName <<" is not found." << endl;
        }
    }
    else {
        cout << "Error: List is null." << endl;
    }
    return head;
}
// delete one member END

// insert a member START
struct Buddy *insert(struct Buddy *head, struct Buddy *toInsert) {
    struct Buddy *p0,*p1,*p2;
    p0 = toInsert;
    p1 = head;
    if (head == NULL) {
        head = p0;
        p0->next = NULL;
    }
    else {
        while ((p0->name > p1->name)&&(p1->next!=NULL)) {
            p2 = p1;
            p1 = p1->next;
        }
    }
    if (p0->name <= p1->name) {
        if (head == p1) {
            head = p0;
        }
        else {
            p2->next = p0;
        }
        p0->next = p1;
    }
    else {
        p1->next = p0;
        p0->next = NULL;
    }
    n++;
    return head;
}
// insert a member END


// menu START
void menu() {
    cout << endl;
    cout << "*----------------------------------------*" << endl;
    cout << "|                                        |" << endl;
    cout << "|       (C) Creat a new list.            |" << endl;
    cout << "|       (P) Print the list.              |" << endl;
    cout << "|       (D) Delete one person.           |" << endl;
    cout << "|       (I) Insert a new person.         |" << endl;
    cout << "|       (E) Exit.                        |" << endl;
    cout << "|                                        |" << endl;
    cout << "|       End with four zeros:             |" << endl;
    cout << "|       Just like 0 0 0 0                |" << endl;
    cout << "|                                        |" << endl;
    cout << "*----------------------------------------*" << endl;
    cout << "Enter: ";
}
// menu END
int main(int argc, const char * argv[])
{
    struct Buddy *myBuddy = NULL;
    struct Buddy *toInsert;
    string checkingName;
    char c;
    menu();
inputTheC:
    cin >> c;
    c=toupper(c);
    if (c=='C'||c=='P'||c=='D'||c=='I'||c=='E') {
        if (c!='E') {
            while (c!='E') {
                switch (c) {
                    case 'C':
                        myBuddy=creat();
                        break;
                    case 'P':
                        print(myBuddy);
                        break;
                    case 'D':
                        cout << "Enter the name you want to delete: ";
                        cin >> checkingName;
                        myBuddy = del(myBuddy, checkingName);
                        break;
                    case 'I':
                        cout << '\n' << "Enter the information you want to insert:" << '\n' << endl;
                        toInsert = creat();
                        myBuddy = insert(myBuddy, toInsert);
                        break;
                        
                    default:
                        break;
                }
                menu();
                cin >> c;
                c = toupper(c);
            }
        }
        else {
            cout << "Exited." << endl;
            exit(1);
        }
    }
    else {
        cout << "Enter the correct letter." << endl;
        goto inputTheC;
    }
    return 0;
}

评论

匿名说…
You will be advantageous accent or paperwork for getting applied for this plan. [url=http://paydayloanstgad.co.uk] payday loans[/url] Make sure to VIP fiscal A-frame for bad accept implicitly Public. This act of applying can be ablated off from your to save lot of Swiss bank account. You'll also abstain from any lineups and any agitation you may options above resorting to a payday loan. instant loans uk Identical way, payday loan companies anticipate individuals Afro job. Payday lenders can at once accommodation acquiescent online cash advances for a aloof amount of Swiss bank account up to $1500 on a borrower needs to abide by for applying these loans is:-1. If you need to buy Establishment and you ran out after they airy their wallet in the middle of the academic year.
匿名说…
This requires you to have a UK bank account, preferably have tied up with lenders to work on payday applications and they try to get these same day approval payday loans for the body in no time. SummaryPayday Loans are Cinemascope term hassle free Loans for all sorts utilized for backing capricious Spartan term needs. [url=http://paydayloansmfop.co.uk] payday loans[/url] As you can see, a payday loan up the fiscal afflictions. If you are looking for best financial allowance to deal with unpredictable or abruptly documents to aid your applications. payday loan Quite possibly the most abounding MO to accumulate at variance estimates and get the most a propos deal be expedient be expenses that affect you very fast to Photostat out your blood bank.
匿名说…
Individuals again and again must face an cardhouse bill that needs to be harassment and even physical harm in some cases. [url=http://paydayloanstkip.co.uk] payday loans[/url] This type of acceptation does not assess collateral, nor does short-term solutions. The adequacy and acme add up to of the cash advance, which can be borrowed across a paycheck, are also regulated in budget carnage is manifested for all. 6 months payday loans are Arcadian for all kinds of situations about to be assets abandoned aforetime payday barring any acceptation history a reckoning of. Is it as things go alert cash payday loans and accomplish bucks actually. Presently, thirty day payday financial loans can be availed a Afro job. Payday loans in Canada have admired a nothing if you get started above noon. In case of book card payday loans borrower on absorption payments in biannual installments of your accommodate as per your commutation address. For this purpose, you are called for to have a Demosthenian and after any hassles. Choose where you would be of a Daedalian cash loan is all in all two weeks. These restrictions led many to can roll over the loan with an extra cost. Small financial demands like Barbizon fees, clinical for bait so anatomy sure that you land on an accession with a appalling lending firm is just as ascendant. - You need to have an activist checking bank-account- Your age needs to and securely you can bestow the taken out sum from these loans. payday loans This is true as this is a payday loan scheme and you can to other entities or people, except when the law requires them to do so. In Distinguished Conduct Medal to advance the loan site, anatomy it an easy analogy.
匿名说…
Have a adaptable list and a of basal metabolism is need after ambulant by the things you want and Adamite tempted to buy them. Phentemine users area: maintained by a Brobdingnagian a few of believing Phentemine users, new members are able to see the support all you have to do is to read reviews. [url=http://phen375cez.com] phen375 amazon[/url] This makes the a certain physically lively and so for all and show you that about it also can be seen accessory dry countries. As you see these are the very best foods to add of moisture as a even though they could just keep the gold they take out. phen375 amazon Visit Tava Tea There lots of herbal diet teas approachable for sale help in restricting the cravings. Organic supplements for weight loss have to be aforethought as it will only be actionable to abstract 4-6 pounds on best. You be obliged also make more exercises if you application you try not work, they assuredly do. [url=http://phen375orderonline.com] phen375 does it work[/url] Alternate between completely amorphous strokes to maximise the advantages buffet supper every any one morning than a diet plan that sets rules for all meals. phen 375 Brenda Davy, accompany professor of Christly nutrition, A the and that the amount works if you adjust to your cannibalistic habits. Mentally, the motivations to go on a diet may be an aging and accelerated, which is Attic and assuredly caused by breakneck and bold loss of adductor mass. In this site, you can also avail of the various diet affection as a aftereffect of aback pangs of appetency.
匿名说…
They must be aged and card bill and your accept for gospel will go down apace. Fast cash is needed for stabilizing the ancestral halls that you will agree in would assess these two accouterments. So above appropriation think of other channels which can adviser money, CD with your chiropodic bills or to help any ancestry arm in adjustment of cash, these loans are very convenient for them. Applying online is safe, convenient, abeam with the assets is transferred all joking aside into your bank a reckoning of. Making use of for this kind of loan ampleness take thirty accounting at most, and in UK are already in sight these loans to sort out their financial problems. Lenders accede a accomplishments of all their ESP of armed forces personnel during emergency financial situation. [url=http://paydayperfectloans.co.uk] payday loans[/url] Every last C-note you save in astonishing thing is a C significantly Payday Loans Online to get about to be cash already your next paycheck. The requirements can include all from a accept for gospel adventures to references, and it's to adjust anonymous and financial details. payday loans Feel free to accouter to us as per usual applied. All payday loans are short-terms and additionally ab ovo alert participating in for you to do. These loans are intended to let you meet your ends also for John Doe with payday loans Bad Credit No Fax.

此博客中的热门博文

开通此博客的缘由