C++练习 | 单链表的创建与输出(结构体格式)

#include <iostream>
#include <stdio.h>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2

int num=0;

typedef struct Book
{
    string IS;
    string Name;
    float price;
    Book *next;
}Book,*PBook;

PBook Create()
{
    PBook Head=new Book;
    PBook Tail=new Book;
    PBook New;
    string t1,t2;
    float t3;
    Tail=Head;
    while(cin>>t1>>t2>>t3&&t3)
    {
        num++;
        New=new Book;
        New->IS=t1;
        New->Name=t2;
        New->price=t3;
        
        Tail->next=New;
        
        New->next=NULL;
        
        Tail=New;
        
    }
    return Head;
}

void Print(PBook pHead)
{
    PBook p;
    p=pHead->next;
    cout<<num<<endl;
    while(p!=NULL)
    {
        cout<<p->IS<<" "<<p->Name;
        printf(" %.2f\n",p->price);
        p=p->next;
    }
}

int main()
{
    PBook Head;
    Head=Create();
    Print(Head);
    return 0;
}

 

posted @ 2019-09-19 00:02  洛枫大人  阅读(977)  评论(0编辑  收藏  举报