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

📄 list.cpp

📁 包括图、二叉树、链表
💻 CPP
字号:
#include"List.h"
List::List()
{
	count=0;
	head=NULL;
}
Error_code List::insert(int position,const int &data)
{
	if(position<0||position>count)
		return rangeerror;
	Node *new_node,*previous,*following;
	if(position>0)
	{
		previous=set_position(position-1);
		following=previous->next;
	}
	else following=head;
	new_node=new Node(data,following);
	if(new_node==NULL)
		return overflow;
	if(position==0)
		head=new_node;
	else
		previous->next=new_node;
	count++;
	return success;
}

Error_code List::replace(int position,const int &data)
{
	if(position<0||position>count)
		return rangeerror;
	Node *old_node=set_position(position);
	old_node->entry=data;
	return success;

}

Error_code List::remove(int position)
{
	if(position<0||position>count)
		return rangeerror;
	Node *previous,*old_node;
	if(position>0)
	{
		previous=set_position(position-1);
		old_node=previous->next;
		previous->next=old_node->next;
		delete old_node;
		return success;
	}
	else 
	{
		old_node=head;
		head=head->next;
		delete old_node;
		return success;
	}
}

Node *List::set_position(int position) const
{
	Node *p=head;
	for(int i=0;i<position;i++) p=p->next;
	return p;
}

void List::print()
{
	Node *p=head;
	for(;p->next!=NULL;p=p->next)
		cout<<p->entry<<"->";
	cout<<p->entry;
	cout<<endl;
}

void List::creat_list()
{
	cout<<"input the sort numbers,end of '0'."<<endl;
	int num;
	cin>>num;
	Node *node=new Node(num);
	head=node;
	count++;
	cin>>num;
	while(num!=0)				//结束符
	{	
		count++;
		node->next=new Node(num);
		node=node->next;
		cin>>num;
	}
}

void List::select()
{
	cout<<"构造链表--c"<<endl;
	cout<<"插入数值--i"<<endl;
	cout<<"打印链表--p"<<endl;
	cout<<"选择一个操作:";
}

int List::size()
{
	return count;
}

⌨️ 快捷键说明

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