Introduction to data structures and implementation of abstract data types

Introduction to data structures

Data structure (English: data structure) is the way in which data is stored and organized in a computer.
Data structure means interface or encapsulation: a data structure can be thought of as an interface between two functions, or as an access method encapsulation of stored contents consisting of a union of data types.
Most data structures are composed of basic types such as arrays, records, identifiable unions, and references. For example, a nullable reference is a combination of a reference and an identifiable union, and the simplest linked list is composed of records and nullable references.
Data structures can be implemented through data types, references, and other operations provided by programming languages. A well-designed data structure should support the operation of various programs while using as little time and space resources as possible.
Different types of data structures are suitable for different types of applications, and some data structures are even designed to solve specific problems. For example, B-tree is a data structure designed to speed up tree structure access and is often used in databases and file systems.

Basic concepts and terminology

Data is a symbolic representation of objective things. In computer science, it refers to the general term for all symbols that can be input into a computer and processed by a computer program.
Data elements are the basic units of data and are usually considered and processed as a whole in computers. A data element can be composed of several data items . For example, a number is a data element, and each item of book title information, such as the book title and author, can be a data item. A data item is the smallest indivisible unit of data.
A data item is a collection of data elements of the same nature and is a subset of data. A data structure is a collection
of data elements that have one or more specific relationships with each other . The relationships between them can be divided into four categories: set, linear, tree, and graph. The data structure can be defined as a tuple: DATA Structure=(D,S); where D is a finite set of data elements, and S is a finite set of relationships on D. A data type is a collection of values ​​and a set of operations defined on this value. For example, int is an integer variable whose value is an integer in a certain interval and the addition, subtraction, multiplication and division operations defined on it.




Abstract data type representation and implementation

basic definition

An abstract data type refers to a mathematical model and a set of operations defined on the model.
It is defined as follows:
ADT complex{ data object: D={real, image | real∈real number, image∈real number} [3] Data relationship: R={<real,image>} [3] Basic operations}


Implementation

#include <stdio.h>
#define ok 1;
#define ERROR 0;
typedef int Status;
typedef int ElemType;

typedef struct
{
    
    
	ElemType e[3];
}Triplet;

Status free(Triplet *T)   //数组内容清空
{
    
    
	for (int i = 0; i < 3; i++)
	{
    
    
		T->e[i] = 0;
	}
	return ok;
}
Status InitTriplet(Triplet *T, ElemType v0, ElemType v1, ElemType v2)  //三元数组初始化
{
    
    
//	if (!T) exit(_CRT_GUARDOVERFLOW);
	T->e[0] = v0;
	T->e[1] = v1;
	T->e[2] = v2;
	return ok;
}
Status DestroyTriplet(Triplet *T)  //三原数组销毁
{
    
    
	free(T);
	T = NULL;
	return ok;
}
Status Get(Triplet *T, int i, ElemType *e)    //得到位置i的数组值
{
    
    
	if (i < 0 || i>3)
		return ERROR;
	e = T->e[i - 1];
	return e;
}
Status Put(Triplet *T, int i, ElemType e)    //将第i个位置的数组值赋值给e
{
    
    
	if (i < 0 || i>3)
		return ERROR;
	T->e[i - 1] = e;
	return ok;
}
Status IsAscending(Triplet *T)   //判断数组是否升序排列
{
    
    
	return ((T->e[0] < T->e[1]) && (T->e[1] < T->e[2]));
}
Status IsDescending(Triplet *T)  //判断是否降序排列
{
    
    
	return ((T->e[0] > T->e[1]) && (T->e[1] > T->e[2]));
}
Status Max(Triplet *T, ElemType *e)  //找出数组的最大值
{
    
    
	if (T->e[0] > T->e[1])
	{
    
    
		if (T->e[0] > T->e[2])
		{
    
    
			e = T->e[0];
		}
		else
			e = T->e[2];
	}
	else
	{
    
    
		if (T->e[1] > T->e[2])
		{
    
    
			e = T->e[1];
		}
		else
			e = T->e[2];
	}
	return e;
}
Status Min(Triplet *T, ElemType *e)  //找出数组的最小值
{
    
    
	if (T->e[0] < T->e[1])
	{
    
    
		if (T->e[0] < T->e[2])
		{
    
    
			e = T->e[0];
		}
		else
			e = T->e[2];
	}
	else
	{
    
    
		if (T->e[1] < T->e[2])
		{
    
    
			e = T->e[1];
		}
		else
			e = T->e[2];
	}
	return e;
}
Status ShowTriplet(Triplet *T)  //打印数组
{
    
    
	printf("三元组的值为:\n");
	for (int i = 0; i < 3; i++)
	{
    
    
		
		printf("%d ", T->e[i]);
	}
	return ok;
}
int main()
{
    
    
	Triplet T;
	Status i, flag;
	ElemType v0, v1, v2, e,e1,e2,e3;
	printf("请输入三元组的值:\n");
	scanf_s("%d %d %d", &v0, &v1, &v2);
	getchar();
	flag = InitTriplet(&T, v0, v1, v2);
	printf("初始化函数后,flag=%d\n,", flag);
	printf("三元组中三个值分别为:%d,%d,%d\n", T.e[0], T.e[1], T.e[2]);
	ShowTriplet(&T);
	Put(&T, 2, 5);
	printf("赋值之后的数组为%d %d %d", T.e[0], T.e[1], T.e[2]);
	e1=Get(&T, 1, &e);
	printf("得到数组第一个数字为:%d\n",e1);
	e2=Max(&T, e);
	printf("得到数组最大数字为:%d\n", e2);
	e3=Min(&T, e);
	printf("得到数组最小数字为:%d\n", e3);
	printf("判断三元组是否为升序:%d\n", IsAscending(&T));
	printf("判断三元组是否为降序:%d\n", IsDescending(&T));
	DestroyTriplet(&T);
	printf("三元组内容为:%d %d %d", T.e[0], T.e[1], T.e[2]);
	getchar();
	return 0;

}

Demonstration results
As shown in the figure is the demonstration result, enter the value of the triple array, and the system will perform the following operations on it. Some people may have questions about why the triplet ascending order is also 0. The reason why it is also 0 is because the second one has been modified in the middle. The value of the array element has been changed to 5, so the following ascending and descending order judgments also use the modified array elements, so there is no problem.

`

Guess you like

Origin blog.csdn.net/ccjjjjdff/article/details/113542396