C++ 链表 3-- 结构体链表-类的封装形式-动态链表-链表的创建、插入、删除、排序

80 篇文章 0 订阅
#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
    15-04~13结构体链表-类的封装形式-动态链表-链表的创建、插入、删除、排序、统计链表长度
---------------------------------*/
class book   //定义为类
{
public:
	int     num;
	float price;
	book *next;
};
book *head=NULL;
void creat();
bool check(string str);
void showbook(book *head);
void deletebook(book *head,int num);
void insertrear(book *head,int num,float price);  //尾部插入法
void insertfront(book *head,int num,float price); //头部插入法
void insertmiddle(book *head,int num,float price); //中间插入法
int  getBookNum(book *head);
int   inputnum();
float inputprice();
void main()
{
	book *p;
	creat();
	p =head;
	while(p != NULL)
	{
		cout<<p->num<<"\t"<<p->price<<endl;
		p =p->next;
	}
	cout<<"传递heaad指针前,head指针值为:"<<head<<endl;
	showbook(head);
	cout<<"传递heaad指针后,head指针值为:"<<head<<endl;


	cout<<"\n删除指定数据:"<<endl;
	int num;
	num =inputnum();
	deletebook(head,num);
	showbook(head);


	cout<<"\n尾插法,插入数据:"<<endl;	
	float price;
	num =inputnum();
	price =inputprice();
	insertrear(head,num,price);
	showbook(head);


	cout<<"\n头插法,插入数据:"<<endl;	
	num =inputnum();
	price =inputprice();
	insertfront(head,num,price);
	showbook(head);


	cout<<"\n中间插法,插入数据:"<<endl;	
	num =inputnum();
	price =inputprice();
	insertmiddle(head,num,price);
	showbook(head);	


	cout<<"图书的数目是:"<<getBookNum(head)<<endl;
}


int  getBookNum(book *head)
{
	int num=0;
	while(head)
	{
		num++;
		head= head->next;
	}
	return num;
}


int inputnum()
{
	int num;
	string str;
	cout<<"请输入要插入的图书编号:"<<endl;
	cin>>str;
	while(!check(str))
	{
		cout<<"输入的不是数字,请重新输入:"<<endl;
		cin>>str;
	}
	num = atoi(str.c_str()); //atoi()字符串转整型数据
	return num;
}
float inputprice()
{
	float price;
	string str;
	cout<<"请输入图书的价格:"<<endl;
	cin>>str;
	while(!check(str))
	{
		cout<<"输入的不是数字,请重新输入:"<<endl;
		cin>>str;
	}
	price = atof(str.c_str()); //atoi()字符串转整型数据
	return price;
}
void insertmiddle(book *head,int num,float price) //中间插入法
{
	book *list =new book;
	list->num =num;
	list->price =price;
	if(num<=head->num)
	{
		list->next =head;
		::head =list;
		return;
	}
	book *temp=NULL;
	while((num>head->num)&&(head->next!=NULL))
	{
		temp =head;
		head =head->next;
	}
	if(num>head->num)  //大于等于,一定在尾部插入
	{
		head->next =list;
		list->next =NULL;
	}
	else               //小于,一定在中间插入
	{
		list->next =head;
		temp->next =list;
	}
}
void insertfront(book *head,int num,float price) //头部插入法
{
	book *list =new book;


	list->next=head;
	::head =list;
	list->num =num;
	list->price =price;
}
void insertrear(book *head,int num,float price)  //尾部插入法
{
	book *list =new book;
	book *l;
	while(head)
	{
		l=head;   //最后用于存储链表的最后一个节点的位置
		head=head->next;
	}
	l->next=list;
	list->num =num;
	list->price =price;
	list->next =NULL;
}


void deletebook(book *head,int num)
{
	book *l;


	if(head->num == num)
	{
		l =head;
		head=head->next;
		::head =head;    //双冒号是全局操作符,标识head是个全局变量
		delete l;        //释放头结点占用的内存空间
		cout<<"操作成功"<<endl;
		return;
	}
	l =head;                //l记录前节点的位置
	head=head->next;        //head记录当前判断节点的位置
	while(head)
	{
		if(head->num == num)
		{
			l->next=head->next;
			delete head;    //释放要删除节点所占用的空间
			cout<<"删除成功"<<endl;
			return;
		}
		else
		{
			l =head;
			head=head->next;
		}
	}
	cout<<"没有该节点,未删除任何数据"<<endl;
}


void showbook(book *p)  //形参指针p,这颗指针最
{                          //终指向的位置并不影响传递进来那个指针的指向
	cout<<"图书的信息如下:"<<endl;
	while(p)
	{
		cout<<"图书编号:"<<p->num<<'\t'<<"价格:"<<p->price<<endl;
		p =p->next;
	}
}


bool check(string str)
{
	for(int i=0;i<str.length();i++)
	{
		if((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
			return false;
	}
	return true;
}


void creat()
{
	book *p1,*p2;
	p1 =new book;
	string str;
	head =p1;
	p2 =p1;
	cout<<"请输入图书编号,以0结束:"<<endl;
	cin>>str;
	while(!check(str))
	{
		cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
		cin>>str;
	}
	p1->num = atoi(str.c_str()); //atoi()字符串转整型数据
	if(p1->num == 0)
		head =NULL;


	while(p1->num != 0)
	{
		cout<<"请输入图书的价格:"<<endl;
		cin>>str;
		while(!check(str))
		{
			cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
			cin>>str;
		}
		p1->price = atof(str.c_str()); //atoi()字符串转浮点数据
		p2 =p1;
		p1 =new book;
		p2->next =p1;
		cout<<"请输入图书编号,以0结束:"<<endl;
		cin>>str;
		while(!check(str))
		{
			cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
			cin>>str;
		}
		p1->num = atoi(str.c_str()); //atoi()字符串转整型数据
	}


	cout<<"before delete p1: "<<p1<<endl;
	delete p1;  //释放p1指向的内存空间,但p1指针仍旧指向那个地方
	cout<<"after delete p1: "<<p1<<endl;
	p1 =NULL;   
	p2->next =NULL;
}



运行结果:

请输入图书编号,以0结束:
1
请输入图书的价格:
12.3
请输入图书编号,以0结束:
2
请输入图书的价格:
23.02
请输入图书编号,以0结束:
3
请输入图书的价格:
25.6
请输入图书编号,以0结束:
0
before delete p1: 00200BE8
after delete p1: 00200BE8
1       12.3
2       23.02
3       25.6
传递heaad指针前,head指针值为:002006D0
图书的信息如下:
图书编号:1     价格:12.3
图书编号:2     价格:23.02
图书编号:3     价格:25.6
传递heaad指针后,head指针值为:002006D0


删除指定数据:
请输入要插入的图书编号:
2
删除成功
图书的信息如下:
图书编号:1     价格:12.3
图书编号:3     价格:25.6


尾插法,插入数据:
请输入要插入的图书编号:
5
请输入图书的价格:
25.36
图书的信息如下:
图书编号:1     价格:12.3
图书编号:3     价格:25.6
图书编号:5     价格:25.36


头插法,插入数据:
请输入要插入的图书编号:
0
请输入图书的价格:
21.36
图书的信息如下:
图书编号:0     价格:21.36
图书编号:1     价格:12.3
图书编号:3     价格:25.6
图书编号:5     价格:25.36


中间插法,插入数据:
请输入要插入的图书编号:
4
请输入图书的价格:
42.3
图书的信息如下:
图书编号:0     价格:21.36
图书编号:1     价格:12.3
图书编号:3     价格:25.6
图书编号:4     价格:42.3
图书编号:5     价格:25.36
图书的数目是:5
Press any key to continue

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值