双链表-学生成绩

#include<iostream.h>    
template<class T>    
struct Node    
{    
    T data;    
    Node<T>*next,*prior;    
};    
template<class T>    
class Student    
{    
    public:    
        Student();    
        Student(T a[],int n);    
        ~Student();   
        int Count();  
        int Locate(T x);    
        void Insert(int i,T x);    
        T Delete(int i);    
        void Print();    
    private:    
        Node<T>*first;    
};    
template<class T>  
Student<T>::Count()  
{  
    Node<T> *r=new Node<T>;  
    r=first->next;int count=0;  
    while(r!=NULL)  
    {  
        r=r->next;  
        count++;}  
    return count;  
}  
  
template<class T>    
Student<T>::Student()    
{    
    first=new Node<T>;    
    first->next=NULL;    
}    
template<class T>    
Student<T>::Student(T a[],int n)    
{    
    Node<T> *r,*s;    
    first=new Node<T>;    
    r=first;    
    for(int i=0;i<n;i++)    
    {    
        s=new Node<T>;    
        s->data=a[i];  
        s->prior=r;  
        r->next=s;r=s;    
    }    
    r->next=NULL;    
}    
template<class T>    
Student<T>::~Student()    
{    
    Node<T> *q=NULL;    
    while(first!=NULL)    
    {    
        q=first;    
        first=first->next;    
        delete q;    
    }    
}    
template<class T>    
void Student<T>::Insert(int i,T x)    
{    
    Node<T>*p=first,*s=NULL;    
    int count=0;    
    while(p!=NULL&&count<i-1)    
    {    
        p=p->next;    
        count++;    
    }    
    if(p==NULL)throw"位置";    
    else{    
        s=new Node<T>;s->data=x;  
        s->prior=p;  
        s->next=p->next;p->next->prior=s;  
        p->next=s;  
    }    
}    
template<class T>    
T Student<T>::Delete(int i)    
{    
    Node <T>*p=first,*q=NULL;    
    T x;    
    int count=0;    
    while(p!=NULL&&count<i-1)    
    {    
        p=p->next;    
        count++;    
    }    
    if(p==NULL||p->next==NULL)    
        throw"位置";    
    else    
    {    
        p->prior->next=p->next;  
        p->next->prior=p->prior;  
        delete q;    
        return x;    
    }    
}    
template<class T>    
int Student<T>::Locate(T x)    
{    
    Node<T> *p=first->next;    
    int count=1;    
    while(p!=NULL)    
    {    
        if(p->data==x)return count;    
        p=p->next;    
        count++;    
    }    
    return 0;    
}    
template<class T>    
void Student<T>::Print()    
{    
    Node<T>*p=first->next;    
    while(p!=NULL)    
    {    
        cout<<p->data<<" ";    
        p=p->next;    
    }    
    cout<<endl;    
}    
void main()    
{    
    float r[5]={91,92,93,94,95};    
    Student<float>L(r,5);    
    cout<<"一共有"<<L.Count()<<"学生成绩"<<endl;   
    L.Print();    
    cout<<"在第3位插入一个分数为100的学生"<<endl;    
    L.Insert(4,100);    
    cout<<"插入后一共有"<<L.Count()<<"学生成绩"<<endl;    
    L.Print();    
    cout<<"分数为93的学生位置为";    
    cout<<L.Locate(93)<<endl;    
    cout<<"执行删除第二个学生分数的操作,删除前数据为:"<<endl;    
    L.Print();    
    L.Delete(2);    
    cout<<"删除后数据为:"<<endl;    
    L.Print();  
} 

我们可以使用双向链表来创建一个学生成绩数据库。每个节点代表一个学生,包含学生的姓名、学号、课程成绩等信息。每个节点还包含两个指针,分别指向前一个节点和后一个节点。 以下是一个示例代码: ```python class Node: def __init__(self, name, id, score): self.name = name self.id = id self.score = score self.prev = None self.next = None class ScoreDB: def __init__(self): self.head = None self.tail = None def add(self, name, id, score): node = Node(name, id, score) if self.head is None: self.head = node self.tail = node else: node.prev = self.tail self.tail.next = node self.tail = node def remove(self, id): node = self.find(id) if node is None: return if node.prev is None: self.head = node.next else: node.prev.next = node.next if node.next is None: self.tail = node.prev else: node.next.prev = node.prev def find(self, id): node = self.head while node is not None and node.id != id: node = node.next return node def print_all(self): node = self.head while node is not None: print(node.name, node.id, node.score) node = node.next ``` 我们可以使用add方法来添加学生信息,使用remove方法来删除学生信息,使用find方法来查找学生信息,使用print_all方法来打印所有学生信息。 例如: ```python db = ScoreDB() db.add("Alice", 1, 90) db.add("Bob", 2, 80) db.add("Charlie", 3, 70) db.print_all() # Output: # Alice 1 90 # Bob 2 80 # Charlie 3 70 db.remove(2) db.print_all() # Output: # Alice 1 90 # Charlie 3 70 node = db.find(1) print(node.name, node.id, node.score) # Output: # Alice 1 90 ``` 这个例子中,我们首先创建一个ScoreDB对象,并使用add方法添加三个学生信息。然后使用print_all方法打印所有学生信息。接着使用remove方法删除学号为2的学生信息,再次使用print_all方法打印所有学生信息。最后使用find方法查找学号为1的学生信息,并打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值