实验二双链表

一、实验目的

巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。


二、实验内容

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

源代码为:
#include<iostream>  
using namespace std;  
  
template <typename T>  
class  Node  
{  
public:  
    T data;  
    Node<T> *prior;  
    Node<T> *next;  
};  
  
template <typename T>  
class DLL {  
public:  
    DLL();  
    DLL(T score[], int n);  //有参构造函数  
    ~DLL();  //析构函数  
    int Length(); //返回单链表长度  
    void insert(int i, T x); //插入操作,在位置i插入元素  
    T get(int i);  //按位查找  
    int locate(T x);  //按值查找  
    T Delete(int i);  //删除操作  
    void print();  //遍历操作  
private:  
    Node<T> *first;  //双链表的头指针  
    int length;   //链的长度计数  
};  
  
template <typename T>  
DLL<T>::DLL(T score[], int n)  
{  
    length=0;  
    first = new Node<T>;  
    first->next = NULL;  
    first->prior = NULL;  
    for (int i = 0; i<n; i++)  
    {  
        Node<T> *s = new Node<T>;  
        s->data = score[i];  
        s->next = first->next;   
        first->next = s;  
    }  
}  
  
template <typename T>  
DLL<T>::~DLL()  
{  
    while (first->next!=first->prior)  
    {  
        //临时指针,存储即将释放的节点的指针  
        Node<T> *temp = first;  
        //脱链  
        first->prior->next = first->next;  
        first->next -> prior = first->prior;  
        //头指针后移  
        first = first->next;  
        //释放内存  
        delete temp;  
    }  
    delete first;  
}  
  
template<typename T>  
int DLL<T>::Length()  
{  
    Node<T> *p; int count;  
    p=first->next;    
    count=0;    
    while(p!=NULL)  
    {    
        p=p->next;    
        count++;    
     }    
    return length;  
}  
  
template <typename T>  
void DLL<T>::insert(int i,T x)  
{  
    Node<T>*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<T>;    
       s->data=x;    
       s->next=p->next;    
       p->next=s;    
     }  
}  
  
template <typename T>  
T DLL<T>::get(int i)  
{  
    Node<T> *p;int count; count = 1;  
    p = first->next;   
    while (p != NULL&&count<i)  
    {  
        p = p->next; count++;  
    }  
    if (p == NULL)throw"位置非法";  
    else return p->data;  
}  
  
template <typename T>  
int DLL<T>::locate(T x)  
{  
    Node<T> *p; int count;  
    p = first->next; count = 1;  
    while (p!= NULL)  
    {  
        if (p->data == x) return count;  
        p = p->next;  
        count++;  
    }  
    return 0;  
}  
  
template <typename T>  
T DLL<T>::Delete(int i)  
{  
    Node<T> *p,*q;  
    p = first->next; int count, x; count = 1;  
    while (p != NULL&&count<i-1)  
    {  
        p = p->next; count++;  
    }  
    if (p == NULL || p->next == NULL) throw"位置非法";  
    else  
    {    
        q = p->next;    
        x=q->data;    
        if (p->next != NULL)  
        {    
            if(q->next!=NULL)    
        q->next->prior = p;  
            else  
            {  
                p->next=NULL;  
                p->next = q->next;  
                delete q;  
                q = NULL;  
                return x;  
            }  
        }  
        p->next = q->next;  
        delete q;    
        q = NULL;    
        return x;  
    }  
}  
  
template <typename T>  
void DLL<T>::print()  
{  
    Node<T> *p;  
    p = first->next;  
    while (p->next != NULL)  
    {  
        cout << p->data << " ";  
        p = p->next;  
    }  
    cout << p->data << endl;  
}  
  
void main()  
{  
    float score[8] = { 69,85,95,74,55,64,100,87 };  
    DLL<float>student(score, 8);  
    cout << "     学生数据结构成绩" << endl;  
    student.print();  
    cout << endl << endl << "在位置5插入成绩68,插入后结果如下:" << endl;  
    student.insert(5,68);  
    student.print();  
    cout << endl << endl << "在位置2删除成绩为:" << student.Delete(2) <<" , "<< "删除后结果如下:" << endl;  
    student.print();  
    cout << endl << endl << "位置4的成绩为:" << student.get(4) << endl;  
    cout << endl << endl << "成绩74所在位置为:" << student.locate(74) << endl;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值