实验二单链表的实现


《数据结构》实验二:

                线性表综合实验

一.实验目的

     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题

 

.实验内容

1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现。

2)用单链表来实现。

3)用双链表实现。

扫描二维码关注公众号,回复: 5566822 查看本文章

4)用静态链表实现。

5)用间接寻址实现。

分开写程序,可以一个方法分别写一博客文章上交作业。

 


四.实验报告

1.在博客中先写上实习目的和内容,画出主要操作运算算法图,然后分别上传程序代码。插入调试关键结果截图。

 

2.单独写一个博文,比较总结线性表的几种主要存储结果。

单链表的实现

源代码:

#include
using namespace std;
const int M=100;

struct Node
{
		int data;
		Node *next;
};
                  
class LinkList             //定义类SeqList 
{
	public:
		LinkList();
		LinkList(int a[],int n);
		~LinkList();
		int Length();               
		int Get(int i);             
		void Insert(int i,int x);   
		int Delete(int i);           
		void PrintList();          
	private:
		Node *first;	
};

LinkList::LinkList()
{   
    first=new Node;
	first->next=NULL;	
}

LinkList::LinkList(int a[],int n)
{   
    Node *r;
    Node *s;
    int i;
    first=new Node;
    r=first;
	for(i=0;idata=a[i];
		r->next=s;r=s;
	}
	r->next=NULL;	 
}

LinkList::~LinkList()
{
	while(first!=NULL)
	{   
	    Node *q;
		q=first;
		first=first->next;
		delete q;
	}
}
		
int LinkList::Length()  //表长 
{
	Node *p;
	int count;
	p=first->next; count=0;
	while(p!=NULL)
	{
		p=p->next;
		count++;
	}
	return count;
}
	
int LinkList::Get(int i) //按位查找 
{
	Node *p;
	int count;
	p=first->next;count=1;
	while(p!=NULL&&countnext;
		count++;
	}
	if(p==NULL) throw"位置";
	else return p->data;	 
}
	

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

int LinkList::Delete(int i)  //删除 
{
	Node *p;
	Node *q;
	Node *x;
	int count;
	p=first->next;count=0;
	while(p!=NULL&&countnext;
		count++;
	}
	if(p==NULL||p->next==NULL)
	throw"位置"; 
	else{
		q=p->next;x=q->next;
		p->next=q->next;
		delete q;
		return x->data;
	} 
}
		
void LinkList::PrintList()  //遍历操作 
{
	Node *p;
	int i=0;	
	p=first->next;
	while(p!=NULL)
	{   
	    i=i+1; 
		cout<<"\t\t\t\t\t第"<data;
		cout<next;
	}
}
		
int main()
{   
    int x,n,loc,len;
    cout<<"\t\t\t\t\t录入学生成绩信息"<>n;
	for(j=0;j>d[j];
	}  	
    LinkList List(d,n);		
    while(1)
	{
	cout<>x;
	cout<>loc;
				n=List.Get(loc);
				cout<<"\t\t\t\t\t该学生的成绩为:"<>loc;
				cout<<"\t\t\t\t\t输入学生成绩:";
				cin>>n;
				List.Insert(loc,n);
				cout<<"\t\t\t\t\t插入成功!"<>loc;
				cout<<"\t\t\t\t\t已删除的学生成绩:"<

执行结果截图:
1)录入学生成绩,输出学生人数:

2)输出学生成绩:

3)插入操作:

4)插入成功后输出学生成绩:

5)按位查找:

6)删除操作:

7)删除成功后输出学生成绩:


猜你喜欢

转载自blog.csdn.net/c77777_/article/details/78197165