⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实现静态单链表[遍历、查找、插入、删除].cpp

📁 这个是我的大学作业哦 里面有些很经典的代码 出自清华大学的数据结构课本
💻 CPP
字号:

#include<iostream>
const int N = 100;
using namespace std;
//**********************
class StatList;

class Node {
	friend StatList;
private:
	int data;
	int link;
};
//----------------------------------
class StatList {
public:
	StatList();
	void display();
	void create();
	void search();
	bool isfull();
	void insert();
	int reverse(int);
	int reverse_num(int);
	void remove();
	bool isin(int);
	int previous(int);

private:
	int avil;
	int head;
	Node node_array[N];
};
//-----------------------------------------------
StatList::StatList() {
	node_array[0].data = 0;
	node_array[0].link = -1;

	for(int i = 1; i < N-1; i++)
	{
		node_array[i].data = 0;
		node_array[i].link = i+1;
	}
	node_array[N-1].data = 0;
	node_array[N-1].link = -1;

	avil = 1;
	
}

//----------------------------------------------------
void StatList::create() {
	int i = 0, total_input;
	loop:cout<<"total numbers: ";
	cin>>total_input;
	if(total_input>=N || total_input<=0)
	{
		cout<<"beyond capacity!"<<endl;
		goto loop;             
	}
	head = 1;
	node_array[0].link = head;         //    --*

	for( i = 1; i<=total_input; i++)
	{
		cin>>node_array[i].data;
	}
	node_array[i-1].link = -1;
	avil = total_input+1;                   //*

}
//-----------------------------------------------------
void StatList::search() {
	int target;
	bool isfound = 0;
	cout<<"Target: ";
	cin>>target;
	int i = 0;
	while(node_array[i].link!=-1)
	{
		i = node_array[i].link;
		if(node_array[i].data==target)
		{
			cout<<"found "<<target<<" at position "<<i<<"."<<endl;
			isfound++;
		}
	}
	if(!isfound)
		cout<<"No target found!"<<endl;
}
//----------------------------------------------------
bool StatList::isfull() {
	int i = 0, j = 0;
	while(node_array[i].link!=-1)
	{
		i = node_array[i].link;
		j++;
	}

	if(j==N-1)

		return 1;
	else
		return 0;
}
//-----------------------------------------------------------------
void StatList::insert() {
	if(isfull())
		cout<<"The list is full , so stop compiling.."<<endl;
	else
	{
		int num, pos;
		//   --*
		loop2:cout<<"position: ";
		cin>>pos;
		if(pos<=0 || pos > avil)          //      --*
		{
			cout<<"Wrong position!"<<endl;
			goto loop2;
		}
		cout<<"number? ";
		cin>>num;
		node_array[avil].data = num;
		node_array[avil].link = pos;
		node_array[reverse(pos)].link = avil;
	}

}
//_
void StatList::remove() {
	int  pos;
		//   --*
		loop3:         cout<<"position: ";
	    
		cin>>pos;
		if(pos<=0 || pos > avil)          //      --*
		{
			cout<<"Wrong position!"<<endl;
			goto loop3;
		}
 
 //*
		node_array[reverse(pos)].link = node_array[pos].link;
	 

}
//-------------------------------------------------
int StatList::reverse(int lk) {
	for(int i = 1; i < avil; i++)
	{
		if(node_array[i].link==lk)
			return i;
	}
	return 0;
}

//------------------------------------------------
bool StatList::isin(int target) {
	int i = 0;
	while(node_array[i].link!=-1)
	{
		if(node_array[i].data==target)
			return true;
		i = node_array[i].link;
	}
	return 0;
}
/*
int StatList::previous(int num) {
	int i = 0;
	while(node_array[i].link!=-1)
	{
		if(node_array[node_array[i].link].data==num)
			return i;
		i = node_array[i].link;
	}
	return 0;


}
*/
//---------------------------------------------------------------------------- 
 

void StatList::display() {
	int i = 0;
	cout<<"List is ";
	while(node_array[i].link!=-1)
	{
		i = node_array[i].link;
		cout<<node_array[i].data<<" ";;
	}
	cout<<endl;


}
//*********************************************************
int main(int argc, char* argv[]) {
	/*
	StatList sl;
	sl.create();
	sl.display();
	sl.search();
	sl.insert();
	sl.display();
	//sl.remove();

*/
	StatList s2;
	s2.create();
	s2.display();
	s2.search();
	s2.remove();
	s2.display();


	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -