数组总结以及数组链表

这里讨论的数组,并不是数组这个结构,而是讨论数组元素的定位方式
由于内存是线性一维的,所以根本不存在所谓的二维数组,矩阵,甚至三维数组,四维数组等,任意给定一个n维数组中确定的某个有效下标,都可以转换成一维的下标
二维数组可以看成行列,三维数组可以看成一张存在行列的纸,四维数组可以看作一本书其中有若干页,每页有固定的最大行列值

数组链表:数组是连续存储空间,它的空间利用率高,但是其长度固定,链表是非连续存储空间,虽然其长度可变,但是由于有链域的存在,其空间利用率低。结合数组和链表的特性,设计一个结构体同时具备数组和链表的优点的结构存储字符串,并可以根据指定下标进行字符的增删查改

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>


#define FALSE 0
#define TRUE  1

typedef unsigned char boolean;

typedef struct ROOM{
	char word[15]; 
	int efficiency; 
	int *nextRoom;
}ROOM;
//每个结构体有16个字节用来存储字符串,4个字节存储当前结构体
//内的有效元素个数,4个字节用来作为链域指向下一个结构体

int j = 0;
int protectj = 0;
int changej = 0;
int countIndex[100] = {0};
//为了控制下标,这里采用了全局变量和全局数组来
//记录和修改每个结构体内存在的元素个数
//有效元素个数的记录是依次累加的


boolean inputStr(ROOM **head);
void showRooms(const ROOM *head);
boolean destroyRoom(ROOM **head);
ROOM *findTheRooMByIndex(ROOM *head, int index);
void showOneRoom(const ROOM *result);
int fineTheCharByIndex(ROOM *result, int index);
void showOneChar(const ROOM *result, const int realIndex);
void replaceOneChar(ROOM *result, int realIndex);
void deleteOneChar(ROOM *result, int realIndex);
ROOM *findTheLastRoom(ROOM *head);
void appendOneRoom(ROOM **head, ROOM *last);
void insertOneChar(ROOM *result, int realIndex);
boolean inputStr(ROOM **head) {

	ROOM *p;
	ROOM *tail;

	int i = 0;
	int tmp = 0;
	int input = 1;
	int strLen = 0;
	char str[128] = {0};

	if(NULL == head || NULL != *head) {
		return FALSE;
	}

	while(input) {

		printf("请输入一个字符串\n");
	    gets(str);
	    _flushall();
	    strLen = strlen(str);

		p = (ROOM *)calloc(sizeof(ROOM), 1);

		for(i = 0; i < strLen; i++) {
			p->word[i] = str[i];
		}

		p->efficiency = strLen;
		p->nextRoom = NULL;

		countIndex[j] = strLen + tmp;
		tmp = countIndex[j];
		j++;

	    if(NULL == *head) {
		  *head = p;
	    } else {
		  tail->nextRoom = p;
	    }

	    tail = p;

	    for(i = 0; i < strLen; i++) {
		  str[i] = 0;
	    }

	    printf("输入0结束 输入1继续\n");
		scanf("%d" ,&input);
		_flushall();
	}

	return TRUE;
}
void showRooms(const ROOM *head) {

	int i = 0;

	if(NULL == head) {
		return;
	}

	while(head) {

		for(i = 0; i < head->efficiency; i++) {
			printf("%c ", head->word[i]);
		}

		head = head->nextRoom;
	}
}

boolean destroyRoom(ROOM **head) {

	ROOM *p;

	if(NULL == head || NULL == *head) {
		return FALSE;
	}

	while(*head) {
		p = *head;
		*head = p->nextRoom;
		free(p);
	}

	return TRUE;
}
ROOM *findTheRooMByIndex(ROOM *head, int index) {

	ROOM *p = NULL;
	ROOM *result = NULL;

	protectj = j;

	j = 0;

	for(p = head; p; p = p->nextRoom) {

		result = p;

		if(index < countIndex[j]) {
			
			return result;
		}
		j++;
		changej = j;
	}

	return result;
}

void showOneRoom(const ROOM *result) {
	int i;

	for(i = 0; i < result->efficiency; i++) {

		printf("%c ",result->word[i]);
	}
}
int fineTheCharByIndex(ROOM *result, int index) {

	int i = 0;
	int num = 0;

	num = countIndex[changej - 1];

	printf("之前room内元素总个数\n");
	printf("%d\n", countIndex[changej - 1]);

	index = index - num;

	return index;
}

void showOneChar(const ROOM *result, const int realIndex) {

	printf("%c\n", result->word[realIndex]);
}

void replaceOneChar(ROOM *result, int realIndex) {

	char newChar;

	printf("请输入一个新字符\n");
	scanf("%c", &newChar);
	_flushall();

	result->word[realIndex] = newChar;
}
void deleteOneChar(ROOM *result, int realIndex) {

	int i;

	for(i = 0; i < (result->efficiency - 1); i++) {
		result->word[realIndex + i] = result->word[realIndex + i +1];
	}
	result->efficiency = result->efficiency - 1;

	for(i = changej; i < j; i++) {
		countIndex[i] = countIndex[i] - 1;
	}
}

ROOM *findTheLastRoom(ROOM *head) {

	ROOM *p = NULL;
	ROOM *last = NULL;

	for(p = head; p; p = p->nextRoom) {
		last = p;
	}

	return last;

}
void appendOneRoom(ROOM **head, ROOM *last) {

	int i;
	int strLen;
	int num = 0;
	char str[128] = {0};

	ROOM *appendRoom;
	appendRoom = (ROOM *)calloc(sizeof(ROOM), 1);
	appendRoom->nextRoom = NULL;

	last->nextRoom = appendRoom;

	
	printf("请输入字符或字符串\n");
	gets(str);
	_flushall();
	strLen = strlen(str);
	printf("新增字符串长度:%d\n", strLen);

	appendRoom->efficiency = strLen;

	for(i = 0; i < strLen; i++) {
		appendRoom->word[i] = str[i];
	}

	printf("新增前room个数%d\n", j);

	for(i = 0; i < j; i++) {
		printf("%d\n", countIndex[i]);
		num = countIndex[i];
	}


	j = j + 1;
	protectj = j;
	printf("当前room个数%d\n", j);

	countIndex[j - 1] = num + strLen;

}
void insertOneChar(ROOM *result, int realIndex){

	char insertChar;
	int i;

	_flushall();

	printf("请输入一个要插入的字符\n");
	scanf("%c", &insertChar);
	_flushall();

	for(i = 0; i > (result->efficiency - realIndex); i++) {

		result->word[result->efficiency - i] = 
		  result->word[result->efficiency - i - 1];
	}
	result->word[realIndex] = insertChar;
	result->efficiency = result->efficiency + 1;

	for(i = changej; i < j; i++) {
		countIndex[i] = countIndex[i] + 1;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值