静态顺序表的一些操作

静态顺序表.h
#ifndef _SHUNXUBIAO_H_
#define _SHUNXUBIAO_H_
#define MAX_SIZE 10
typedef int DataType;

//静态顺序表
typedef struct SeqList
{
	DataType array[MAX_SIZE];
	int size;//表示顺序表中有效元素的个数
}SeqList, *PSeqList;

void InitSeqList(PSeqList pSeq);//初始化顺序表
void SeqListPushBack(PSeqList pSeq, DataType data);//向顺序表中最后一个元素后面插入新元素(尾插)
void SeqListPopBack(PSeqList pSeq);//尾删
void SeqListPushFront(PSeqList pSeq, DataType data);//头插
void SeqListPopFront(PSeqList pSeq);//头删
void SeqListInsert(PSeqList pSeq, int pos, DataType data);//在顺序表的pos位置插入元素data
void SeqListErase(PSeqList pSeq, int pos);//删除顺序表pos位置的元素

#endif

text.c
#include "静态顺序表.h"
#include <assert.h>

//初始化顺序表
void InitSeqList(PSeqList pSeq)
{
	assert(pSeq);
	pSeq->size = 0;
}

//尾插
void SeqListPushBack(PSeqList pSeq, DataType data)
{
	assert(pSeq);
	if (MAX_SIZE == pSeq->size)
	{
		printf("顺序表已经满了\n");
		return;
	}
	pSeq->array[pSeq->size++] = data;      
}

//尾删
int Empty(PSeqList pSeq)
{
	assert(pSeq);
	return pSeq->size == 0;
}
void SeqListPopBack(PSeqList pSeq)
{
	assert(pSeq);
	if (Empty(pSeq))
	{
		printf("顺序表已为空\n");
		return;
	}
	pSeq->size--;
}

//头插
//步骤:1.参数检测   2.判满   3.搬移元素
void SeqListPushFront(PSeqList pSeq, DataType data)
{
	int i = 0;
	assert(pSeq);
	if (MAX_SIZE == pSeq->size)
	{
		printf("顺序表已经满了\n");
		return;
	}
	for (i = pSeq->size; i > 0; --i)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}
	pSeq->array[0] = data;
	pSeq->size++;
}

//头删
//步骤:1.参数检测   2.判空   3.往前搬移元素
void SeqListPopFront(PSeqList pSeq)
{
	int i = 0;
	assert(pSeq);
	if (Empty(pSeq))
	{
		printf("顺序表已经为空\n");
		return;
	}
	for (i = 0; i < pSeq->size - 1; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}

//在顺序表的pos位置插入元素data
void SeqListInsert(PSeqList pSeq, int pos, DataType data)
{
	int i = 0;
	assert(pSeq);
	if (MAX_SIZE == pSeq->size)
	{
		printf("顺序表已经满了\n");
		return;
	}
	//检测pos位置是否合法  pos[0,pSeq->size]
	if (pos<0 || pos>pSeq->size)
	{
		printf("%d 位置非法\n", pos);
		return;
	}
	//搬移元素
	for (i = pSeq->size; i >= pos; i--)
	{
		pSeq->array[i+1] = pSeq->array[i];
	}
	pSeq->array[pos] = data;
	pSeq->size++;
}

//删除顺序表pos位置的元素
void SeqListErase(PSeqList pSeq, int pos)
{
	int i = 0;
	assert(pSeq);
	if (Empty(pSeq))
	{
		printf("顺序表为空\n");
		return;
	}
	if (pos < 0 || pos >= pSeq->size)
	{
		printf("%d 位置非法\n");
		return;
	}
	for (i = pos; i < pSeq->size - 1; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}

void PrintSeqList(PSeqList pSeq)
{
	int i = 0;
	assert(pSeq);
	for (i = 0; i < pSeq->size - 1; i++)
	{
		printf(" %d ", pSeq->array[i]);
	}
	printf("\n");
}

int main()
{
	SeqList s;
	InitSeqList(&s);

	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 4);
	SeqListPushBack(&s, 5);
	PrintSeqList(&s);

	SeqListInsert(&s, 2, 6);
	PrintSeqList(&s);

	SeqListPopBack(&s);
	PrintSeqList(&s);

	SeqListPushFront(&s, 0);
	PrintSeqList(&s);

	SeqListPopFront(&s);
	PrintSeqList(&s);

	SeqListErase(&s, 2);
	PrintSeqList(&s);

	return 0;
}



 
 
 

猜你喜欢

转载自blog.csdn.net/bit666888/article/details/78747453