单链表

题目:  单链表相关算法的实验验证
[实验目的]
验证单链表及其上的基本操作;单链表的封装
[实验内容及要求]
1、定义单链表类。
2、实验验证如下算法的正确性、各种功能及指标:
1)创建单链表;
2)插入操作:分别在当前结点后、表头、表尾插入值为x 的结点;
3)删除操作:分别删除表头结点、表尾结点和当前结点的后继结点;
4)存取操作:分别存取当前结点的值和单链表中第k 个结点的值;
5)查找操作:查找值为x 的元素在单链表中出现的位置(是链表中的第几个元素)。
3、当前结点的引入是为了加速单链表上的操作,使用当前指针指向,至少实现以下功能:
1)初始化:哨位结点 ;
2)查找操作后:若找到,当前指针指向找到的结点,否则不动;
3)插入操作后:当前指针指向新插入的结点,便于连续插入;
4、为便于观察程序的运行结果,设计输出函以图形或表格或其它直观的形式展现、存储计算结果。
5、测试程序时,对所有输入变量取遍各种有代表性的值。
6、为了增强程序的可读性,程序中要有适当的注释。

//main.cpp

#include<iostream>
#include"SLList.h"
#include<windows.h> 
#include<conio.h>
using namespace std;

int main()
{
    system("color 4f");
	SLList list;
	int item=0,temp=0;
	
	cout<<"please input 12 data!"<<endl;
	for(int i=0;i<12;i++)
	{
	    cin>>temp;
	    list.Insert(temp);
	}
	cout<<endl;
	system("cls");
	



	cout<<"所存储的单链表表是:"<<"\n\n\n";
	list.Print(); 
	
	cout<<"存取当前节点的值:"<<"\n\n\n";
	list.Read(item);
	cout<<"当前节点的值是:"<<item<<"\n\n\n";
	
	cout<<"存取第3个节点的值:"<<"\n\n\n";
	list.Read(3,item);
	cout<<"第三个节点的值是:"<<item<<"\n\n\n";
	
	cout<<"按任意键继续.........";
	getch();
	system("cls");
	



	cout<<"插入前的线性表是:"<<"\n\n\n";
	list.Print(); 
	
	cout<<"在当前节点后面插入一个值为8的节点:"<<"\n\n";
	item=8;
	list.Insert(item);
	cout<<"插入后的线性表是:"<<"\n\n\n";
    list.Print();
    
    cout<<"在表头节点插入一个值为7的节点:"<<"\n\n";
	item=7;
	list.InsertFromHead(item);
	cout<<"插入后的线性表是:"<<"\n\n\n";
	list.Print();
	
	cout<<"在表尾节点插入一个值为6的节点:"<<"\n\n";
	item=6;
	list.InsertFromTail(item);
	cout<<"插入后的线性表是:"<<"\n\n\n";
	list.Print();
	
	cout<<"按任意键继续.........";
	getch();
	system("cls");
	


	cout<<"所存储的线性表是:"<<"\n\n\n";
	list.Print();
	
    cout<<"查找值为8的元素的下标:"<<"\n\n\n";
	int array[30]={0};
    item=8;
    list.Search(item,array); 
    cout<<"其下标分别为 :";
    for(int i=0;i<30;i++)
       if(array[i]!=0)
          cout<<" ["<<array[i]<<"]  ";
    cout<<"\n\n\n\n";
    cout<<"按任意键继续.........";
	getch();
	system("cls");
	


	cout<<"删除前的线性表是:"<<"\n\n\n";
	list.Print();
	
	cout<<"删除当前节点的后继结点:"<<"\n\n";
	list.Delete(item);
	cout<<"当前节点的值是:"<<item<<"\n\n";
	cout<<"删除后的线性表是:"<<"\n\n\n";
    list.Print();
    
    cout<<"删除表头节点:"<<"\n\n";
	list.DeleteFromHead(item);
	cout<<"表头节点的值是:"<<item<<"\n\n";
	cout<<"删除后的线性表是:"<<"\n\n\n";
    list.Print();
    
    cout<<"删除表尾节点:"<<"\n\n";
    list.DeleteFromTail(item);
	cout<<"表尾节点的值是:"<<item<<"\n\n";
	cout<<"删除后的线性表是:"<<"\n\n\n";
    list.Print();
    
	cout<<"按任意键继续.........";
	getch();
	system("cls");
	
	return 0;
}




//SLList.h

#ifndef SLLIST_H
#define SLLIST_H


struct SLNode
{
	public:
		int data;
		SLNode*next;
		SLNode(SLNode*nextnode=NULL):next(nextnode) { }
	    SLNode(const int item,SLNode*nextnode=NULL):data(item),next(nextnode) { };
} ;

class SLList
{
	private:
		SLNode*head,*tail,*currptr;
		int length;
	public:
		SLList();
		~SLList(){clear();}
		int Length()const {return length;}
		bool IsEmpty()const{return head->next==NULL;}
		void clear();
		bool Pre();
		bool Next();
		void SetStart(){currptr=head;}
		void SetEnd(){currptr=tail;}
		bool Insert(const int& item);
		bool InsertFromHead(const int& item){SetStart();Insert(item);}
		bool InsertFromTail(const int& item){SetEnd();Insert(item);}
		bool Delete(int& item);
		bool DeleteFromHead(int& item){SetStart();Delete(item);} 
	    bool DeleteFromTail(int& item){SetEnd();Pre();Delete(item);}
	    bool Search(const int& item,int (&array)[30]);
	    bool Read(int& item);
	    bool Read(int k,int& item);
	    void Print(); 
};

#endif

//SLList.cpp


#include<iostream>
#include"SLList.h"
using namespace std;

SLList::SLList()
{
	head=tail=currptr=new SLNode();
	length=0;
}

void SLList::clear()
{
	while(!IsEmpty())
	{
		currptr=head->next;
		head->next=currptr->next;
		delete currptr;
	}
	delete head;
}


bool SLList::Pre()
{
	if(IsEmpty()||currptr==head){cout<<"no previous node!"<<endl;return false;}
	SLNode*temp=head;
	while(temp->next!=currptr)
	   temp=temp->next;
	currptr=temp;
	return true;
}

bool SLList::Next()
{
	if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}
	currptr=currptr->next;
	return true;
}

bool SLList::Insert(const int& item)
{
	currptr->next=new SLNode(item,currptr->next);
	if(currptr==tail) tail=currptr->next;
	currptr=currptr->next;
	length++;
	return true;
}

bool SLList::Delete(int& item)
{
	if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}
	SLNode*temp=currptr->next;
	currptr->next=temp->next;
	item=temp->data;
	if(temp==tail)  tail=currptr;
	delete temp;
	length--;
	return true;
	
}

bool SLList::Search(const int& item,int (&array)[30])
{
	if(IsEmpty()) return false;
	SLNode*temp=head->next;
	int i=1;
	while(temp!=NULL)
	{
		if(temp->data!=item)
		{
			temp=temp->next;
		    i++;
		}   
		else
        {
        	array[i]=i;
        	currptr=temp;
        	temp=temp->next;
		    i++;
        }	
	} 
}

bool SLList::Read(int& item)
{
	if(IsEmpty()||currptr==head) {cout<<"no data!"<<endl;return false;}
	item=currptr->data;
	return true;
}

bool SLList::Read(int k,int& item)
{
	if(IsEmpty()||currptr==head) {cout<<"no data!"<<endl;return false;}
	if(k<1||k>length){cout<<"invaild number!"<<endl;return true;}
	SLNode*temp=head;
	for(int i=0;i<k;i++) temp=temp->next;
	item=temp->data;
	currptr=temp;
	return true;
}

void SLList::Print()
{
	SLNode*temp=head->next;
	cout<<"*****************************************"<<endl;
	for(int i=0;i<length;i++)
    {
        cout<<"*"<<temp->data<<"*";
        temp=temp->next;
    }
	cout<<"\n*****************************************\n\n"<<endl;
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一次实验: 题目1 单链表相关算法的实验验证。 [实验目的] 验证单链表及其上的基本操作。 [实验内容及要求] 1、 定义单链表类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建单链表; 2)插入操作:分别在当前结点后、表头、表尾插入值为x的结点; 3)删除操作:分别删除表头结点、表尾结点和当前结点的后继结点; 4)存取操作:分别存取当前结点的值和单链表中第k个结点的值; 5)查找操作:查找值为x的元素在单链表中的位置(下标)。 题目2 分别给出堆栈、队列相关算法的实验验证。 [实验目的] 验证堆栈、队列及其上的基本操作。 [实验内容及要求](以队列为例) 1、 定义队列类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建队列; 2)插入操作:向队尾插入值为x的元素; 3)删除操作:删除队首元素; 4)存取操作:读取队首元素。 第二次实验 题目1 二叉树相关算法的实验验证。 [实验目的] 验证二叉树的链接存储结构及其上的基本操作。 [实验内容及要求] 1、 定义链接存储的二叉树类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一棵二叉树,并对其初始化; 2)先根、中根、后根遍历二叉树(递归算法); 3)在二叉树中搜索给定结点的父结点; 4)搜索二叉树中符合数据域条件的结点; 5)从二叉树中删除给定结点及其左右子树。 题目2 树和森林的遍历算法的实验验证。 [实验目的] 验证树和森林的遍历算法。 [实验内容及要求] 1、 定义左儿子—右兄弟链接存储的树类和森林类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建树和森林; 2)树和森林的先根遍历的递归和迭代算法; 3)树和森林的后根遍历的递归和迭代算法; 4)树和森林的层次遍历算法。 题目3 二叉查找树的验证实验。 [实验目的] 验证二叉查找树及其相关操作。 [实验内容及要求] 1、 定义二叉查找树的类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)实现二叉查找树结构; 2) 实现二叉查找树的查找、插入和删除等算法; 第三次实验 题目1 邻接表存储的图相关算法的实验验证。 [实验目的] 验证邻接表存的图及其上的基本操作。 [实验内容及要求] 1、 定义邻接表存储的图类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一个邻接表存储的图; 2)返回图中指定边的权值; 3)返回图中某顶点的第一个邻接顶点; 4)返回图中某顶点关于另一个顶点的下一个邻接顶点的序号; 5)插入操作:向图中插入一个顶点,插入一条边; 6)删除操作:从图中删除一个顶点,删除一条边。 题目2 图的遍历算法的实验验证。 [实验目的] 验证图的遍历算法。 [实验内容及要求] 1、 定义邻接表存储的图。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一个图; 2)图的深度优先遍历的递归算法; 3)图的深度优先遍历的迭代算法; 4)图的广度优先遍历算法。 第四次实验 折半插入排序,堆排序,快速排序 请阅读说明文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值