顺序表、堆栈、队列的代码区别详解

首先感谢@万物皆为二叉树的文章,让我茅塞顿开

https://blog.csdn.net/qq_45849625/article/details/115709622​​​​​​​

创建一个长度为length的线性表:ListType* CreateList(int length);

销毁线性表:void DeleteList(ListType* pList);

置空线性表:void ClearList(ListType* pList);

检测线性表是否为空:int IsEmptyList(ListType* pList);

获取线性表长度:int GetListLength(ListType* pList);

获取线性表第n个元素:int GetListElement(ListType* pList,int n,DataType* data);

从pos起查找data第一次出现的位置:int FindElement(ListType* pList,int pos,DataType data);

获取线性表中第n个元素的前驱:int GetPriorElement(ListType*pList,int n,DataType* data);

获取线性表中第n个元素的后继:int GetNextElement(ListType*pList,int n,DataType* data);

将data插入到pos处:int InsertToList(ListType*pList,int pos,DataType data);

删除线性表上位置为pos的元素:int DeleteFromList(ListType* pList,int pos);

输出线性表:void PrintList(ListType*pList);

一、结构体的区别

1.顺序表

typedef int DataType;
typedef struct List
{
	DataType* list;  //指向线性表的指针
	int length;      //表长
	int maxLength;   //表容量
}ListType;

2.堆栈

typedef char DataType;
typedef struct STACK
{
	DataType* stackArray;  //存储元素的空间指针
	int top;  //栈顶指针,存储栈顶元素的下标
	int maxLength;  //堆栈最大可分配空间数量
}Stack;

3.队列

typedef char DataType;
typedef struct QUEUE
{
	DataType* QueueArray;
	int front;  //队头
	int rear;   //队尾
	int maxLength;  //队的最大容量
}Queue;

二、创建线性表,堆栈,队列

1.线性表

ListType* CreateList(int length)
{
	ListType* sqList = (ListType*)malloc(sizeof(ListType));
	if (sqList != NULL)
	{
		sqList->list = (DataType*)malloc(sizeof(DataType) * length);
		if (sqList->list != NULL)
		{
			sqList->length = 0;  //置为空表
			sqList->maxLength = length;  //最大长度
		}
	}
	return sqList;
}

2.堆栈

Stack* CreateStack(int length)
{
	Stack* stack = (Stack*)malloc(sizeof(Stack));
	if (stack != NULL)
	{
		stack->StackArray = (DataType*)malloc(sizeof(DataType) * length);
		if (stack->StackArray == NULL)
		{
			return NULL;
		}
		//改变结构体里的内容需要指针!
		stack->top = -1;  //置于空栈
		stack->maxLength = length;
	}
	return stack;
}

3.队列

Queue* CreateQueue(int length)  //输入的参数必须加上参数类型
{
	Queue* queue = (Queue*)malloc(sizeof(Queue));
	if (queue != NULL)
	{
		queue->QueueArray = (DataType*)malloc(sizeof(DataType) * length);
		if (queue->QueueArray == NULL)
		{
			return NULL;
		}
		queue->front = 0;
		queue->rear = 0;
		queue->maxLength = length;
	}
	return queue;
}

三、插入数据

1、顺序表

void InsertToList(ListType* pList,DataType data,int pos)
{
	if (pos<0 || pos>pList->length || pList->length == pList->maxLength)
	{
		return;
	}
	for (int i = pList->length - 1; i > =pos; i--)
	{
		pList->list[i + 1] = pList[i];
	}
	pList->list[pos] = data;
	pList->length = pList->length + 1;
}

2、堆栈

void PushToStack(Stack* stack, DataType data)
{
	if (stack->top < stack->maxLength - 1)
	{
		stack->top = stack->top + 1;
		stack->stackArray[stack->top] = data;
	}
}

3.队列

void InsertToQueue(Queue* queue, DataType data)
{
	if ((queue->rear + 1) % queue->maxLength == queue->front)
	{
		printf("队列已满");
	}
	else
	{   //队尾向后移动一位
		queue->rear = (queue->rear + 1)%queue->maxLength;
		queue->queueArray[queue->rear] = data;  //入队
	}
}

四、删除数据

1、线性表

void DeleteFromList(ListType* pList, int pos)
{
	if (pos >= 0 || pos < pList->length - 1)
	{
		for (int i = pos; i < pList->length-1; i++)
		{
			pList->list[n] = pList->list[n + 1];
		}
		pList->length = pList->length - 1;
	}
}

2、栈

void Pop(Stack* stack,DataType data)
{
	if (stack->pop >-1)
	{
		data=stack->stackArray[stack->top]  //结构体的变量需要指针引用
		stack->pop = stack->pop - 1;
	}
}

3、队列

DataType DlFromQueue(Queue* queue)  //出队
{
	if (queue->rear == queue->front)  //队空条件
	{
		return -1;
	}
	else 
	{
		queue->front = (queue->front + 1) % queue->maxLength;//注意指针引用!
		return queue->queueArray[queue->front];
	}
}

五、获取表长

1、线性表

int GetListLength(ListType* pList)
{
	return pList->length;
}

2、堆栈

int GetLinkLength(Stack* stack)
{
	return stack->top +1;  //注意这里是+1
}

3、队列

int GetLinkLength(Queue* queue)
{
	int length;
	if (queue->rear >= queue->front)
	{
		length = queue->rear - queue->front;
	}
	else
	{
		length = queue->length - queue->front + queue->maxLength;
	}
	return length;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挣钱钱暴富富

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值