线性表的实验

建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现。

#include<iostream>
using namespace std;
const int Maxsize = 30;
class SeqList
{


public:
SeqList(){ length = 0; }
SeqList(int a[], int n);
~SeqList(){}
void Insert(int i, int x);
int Delete(int i);
int Locate(int x);
void PrintList();
private:
int data[Maxsize];
int length;
};


SeqList::SeqList(int a[], int n)
{
if (n > Maxsize) throw "参数非法";
for(int i=0;i<n;i++)
data[i] = a[i];
length = n;
}
void SeqList::Insert(int i, int x)
{
if (length >= Maxsize) throw "上溢";
if (i<1 || i>length + 1) throw"位置非法";
for (int j = length; j >= i; j--)
data[j] = data[j - 1];
data[i - 1] = x;
length++;
}
int SeqList::Delete(int i)
{
if (length == 0) throw"下溢";
if (i<1 || i>length) throw "位置非法";
int x = data[i - 1];
for (int j = i; j < length; j++)
data[j - 1] = data[j];
length--;
return x;
}
int SeqList::Locate(int x)
{
for (int i = 0; i<length; i++)
if (data[i] == x) return i + 1;
return 0;
}
void SeqList::PrintList()
{
for (int i = 0; i < length; i++)
cout << data[i] << " ";
cout << endl;
}


void main(){
int r[5] = {50, 60, 70, 80, 90};
SeqList L(r, 5);
cout << "执行插入前的数据为:" << endl;
L.PrintList();
try
{
L.Insert(2, 100);
}
catch (char *s)
{
cout << s << endl;
}
cout << "执行插入操作后的数据为:" << endl;
L.PrintList();
cout << "值为100的元素的位置为:";
cout << L.Locate(100) << endl;
cout << "执行删除第一个元素操作,删除前的数据为:" << endl;
L.PrintList();
try
{
L.Delete(1);
}
catch (char *s)
{
cout << s << endl;
}
cout << "删除后的数据为:" << endl;
L.PrintList();


}

2)用单链表来实现。

#include<iostream>       
using namespace std;  
  
template<class T>  
struct Node     
{   
    T data;
Node<T>* next;  
};    
  
   
template<class T>   
class LinkList   
{  
public:    
    LinkList (); 
LinkList(T a[],int n);    
    ~LinkList(); 
void Insert (int i,T x);    
int Locate(T x) ;  
T Delete(int i);    
void PrintList();    
private:
Node<T>* first;
  
};    
  
   
template<class T>  
LinkList<T>::LinkList()  
{   
first = new Node<T>;
first->next=NULL;
}   


template<class T>
LinkList<T>::LinkList(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];
r->next =s;
r=s;
}
r->next= NULL;
}


template<class T>
LinkList<T>::~LinkList()
{
Node<T> *q = NULL;
while(first != NULL)
{
q = first;
first= first->next;
delete q;
}
}
template<class T>
void LinkList<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->next=p->next;
p->next= s;
}
}


template<class T>
T LinkList<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
{
q=p->next;
x=q->data;
p->next=q->next;
return x;
}
}


template<class 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 0;
}


template<class T>
void LinkList<T>::PrintList()
{
Node<T>*p =first-> next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}


void main(){
int r[5] = {50, 60, 70, 80, 90};
LinkList<int> L(r, 5);
cout << "执行插入前的数据为:" << endl;
L.PrintList();
try
{
L.Insert(2, 100);
}
catch (char *s)
{
cout << s << endl;
}
cout << "执行插入操作后的数据为:" << endl;
L.PrintList();
cout << "值为100的元素的位置为:";
cout << L.Locate(100) << endl;
cout << "执行删除第一个元素操作,删除前的数据为:" << endl;
L.PrintList();
try
{
L.Delete(1);
}
catch (char *s)
{
cout << s << endl;
}
cout << "删除后的数据为:" << endl;
L.PrintList();


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一些关于数据结构线性实验停车场方面的帮助。在停车场管理系统中,可以使用线性示停车场的车位情况,每个车位可以用一个节点来示。下面是一个简单的示例: ```C++ struct car_node { char car_number[10]; int park_time; }; struct park_node { car_node car; park_node *next; }; class Park { private: int capacity; park_node *head; public: Park(int n) { capacity = n; head = new park_node; head->next = NULL; } bool is_full() { return (size() == capacity); } bool is_empty() { return (head->next == NULL); } int size() { int count = 0; park_node *p = head->next; while (p != NULL) { count++; p = p->next; } return count; } bool park(car_node car) { if (is_full()) { return false; } park_node *p = new park_node; p->car = car; p->next = head->next; head->next = p; return true; } bool leave(char *car_number, int &park_time) { park_node *p = head->next, *pre = head; while (p != NULL && strcmp(p->car.car_number, car_number) != 0) { pre = p; p = p->next; } if (p == NULL) { return false; } pre->next = p->next; delete p; park_time = time(NULL) - p->car.park_time; return true; } }; ``` 在上面的代码中,`Park` 类示一个停车场,包括车位数量 `capacity` 和车辆信息链的头节点 `head`。该类提供了以下几个方法: - `is_full()`:判断停车场是否已满 - `is_empty()`:判断停车场是否为空 - `size()`:获取停车场中当前车辆数量 - `park(car_node car)`:停车,将一辆车停放到停车场中 - `leave(char *car_number, int &park_time)`:离开,将一辆车从停车场中取出,并返回停车时长 以上是一个简单的停车场管理系统示例,希望能对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值