实验二线性表的综合实验5

该实验旨在巩固线性表数据结构的存储和操作,包括顺序表和单链表。实验者需创建包含n个学生成绩的线性表,执行插入、删除和查找操作,并展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.实验目的

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

二..实验内容

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

要求如下:

2)用单链表来实现。

三、源代码

#include<iostream>
using namespace std;
template< typename T>
class Node {
public:
    T data;   
    Node *next;  
};
const int MaxSize = 100;   



template<typename T>
class LinkList
{
public:
    LinkList(T a[], int n);  
    ~LinkList();      
    int Length();     
    T Get(int i);        
    int Locate(T x);     
    bool Insert(int i, T x); 
    bool Delete(int i);     
    bool InsertHead(T x);
    bool InsertTail(T x);
    void ListTraverse(); 
    bool changeList(int i, T x); 
private:
    Node<T> *first;
    int  m_Length;     
    Node<T> *address[MaxSize];   

};
template<typename T>
LinkList<T>::LinkList(T a[], int n)  {
 
    if (n > MaxSize) {               
        throw string("数组长度长于链长度,录入失败");
        exit(1);
    }
    first = new Node<T>;      
    first->next = NULL;
    for (int i = 0; i<n; i++)      
    {
        Node<T> *s = new Node<T>;
        s->data = a[i];
        s->next = first->next;
        first->next = s;
    }
    m_Length = n;
    Node<T> *p =first; 
    for (int j = 0; j < m_Length;j++) {   
        p = p->next;
        address[j] = p;
    }
    for (int i = 0; i < n; i++)    
    {
    InsertHead(a[i]);
    }


}
template<typename T>
LinkList::LinkList(T a[], int n)         
{
 Node<T> *first = new NOde<T>;
  Node<T> *r = first;    
  for (int i = 0; i < n; i++)
  {
      s = new Node;   
      s->data = a[i];   
      r->next = s;  
      r = s; 
  }
  r->next = NULL; 
}

template<typename T>
bool LinkList<T>::InsertHead(T x)   
{
    if (m_Length>=MaxSize) {
        throw string("链满,无法插入数据");
    }
    Node<T> *Temp = first->next;       
    Node<T> *s = new Node<T>;
    if (s == NULL)
    {
        return false;
    }
    s->data = x;   
    s->next = Temp;   
    first->next = s;
    m_Length++;    
    for (int i = m_Length-1; i > 0;i--) {    
        address[i] = address[i - 1];
    }
    address[0] = first->next;
    return true;    
}

template<typename T>
bool LinkList<T>::InsertTail(T x)   
{
    if (m_Length >= MaxSize) {
        throw string("链满,无法插入数据");
    }
    Node<T> *p = first;        
    Node<T> *s = new Node<T>;  
    if (s == NULL)     
    {
        return false;
    }
    s->data = x;
    while (p->next != NULL)       
    {
        p = p->next;
    }
    p->next = s;       
    s->next = NULL;    
    m_Length++;
    address[m_Length - 1] = s; 
    return true;  
}

template<typename T>
LinkList<T>::~LinkList()      
{
    while (first != NULL)
    {
        Node<T> *q = first;    
        first = first->next;
        delete q;      
    }
    m_Length = 0;
}
template<typename T>
int LinkList<T>::Length()         
{
    int num=0;
    Node<T> *p = first->next;
    while (p!= NULL)
    {
    p = p->next;
    num++;
    }
    return m_Length;   
                     
}
template<typename T>
T LinkList<T>::Get(int i)  
{
    if (i > m_Length) {
        throw string("位置大于当前链长度");    
    }
    else if(i<=0){
        throw string("位置小于等于0,无效!");
    }
    Node<T> *p = address[i - 1];
    return p->data;
    Node<T> *p = first->next;
    int count = 1;
    while (p != NULL&&count < i)
    {
        p = p->next;
        count++;
    }
    if (p == NULL)
    {
        throw"位置";
    }
    else
    {
        return p->data;
    }
}
template<typename T>
int LinkList<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 -1;    
}
template<typename T>
bool LinkList<T>::Insert(int i, T x)     
{
    if (i>m_Length) {
        throw string("位置大于当前链表长度!");
    }
    else if (i<=0) {
        throw string("位置小于等于0,无效!");
    }
    else if (m_Length>=MaxSize) {
        throw string("链满");
    }
    Node<T> *p = first;
    int count = 0;
    int num = i - 1;
    while (p != NULL&&count <num)
    {
        p = p->next;
        count++;
    }
    if (p == NULL)
    {
        return false;
    }
    else
    {
        Node<T> *s = new Node<T>;
        s->data = x;
        s->next = p->next;
        p->next = s;
        m_Length++;
        for (int j = m_Length-1; j >= i; j--) {    
            address[j] = address[j - 1];
        }
        address[i] = s;      
        return true;
    }
}
template<typename T>
void LinkList<T>::ListTraverse()
{
    Node<T> *p = first->next;
    while (p != NULL)
    {
        cout << p->data << ",";
        p = p->next;           
    }
}
template<typename T>
bool LinkList<T>::Delete(int i)
{
    if (i>m_Length||i<=0) {   
        throw string("位置无效!");
    }
    Node<T> *p = first;
    int count = 0;
    while (p != NULL&&count < i - 1)
    {
        p = p->next;
        count++;
    }
    if (p == NULL)
    {
        return false;
    }
    else
    {
        Node<T> *q;
        q = p->next;
        p->next = q->next;
        delete q;
        m_Length--;
        for (int j = i; j <= m_Length; j++) {   
            address[j-1] = address[j];
        }
        return true;
    }
}

template<typename T>
bool LinkList<T>::changeList(int i,T x) {
    if (i>m_Length||i<=0) {
        throw string("位置有误");    
    }
    Node<T> *p = address[i - 1];
    p->data = x;
    return true;
}
int main() {
    string a[5] = { "plus","plus","c","world","hello" };
    try
    {
        LinkList<string> MyList(a, 5);
        string L = MyList.Get(1);
        cout << "第一个节点位置的元素为:" << L << endl;
        cout <<"当前链长度为:"<< MyList.Length() << endl;
        int Lo = MyList.Locate("hello");
        if (Lo == -1) {
            cout << "未找到" << endl;
        }
        else {
            cout << "元素hello所在的位置为:" << MyList.Locate("hello") << endl;
        }
        MyList.ListTraverse();
        cout << endl;
        cout << "尾插法插入元素(插在最后):";
        MyList.InsertTail("where");
        MyList.ListTraverse();
        cout << endl;
        cout << "头插法插入元素,插在第一个:";
        MyList.InsertHead("areyou");
        MyList.ListTraverse();
        cout << endl;
        cout << "插入元素到指定位置:";
        MyList.Insert(3, "thereismiddle");
        MyList.ListTraverse();
        cout << endl;
        cout << "删除指定位置的元素:";
        MyList.Delete(3);
        MyList.ListTraverse();
        cout << endl;
        cout << "改变指定位置元素的值:";
        MyList.changeList(1, "ppp");      
        MyList.ListTraverse();
    }
    catch (string& aval)
    {
        cout << aval << endl;
    }
    return 0;
}
四、实验结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值