回 帖 发 新 帖 刷新版面

主题:[讨论]帮忙看些 谢谢

#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>//使用随机数表 
using namespace std;
class list
{
     public:
        int num,score;
        char name[10];
        class list *next;     
};
list del_ptr(list *head,list *ptr);
int main()
{
    list ptr;
    int findword=0,find,data[12][2];
    char namedata[12][10]={{"Allen"},{"Moko"},{"Lean"},{"Meeild"},{"Angel"},
                            {"Sdfs"},{"prosoul"},{"protafee"},{"tafee"},
                            {"devil"},{"speed"},{"ggcbl"}};
    srand((unsigned)time(NULL));
    cout<<"座位 成绩 座位 成绩 座位 成绩 座位 成绩 "<<endl;
    cout<<"========================================="<<endl;
    for(int i=0;i<12;i++)
     {
          data[i][0]=i+1;
          data[i][1]=rand()%50+51;  
     }
    for(int i=0;i<3;i++)
       for(int j=0;j<4;j++)
          cout<<"["<<data[j*3+i][0]<<"] ["<<data[j*3+i][1]<<"]";
       cout<<endl;                        
}
list *head=new list;//建立表头 
if(!head)
{
       cout<<"Error!!内存申请失败"<<endl;
        exit(1);  
}
head->num=data[0][0];
for(int j=0;j<10;j++)
haed->name[j]=namedata[0][j];
head->score=data[0][1];
head->next=NULL;
ptr=head;
for(int i=1;i<12;i++)
{
    list *newnode=new node;
    newnode->num=data[i][0];
    for(int j=0;j<10;j++)
    new->nate[j]=namedata[i][j];
    newnode->next=NULL;
    ptr->next=newnode;
    ptr=ptr->next;
}
while(1)
{
     cout<<"请输入要删除的数据,结束输入-1"<<endl;
     cin>>findword;
     if(findword==-1)  break;
     else
     {
         ptr=head;
         find=0;
         while(ptr!=NULL)
         {
             if(ptr->score==findword)
             {
                *ptr=del_ptr(head,ptr);
                find++;
             }
             ptr=ptr->next;                    
         }
         if(find==0)
         cout<<"#####没有找到#####"<<endl;
     }
     ptr=head;
     cout<<"\n\t座位\t名字\t成绩"<<endl;
     cout<<"\t============================"<<endl;
     while(ptr!=NULL)
     {
           cout<<"\t["<<ptr->num<<"]\t["<<setw(10)<<ptr->name
            <<"]\t["<<ptr->score<<"]"<<endl;
            ptr=ptr->next;         
     }
     system("pause");
        return 0;
}
list del_ptr(list *head,list *ptr)
{
     list *top=head;
     if(ptr==head)//删除点在表头 
     {
         head=head->next;
         cout<<"已删除第"<<ptr->num<<" 号学生!!名字"<<ptr->name<<endl;         
     }
     else
     {
         while(top->next!=ptr)
         top=top->next;
         if(ptr->next==NULL)//删除点在表尾 
         {
              top->next=NULL;
              cout<<"已删除第 "<<ptr->num<<" 号学生!!名字"<<ptr->name<<endl;              
         }
         else//删除任意点 
         {
             top->next=ptr->next;
             cout<<"已删除第 "<<ptr->num<<" 号学生!!名字"<<ptr->name<<endl; 
         }
     }
     delete []ptr;//释放内存 
     return *head;//返回表头地址 
}

这是关于学生信息的链表。。。。有点错误。。请高人点明

回复列表 (共1个回复)

沙发

不麻烦你们了。。我犯了低级错误。。自己改好了

我来回复

您尚未登录,请登录后再回复。点此登录或注册