实验二 双链表实现学生成绩

本文详细介绍了使用结构体实现双链表的过程,包括构造函数、析构函数等基本操作,以及如何进行查找、插入、删除等高级操作。通过具体实例展示了双链表的基本特性和应用场景。

算法

结构体:用结构体类型来描述双链表的结点,定义了一个数据域和prior、 next两个指针域,分别存放该结点的前驱结点和后继结点的地址。

无参构造函数:生成头结点,头结点也是尾结点,生成含头结点的空链表。

有参构造函数:生成有N个结点的双链表。为每个数组元素建立一个结点,每次将新申请的结点插在头结点的后面,双链表建立完毕,将终端结点的指针指向头结点,头结点指向终端结点

析构函数:释放结点的存储空间

求长度:设置一个工作指针p和累加器count,当p!=NULL时,指针后移,count加一。

按位查找:设置一个工作指针p和累加器count,当 p!=NULL&&count<i 时,指针后移,count加一。如果p为空值,抛出“位置异常”,否则,返回p的数据域。

按值查找:设置一个工作指针p和累加器count,当p!=NULL时,如果p的数据域为所输入的值x,返回count,指针后移,count加一。

插入:设置一个工作指针p和累加器count,当 p!=NULL&&count<i 时,指针后移,count加一。如果p为空值,抛出“位置异常”,否则,申请一个结点s,其数据域为x,将结点s的前驱指针域指向结点p,后继指针域指向p->next。

删除:设置一个工作指针p和累加器count,当 p!=NULL&&count<i 时,指针后移,count加一。如果p为空值或p->next为空,抛出“位置异常”,否则,(p->prior)->next=p->next;
(p->next)->prior=p->prior;删除p,返回x的值。

遍历:设置一个工作指针p,当p!=first时,输出p的数据域,指针后移,依次输出。


源代码

#include
using namespace std;
template
struct Node
{
	T data;
	Node *prior,*next;
};
template
class Score
{
	public:
			Score();
			Score(T a[],int n);
			~Score();
			int Length();
			T Get(int i);
			int Locate(T x);
			void Insert(int i,T x);
			T Delete(int i);
			void PrintList();
	private:
		Node *first;
};

template
Score::Score()
{
	first=new Node;
	first->next=first;
	first->prior=first;
}

template
Score::Score(T a[],int n)
{
	Node *r,*s;
	first=new Node;
	r=first;
	for(int i=0;i;
		s->data=a[i];
		s->prior=r;
		r->next=s;
		r=r->next;
	}
	r->next=first;
	first->prior=r;
}

template
Score::~Score()
{
	Node *q;
	first=new Node;
	while(first!=NULL)
	{
		q=first;
		first=first->next;
		delete q;
	}
}

template
int Score::Length()
{
	Node *p;int count=0;
	p=first->next;
	while(p!=first)
	{
		p=p->next;
		count++;
	}
	return count;
}

template
T Score::Get(int i)
{
	Node *p;int count=1;
	p=first->next;
	while(p!=NULL && countnext;
		count++;
	}
	if(p==NULL)throw"location";
	else return p->data;
}

template
int Score::Locate(T x)
{
		Node *p;int count=1;
		p=first->next;
		while(p!=NULL)
		{
			if(p->data==x)return count;
			p=p->next;
			count++;
		}
		return 0;
}

template
void Score::Insert(int i,T x)
{
	Node *p,*s;int count=0;
	p=first;
	while(p!=NULL && countnext;
		count++;
	}
	if(p==NULL)throw"location";
	else{
		s=new Node;s->data=x;
		s->prior=p;
		s->next=p->next;
		s->next->prior=s;
		p->next=s;
	}
}
		
template
T Score::Delete(int i)
{
	Node *p;int count=0;
	p=first;
	T x;
	while(p!=NULL && countnext;
		count++;
	}
	if(p==NULL || p->next==NULL)
		throw"location";
	else{
		(p->prior)->next=p->next;
		(p->next)->prior=p->prior;
		delete p;
		return x;

	}
}

template
void Score::PrintList()
{
	Node *p;
	p=first->next;
	while(p!=first)
	{
		cout<data<<" ";
		p=p->next;
	}
}

int main()
{

		cout<<"\t ******************学生成绩双链表的实现**************\n";
		cout<<"\t ****************************************************\n";
		cout<<"\t *------------------------------------------*********\n";
		cout<<"\t *****************[1]——输出表长********************\n";
		cout<<"\t *****************[2]——按位查找********************\n";
		cout<<"\t *****************[3]——按值查找********************\n";
		cout<<"\t *****************[4]——插入************************\n";
		cout<<"\t *****************[5]——删除************************\n";
		cout<<"\t *****************[6]——遍历************************\n";
		cout<<"\t *****************[7]——输出主菜单******************\n";
		cout<<"\t *****************[8]——退出************************\n";
		cout<<"\t *------------------------------------------*********\n";
		cout<<"\t ****************************************************\n";
	
		int a[]={34,45,56,67,78,83,89,90,95};
		Scores(a,9);
		int flag,i,x,t,l;
		flag=0;	
		while(flag==0)
		{
			cout<<"please input the command(1~8):"<>t;
			switch(t)
			{
			case 1:
				l=s.Length();				
				cout<<"the length is:"<>i;
				x=s.Get(i);		
				cout<<"the number is:"<>x;
				i=s.Locate(x);
				cout<<"the location is:"<>i;
				cout<<"the insert number is:";
				cin>>x;
				s.Insert(i,x);
				cout<<"insert successfully!"<>i;
				s.Delete(i);
				cout<<"delete successfully!"<

运行结果

运行程序,出现主界面


输入1,输出双链表的长度


输入2,查询第七个位置的成绩


输入3,查询成绩83所在的位置


输入6,输出双链表里的所有成绩


输入4,在第二个位置插入成绩98


输入5,删除第五个位置的成绩


输入7,输出主界面


输入8,退出程序


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值