none
C++constructor…… RRS feed

  • 问题

  • 下面是一个简单的单向链表。。。

    为什么会报错:expected constructor,destructor,or type conversion before';'token

    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    
    int n;
    struct ddl{
           int info;
           char sex;
           struct ddl *next;
    }stu;
    struct ddl *head,*rear;
    void build_ddl(){
             struct ddl *p1=NULL,*p2;
             int i=0;
             for(i=0;i<100;i++){
                 p1=(struct ddl *)malloc(sizeof(struct ddl));
                 if(!p1){printf("dynamic ditribution of memo failed!\n");break;}
                 if(!i)head=p1;
                 if(i)p2->next=p1;
                 p2=p1;
             }
             rear=p2;
             rear->next=NULL;
    }
    void output_(){
         struct ddl *p=head;
         int i;
         for(i=1;i<=n;i++){
             printf("number %d students' info: %d %c\n",i,p->info,p->sex);
             p=p->next;
         }
    }
    void input(){
         int i;
         struct ddl *p;
         printf("input number of students,students' info: ID and sex:\n");
         scanf("%d",&n);
         for(i=0;i<n;i++)
               scanf("%d %c",&p->info,&p->sex);
               p=p->next;
         }
         output_();/*这里报错:expected constructor,destructor,or type conversion before';'token*/
    }
    void insert_afteri_pos(){
         int i;
         struct ddl *p=NULL;
         printf("please input the position(i<=n),info and sex:\n");
         scanf("%d",&i);
         if(i<0||i>n){
             printf("invlalid input!please input again:\n");
             insert_afteri_pos();
         }
         else{
              p=malloc(sizeof(struct stu));
              if(!p){
                 printf("insert data failed!\n");
                 return ;
              }
              if(i==0){
                 p->next=head;
                 head=p;
              }
              else if(i<n){
                      struct *p1=head;
                      for(int j=1;j<i;j++) p1=p1->next;
                      p->next=p1->next;
                      p1->next=p;
              }
              else if(i==n){
                      struct stu *p1=rear;
                      rear->next=p;
                      p->next=NULL;
              }
              scanf("%d %c",&p->info,&p->sex);
              n++;
              output_();
         }
    }
    void delete_y(){
         int i,j;
         struct stu *p=head;
         printf("please input the ID of student:\n");
         scanf("%d",&i);
         while(p-info!=i)p=p->next;
         if(p==rear&&(i!=p->info))printf("no such student!");
         else{
              if(p==head){
                 head=head->next;
                 free(p);
              }
              else if(p==rear){
                      p=head;
                      while(p->next!=rear)p=p->next;
                      p->next=NULL;
                      free(rear);
                      rear=p;
              }
              else{
                  struct stu *p1=head;
                  while(p1->next!=p)p1=p1->next;
                  p1->next=p->next;
                  free(p);
              }
         }
         n--;
         output_();
    }
    void free_ddl(){
         free(head);
    }
    int main(){
        build_ddl();
        input();
        insert_afteri_pos();
        delete_y();
        free_ddl();
        return 0;
    }
    
    

    2012年8月6日 6:44

全部回复