10_4_3.cpp

来自「本文档是(作者:钱能)《C++程序设计教程》课后习题答案。 选题编辑:张朝阳 」· C++ 代码 · 共 48 行

CPP
48
字号
//10_4_3

#include <iostream.h>

struct Lnode
{
  double data;
  Lnode* next;
};

void ShowList(Lnode* head);
void AddToEnd(Lnode* pnew, Lnode*& head);
Lnode* GetNode();
void DeleteList(Lnode* head);

void main()
{
  Lnode* head=NULL;
  Lnode* temp;
  double d;
  cout <<"data? ";
  cin >>d;
  while(d>0&&(temp=GetNode())){
    temp->data=d;
    AddToEnd(temp, head);
    cout <<"data? ";
    cin >>d;
  }
  ShowList(head);
  DeleteList(head);
}

void ShowList(Lnode* head)
{
  if(head){
    cout <<head->data <<endl;
    if(head->next)
      ShowList(head->next);         //递归调用
  }
}

void AddToEnd(Lnode* pnew, Lnode*& head)
{
  if(!head){
    head=pnew;
    pnew->next=NULL;
  }else
    AddToEnd(pnew, head->next);     //递归调用

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?