线性表(学习笔记)

#include<iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define size 10
/*
//线性表

struct SqList
{
	int* LHead;
	int length;
};

bool Init(SqList &L) {
	L.LHead = new int[100];
	if (!L.length) {
		cout << "error" << endl;
		return false;
	}
	L.length = 0;
	return 1;
}
void Del(SqList& L) {
	if (L.LHead) {
		delete L.LHead;
	}
}

int main() {
	SqList l;
	l.LHead = (int*)malloc(sizeof(int) * 10);
	cout << l.LHead << endl;
	free(l.LHead);
	return 0;
}

*/


//struct Book
//{
//	string name;
//	int list[20];
//	int count[5];
//};
//
//struct Tushu
//{
//	Book* p;
//	int length;
//};
//
//int main() {
//	Tushu b1={}
//
//	return 0;
//}







//#define MAXSIZE 10000
//typedef struct {
//	char no[20];
//	char name[50];
//	float price;
//}Book;
//typedef struct {
//	Book* elem;
//	int length;
//}SqList;
//

struct Book
{
	string no;
	string name;
	double price;
};
struct Booklist
{
	Book* p;
	int length;
};

bool Blinit(Booklist &BL) {
//	BL.p = (Book*)malloc(sizeof(Book) * size);//c语言的创建方法
	BL.p = new Book [100];//c++的创建方法
	if (!BL.length) {
		cout << "error" << endl;
		return false;
	}
	BL.length = 0;
	return true;
}

int Blfind(string str, Booklist BL) {
	for (int i = 0; i < BL.length; i++) {
		cout << BL.p[i].name << endl;
		if (BL.p[i].name == str) {
			return i + 1;
		}
		else continue;
	}
	return 0;
}

//线性表插入元素
void ListInsert(Booklist b, Book b3,int i) {
	if (i<1 || i>b.length + 1) cout<<"false,nothing happend"<<endl;
	if (b.length == size) cout << "no room" << endl;
	for (int j = b.length - 1; j >= i-1; j--) {
		b.p[j + 1] = b.p[j];
	}
	b.p[i - 1] = b3;
	b.length++;
}
//线性表删除元素
void ListDelete(Booklist &b, int i) {
	if (i<1 || i>b.length + 1) cout << "false,nothing happend" << endl;
	for (int j = i; j<=b.length-1; j++) {
		b.p[j] = b.p[j + 1];
	}
	b.length--;
}



int main() {
	Booklist b;
	Book b1;
	Book b2;
	Book b3;
	Blinit(b);
	//cout << Blinit(b) << endl;
	//cout << b.p << endl;
	//string str1;
	//cin >> str1;

	b1.name = "c++ primer";
	b1.no = "ISB-123";
	b1.price = 10.00;
	b.p[b.length++] = b1;
	

	b2.name = "c++ primer plus";
	b2.no = "ISB-321";
	b2.price = 20.00;
	b.p[b.length++] = b2;


	b3.name = "c与指针";
	b3.no = "ISB-213";
	b3.price = 30.00;
	//b.p[b.length++] = b3;
	
	/*cout << Blfind(str1, b) << endl;*/
	//cout << b.p[0].name << endl;

	ListInsert(b, b3, 3);//线性表的插入

	for (int i = 0; i <= b.length; i++) {
		cout << b.p[i].name<< endl;
		cout << "------" << endl;
	}//打印

	cout << endl;
	cout << "after" << endl;
	cout << endl;


	ListDelete(b, 1);
	for (int i = 0; i <= b.length; i++) {
		cout << b.p[i].name << endl;
		cout << "------" << endl;
	}//打印
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值