实验3-线性表综合实验单链表

建立一个由n个学生成绩的链表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

#include<iostream.h>            

struct Node       //结构体
{      
    float data;      
    Node *next;      
};        
class LinkList
{    
public:    
LinkList();              //无参构造函数,建立只有头结点的空链表    
LinkList(float score[],int n); //构造函数    
    ~LinkList()                        //析构函数    
{    
Node *q;    
while(first!=NULL)    
{    
q=first;    
first=first->next;    
delete q;    
}    
}    
void Insert(int i,float x);          //插入操作,在位置i插入元素x    
float Delete(int i);            //删除操作,删除位置i的元素    
float Get(int i);          //按位查找    
    int Locate(float x);   //按值查找    
void Print();             //输出操作    
private:    
        Node *first; //头指针      
};          
LinkList::LinkList()          //构造函数   
{      
    first = new Node;    //建立头结点   
    first->next = NULL;      
}            
LinkList::LinkList(float score[],int n)    
{    
    Node *s;    
    first=new Node; 
first->next=NULL;  //初始化一个空链表    
    for(int i=0;i<n;i++)    
    {    
        s=new Node;
s->data=score[i];    
        s->next=first->next;
first->next=s;      
    }    
}    
void LinkList::Insert(int i,float x)   //插入 
{    
    Node *p,*s; 
int count;    
    p=first;count=0;    
    while(p!=NULL&&count<i-1)    
    {    
        p=p->next;    
        count++;    
    }    
    if(p==NULL)throw"位置非法";    
    else{    
        s=new Node;
s->data=x;    
        s->next=p->next;
p->next=s;    
    }    
}       
float LinkList::Delete(int i)      //删除
{    
Node *q,*p;
float x; 
int count;    
p=first;count=0;    
while(p!=NULL&&count<i-1)       
{    
p=p->next;    
count++;    
}    
if(p==NULL||p->next==NULL)throw"位置";      
else{    
q=p->next;x=q->data;      
p->next=q->next;    
delete q;    
return x;    
}    
}           
float LinkList::Get(int i)      //按位查找
 {    
Node *p;
int count;    
p=first->next;count=1;    
while(p!=NULL&&count<i)    
{
p=p->next;count++;
}    
if(p==NULL)throw"位置非法";    
else return p->data;    
}         
int LinkList::Locate(float x)  //按值查找  
{    
Node *p;
int count;    
p=first->next;
count=1;    
while(p!=NULL)    
{    
if(p->data==x)return count;    
p=p->next;    
count++;    
}    
return 0;    
}    
void LinkList::Print()   //输出操作  
 {    
Node *p;    
p=first->next;    
while(p!=NULL)    
{
cout<<p->data<<"  ";;    
p=p->next;    
}    
}       
void main()    
{    
    float score[10]={55.5,60,98,85.5,67,89,77,80.5,60,67};    
    LinkList student(score,10);        
      cout<<"插入前学生成绩:";    
    student.Print();   
    cout<<endl<<endl<<"在位置5插入成绩90"<<endl;  
    cout<<"插入后:";  
    student.Insert(5,99);      
    student.Print();   
    cout<<endl<<endl<<"在删除前成绩:"<<endl;   
    student.Delete(1);  
    cout<<"删除后:";  
    student.Print();    
    cout<<endl<<endl<<"位置10的成绩:";  
    cout<<student.Get(10);   
    cout<<endl<<endl<<"成绩77所在位置:";   
    cout<<student.Locate(77);  
    cout<<endl;      

}   


猜你喜欢

转载自blog.csdn.net/weixin_40803490/article/details/80714655