Big Bang——数据结构(C++动态顺序表的增、删、改、查)

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string>
using namespace std;
#define LIST_INIT_SIZE 100	//线性表存储空间的初始分配量
#define LISTINCREMENT 10	//线性表存储空间的分配增量

typedef struct {
	char *elem;				//字符数组
	int length;
	int listSize;
}Sqlist;

//返回位置
typedef struct {
	int pos;			//记录字符串所属位置 1 2 3.。。
	int index;			//记录字符起始位置 0 1 2 .。。。
}Position;
bool initSqlist(Sqlist &L) {
	L.elem = (char *)malloc(sizeof(char)*LIST_INIT_SIZE);
	if (L.elem == nullptr)
		return false;
	L.length = 0;
	L.listSize = LIST_INIT_SIZE;
	return true;
}

bool ListInsert_Sq(Sqlist &L, int index, string elem) {
	while ((L.length + elem.size() + 1) > L.listSize)			//循环分配,也许一次分配不够
	{
		char *newbase = (char *)realloc(L.elem, (L.listSize
			+ LISTINCREMENT) * sizeof(char));//实际上相当于用malloc申请了更大的,然后把已存的复制过来
		if (!newbase)
			return false;//存储分配失败
		L.elem = newbase;//新基地址
		L.listSize += LISTINCREMENT;//增加存储容量	
	}
	int pos = 0;
	int k = 0;					//记录字符串实际插入char中的起始位置
	for (k;k < L.listSize && pos != (index - 1);k++)	//找到插入的起始位置
		if (L.elem[k] == ' ') pos++;
	for (int i = L.length ;i >=k;i--) {			//将要插入的位置全部后移elem.size()+1个单位
		L.elem[i + elem.size()+1] = L.elem[i];
	}
	if (k == 0) {
		int j = 0;
		for (int i =k;i < elem.size();i++)		//起始位置开始插入
			L.elem[i] = elem[j++];
	}
	else {
		int j = 0;
		for (int i = k;i <=(elem.size()+k);i++)		//将挪好的位置,插入
			L.elem[i] = elem[j++];
	}
	L.elem[k + elem.size()] = ' ';
	L.length += elem.size() + 1;
	return true;
}

void printSqList(Sqlist L) {
	int p = 0;
	while (p < (L.length-1))
			cout << L.elem[p++];
	cout << endl;
}
//字符串查找,返回位序
Position searchSqlist(Sqlist L, string str) {
	const char *temp = L.elem;
	string strTemp = temp;
	string::size_type pos=0;
	Position position;
	int n = 0;
	while ((pos = strTemp.find(str, pos)) != string::npos) {		//循环查字串
		if (pos == 0 && L.elem[str.size()] == ' ') {				//如果为第一个位置,单独处理,判断后继是否为' ',而不是字串
			position.pos = pos + 1;
			position.index = pos;
			return position;
		}
			
		else if (pos != 0 && L.elem[pos - 1] == ' ' && L.elem[pos + str.size()] == ' ') {//其他情况,判断前后是否为' ',确定不是字串
			position.pos = count(strTemp.begin(), strTemp.begin() + pos, ' ') + 1;		//起始位置到查到的位置,空格个数加1即是所需要位置
			position.index = pos;
			return position;
		}
		pos++;		//循环查找,不加加,会一直查找第一个。
	}
	position.pos = -1;
	position.index = -1;
	return position;
}

bool delSqlist(Sqlist &L, string delStr) {
	int delPos = searchSqlist(L, delStr).index;		//查到字符索引
	if (delPos != -1) {
		for (int i = delPos;i < L.length;i++) {		//将元素后移,腾出位置
			L.elem[i] = L.elem[i + delStr.size()+1];
		}
		L.length -= delStr.size()+1;				//长度减串长加1(因为空格一起减去)
		return true;
	}
	return false;
}
int main() {
	string commond;
	int count = 0;
	Sqlist L;
	initSqlist(L);
	while (cin >> commond) {
		if (commond == "insert") {
			int index;
			cin >> index;
			string insertStr;
			cin >> insertStr;
			ListInsert_Sq(L, index, insertStr);
		}
		else if (commond == "show") {
			printSqList(L);
		}
		else if (commond == "delete")
		{
			string delStr;
			cin >> delStr;
			delSqlist(L,delStr);
		}
		else if (commond == "search") {
			string serStr;
			cin >> serStr;
			int pos = searchSqlist(L, serStr).pos;
			if (pos != -1)
				cout << pos;
			cout << endl;
		}
	}


	//system("pause");
	return 0;
}

链接: 原题链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值