链表,顺序表的一些基本函数的实现

最近在学习数据 结构的顺序表,和链表,完成了它们的接口函数,想来想去,觉得它们就是在栈中开辟的一些临时的存储空间,然后我们编写程序,对它们进行操作,来完成我们想要的输出。

一 下面是单链表的一些基本接口的操作函数

#include"SList.h"
typedef int DataType;
typedef struct ListNode
{
	struct SListNode* _next;
	DataType          _data;
}SListNode;
SListNode* BuySListNode(DataType x)//买一个结点
{
	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	assert(node);
	node->_data = x;
	node->_next = NULL;
	return node;
}
void SListPrint(SListNode* pHead)//这个可以
{
	assert(pHead);
	SListNode* cur = pHead;
	while (cur)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("NULL\n");
}


void SListPushBack(SListNode**ppHead, DataType x)//这个结构体类型的指针
{
	if (*ppHead == NULL)
	{
		*ppHead=BuySListNode(x);
	}
	else
	{
		SListNode*cur=*ppHead;
		while (cur->_next)
		{
			cur=cur->_next;


		}
		cur->_next= BuySListNode(x);


	}


}
void SListPushFront(SListNode**ppHead, DataType x)//前插
{
	if (*ppHead == NULL)
	{
		*ppHead = BuySListNode(x);
	}
	else
	{
		SListNode* newnode = BuySListNode(x);
		newnode->_next = *ppHead;
		*ppHead = newnode;
	}
}
void SListPopBack(SListNode**ppHead)
{
	assert(*ppHead);
	if (NULL == *ppHead)
	{
		return;
	}
	else if ((*ppHead)->_next==NULL)//一个结点
	{
		free(*ppHead);
		*ppHead = NULL;
	}
	else
	{
		SListNode *prev=NULL, *cur=*ppHead;
		while (cur->_next)
		{
			prev = cur;
			cur = cur->_next;
		}
		free(cur);
		prev->_next= NULL;
	}
	
}
void SListDestory(SListNode**ppHead)//销毁单链表
{
	assert(*ppHead);
	if (NULL == ppHead)
	{
		return;
	}
	else
	{
		SListNode* cur=*ppHead;
		while (cur)
		{
			SListNode* next = cur->_next;
				free(cur);
				cur = next;
		}
		*ppHead = NULL;
	}
	
	
}
SListNode* SListFind(SListNode**ppHead, DataType x)//将链表遍历一遍
{
	
		SListNode* cur = *ppHead;
		while (cur)
		{
			if (cur->_data = x)
				return cur;
			cur = cur->_next;
		}
		return NULL;
}
void SListInsert(SListNode** ppHead, SListNode*pos, DataType x)
{
	if (pos == *ppHead)
	{
		SListPushFront(ppHead, x);
	}
	else
	{
		SListNode* newnode = BuySListNode(x);
		SListNode* prev = *ppHead;
		while (prev != pos)
		{
		 prev = prev->_next;
		}
		prev->_next = newnode;
		newnode->_next = pos;
		
	}
}
void SListErase(SListNode**ppHead, SListNode*pos)//删除一个结点
{
	assert(*ppHead&&pos);
	if ((*ppHead) == pos)
	{
		SListPopFront(ppHead);
	}
	else if (pos->_next == NULL)
	{
		SListPopBack(ppHead);
	}
	else
	{
		SListNode* prev = *ppHead;
		while (prev->_next != pos)
		{
			prev = prev->_next;
		}
		prev->_next = pos->_next;
		free(pos);


		
	}
		
}
int main()
{
	SListNode* s= NULL;
    SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushBack(&s, 4);
	SListPrint(s);
	/*SListPushFront(&s, 4);
	SListPushFront(&s, 3);
	SListPushFront(&s, 2);
	SListPushFront(&s, 1);
	SListPrint(s);
	SListPopBack(&s);
	SListPopBack(&s);
	SListPopBack(&s);
	SListPopBack(&s);
	printf("%p", SListFind(&s, 4));*/
	system("pause");
	return 0;
}

二 下面是顺序表的

#include"link.h"
typedef unsigned int size_t;
typedef int DataType;
typedef struct SeqList
{
	DataType _array[MAX_SIZE];
	int _size; // 顺序表中有效元素的个数 
}SeqList, *PSeqList
void SeqListInit(PSeqList ps)//其实线性表就是一个数组,给线性表初始化,就相当于给数组初始化
{


	assert(ps);
	ps->_size = 0;
	memset(ps->_array, 0, sizeof(DataType)*MAX_SIZE);



}
void PrintSeqList(PSeqList ps)//打印线性表中的元素
{
	int i;
	for (i = 0; i < ps->_size; i++)
	{
		printf("%d", ps->_array[i]);
	}
}
void SeqListPushBack(PSeqList ps, DataType data)//向线性表后面尾插一个元素
{
	if (NULL == ps)
		return;
	if (ps->_size == MAX_SIZE)//那就是判断一下,没满就可以插咯
		return;
	ps->_array[ps->_size++] = data;


}
void SeqListPopBack(PSeqList ps)//尾删
{
	assert(ps);
	if (ps->_size == 0)
	{
		printf("线性表的有效元素个数为0,所以不可以尾删");
	}
	else
	{
		ps->_size--;
	}


}
int SeqListSize(PSeqList ps)// 获取顺序表中元素的个数 
{
	SeqListpEmpty(ps);
	return ps->_size;
}
void SeqListpEmpty(PSeqList ps)// 判断传过来的链表指针是否为空 
{
	assert(ps);
	if (NULL == ps)
	{
		return;
	}
}
int SeqListFind(PSeqList ps, DataType data)// 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标 
{
	int i = 0;
	/*SeqListEmpty(ps);*/
	SeqListpEmpty(ps);
	for (i = 0; i < ps->_size; i++)
	{
		if (data == ps->_array[i])
			return i;//查找到
	}
	return -1;//没查找到

}
void SeqListRemoveAll(PSeqList ps, DataType data)// 删除顺序表中所有值为data的元素 
{
	int i;
	SeqListpEmpty(ps);
	for (i = 0; i < ps->_size; i++)
	{
		if (data == ps->_array[i])
			SeqListErase(ps, i + 1);

	}


}
void SeqListErase(PSeqList ps, size_t pos)//删除任意位置中的元素,既要从编程可能真的写的这个程序有bug
{
	int i = pos - 1;
	assert(ps);
	for (; i < ps->_size; i++)
	{
		ps->_array[i] = ps->_array[i + 1];
	}
	ps->_size--;
}
void SeqListInsert(PSeqList ps, size_t pos, DataType data)// 任意位置中插入值为data的元素 
{
	int i = pos - 1;
	SeqListpEmpty(ps);
	if (ps->_size = MAX_SIZE)
	{
		return;
	}
	for (; i < ps->_size; i++)//每个元素都有自己的任务
	{
		ps->_array[i + 1] = ps->_array[i];
	}
	ps->_array[i] = data;
	ps->_size++;
}
void BubbleSort(int* array, int size)// 用冒泡排序对顺序表中的元素进行排序,程序中每一个元素都会有它所代表的意义
{
	int i = 0, j = 0;
	int tmp;
	for (i = 0; i < size - 1; i++)//外层循环控制次数
	{
		for (j = 0; j < size - 1 - i; j++)//内层循环控制实现方法,循环就是每一次都是按照自己的方法去实现
		{
			if (*array>*(array + 1))
			{
				tmp = *array;
				*array = *(array + 1);
				*(array + 1) = *array;
			}

		}
	}
}
void SelectSort(int* array, int size)// 用选择排序对顺序表中的元素进行排序,知道选择排序的原理后,要会写算法
{

	int i = 0, j = 0;
	int min;
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size - 1 - i; j++)
		{
			if (array[j]>array[j + 1])
				min = array[j + 1];
		}
		array[i] = min;



	}


}
void SelectSort_OP(int* array, int size)// 选择排序优化---一次找出最大最小元素所在的位置 
{
	int i = 0, j = 0;
	int min, max;//用来保存最大最小元素所在的位置
	for (i = 0; i < size; i++)
	{
		for (j = 0; j < size - 1 - i; j++)
		{
			if (array[j]>array[j + 1])
			{
				min = j + 1;
				max = j;
			}
			else
			{
				min = j;
				max = j + 1;
			}


		}

	}
}


猜你喜欢

转载自blog.csdn.net/weixin_40797414/article/details/80256622
今日推荐