Begin to sort out the university curriculum system (2) -- a summary of the data structure of the word

Chapter 1 Overview

Today's computers store and process data with a certain structure such as characters, tables, and images. However, to distinguish the internal relationship of data, organize data reasonably, and process data efficiently, this is the main research problem of "data structure".

1.1 Basic Concepts and Terminology

  • data
    It is a symbolic representation of objective things, and it is a general term for all symbols that can be input into a computer and processed by a computer program.

  • data element
    Is the basic unit of data, usually considered and processed as a subject in computers. In some cases, data elements are also called elements, records.

  • data item
    It is the smallest unit that makes up data elements, has independent meaning, and is indivisible.

  • data object
    It is a collection of data elements with the same nature and is a subset of data.

1.2 Data structure

A data structure is a collection of data elements that have one or several specific relationships with each other. In this way, a data structure is a collection of data elements with a "structure", and a "structure" is the relationship between data elements. Structure can be divided into logical structure and physical structure.

1. 2.1 Logical structure

The logical structure of data has two elements: one is the data element, and the other is the relationship. The meaning of the data elements As mentioned earlier, the relationship refers to the relationship between data elements.
There are generally four basic types of structures:

  1. set structure
  2. linear structure
  3. tree structure
  4. Graph structure or network structure
    Among them, set structure, tree structure and graph structure are nonlinear structures.

1.2.2 Storage structure

The storage representation of data objects in the computer is called the data storage structure, also known as the physical structure.

  1. sequential storage structure
  2. Chain storage structure

1.3 Data types and abstract data types

1.3.1 Data types

Data type is a basic concept in high-level programming language, which is a general term for a set of values ​​and a set of operations defined on this set of values.
Data types reflect the data description and processing capabilities of programming languages.

1.3.2 Abstract data types

Abstraction is to extract the essence of the actual problem.
The abstract data type generally refers to the mathematical model defined by the user, which represents the title of the application, and the general term for a set of operations defined on this model. Specifically, it includes three parts: data objects, a collection of relationships on data objects, and a collection of basic operations on data objects.

ATD 抽象数据类型名{
    
    
	数据对象:<数据对象的定义>
	数据关系:<数据关系的定义>
	基本操作:<基本操作的定义>
}ATD 抽象数据类型名

1.4 Algorithm and Algorithm Analysis

When it comes to operations, there is always a connection between the objects processed by the algorithm and the resulting data. In order to describe certain operations of practice, it is often necessary to design algorithms, so algorithms are an important way to study data structures.

1.4.1 Definition and characteristics of the algorithm

algorithmIt is a finite sequence of operations prescribed to solve a certain type of problem.
Five characteristics:

  1. Infinity
  2. Certainty
  3. feasibility
  4. enter
  5. output

1.4.2 Basic criteria for evaluating the pros and cons of algorithms

  1. Accuracy: Accurate results can be obtained within a limited running time with reasonable data input.
  2. Readability: easy for people to understand and communicate with each other, easy to debug and modify
  3. Robustness: when the input data is illegal, it can react accurately or process it
  4. Efficiency: Efficiency includes two aspects of time and space

1.4.3 Time complexity of the algorithm

The purpose of algorithm efficiency analysis is to see whether the algorithm is actually feasible, and to compare the time and space performance among multiple algorithms for the same problem, so as to select the more worrying algorithm.

  1. The scale of the problem is the amount of input for the algorithm to solve the problem, which is the essence of the problem size.
  2. Sentence frequency refers to the number of times a sentence is repeated.
    In general, the number of repeated executions of the basic statement in the algorithm is a function f(n) of the problem size n, and the time measurement of the algorithm is recorded as:
    T(n) = O(f(n))
    means that with the problem size n Increase, the growth rate of the algorithm execution time is the same as the growth rate of f(n), which is called the asymptotic time complexity of the algorithm, or time complexity for short.
    The time complexity of the algorithm in the best case is called the best time complexity, which means that the calculation amount of the algorithm may reach the minimum value; the time complexity of the algorithm in the worst case
    is called the worst time complexity, which means What is the calculation amount of the algorithm is the maximum value;

1.4.4 Space complexity of the algorithm

Similar to the time complexity of the algorithm, the asymptotic space complexity is used as the measure of the storage space required by the algorithm, referred to as the space complexity. Write it as:
S(n) = O(f(n))
In general, when a program is executed on the machine, in addition to registering its own instructions, constant
variables and input data, it also needs some operations on the data. Secondary storage space.


Chapter 2 Linear Table

The basic characteristic of the linear structure is that except the first element has no direct predecessor and the last element has no direct successor, all other elements have a predecessor and a successor.

2.1 Definition of linear table

A finite sequence composed of n(n>0) elements with the same data characteristics is called a linear table.
The characteristics are:

  1. There is only one data element called "first"
  2. There is only one data element called "last"
  3. Each data element in the structure has exactly one predecessor, except the first
  4. Every data element in the structure except the last has exactly one successor

2.2 Sequential representation and realization of linear tables

The sequential representation of the linear table refers to storing the data elements of the linear table sequentially with a group of storage units with continuous addresses, which is also called the sequential storage structure or sequential image of the linear table. The characteristic of the linear table is that the logically adjacent data elements are also adjacent in physical order

//顺序表的存储结构
#define MAXSIZE 100
typedef struct
{
    
    
	ElemType *elem;    //存储空间的基地址
	int length;       //当前长度
}SqList;              //顺序表的结构类型为SqList

2.2.1 Implementation of basic operations in the sequence table

  1. Initialization
    The initialization operation of the sequence table is to construct an empty sequence table
    1. Dynamically allocate an array space of a predefined size for the sequence table L, and make elem point to the base address of this space
    2. Set the current length of the table to 0
Status InitLIst(SqList & L)
{
    
     //构造一个空的顺序表
	L.elem = new ElemType[MAXSIZE];     //为顺序表分配一个大小为MAXSIZE的数组空间、
	if(!elem) exit (OVERFLOW);         //存储失败退出
	L.length = 0;                     //空表长度为0
	return ok;
}
  1. Get
    value Get the value of the i-th data element in the sequence table according to the specified position number i during the value-taking operation
    1. Determine whether the value of the specified position number i is reasonable (1 <= i <= L.length), if not, return ERROR
    2. If the value of i is reasonable, assign the i-th data element L.elem[i-1] to the parameter e, and return the passed value of the i-th element through e.
Status GetElem(SqList L,int i,ElemType &e)
{
    
    
	if (i<1 || i>L.length) return ERROR;    //判断i值是否合理,
	e = L.elem[i-1];                       //elem[i-1]单元存储第i个数据元素
	return ok;
}
  1. Search
    During the search operation, according to the specified element value e, the first element in the sequence table that is equal to e is searched. If the search is successful, the position number of the element in the table is returned.
    1. Repeat the first element and compare it with e1 in turn. If an element L.elem[i] equal to e1 is found, the search is successful, and the serial number i + 1 of the element is returned
    2. If the sequence table is not found, it will return 0
int LocatElem(SqList L,ElemType e)
{
    
    
	for(i=0;i<length;i++)
		if(L.elem[i == e)  rteurn i+1;
	return o;
}
  1. Insert
    Insert a new data element e at the i-th position in the table, so that a linear table of length n becomes a linear table of length n+1
    1. Determine whether the insertion position i is legal, (1<= i <= n+1), if not, return ERROR,
    2. Determine whether the storage space of the sequence table is full, if it is full, return ERROR,
    3. Move the element from the nth element to the i-th position backward one position at a time, leaving the i-th position empty
    4. Put the new element e to be inserted into the i-th position
    5. Table extension 1
Status ListInser(SqList &L,int i,ElemType e)
{
    
    
	if((i<1)||(i>L.length+1)) return ERROR;   //i值不合法
	if(L.length == MAXSIZE) return ERROR;    //当前存储空间已满
	for(j=L.length-1;j>=i-1;j--)
		L.elem[j+1] =L.elem[j];    //插入位置及之后的元素后移
	L.elem[i-1e;                  //将新元素e放入第i个元素
	++L.length;                   //表长加1
	return ok;
}
  1. Delete
    The delete operation is to delete the i-th element of the table, and change the linear table of length n into a linear table of length n-1
    1. Determine whether it is legal to delete position i
    2. Move the i+1th to nth elements one position forward in sequence
    3. table length minus 1
Status ListDelete(SqList &L,int i)
{
    
    
	if((i<1)|| (i>L.length)) return ERROR;
	for(j=i;j<=.length-1;j++)
		L.elem[j-1] = L.elem[j];
	--L.elemth;
	return ok;
}

2.3 Chain representation and realization of linear table

2.3.1 Definition and representation of singly linked list

The characteristics of the linear list chain storage structure: use a group of arbitrary storage units to store the data elements of the linear list.

  1. Initialize
    constructs an empty table
    1. Generate a new node as the head node, and use the head pointer L to point to the head node
    2. The pointer field of the head node is empty
Status IntList(LinkList &L)
{
    
    
	L = new LNode;
	L->next = NULL;   //生成新结点作为头结点
	return ok;         //头结点的指针置空
}
  1. Different from the
    sequence list, logically adjacent nodes in the linked list are not stored in physically adjacent units, so you can only start from the head node of the linked list and follow the chain field next to access downwards.
    1. Use the pointer p to point to the head node, use j as the counter initialization and assign it to 1
    2. Starting from the head node, visit down the chain field sequentially, as long as the pointer p of the current node is not empty, the operation will be performed in a loop.
    3. exit the loop.
Status GetElem(LinkList L,int i, ElemType &e)
{
    
    
	p = L->next ;           //初始化,p指向首元结点,
	j=1;                 //计数器j初值赋为1
	while(p&&j<i)       //顺链域向后扫描,直到p为空或p指向第i个元素
	{
    
    
		p=p->next;     //p指向下一个节点
		++j;          //计数器加1
	}
	if(!p||j>i) return ERROR;  //i值不合法i>n或i<0
	e = p->data;               //去第i个节点的数据域
	return ok;
}
  1. Search
    Start from the head node of the linked list, compare the node value with the specified value e in turn, and return the search result
    1. Use the pointer p to point to the head node
    2. Starting from the first node, search down the chain domain next in turn, as long as the p part is empty, it will keep looping.
    3. return p
LNode *LocateElem(LinkList L,ElemType e)
{
    
    
	p = L->next;       //初始化,p指向首元结点
	while(p && p->data!=e)   //顺着链域扫描,直到p为空或p所指的的数据域等于e
		p=p->next;            //p指向下一个结点
	return p;
}
  1. Insert
    s->next = p->next; p->next=s
    1. Find node a(i-1) and point to it by pointer p.
    2. Generate a new node *s
    3. Set the pointer field of the new node *s to e
    4. Point the pointer field of the new node *s to node a(i)
    5. Point the pointer field of node p1 to the new node s
Status ListInsert(LinkList &L,int i ,ElemType e)
{
    
    
	p=L;j=0;
	while(p&& (j<i-1))
	{
    
    
		p = p ->next;   //查找第i-1个结点,p指向该结点
		++j;
	}
	if(!p || j>i-1)  return ERROR;   //i > n+1 或者 i<1
 	s = new LNode;                  //生成新结点*s
	s ->data = e;                   // 将结点*s 的数据域置为e
	s ->next = p->next;             //将结点*s的数据域指向结点a(i)
	p ->next =s;                 //将结点*p的指针域指向结点*s
	return ok;
}
  1. Deletion
    is the same as insertion, first find the predecessor node at this position, and modify the statement as follows:
    p ->next = p ->next ->next;
    1. Find node a(i)-1 and point to it by pointer p
    2. Temporarily save the address of the node a(i) to be deleted in q for release
    3. Point the pointer field of node *p to the direct successor node of a(i)
    4. Free up space in node ah(i)
 Status ListDelete (LinkList &L,int i)
	{
    
    
		p = L;j=0;
		while((p ->next) && (j<i-1))          //查找第i-1个结点,p指向该结点
		{
    
    
			p =p ->next; ++j;
		}
		if(!
		(p ->next) || (j>i-1))  rteurn ERROR;
		q = p ->next;
		delete q;
		return ok;
	}
  1. Creating a singly linked list
    According to the different insertion positions of the nodes, the creation method of the linked list can be divided into forward insertion method and back insertion
    method
    . n times of execution operations: generate a new node, p, assign the input element to the data field of the new node p, and insert the new node p after the head node.
    Post-interpolation method: Create an empty linked list with only the head node, initialize the tail pointer r to point to the head node, and perform the operation n times according to the number n of elements included in the linked list: generate a new node p, and assign the input element
    to The data field of the new node p, insert the new node p into the tail node, and the tail pointer r points to the new tail node *p

2.3.2 Circular linked list

Circular linked list is another form of linked storage structure. Its characteristic is that the pointer field of the last node in the list points to the head node, and the whole linked list forms a ring. The statement: p = B ->next ->next;
B
- >next = A ->next;
A ->next =p;
circular linked list

2.3.3 Doubly linked list

There are two pointer fields in the node of the doubly linked list, one points to the immediate successor and the other points to the immediate predecessor
Doubly linked list

2.4 Comparison between sequence list and linked list

compare items sequence table linked list
storage Pre-allocated, will lead to space idle or overflow phenomenon Dynamic allocation, will not cause space idle or overflow phenomenon
storage density There is no need to add additional storage overhead to represent the logical relationship between nodes, and the storage density is equal to 1 Pointers are needed to reflect the logical relationship between elements, and the storage density is less than 1
storage element Random access, the time complexity of accessing elements by position is O(1) Sequential storage, the time complexity of accessing elements by position is O(n)
insert, delete Move about half of the elements in the list on average, and the time complexity is O(n) There is no need to move elements, and the time complexity after determining the insertion and deletion positions is O(1)
Applicable situation The table does not change much, and the scope of the change can be determined in advance; insert or delete operations are rarely performed, and data elements are often accessed according to the element position ordinal number. The length changes greatly, and frequent insertion or deletion operations

Chapter 3 Definition and Characteristics of Stack and Queue

3.1 Definition and characteristics of stack

The stack is a linear table limited to insert and delete operations only at the end of the table. The tail of the table is called the top of the stack, and the head of the table is called the tail of the stack. The modification of the stack is carried out according to the principle of first in first out.

3.2 Representation and Implementation of Sequential Stack

The sequential stack refers to the stack realized by using the sequential storage structure, that is, a group of storage units with continuous addresses are used to store the data elements from the bottom of the stack to the top of the stack in sequence, and a pointer top is attached to indicate the position of the top element in the sequential stack.

  1. Initialization
    dynamically allocates an array space of a predefined size for the sequential stack
    1. Dynamically allocate an array space with a capacity of MAXSIZE for the sequential stack, and make base point to the base address of this space, that is, the bottom of the stack
    2. The top pointer top of the stack is initialized to base, indicating that the stack is empty
    3. Set as the stack space capacity MAXSIZE
Statys IntStack(SqStack &s)
{
    
    
		s.base = new SElemTyoe[MAXSIZE];   //动态分配数组
		if(!S.base) exit(OVERFLOW);    //存储动态失败
		S.top = S.base;              //top初始为base,空栈
		S.stacksize = MAXSIZE;    //栈的容量为,MAXSIZE
		return ok;
}
  1. PUSH
    Inserts a data element at the top of the stack
    1. Determine whether it is full, if it is full, return ERRROR
    2. Push the new element onto the top of the stack, and increment the top pointer of the stack by 1
Status Push(SqStack &S, SElemType e)
{
    
    
 	if(S.top-S.base == S.atacksize)  ERROR;
 	*S.top++=;
 	return ok;
 }	
  1. Pop
    means to delete the top element of the stack
    1. Check if the stack is empty,
    2. The top pointer of the stack is decremented by 1, and the top element is popped out of the stack
Status Pop(SqStack &S,SElemTyep &e)
{
    
    
	if(S.top ==S.base) return ERROR;
	e = *--S.top;
	return ok;
}
  1. Get stack elements
SElemType GetTop(SqStack S)
{
    
    
	if(S.top!= S.base)
		return *(S.top-1);
}

3.3 Representation and implementation of chain stack

A stack implemented using a chained storage structure.

  1. Initialization
    is to construct an empty stack.
Status IntStack(LinkStack &s)
{
    
    
	S= NULL;
	return ok;
}
  1. There is no need to judge whether the stack is full before the chain
    stack is pushed into the stack, only need to dynamically allocate a node space,
    1. Allocate space for the stacked element e, pointing to it with the pointer p
    2. Set the new node data field to e
    3. Insert a new node on top of the stack
    4. Modify the top pointer of the stack to p
Stataus Push(LinkStack &S,SElemType e)
{
    
    
	p = new StackNode;    //生成新结点
	p -> data =e;        //将新结点数据域置为e
	p ->next = S;        //将新结点插入栈顶
	S = p;             //修改栈顶元素指针为p
	return ok;
}
  1. pop out
    1. Check if the stack is empty
    2. Assign the top element of the stack to e
    3. Temporarily save the space of the top element of the stack for release
    4. Modify the top pointer of the stack to point to the new top element of the stack
    5. Release the space of the original top element of the stack
Stataus Push(LinkStack &S,SElemType e)
{
    
    
	if(S == NULL)  RETURN ERROR;
	e = S ->data;
	p = S ;
	S =S ->next;
	delete p;
	return ok;
}

ps: Now I will write this first, and I will continue to complete the writing work later. Thank you for coming in to learn and communicate.

Guess you like

Origin blog.csdn.net/m0_58353740/article/details/130740406