Detailed explanation of linear table

Explanation of linear table

We first define the user-defined type

typedef int USER_TYPE;			// 用户也可根据具体情况,替换int类型。

typedef struct LINEAR {
    
    
	USER_TYPE *data;			//声明了一个名为data的长度不确定的数组,也叫“动态数组”
	int capacity;				//空间容量
	int count;					//有效元素个数
}LINEAR;

The following is a header file that I often use. It is used in the code behind, which will make the whole program look more convenient.

typedef unsigned char boolean;		
#define		TRUE		1
#define		FALSE		0
#define		NOT_FOUND	-1

I would write the following functions

boolean initLinear(LINEAR **head, int capacity);		//初始化线性表
void destoryLinear(LINEAR **head);						//释放申请的空间
int getCapacity(const LINEAR *head);					//得到申请的空间大小
int getCount(const LINEAR *head);						//得到有效元素个数
boolean isLinearFull(const LINEAR *head);				//判断空与满
boolean isLinearEmpty(const LINEAR *head);				
boolean setElementAt(const LINEAR *head, int index, USER_TYPE data);		//修改操作
boolean getElementAt(const LINEAR *head, int index, USER_TYPE *data);	//取出操作
boolean appendElementAt(LINEAR *head, USER_TYPE data);		//增加函数
boolean insertElementAt(LINEAR *head, int index, USER_TYPE data);	//插入函数
void clearLinear(LINEAR *head);							//清空整个线性表
boolean removeElementAt(LINEAR *head, int index, USER_TYPE *data);	//删除函数

After completing the previous initial conditions, now we start to formally write the linear table. Initialize the linear table first, and apply for space and data for the structure first.

boolean initLinear(LINEAR **head, int capacity) {
    
    
	if (NULL != *head) {
    
    		
		return FALSE;			//申请失败返回FALSE,即前面的宏定义0;
	}

	*head = (LINEAR *) calloc(sizeof(LINEAR), 1);	//给“动态数组”申请空间
	(*head)->data = (USER_TYPE *) calloc(sizeof(USER_TYPE), capacity);
	(*head)->capacity = capacity;
	(*head)->count = 0;

	return TRUE;
}

Whenever there is an application for space, it must be accompanied by a release, otherwise it will cause a waste of space

void destoryLinear(LINEAR **head) {
    
    
	if (NULL == head || NULL == *head) {
    
    
		return;
	}
	
	free((*head)->data);
	free(*head);
	*head = NULL;
}

Next, judge whether all the applications have been used, that is, the problem of being empty.

boolean isLinearFull(const LINEAR *head) {
    
    
	return head->count >= head->capacity;
}
boolean isLinearEmpty(const LINEAR *head) {
    
    
	return head->count <= 0;
}

Get the requested space size and number of elements, which will be used in the future

int getCapacity(const LINEAR *head) {
    
    
	return head->capacity;
}

int getCount(const LINEAR *head) {
    
    
	return head->count;
}

In order to make the program more complete, we judge whether it is empty or not

boolean isLinearEmpty(const LINEAR *head) {
    
    
	return head->count <= 0;
}

Modify operation function

boolean setElementAt(const LINEAR *head, int index, USER_TYPE data) {
    
    
	if (NULL == head || index < 0 || index >= head->count) {
    
    
		return FALSE;
	}
	head->data[index] = data;

	return TRUE;
}

Take out the operation function

boolean getElementAt(const LINEAR *head, int index, USER_TYPE *data) {
    
    
	if (NULL == head || index < 0 || index >= head->count) {
    
    
		return FALSE;
	}
	*data = head->data[index];

	return TRUE;
}

Below is the insert function

boolean insertElementAt(LINEAR *head, int index, USER_TYPE data) {
    
    
	if (NULL == head || isLinearFull(head)
			|| index < 0 || index >= head->count) {
    
    
		return FALSE;
	}

	int i;
	for (i = head->count; i > index; i--) {
    
    
		head->data[i] = head->data[i-1];
	}
	head->data[index] = data;
	++head->count;

	return TRUE;
}

If you want to add a data at the end, insert a data at the end.

boolean appendElementAt(LINEAR *head, USER_TYPE data) {
    
    
	return insertElementAt(head, head->count, data); 		//将插入位置移到末尾
}

delete function

boolean removeElementAt(LINEAR *head, int index, USER_TYPE *data) {
    
    
	if (NULL == head || isLinearEmpty(head) 
			|| index < 0 || index >= head->count) {
    
    
		return FALSE;
	}

	for (; index < head->count - 1; index++) {
    
    
		head->data[index] = head->data[index+1];
	}
	head->count--;

	return TRUE;
}

If you need to clear the entire linear table, re-enter

void clearLinear(LINEAR *head) {
    
    
	head->count = 0;
}

Directly change the number of valid elements to 0; the data you stored above directly becomes garbage data.

Well, the knowledge of linear tables is here.

Guess you like

Origin blog.csdn.net/adaizzz/article/details/104460025