C++/链表类

实现常见的链表操作,插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序

重载运算符: 下标,+(求和),-(求差),<<(输出插入符),>>(输入插入符)

#include<iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;

//---------------------------------------------------------------------------------
class Node
{
	public:
		char data;
		Node * next;
};
//----------------------
class Link
{
	public:
		Node * Searchpoint;
		Node * Searchpointlast;
		//
		friend void menu(Link A);
		friend void menu2(Link A);
		//
		friend istream & operator>>( istream& putin,Link &c);
		friend ostream & operator<<( ostream& output,const Link &c);
		//
		void Fileout();
		void createSort();
		void HeadSort();
		void TailSort();
		void print();
		void Scarch(char a); 
		Node * Searchit(char a);
		Node * Searchlast(char a);
		void insert(Node *p);
		void deleteit(Node *p,Node *q);
		void * BubbleSort();
		void FileIn();
	private:
		Node * head;
		Node * tail;
		int length;
};
//-----------------------------------------------------------------------------------

//------------------------------------------------------------------------函数重载
istream & operator>>( istream& putin,Link &c)
{
	for(int i=0;i<c.length;i++)
	c.tail=c.tail->next;
	int n;
	cout<<"输入你要继续输入的字符数目:";
	cin>>n;
	Node *p;
	char elem;
	for(int j=0;j<n;j++)
	{
		p=new Node;
		putin>>elem;
		p->data=elem;
		p->next=NULL;
		c.tail->next=p;
		c.length++;
		c.tail=p; 
	}
return putin;
}

ostream & operator<<( ostream& output,const Link &c)
{
	Node *p;
	p=c.head->next; 
	for(int i=0;i<c.length;i++)
	{
		output<<p->data<<" ";
		p=p->next; 
	}
	cout<<endl;
	return output;
} 




//---------------------------------------------------------------------------- 

//------------------------------------------------------------------文件输入 、输出 

void Link::FileIn()
{
	Node *p,*c;
	p=head;
	
	//----------------------------------File
	fstream infile;
	infile.open("实验3.dat",ios::in);
	if(!infile)
	{
		cout<<"实验3.dat can't open."<<endl;
		abort();
	}

	//----------------------------------Read
	char ch;
	while(infile.get(ch))
	{
		c = new Node;
		if(c==NULL)
		{
			cout<<"Unable to allocate c memory space."<<endl;
			exit(0);
		}
		p->next=c;
		cout<<"ch= "<<ch<<endl;
		c->data=ch;
		cout<<"c->data= "<<c->data<<endl;
		c->next=NULL;
		p=c;
		length++; 
	}
	infile.close();
}

void Link::Fileout()
{
	Node* p;
	p=head->next;
	fstream outfile;
	outfile.open("实验3.dat",ios::out);
	if(!outfile)
	{
		cout<<"实验3.dat can't open."<<endl;
		abort();
	}
	while(p!=NULL)
	{
		outfile.put(p->data);
		p=p->next;
	}
	outfile.close();
	cout<<endl<<"已经输出到文件中"<<endl;
}

//------------------------------------------------------------------------------------ 
void * Link::BubbleSort()
{
	Node *h=head;
	int len=length; 
	Node * t;
	Node * x;
	t=h->next;
	x=h->next;
	x=x->next; 
	while(t->next!=NULL)
	{
		while(x!=NULL)
		{
			if(t->data>x->data)
			{
				int e;
				e=t->data;
				t->data=x->data;
				x->data=e; 
			}
			x=x->next; 
		}
		
		t=t->next;
		x=t->next;
	}
	return h;
}

void Link::deleteit(Node *p,Node *q)
{
	Node *s;
	s=q;
	p->next=q->next;
	free(s);
	length--;
}

Node * Link::Searchlast(char a)
{
	int i=0;
	Node *p;
	Node *s;
	p=head->next;
	s=head;
	while(p!=NULL)
	{
		if(p->data==a)
		{
			i++;
			return s;
		}
		else
		p=p->next;
		s=s->next; 
	}
	if(i==0)
	cout<<"未找到!";
	exit(0);
}


void Link::insert(Node *p)
{
	Node *s;
	s = new Node;
	if(s==NULL)
	{
		cout<<"Unable to allocate memory space."<<endl;
		exit(0);
	}
	cout<<"输入你要插入的字符"<<endl;
	cin>>s->data;
	s->next=p->next;
	p->next=s;
	length++;
}


Node * Link::Searchit(char a)
{
	int i=0;
	Node *p;
	p=head;
	while(p!=NULL)
	{
		if(p->data==a)
		{
			i++;
			return p;
		}
		else
		p=p->next;
	}
	if(i==0)
	cout<<"未找到!";
	exit(0);
}


void Link::createSort()
{
	head = new Node;
	head->next=NULL;
	tail=head;
	length=0;
}

void Link::HeadSort()
{
	int n;
	cout<<"输入你需要”头插法“插入的字符数:";
	cin>>n;
	Node *p;
	char elem;
	for(int i=0;i<n;i++)
	{
		p=new Node;
		cin>>elem;
		p->data=elem;
		p->next=head->next;
		head->next=p;
		length++;
	} 
}

void Link::TailSort()
{
	for(int i=0;i<length;i++)
	tail=tail->next;
	int n;
	cout<<"输入你需要”尾插法“插入的字符数:";
	cin>>n;
	Node *p;
	char elem;
	for(int j=0;j<n;j++)
	{
		p=new Node;
		cin>>elem;
		p->data=elem;
		p->next=NULL;
		tail->next=p;
		length++;
		tail=p; 
	}
	
}

void Link::Scarch(char a)
{
	int j=1;
	Node *p;
	p=head->next;
	int x=1;
	int o=0;
	for(int i=0;i<length;i++)
	{
		if(p->data==a)
		{
		cout<<"第"<<j<<"个"<<a<<"位于链表的第"<<x<<"位"<<endl;
		o++;
		}
		p=p->next;
		x++;
	}
	if(o==0)
	cout<<"未找到!"<<endl;
}




void Link::print()
{
	Node *p;
	p=head->next; 
	for(int i=0;i<length;i++)
	{
		cout<<p->data<<" ";
		p=p->next; 
	}
	cout<<endl;
} 



void menu(Link A)
{
	system("cls");
	cout<<"\t\t\t\tMENU\t\t"<<endl;
	cout<<"\t\t\t|---------------------------|\t\t"<<endl;
	cout<<endl;
	cout<<"\t\t\t|   [1]  建立数据/继续输入  |"<<endl;
	cout<<"\t\t\t|   [2]     查 找 数 据     |"<<endl;
	cout<<"\t\t\t|   [3]     插 入 数 据     |"<<endl;
	cout<<"\t\t\t|   [4]     删 除 数 据     |"<<endl;
	cout<<"\t\t\t|   [5]     打 印 数 据     |"<<endl;
	cout<<"\t\t\t|   [6]     排 序 数 据     |"<<endl;
	cout<<"\t\t\t|   [7]     函 数 重 载     |"<<endl;
	cout<<"\t\t\t|   [8]       退   出       |"<<endl;
	cout<<endl;
	cout<<"\t\t\t|---------------------------|\t\t"<<endl;
	if(A.length!=0)
	{
		cout<<"链表内容为:";
		A.print();
	}
	
}

void menu2(Link A)
{
	system("cls");
	cout<<"\t\t\t\tMENU\t\t"<<endl;
	cout<<"\t\t\t|-------------------------|\t\t"<<endl;
	cout<<endl;
	cout<<"\t\t\t|     [1]     头  插      |"<<endl;
	cout<<"\t\t\t|     [2]     尾  插      |"<<endl;
	cout<<"\t\t\t|     [3]   文 件 输 入   |"<<endl;
	if(A.length!=0)
	{
		cout<<"链表内容为:";
		A.print();
	}
}


//------------------------------------------------------------------------------
int main()
{
	Link A;
	A.createSort();
	int choose;
	int choose2;
	while(1)
	{
		menu(A);
		cin>>choose;
		switch(choose)
		{
			case 1:
				menu2(A);
				cin>>choose2;
				if(choose2==1)
				A.HeadSort();
				else if(choose2==2)
				A.TailSort();
				else if(choose2==3)
				A.FileIn();
				else 
				cout<<"输入错误!"<<endl;
				system("pause");
				break;
			case 2:
				cout<<"输入要查找内容:";
				char a;
				cin>>a;
				A.Scarch(a);
				system("pause");
				break;
			case 5:
				A.print();
				A.Fileout();
				system("pause");
				break;
			case 8:
				exit(0);
				cout<<endl;
				system("pause");
				break;
			case 3:
				int num;
				char x;
				cout<<"输入”1“你会在选定字符后插入/输入”2“你会在选定字符前插入:";
				cin>>num; 
				if(num==1)
				{
					cout<<"你需要在哪个字符后插入:";
					cin>>x;
					A.Searchpoint=A.Searchit(x);
					A.insert(A.Searchpoint);
				}
				else if(num==2)
				{
					cout<<"你需要在哪个字符前插入:";
					cin>>x;
					A.Searchpointlast=A.Searchlast(x);
					A.insert(A.Searchpointlast);
				}
				else
				cout<<"输入错误!";
				system("pause");
				break;
			case 4:
				char e;
				cout<<"输入你需要删除的数";
				cin>>e;
				A.Searchpoint=A.Searchit(e);
				A.Searchpointlast=A.Searchlast(e);
				A.deleteit(A.Searchpointlast,A.Searchpoint);
				system("pause");
				break;
			case 6:
				A.BubbleSort();
				system("pause");
				break;
			case 7:
				cout<<"输入运算符重载:"<<endl;
				cin>>A;
				 cout<<A;
				 break;
			default:
				cout<<"你使用了非法字符!"<<endl;
				cout<<endl;
				system("pause");
				break;
		}
		
	}
	//-------------
	return 0;
}

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值