当前位置:编程学习 > C/C++ >>

c++ 图的根顶点

仔有向图G中顶点只有编号的信息,如果r到G中的每个顶点都有路经可达,则称顶点r为G的根顶点。编写算法判断有向图G是否有根,若有,则显示所有的根顶点。
答案: 
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
struct Book
{
 char bookname[20];
 char author[20];
 char ISBN[10];
 char date[10];
 char publisher[50];
};
struct Node
{
 struct Book book; 
 struct Node *next;
};
void option();
void select();
Node *head;
Node *pt[10];
FILE *fp;
//新增
void ADD(Node *head)
{
 Node *p,*s;
 s = head;
 cout<<"输入图书信息:"<<endl;
 p=new Node;
 
 cin.clear();
 cin.sync();
 cout<<"书名:";
 cin.getline(p->book.bookname,20);
 strcat(p->book.bookname,"\n");
 cin.clear();
 cout<<"出版社:";
 cin.getline(p->book.publisher,10);
 strcat(p->book.publisher,"\n");
 cin.clear();
 cin.sync();
 cout<<"作者名:";
 cin.getline(p->book.author,20);
 strcat(p->book.author,"\n");
 cin.clear();
 cin.sync();
 cout<<"图书号:";
 cin.getline(p->book.ISBN,5);
 strcat(p->book.ISBN,"\n");
 cin.clear();
 cin.sync();
 cout<<"出版日期:";
 cin>>p->book.date;
 strcat(p->book.date,"\n");
 while(s->next)
  s = s->next;
 s->next = p;
 p->next = NULL;
}
//初始化
Node *Initial()
{
 Node *head;
 head = new Node;
 head->next = NULL;
 return head;
}
//查找
int findauthor(const Node *head)
{
 Node *ps;
 char author[20];
 int count = 0;
 ps = head->next;
 cout<<"请输入作者名:";
 cin.getline(author,20);
 strcat(author,"\n");
 while(ps)
 {
  if(strcmp(ps->book.author,author) == 0)
  {
   pt[0] = ps;
   count++;
  }
  ps = ps->next;
 }
 if(count == 0)
  cout<<"图书不存在!"<<endl;
 return count;
}
int findbookname(const Node *head)
{
 Node *ps;
 char bookname[20];
 int count = 0;
 int i = 0;
 ps = head->next;
 cout<<"请输入书名:";
 cin.getline(bookname,20);
 strcat(bookname,"\n");
 while(ps)
 {
  if(strcmp(ps->book.bookname,bookname) == 0)
  {
   pt[i] = ps;
   count++;
   i++;
  }
  if(ps->next ==NULL)
   break;
  ps = ps->next;
 }
 if(count == 0)
  cout<<"图书不存在!"<<endl;
 return count;
}
int findpublisher(const Node *head)
{
 Node *ps;
 char publisher[50];
 int count=0;
 int i=0;
 ps = head->next;
 cout<<"请输入出版社名:";
    cin.getline(publisher,50);
    strcat(publisher,"\n");
    while(ps)
 {
     if(strcmp(ps->book.publisher,publisher) == 0)
  {
      pt[i] = ps;
      count++;
      i++;
  }
     if(ps->next ==NULL)
      break;
     ps = ps->next;
 }
    if(count == 0)
     cout<<"图书不存在!"<<endl;
   return count;
}
int findISBN(const Node *head)
{
 Node *ps;
    char ISBN[10];
 int count=0;
 int i=0;
 ps = head->next;
 cout<<"请输入图书号:";
    cin.getline(ISBN,10);
    strcat(ISBN,"\n");
    while(ps)
 {
     if(strcmp(ps->book.ISBN,ISBN) == 0)
  {
      pt[i] = ps;
      count++;
      i++;
  }
     if(ps->next ==NULL)
      break;
     ps = ps->next;
 }
    if(count == 0)
     cout<<"图书不存在!"<<endl;
   return count;
}
int finddate(const Node *head)
{
 Node *ps;
    char date[10];
 int count=0;
 int i=0;
 ps = head->next;
 cout<<"请输入出版日期:";
    cin.getline(date,10);
    strcat(date,"\n");
    while(ps)
 {
     if(strcmp(ps->book.date,date) == 0)
  {
      pt[i] = ps;
      count++;
      i++;
  }
     if(ps->next ==NULL)
      break;
     ps = ps->next;
 }
    if(count == 0)
     cout<<"图书不存在!"<<endl;
   return count;
}
void Show(int n)
{
 for(int i=0;i<n;i++)
 {
  cout<<"书名:"<<pt[0]->book.bookname;
  cout<<"出版社:"<<pt[i]->book.publisher;
  cout<<"作者名:"<<pt[i]->book.author;
  cout<<"图书号:"<<pt[i]->book.ISBN;
  cout<<"出版日期:"<<pt[i]->book.date;
  cout<<endl;
 }
 cin.get();
}
int Find(Node *head)
{
 int n,num;
 system("cls");
 if(head->next == NULL)
 {
  cout<<"没有任何图书"<<endl;
  cin.get();
 }
 else
 {
  cout<<"请选择查找方式(1.按作者名查找  2.按书名查找  3.按出版社名查找  4.按图书号查找  5.按出版日期查找):";
  cin>>n;
  cin.clear();
  cin.sync();
  switch(n)
  {
  case 1:
   num = findauthor(head);
   if(num != 0)
    Show(num);
   break;
  case 2:
   num = findbookname(head);
   if(num !=0)
    Show(num);
   break;
  case 3:
 num=findpublisher(head);
 if(num!=0)
 Show(num);
 break;
  case 4:
    num=findISBN(head);
 if(num!=0)
 Show(num);
 break;
  case 5:
    num=finddate(head);
 if(num!=0)
 Show(num);
 break;
  default:
   cout<<"请重新输入!"<<endl;
   cin.get();
   system("cls");
   option();
   select();
  }
 }
 return num;
}
int Len(const Node *head)
{
 Node *ps;
 int count = 0;
 ps = head->next;
 while(ps)
 {
  count++;
  ps = ps->next;
 }
 return count;
}
//删除图书信息
void Deletebook(Node *head)
{
 Node *p,*q;
 char bookname[20];
 int count = 0;
 int i = 0;
 p = head;
 q = p->next;
 if(q == NULL)
  cout<<"没有任何图书!"<<endl;
 else
 {
  cout<<"请输入书名:";
  cin.getline(bookname,20);
  strcat(bookname,"\n");
  while(q != NULL)
  {
   q = p->next;
   if(strcmp(q->book.bookname,bookname) == 0)
   {
    p->next = q->next;
    delete q;
    count++;
   }
   p = q;
   q = q->next;
  }
  if(count == 0)
   cout<<"没找到该图书!"<<endl;
  else
 

上一个:二次函数问题(C++)
下一个:求C++游戏编程入门知识

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,