.实验内容
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果---------用双链表实现。
#include<iostream>
using namespace std;
struct DulNode
{
double data;
DulNode*prior, *next;
};
class stu
{
private:
DulNode*first,*r,*s,*q,*S;
int count;
public:
stu(double a[], int n);
~stu();
double insert(int i, double x);
double Get(int i);
int Locate(double x);
double Delete(int i);
};
stu::stu(double a[], int n)
{
first = new DulNode;
r = first;
int i;
for (i = 0; i < n; i++)
{
s = new DulNode; s->data = a[i];
r->next = s; r = s;
}
r->next = NULL;
for (i; i > 0; i--)
{
S = new DulNode; S->data = a[i - 1];
r->prior = S; r = S;
}
r->prior = NULL;
}
stu::~stu()
{
while (first != NULL)
{
q = first;
first = first->next;
delete q;
}
}
double stu::insert(int i, double x)
{
DulNode*p;
p = first; count = 0;
while (p != NULL&&count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL) throw"位置";
else
{
DulNode*s;
s = new DulNode; s->data = x;
s->prior=p ; s->next = p->next;
p->next->prior = s;
p->next = s;
}
return 0;
}
double stu::Get(int i)
{
DulNode*p;
p = first->next; count = 1;
while (p != NULL&&count < i)
{
p = p->next;
count++;
}
if (p == NULL) throw"位置";
else return p->data;
}
int stu::Locate(double x)
{
DulNode*p;
p = first->next; count = 1;
while (p != NULL)
{
if (p->data == x) return count;
p = p->next;
count++;
}
return 0;
}
double stu::Delete(int i)
{
DulNode*p;
p = first; count = 0;
while (p != NULL&&count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL || p->next == NULL) throw"位置";
else
{
double x = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
return x;
}
}
int main()
{
double a[10] = { 91,92,93,94,95,96 };
stu student(a, 6);
student.insert(2, 100);
student.Delete(6);
cout << student.Get(2) << endl;
cout << student.Locate(96) << endl;
}
唉!!!!我想静静
![委屈](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)