建立一个由 n 个学生成绩的顺序表

本文通过顺序表、单链表、双链表、静态链表及间接寻址方式实现了基本的数据结构操作,包括插入、删除、定位等,并提供了完整的C++代码示例。

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

1、顺序表实现

#include<iostream.h>
#include<string.h>
const int max = 10;
class Student
{
private:
	int length;
	int data[max];
	char Na[max][10];
public:
	Student(){length = 0;}
	Student(int a[max],int n,char na[max][10]);
	~Student(){}
	void Insert(int i, int x,char name[10]);
	int Delete(int i);
	void Locate(int i);
	void PrintList();
};
Student::Student(int a[max],int n,char na[max][10])
{
	if(n>max)throw"MAX";
	for(int i=0;i<n;i++)
	{
		data[i]=a[i];
		strcpy(Na[i],na[i]);
	}
		length=n;
}

void Student::Insert(int i,int x,char name[])
{
	if(length>=max)throw"上溢";
	if(i<1||i>length+1)throw"位置非法";
	for(int j=length;j>=i;j--)
	{
		data[j]=data[j-1];
		strcpy(Na[j],Na[j-1]);
	}
	data[i-1]=x;
	strcpy(Na[i-1],name);
	length++;
}

int Student::Delete(int i)
{
	if(length=0)throw"下溢";
	if(i<1||i>length)throw"位置非法";
	int x=data[i-1];
	char *n;
	strcpy(n,Na[i-1]);
	for(int j=i; j<length;j++)
	{
		data[j-1]=data[j];
		strcpy(Na[j-1],Na[j]);
	}
	length--;
	return x;
}

void Student::Locate(int i)
{
	cout<<Na[i]<<","<<data[i]<<endl;
		cout<<endl;
}

void Student::PrintList()
{
	for(int i = 0; i< length; i++)
		cout<<Na[i]<<","<<data[i]<<endl;
}

void main()
{
	int r[3]={90,80,70};
	char na[3][10]={"a","b","c"};
	Student L(r,3,na);
	cout<<"插入前:"<<endl;
	L.PrintList();
	try
	{
		L.Insert(4,85,"d");
	}
	catch(char *s)
	{
		cout<<s<<endl;
	}
	cout<<"插入后:"<<endl;
	L.PrintList();
	cout<<"删除第三位同学的信息"<<endl;
	L.Delete(3);
	L.PrintList();
	cout<<"查找第三位学生的成绩:"<<endl;
	L.Locate(3);
}

2、单链表实现

#include<iostream.h>
#include<string>
typedef struct student
{
	int id;
	float score;
	char name[10];
	student *next;
}stu;


stu *head=NULL;


void record();
void insert();
void delt();
void locate();
void print();






int main()
{
	int c,i=1;
	cout<<"1、输入学生成绩     2、插入    3、删除     4、查找    5、显示所有    0、退出"<<endl;
	cin>>c;
	do
	{
		switch(c)
		{
		case 0: i=0; break;
		case 1: record(); break;
		case 2: insert(); break;
		case 3: delt(); break;
		case 4: locate(); break;
		case 5: print(); break;
		default: cout<<"输入有误!"<<endl; break;
		}
		cout<<"输入选择(0-7): ";  
        cin>>c;
	}while(c!=0);
	return 0;
}




 void record()
 {
	 stu *p,*q;
	 int i,n;
	 cout<<"输入学生人数:";
	 cin>>n;
	 i=0;
	 cout<<"输入学生信息:姓名   学号   成绩"<<endl;
	 while(i<n)
	 {
		 p=new stu;
		 if(head==NULL)
		 {
			 head=p;
			 q=head;
		 }
		 else
		 {
			 cin>>p->name>>p->id>>p->score;
			 q->next=p;
			 q=p;
			 i++;
		 }
	 }
 }


void insert()
{
	stu *p=head, *s=NULL;
	p=new stu;
	int x,count=0;
	cout<<"插入的位置为:";
	cin>>x;
	while(p!=NULL && count<x-1)
	{ 
        p=p->next;  
		count++;
	}
	if(p==NULL) cout<<"位置错误";
	else
	{
		cout<<"请输入插入学生的信息:姓名   学号   成绩"<<endl;
		s=new stu;
		cin>>p->name>>p->id>>p->score;
		s->next=p->next;
		p->next=s;
	}
}


void delt()
{
	stu *p,*q;
	int id,i=1;
    cout<<"输入要删除学生的学号: ";  
    cin>>id;
    q=head;  
    p=q->next;  
    while(i)  
    {  
        if(p->id==id)  
        {  
            q->next=p->next;  
            delete p;
			i=0;
            break;  
        }  
        q=p;  
        p=q->next;  
    }  
} 


void locate()
{stu *p,*q;
	int id,i=1;
    cout<<"输入要查找学生的学号: ";  
    cin>>id;
    q=head;  
    p=q->next;  
    while(i)  
    {  
        if(p->id==id)  
        {
			cout<<p->name<<","<<p->score<<endl;
			i=0;
		}
		q=p;  
        p=q->next;  
    }  
}  


void print()  
{
	if(head==NULL) return;
    stu *p;
    p=head->next;  
    while(p)  
    {  
        cout<<p->name<<","<<p->score<<endl;  
        p=p->next;  
    }return;} 

3、双链表实现

#include<iostream.h>
struct Node
{
	int data;
	Node *prior,*next;
};
class Score
{
public:
	Score();
	Score(int a[],int n);
	~Score();
	void Insert(int i,int x);
	int Locate(int x);
	int Delete(int i);
	void PrintList();
private:
	Node *first;
};

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

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

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

void Score::Insert(int i,int x)
{
	Node *p,*s;
	int count=0;
	p=first;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL)throw"位置";
	else
	{
		s=new Node;
		s->data=x;
		s->prior=p;
		s->next=p->next;
		p->next->prior=s;
		p->next=s;
	}
}

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

int Score::Delete(int i)
{
	Node *p=first;
	int count=0;
	while(p!=NULL&&count<i)
	{
		p=p->next;
		count++;
	}
	if(p==NULL) throw"位置";
	else
	{
		(p->prior)->next=p->next;
		(p->next)->prior=p->prior;
		delete p;
	}
	return 0;
}

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

int main()
{
	int a[]={60,70,80,90,100};
	Score L(a,5);
	cout<<"成绩表数据为:"<<endl;
	L.PrintList();
	cout<<"在第三个位置插入成绩"<<endl;
    try    
    {    
        L.Insert(3,78);    
    }    
    catch(char *s)    
    {    
        cout<<s<<endl;    
    }
	    cout<<"插入后数成绩表为:"<<endl;    
    L.PrintList();    
    cout<<"删除前成绩表为:"<<endl;    
    L.PrintList();    
    cout<<"删除第二个成绩"<<endl;    
    try    
    {    
        L.Delete(2);    
    }    
    catch(char *s)    
    {    
        cout<<s<<endl;    
    }    
    cout<<"删除后成绩表为:"<<endl;    
    L.PrintList();
	return 0;
}
4、静态链表
#include<iostream.h>
const int MaxSize=100;
struct Node
{
	int data;
	int next;
}SList[MaxSize];

class StaticList
{
public:
	StaticList();
	StaticList(int a[],int n);
	~StaticList(){};
	int Get(int i);
	int Locate(int x);
	bool Insert(int i,int x);
	bool Delete(int i);
	void PrintList();
private:
	int first;
	int avail;
	int length;
	Node SList[MaxSize];
};

StaticList::StaticList(int a[],int n)
{
	for (int i = 0;i < MaxSize; i++)
	{
		SList[i].next=i+1;
	}
	length = 0;
	SList[MaxSize-1].next=-1;
	avail=2;
	first=1;
	SList[first].next=-1;
	for(int j = n-1; j>=0; j--)
	{
		if(avail==-1)
		{
			break;
		}
		int s = avail;
		avail = SList[avail].next;
		SList[s].data = a[j];
		SList[s].next=SList[first].next;
		SList[first].next=s;
		length++;
	}
}

int StaticList::Get(int i)
{
	if(i<=0||i>length)throw "location error";
	int s = first;
	for(int j=0;j<i;j++)
	{
		s=SList[s].next;
	}
	return SList[s].data;
}

bool StaticList::Insert(int i,int x)
{
	if(SList[avail].next==-1)return false;
	int s=first;
	int temp = avail;
	SList[temp].data=x;
	avail = SList[avail].next;
	int count = 0;
	while(count<i-1&&count<length)
	{
		s=SList[s].next;
		count++;
	}
	SList[temp].next=SList[s].next;
	SList[s].next=temp;
	length++;
	return true;
}

bool StaticList::Delete(int i)
{
	if(i<=0||i>length) return false;
	int count = 0;
	int s = first;
	while(count<i-1&&count<length)
	{
		s=SList[s].next;
		count++;
	}
	int q=SList[s].next;
	SList[s].next = SList[q].next;
	SList[q].next=avail;
	avail=q;
	length--;
	return true;
}

int StaticList::Locate(int x)
{
	int count = 0;
	int s = first;
	while(SList[s].next!=-1)
	{
		s=SList[s].next;
		if(SList[s].data==x) return count+1;
		count++;
	}
	return -1;
}

void StaticList::PrintList()
{
	int s=SList[first].next;
	for(int i=1;i<=length;i++)
	{
		cout<<SList[s].data<<",";
		s=SList[s].next;
	}
}

void main()
{
	int score[5]={60,70,80,90,100};
	StaticList L(score,5);
	cout<<"score:"<<endl;
	L.PrintList();
	cout<<"Insert location 4 score 85"<<endl;
	L.Insert(4,85);
	L.PrintList();
	cout<<"Delete location 4"<<endl;
	L.Delete(4);
	L.PrintList();
	cout<<endl<<endl<<"Location 1 score:"<<L.Get(1)<<endl;
	cout<<"score 80 location:"<<L.Locate(80)<<endl;
}

5、间接寻址

#include<iostream.h>
const int Max=100;
struct Node
{
	int data;
};

class Link
{
public:
	Link();
	Link(int a[],int n);
	int Get(int i);
	void Insert(int i,int x);
	int Delete(int i);
	void Print();
private:
	Node *first[Max];
	int length;
};

Link::Link()
{
	for(int i=0;i<100;i++);
	first[i]=NULL;
	length=0;
}

Link::Link(int a[],int n)
{
	for(int i=0;i<100;i++)
	{
		first[i]=new Node;
		first[i]->data=a[i];
	}
	length=n;
}

int Link::Get(int i)
{
	if(i<1||i>length)
		throw"location error";
	else return first[i-1]->data;
}

void Link::Insert(int i,int x)
{
	if(length>=Max)throw"Max";
	if(i<1||i>length+1)throw"location error";
	for(int j=length;j>=i;j--)
	first[j]->data=first[j-1]->data;  
    first[i-1]->data=x;  
    length++;   
} 

int Link::Delete(int i)
{   
    int x,j;  
    if(length==0)
		throw"下溢";  
    if(i<1||i>length)
		throw"location error";  
    x=first[i-1]->data;  
    for(j=i;j<length;j++)  
    first[j-1]->data=first[j]->data;  
    length--;  
    return x;  
}   

void Link::Print() 
{  
    int i;   
    for( i=0;i<length;i++)  
    {         
        cout<<endl<<"第"<<i+1<<"个学生成绩"<<first[i]->data<<" ";  
    }  
}
	
int main()
{
	int score[5]={60,70,80,90,100};
	Link L(score,5);
	cout<<"学生的成绩为:"<<endl;
	L.Print();
	cout<<endl<<endl<<"在位置4插入成绩85"<<endl;
	L.Insert(4,85);
	L.Print();
	cout<<endl<<endl<<"在位置1删除:"<<endl;
	L.Delete(1);
	L.Print();
	cout<<endl<<endl<<"位置3的成绩为:"<<endl;
	cout<<L.Get(3)<<endl;
	cout<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值