动态链表

DynSeqlist.h


#ifndef __DYNSEQLIST_H__
#define __DYNSEQLIST_H__

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<string.h>
#define SIZE 5
typedef int DataType;
typedef struct seqlist
{
	DataType* array;
	size_t size;	//有效值
	size_t capacity;//总容量
}seqlist;
typedef enum tag
{
	one,//删去第一个
	more,//全部都删去
}Tag;
void Init(seqlist* pSeq);
void CheckExpand(seqlist* pSeq);
void Destory(seqlist* pSeq);
void Print(seqlist* pSeq);
void PushBack(seqlist* pSeq, DataType x);
void PushFront(seqlist* pSeq, DataType x);
void Insert(seqlist* pSeq, size_t index, DataType x);
void PopBack(seqlist* pSeq);
void PopFront(seqlist*pSeq);
void removed(seqlist* pSeq, size_t index);
void Modify(seqlist* pSeq, size_t index, DataType x);
void Erase(seqlist* pSeq, DataType x, Tag all);
int Find(seqlist* pSeq, DataType x,size_t index);
void swap(DataType* x, DataType* y);
void BubbleSort(seqlist* pSeq);//冒泡排序升序
void SelectSort(seqlist* pSeq);//选择排序降序
int BinarySearch(seqlist* pSeq, DataType x);//二分查找时用升序测试
#endif		//__DYNSEQLIST_H__

DynSeqlist.c


#include"DynSeqlist.h"
void Init(seqlist* pSeq)
{
	assert(pSeq);
	pSeq->array = (DataType *)malloc(SIZE*sizeof(DataType));
	memset(pSeq->array, 0, SIZE*sizeof(DataType));
	pSeq->size = 0;
	pSeq->capacity = SIZE;
}
void CheckExpand(seqlist* pSeq)
{
	assert(pSeq);
	DataType* S;
	if (pSeq->size == pSeq->capacity)
	{
		S = (DataType*)malloc(2 * pSeq->capacity *sizeof(DataType));
		memcpy(S, pSeq->array, SIZE*sizeof(DataType));
		free(pSeq->array );
		pSeq->array = S;
		pSeq->capacity = 2 * pSeq->capacity;
	}
}
void Destory(seqlist* pSeq)
{
	assert(pSeq);
	if (pSeq->array != NULL)
	{
		free(pSeq->array );
	}
}
void Print(seqlist* pSeq)
{
	assert(pSeq);
	size_t i=0;
	for (; i < pSeq->size; i++)
	{
		printf("%d ", pSeq->array[i]);
	}
	printf("\n");
}
void PushBack(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	assert(pSeq->size < pSeq->capacity);
	pSeq->array[pSeq->size] = x;
	pSeq->size++;
}
void PushFront(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	assert(pSeq->size < pSeq->capacity);
	int i = pSeq->size - 1;
	for (; i >= 0; i--)
	{
		pSeq->array[i + 1] = pSeq->array[i];
	}
	pSeq->array[0] = x;
	pSeq->size++;
}
void Insert(seqlist* pSeq, size_t index, DataType x)
{
	assert(pSeq);
	assert(index < pSeq->size);
	size_t i = pSeq->size;
	for (; i > index; i--)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}
	pSeq->array[index] = x;
	pSeq->size++;
}
void PopBack(seqlist* pSeq)
{
	assert(pSeq);
	pSeq->size--;
}
void PopFront(seqlist*pSeq)
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}
void removed(seqlist* pSeq, size_t index)
{
	assert(pSeq);
	assert(index <  pSeq->size);
	size_t i = index;
	for (; i <pSeq->size ; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}
void Modify(seqlist* pSeq, size_t index, DataType x)
{
	assert(pSeq);
	assert(index < pSeq->size);
	pSeq->array[index] = x;
}
//返回的值为-1时,找不到此元素;否则返回的值为找到的元素的下标。
int Find(seqlist* pSeq, DataType x, size_t index)
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		if (pSeq->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void Erase(seqlist* pSeq, DataType x, Tag all)
{
	assert(pSeq);
	size_t i = 0;
	int ret ;
	if (pSeq->size == 0)
	{
		printf("链表为空\n");
		return;
	}
	ret = Find(pSeq, x, 0);
	if (ret == -1)
	{
		printf("表中找不到这个元素\n");
	}
	else if (all == one)
	{
		removed(pSeq, ret);
	}
	else
	{
		while (ret != -1)
		{
			removed(pSeq, ret);
			ret = Find(pSeq, x, ret);

		}
	}
}
void swap(DataType* x, DataType* y)
{
	DataType temp =*x;
	*x = *y;
	*y = temp;
}
void BubbleSort(seqlist* pSeq)
{
	assert(pSeq);
	size_t i = 0; 
	size_t j = 0;
	for (i = 0; i < pSeq->size - 1; i++)
	{
		for (j = 0; j < pSeq->size-i-1; j++)
		{
			if (pSeq->array[j]>pSeq->array[j+1])
			{
				swap(&(pSeq->array[j]), &(pSeq->array[j + 1]));
			}
		}
	}
}
void SelectSort(seqlist* pSeq)
{
	assert(pSeq);
	size_t i = 0;
	size_t j = 0;
	size_t max;
	for (i = 0; i < pSeq->size-1; i++)
	{
		max = i;
		for (j = i + 1; j < pSeq->size; j++)
		{
			if (pSeq->array [max]< pSeq->array[j])
			{
				max = j;
			}
		}
		if (max != i)
		{
			swap(&(pSeq->array[max]), &(pSeq->array[i]));
		}
	}
}
//返回的值为-1时,找不到此元素;否则返回的值为要找的元素的下标
int BinarySearch(seqlist* pSeq, DataType x)
{
	assert(pSeq);
	DataType left = 0;
	DataType right = pSeq->size - 1;
	DataType mid = left + (right - left) / 2;//这种写法的原因
	while (left <= right)
	{
		DataType mid = left + (right - left) / 2;
		if (pSeq->array[mid] == x)
		{
			return mid;
		}
		if (pSeq->array[mid] < x)
		{
			left = mid + 1;
		}
		else
		{
			right = mid - 1;
		}
	}
	return -1;
}
	



void Test()
{
	seqlist s;
	int ret = 0;
	Init(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PushBack(&s, 3);
	CheckExpand(&s);
	PushBack(&s, 5);
	PushBack(&s, 3);
	PushBack(&s, 6);
	Print(&s);
	PushFront(&s, 99);
	Print(&s);
	Insert(&s, 0, 120);
	Print(&s);
	PopBack(&s);
	Print(&s);
	PopFront(&s);
	Print(&s);
	removed(&s, 5);
	Print(&s);
	Modify(&s, 1, 24);
	Print(&s);
	Erase(&s, 3, more); 
	Print(&s);
	SelectSort(&s);
	Print(&s);
	BubbleSort(&s);
	Print(&s);
	ret=BinarySearch(&s, 2);
	printf("ret=%d\n", ret);
}

void main()
{
	Test();
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值