嵌入式常见的数据结构


  • 0 引言
  • 1 线性表
    • 1.1 顺序表
      • 1.1.1 定义类型
      • 1.1.2 相关操作
      • 1.1.3 相关操作的实现
    • 1.2 链表
      • 1.2.1 定义类型
      • 1.2.2 相关操作
      • 1.2.3 相关操作的实现
  • 2 栈
    • 2.1 顺序栈
      • 2.1.1 定义类型
      • 2.1.2 相关操作
      • 2.1.3 相关操作的实现
    • 2.2 链栈
      • 2.2.1 定义类型
      • 2.2.2 相关操作
      • 2.2.3 相关操作的实现
  • 3 队列
    • 3.1 顺序队列
      • 3.1.1 定义类型
      • 3.1.2 相关操作
      • 3.1.3 相关操作的实现
    • 3.2 链队列
      • 3.2.1 定义类型
      • 3.2.2 相关操作
      • 3.2.3 相关操作的实现


0 引言

  嵌入式对数据结构要求到不是特别的高,但是在学习操作系统时往往需要理解一些常见的数据结构,并且在面试的时候也会进行适当的考察。因此,本文对嵌入式常见的数据结构进行介绍。
  文章主要分为三部分:线性表,栈,队列。按照这三种数据类型的存储形式又能各自分为两部分:顺序存储结构,链式存储结构。有的数据结构在顺序存储结构中还能分出顺序型和环形(如队列)。有的数据结构在链式存储结构中还能分出单链,双链,循环链(如链表)。
  通过C/C++语言实现上述数据结构的创建,初始化,插入,删除等等基本运算,并将代码贴出以供学习查阅。

1 线性表

1.1 顺序表

1.1.1 定义类型

//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentList
{
	Student Stu[5];
	int Length;
}StudentList;

1.1.2 相关操作

void CreatList(StudentList * & L, Student Stu[], int n);
void InitList(StudentList * & L);
void DestroyList(StudentList * L);
bool ListEmpty(StudentList * L);
int ListLength(StudentList * L);
void ListDisp(StudentList * L);
int LocateElem(StudentList * L, Student E);
bool GetElem(StudentList * L, int i, Student & E);
bool InsertList(StudentList * L, int i, Student E);
bool DeleteList(StudentList * L, int i, Student & E);

1.1.3 相关操作的实现

//0建立顺序表
void CreatList(StudentList * & L, Student Stu[], int n)
{
	int i;
	L = (StudentList *)malloc(sizeof(StudentList));
	for (i = 0; i < n; i++)
		L->Stu[i] = Stu[i];
	L->Length = n;
}

//1初始化顺序表
void InitList(StudentList * & L)
{
	L = (StudentList *)malloc(sizeof(StudentList));
	L->Length = 0;
}

//2销毁顺序表
void DestroyList(StudentList * L)
{
	free(L);
	L = NULL;
}

//3判断顺序表是否为空
bool ListEmpty(StudentList * L)
{
	return (L->Length == 0);
}

//4输出顺序表长度
int ListLength(StudentList * L)
{
	return L->Length;
}

//5输出顺序表内容
void ListDisp(StudentList * L)
{
	printf("Length:%d\n\n", ListLength(L));
	for (int i = 0; i < L->Length; i++)
	{
		printf("Name:%s\n", L->Stu[i].Name);
		printf("Num:%d\n", L->Stu[i].Num);
		printf("Sex:%c\n\n", L->Stu[i].Sex);
	}
}

//6按值搜索顺序表,输出索引
int LocateElem(StudentList * L, Student E)
{
	int i = 0;
	while (i < L->Length && !((L->Stu[i].Name == E.Name) && (L->Stu[i].Num == E.Num) && (L->Stu[i].Sex == E.Sex)))
		i++;
	if (i == L->Length)
		return 0;
	else
		return i+1;
}

//7按索引搜索顺序表,得到元素
bool GetElem(StudentList * L, int i, Student & E)
{
	if (i<1 || i>L->Length)
		return false;
	else
	{
		E = L->Stu[i - 1];
		return true;
	}
}

//8顺序表插入元素
bool InsertList(StudentList * L, int i, Student E)
{
	if (i<1 || i>L->Length + 1)
		return false;
	else
	{
		i--;
		for (int j = (L->Length-1); j >= i; j--)
			L->Stu[j + 1] = L->Stu[j];
		L->Stu[i] = E;
		L->Length++;
		return true;
	}
}

//9顺序表删除元素
bool DeleteList(StudentList * L, int i, Student & E)
{
	if (i<1 || i>L->Length + 1)
		return false;
	else
	{
		i--;
		E = L->Stu[i];
		for (int j = i; j < (L->Length - 1); j++)
			L->Stu[j] = L->Stu[j + 1];
		L->Length--;
		return true;
	}
}

1.2 链表

1.2.1 定义类型

/*************************单链表*************************/
//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentList
{
	Student Stu;
	StudentList * next;
}StudentList;
/*************************单链表*************************/


/*************************双链表*************************/
//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentList
{
	Student Stu;
	StudentList * next;
	StudentList * prior;
}StudentList;
/*************************双链表*************************/

1.2.2 相关操作

void CreatListR(StudentList * & L, Student Stu[], int n);
void InitList(StudentList * & L);
void DestroyList(StudentList * L);
bool ListEmpty(StudentList * L);
int ListLength(StudentList * L);
void ListDisp(StudentList * L);
int LocateElem(StudentList * L, Student E);
bool GetElem(StudentList * L, int i, Student & E);
bool InsertList(StudentList * L, int i, Student E);
bool DeleteList(StudentList * L, int i);

1.2.3 相关操作的实现

/*************************单链表*************************/
//0尾插法建立单链表
void CreatListR(StudentList * & L, Student Stu[], int n)
{
	StudentList * pre = NULL;
	StudentList * p = NULL;
	int i;
	L = (StudentList *)malloc(sizeof(StudentList));
	pre = L;
	for (i = 0; i < n; i++)
	{
		p = (StudentList *)malloc(sizeof(StudentList));
		p->Stu = Stu[i];
		pre->next = p;
		pre = pre->next;
	}
	p->next = NULL;
}

//1初始化单链表
void InitList(StudentList * & L)
{
	L = (StudentList *)malloc(sizeof(StudentList));
	L->next = NULL;
}

//2销毁单链表
void DestroyList(StudentList * L)
{
	StudentList * pre = L, * p = L->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}

//3判断单链表是否为空
bool ListEmpty(StudentList * L)
{
	return(L->next == NULL);
}

//4输出单链表长度
int ListLength(StudentList * L)
{
	StudentList * p = L->next;
	int i = 0;
	while (p != NULL)
	{
		i++;
		p = p->next;
	}
	return i;
}

//5输出单链表内容
void ListDisp(StudentList * L)
{
	StudentList * p = L->next;
	printf("Length:%d\n\n", ListLength(L));
	while (p != NULL)
	{
		printf("Name:%s\n", p->Stu.Name);
		printf("Num:%d\n", p->Stu.Num);
		printf("Sex:%c\n\n", p->Stu.Sex);
		p = p->next;
	}
	printf("\n");
}

//6按值搜索单链表,输出索引
int LocateElem(StudentList * L, Student E)
{
	StudentList * p = L;
	int i = 0;
	while (p != NULL && (!((p->Stu.Name == E.Name) && (p->Stu.Num == E.Num) && (p->Stu.Sex == E.Sex))))
	{
		i++;
		p = p->next;
	}
	if (p == NULL)
		return 0;
	else
		return i;
}

//7按索引搜索单链表,得到元素
bool GetElem(StudentList * L, int i, Student & E)
{
	StudentList * p = L;
	int n = 0;
	while ((p != NULL) && (n < i))
	{
		n++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		E = p->Stu;
		return true;
	}
}

//8单链表插入元素
bool InsertList(StudentList * L, int i, Student E)
{
	StudentList * p = L;
	StudentList * q = NULL;
	int n = 0;
	while ((p != NULL) && (n < i - 1))
	{
		n++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		q = (StudentList *)malloc(sizeof(StudentList));
		q->Stu = E;
		q->next = p->next;
		p->next = q;
		return true;
	}
}

//9单链表删除元素
bool DeleteList(StudentList * L, int i)
{
	StudentList * p = L;
	StudentList *q = NULL;
	int n = 0;
	while ((p != NULL) && (n < i - 1))
	{
		n++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		q = p->next;
		if (q == NULL)
			return false;
		else
		{
			p->next = p->next->next;
			free(q);
			return true;
		}
	}
}
/*************************单链表*************************/


/*************************双链表*************************/
//0尾插法建立双链表
void CreatListR(StudentList * & L, Student Stu[], int n)
{
	StudentList * pre = NULL;
	StudentList * p = NULL;
	int i;
	L = (StudentList *)malloc(sizeof(StudentList));
	pre = L;
	pre->prior = NULL;
	for (i = 0; i < n; i++)
	{
		p = (StudentList *)malloc(sizeof(StudentList));
		p->Stu = Stu[i];
		pre->next = p;
		p->prior = pre;
		pre = pre->next;
	}
	p->next = NULL;
}

//1初始化双链表
void InitList(StudentList * & L)
{
	L = (StudentList *)malloc(sizeof(StudentList));
	L->next = NULL;
	L->prior = NULL;
}

//8双链表插入元素
bool InsertList(StudentList * L, int i, Student E)
{
	StudentList * p = L;
	StudentList * q = NULL;
	int n = 0;
	while ((p != NULL) && (n < i - 1))
	{
		n++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		q = (StudentList *)malloc(sizeof(StudentList));
		q->Stu = E;
		q->next = p->next;
		if (p->next != NULL)
			p->next->prior = q;
		p->next = q;
		q->prior = p;
		return true;
	}
}

//9双链表删除元素
bool DeleteList(StudentList * L, int i)
{
	StudentList * p = L;
	StudentList *q = NULL;
	int n = 0;
	while ((p != NULL) && (n < i - 1))
	{
		n++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		q = p->next;
		if (q == NULL)
			return false;
		else
		{
			p->next = q->next;
			if (q->next != NULL)
				q->next->prior = p;
			free(q);
			return true;
		}
	}
}
/*************************双链表*************************/


2 栈

2.1 顺序栈

2.1.1 定义类型

#define MaxSize 5

//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentStack
{
	Student Stu[MaxSize];
	int top;
}StudentStack;

2.1.2 相关操作

void InitStack(StudentStack * & S);
void DestroyStack(StudentStack * S);
bool StackEmpty(StudentStack * S);
bool PushStack(StudentStack * &S, Student E);
bool PopStack(StudentStack * &S, Student & E);
bool GetTop(StudentStack * S, Student & E);

2.1.3 相关操作的实现

//1初始化栈
void InitStack(StudentStack * & S)
{
	S = (StudentStack *)malloc(sizeof(StudentStack));
	S->top = -1;
}

//2销毁栈
void DestroyStack(StudentStack * S)
{
	free(S);
}

//3判断栈是否为空
bool StackEmpty(StudentStack * S)
{
	return(S->top == -1);
}

//4压栈
bool PushStack(StudentStack * &S, Student E)
{
	if (S->top == (MaxSize - 1))
		return false;
	else
	{
		S->top++;
		S->Stu[S->top] = E;
		return true;
	}
}

//5退栈
bool PopStack(StudentStack * &S, Student &E)
{
	if (S->top == -1)
		return false;
	else
	{
		E = S->Stu[S->top];
		S->top--;
		return true;
	}
}

//6取栈顶元素
bool GetTop(StudentStack *S, Student &E)
{
	if (S->top == -1)
		return false;
	else
	{
		E = S->Stu[S->top];
		return true;
	}
}

2.2 链栈

2.2.1 定义类型

//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentStack
{
	Student Stu;
	StudentStack * next;
}StudentStack;

2.2.2 相关操作

void InitStack(StudentStack * & S);
void DestroyStack(StudentStack * S);
bool StackEmpty(StudentStack * S);
void PushStack(StudentStack * &S, Student E);
bool PopStack(StudentStack * &S, Student & E);
bool GetTop(StudentStack * S, Student & E);

2.2.3 相关操作的实现

//1初始化栈
void InitStack(StudentStack * & S)
{
	S = (StudentStack *)malloc(sizeof(StudentStack));
	S->next = NULL;
}

//2销毁栈
void DestroyStack(StudentStack * S)
{
	StudentStack * pre = S, *p = S->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}

//3判断栈是否为空
bool StackEmpty(StudentStack * S)
{
	return(S->next == NULL);
}

//4压栈
void PushStack(StudentStack * &S, Student E)
{
	StudentStack * p = NULL;
	p = (StudentStack *)malloc(sizeof(StudentStack));
	p->Stu = E;
	p->next = S->next;
	S->next = p;
}

//5退栈
bool PopStack(StudentStack * &S, Student &E)
{
	StudentStack * p = NULL;
	if (S->next == NULL)
		return false;
	else
	{
		p = S->next;
		E = p->Stu;
		S->next = p->next;
		free(p);
		return true;
	}
}

//6取栈顶元素
bool GetTop(StudentStack * S, Student & E)
{
	if (S->next == NULL)
		return false;
	else
	{
		E = S->next->Stu;
		return true;
	}
}

3 队列

3.1 顺序队列

3.1.1 定义类型

#define MaxSize 5

//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentQueue
{
	Student Stu[MaxSize];
	int front;
	int rear;
}StudentQueue;

3.1.2 相关操作

void InitQueue(StudentQueue * & Q);
void DestroyQueue(StudentQueue * & Q);
bool QueueEmpty(StudentQueue * Q);
bool EnQueue(StudentQueue *Q, Student E);
bool DeQueue(StudentQueue *Q, Student &E);

3.1.3 相关操作的实现

/*************************顺序队列*************************/
//1初始化队列
void InitQueue(StudentQueue * & Q)
{
	Q = (StudentQueue *)malloc(sizeof(StudentQueue));
	Q->front = -1;
	Q->rear = -1;
}

//2销毁队列
void DestroyQueue(StudentQueue * & Q)
{
	free(Q);
}

//3判断队列是否为空
bool QueueEmpty(StudentQueue * Q)
{
	return(Q->rear == Q->front);
}

//4进队列
bool EnQueue(StudentQueue *Q, Student E)
{
	if ((Q->rear + 1) == (MaxSize - 1))
		return false;
	else
	{
		Q->rear++;
		Q->Stu[Q->rear] = E;
		return true;
	}
}

//5出队列
bool DeQueue(StudentQueue *Q, Student &E)
{
	if (Q->rear == Q->front)
		return false;
	else
	{
		Q->front++;
		E = Q->Stu[Q->front];
		return true;
	}
}
/*************************顺序队列*************************/



/*************************环形队列*************************/
//1初始化队列
void InitQueue(StudentQueue * & Q)
{
	Q = (StudentQueue *)malloc(sizeof(StudentQueue));
	Q->front = 0;
	Q->rear = 0;
}

//2销毁队列
void DestroyQueue(StudentQueue * & Q)
{
	free(Q);
}

//3判断队列是否为空
bool QueueEmpty(StudentQueue * Q)
{
	return(Q->rear == Q->front);
}

//4进队列
bool EnQueue(StudentQueue *Q, Student E)
{
	if ((Q->rear + 1) % MaxSize == Q->front)
		return false;
	else
	{
		Q->rear = (Q->rear + 1) % MaxSize;
		Q->Stu[Q->rear] = E;
		return true;
	}
}

//5出队列
bool DeQueue(StudentQueue *Q, Student &E)
{
	if (Q->rear == Q->front)
		return false;
	else
	{
		Q->front = (Q->front + 1) % MaxSize;
		E = Q->Stu[Q->front];
		return true;
	}
}
/*************************环形队列*************************/

3.2 链队列

3.2.1 定义类型

#define MaxSize 5

//以Student举例
typedef struct Student
{
	int Num;
	char Sex;
	char * Name;
}Student;

typedef struct StudentNode
{
	Student Stu;
	StudentNode * next;
}StudentNode;

typedef struct StudentQueue
{
	StudentNode * front;
	StudentNode * rear;
}StudentQueue;

3.2.2 相关操作

void InitQueue(StudentQueue * & Q);
void DestroyQueue(StudentQueue * & Q);
bool QueueEmpty(StudentQueue * Q);
void EnQueue(StudentQueue *Q, Student E);
bool DeQueue(StudentQueue *Q, Student &E);

3.2.3 相关操作的实现

//1初始化队列
void InitQueue(StudentQueue * & Q)
{
	Q = (StudentQueue *)malloc(sizeof(StudentQueue));
	Q->front = NULL;
	Q->rear = NULL;
}

//2销毁队列
void DestroyQueue(StudentQueue * & Q)
{
	StudentNode *pre = Q->front;
	StudentNode *p;
	if (pre != NULL)
	{
		p = pre->next;
		while (p != NULL)
		{
			free(pre);
			pre = p;
			p = p->next;
		}
		free(pre);
	}
	free(Q);
	Q = NULL;
}

//3判断队列是否为空
bool QueueEmpty(StudentQueue * Q)
{
	return(Q->rear == NULL);
}

//4进队列
void EnQueue(StudentQueue *Q, Student E)
{
	StudentNode *p = NULL;
	p = (StudentNode *)malloc(sizeof(StudentNode));
	p->Stu = E;
	p->next = NULL;
	if (Q->rear == NULL)
	{
		Q->front = p;
		Q->rear = p;
	}
	else
	{
		Q->rear->next = p;
		Q->rear = p;
	}
}

//5出队列
bool DeQueue(StudentQueue *Q, Student &E)
{
	StudentNode *p = NULL;

	if (Q->rear == NULL)
		return false;
	else
	{
		p = Q->front;
		if (Q->rear == Q->front)
		{
			Q->rear = NULL;
			Q->front = NULL;
		}
		else
			Q->front = p->next;
		E = p->Stu;
		free(p);
		return true;
	}
}



你可能感兴趣的:(嵌入式学习)