数据结构二之双链表(找不出异常原因抓狂)


.实验内容

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;
}


唉!!!!我想静静 委屈
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值