1. 程式人生 > >靜態順序表

靜態順序表

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

#define MAX_SIZE 10 
typedef unsigned int size_t;
typedef int DataType;

typedef struct SeqList
{
	DataType _array[MAX_SIZE];
	int _size;
}SeqList,*PSeqList;

void SeqListInit(PSeqList ps);
void PrintSeqList(PSeqList ps);

void SeqListInit(PSeqList ps)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	ps->_size = 0;
	for (i = 0; i < MAX_SIZE;i++)
	{
		scanf("%d", &(ps->_array[i]));
		ps->_size++;
	}

	PrintSeqList(ps);
}

void SeqListPushBack(PSeqList ps, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	if (ps->_array != MAX_SIZE)
	{
		ps->_array[ps->_size] = data;
		ps->_size++;
	}
	else
		printf("對不起,順序表元素已滿。\n");
}

void SeqListPopBack(PSeqList ps)
{
	if (NULL == ps)
	{
		return;
	}
	ps->_array[ps->_size - 1] = 0;
	ps->_size--;
}

void SeqListPushFront(PSeqList ps, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	if (ps->_array != MAX_SIZE)
	{
		for (i = ps->_size; i > 0; i--)
		{
			ps->_array[i] = ps->_array[i - 1];
		}
		ps->_array[0] = data;
	}
	else
		printf("對不起,順序表元素已滿。\n");
}

void SeqListPopFront(PSeqList ps)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
		for (i = 0; i <ps->_size; i++)
		{
			ps->_array[i] = ps->_array[i + 1];
		}
		ps->_size--;
}

// 任意位置中插入值為data的元素 
void SeqListInsert(PSeqList ps, size_t pos, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	if (ps->_array != MAX_SIZE)
	{
		for (i = ps->_size ; i >= pos; i--)
		{
			ps->_array[i] = ps->_array[i - 1];
		}
		ps->_array[pos-1] = data;
	}
	else
		printf("對不起,順序表元素已滿。\n");
}

// 刪除任意位置中的元素 
void SeqListErase(PSeqList ps, size_t pos)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	for (i = pos-1; i < ps->_size; i++)
	{
		ps->_array[i] = ps->_array[i + 1];
	}
	ps->_size--;
}

// 在順序表中查詢值為data的元素,返回該元素在順序表中的下標 
int SeqListFind(PSeqList ps, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	for (i = 0; i < ps->_size; i++)
	{
		if(ps->_array[i] = data);
		return i;
	}
	return -1;
}
// 刪除順序表中值為data的元素 
void SeqListRemove(PSeqList ps, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0; int j = 0;
	for (i = 0; i < ps->_size; i++)
	{
		if (ps->_array[i] = data)
		{
			for (j = i; j < ps->_size; i++)
			{
				ps->_array[i] = ps->_array[i + 1];
			}
			ps->_size--;
			return;
		}
	}
}

// 刪除順序表中所有值為data的元素 
void SeqListRemoveAll(PSeqList ps, DataType data)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0; int j = 0;
	for (i = 0; i < ps->_size; i++)
	{
		if (ps->_array[i] = data)
		{
			for (j = i; j < ps->_size; i++)
			{
				ps->_array[i] = ps->_array[i + 1];
			}
			ps->_size--;
		}
	}
}

// 判斷順序表是否為空 
int SeqListEmpty(PSeqList ps)
{
	if (ps->_size)
	{
		return 1;
	}
	return -1;
}

// 用氣泡排序對順序表中的元素進行排序 
void BubbleSort(int* array, int size)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < size - 1; i++)
	{
		for (j = 0; j < size - 1 - i; j++)
		{
			if (array[j]>array[j + 1])
			{
				array[j] ^= array[j + 1];
				array[j + 1] ^= array[j];
				array[j] ^= array[j + 1];
			}
		}
	}
}

// 用選擇排序對順序表中的元素進行排序 
void SelectSort(int* array, int size)
{
	int min = array[0];
	int begin = 0;
	int j = 0;
	for (begin = 0; begin< size-1; begin++)
	{
		for (j = begin+1; j < size; j++)
		{
			if (min>array[j])
				min = array[j];
		}
		array[begin] = min;
	}
}

void PrintSeqList(PSeqList ps)
{
	if (NULL == ps)
	{
		return;
	}
	int i = 0;
	for (i = 0; i < ps->_size; i++)
	{
		printf("%d  ", ps->_array[i]);
	}
}