동적 순서 표 생 성 및 간단 한 조작

7524 단어 데이터 구조
우선 순서 표를 정 해 야 합 니 다.
typedef struct SeqList
{
	DataType* _a;
	size_t _size;	//        
	size_t _capacity;	//    
}SeqList;

void SeqPrint(SeqList* pSeq);//     
void SeqInit(SeqList* pSeq);//      
void SeqDestory(SeqList* pSeq);//     
int CheckCapcity(SeqList* pSeq);//           

void SeqPushBack(SeqList* pSeq, DataType x);//       
void SeqPopBack(SeqList* pSeq);////       
void SeqPushFront(SeqList* pSeq, DataType x);//       
void SeqPopFront(SeqList* pSeq);////       

void SeqInsert(SeqList* pSeq, size_t pos, DataType x);//         
void SeqErase(SeqList* pSeq, size_t pos);//         

int SeqFind(SeqList* pSeq, DataType x);//        
void SeqAt(SeqList* pSeq, size_t pos, DataType x);//           

void BubbleSort(SeqList* pSeq);//            
void SelectSort(SeqList* pSeq);//            
int BinarySearch(SeqList* pSeq,DataType x);//            

SeqList.h typedef

SeqList.h:

#pragma once
#define N 3//             
typedef int DataType;

typedef struct SeqList
{
	DataType* _a;
	size_t _size;	//        
	size_t _capacity;	//    
}SeqList;

void SeqPrint(SeqList* pSeq);//     
void SeqInit(SeqList* pSeq);//      
void SeqDestory(SeqList* pSeq);//     
int CheckCapcity(SeqList* pSeq);//           

void SeqPushBack(SeqList* pSeq, DataType x);//       
void SeqPopBack(SeqList* pSeq);////       
void SeqPushFront(SeqList* pSeq, DataType x);//       
void SeqPopFront(SeqList* pSeq);////       

void SeqInsert(SeqList* pSeq, size_t pos, DataType x);//         
void SeqErase(SeqList* pSeq, size_t pos);//         

int SeqFind(SeqList* pSeq, DataType x);//        
void SeqAt(SeqList* pSeq, size_t pos, DataType x);//           

void BubbleSort(SeqList* pSeq);//            
void SelectSort(SeqList* pSeq);//            
int BinarySearch(SeqList* pSeq,DataType x);//            

SeqList.c , main

SeqList.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"SeqList.h"

void SeqInit(SeqList* pSeq)//      
{
	assert(pSeq);
	pSeq->_a = NULL;
	pSeq->_size = 0;
	pSeq->_capacity = 0;
}

void SeqPrint(SeqList* pSeq)//     
{
	assert(pSeq);
	size_t  i = 0;
	if (pSeq->_size == 0)
	{
		printf("       !
"); return; } for (i = 0; i < pSeq->_size; i++) { printf("%d ", pSeq->_a[i]); } printf("
"); } void SeqDestory(SeqList* pSeq)// { pSeq->_capacity = 0; pSeq->_size = 0; free( pSeq->_a); } int CheckCapcity(SeqList* pSeq)// { if (pSeq->_size >= pSeq->_capacity) { DataType* tmp = NULL; int capacity = pSeq->_capacity + N;// N DataType tmp = (DataType*)realloc(pSeq->_a, sizeof(DataType)*capacity); //if (pSeq->_size == 0) //{ // tmp = (DataType*)malloc(sizeof(DataType)*capacity); // //tmp = (DataType*)realloc(pSeq->_a, sizeof(DataType)*capacity); //} //else //{ // tmp = (DataType*)realloc(pSeq->_a,sizeof(DataType)*capacity); //} if (tmp != NULL) { pSeq->_capacity = capacity; pSeq->_a = tmp; return 1; } printf(" !!
"); return 0; } return 1; } void SeqPushBack(SeqList* pSeq, DataType x)// { assert(pSeq); if (!CheckCapcity(pSeq)) { return; } pSeq-> _a[pSeq->_size]= x; pSeq->_size++; } void SeqPopBack(SeqList* pSeq)// { assert(pSeq); //pSeq->_a[pSeq->_size] = 0; pSeq->_size--; } void SeqPushFront(SeqList* pSeq, DataType x)// { assert(pSeq); if (!CheckCapcity(pSeq)) { return; } size_t i = pSeq->_size; for (; i > 0; i--) { pSeq->_a[i] = pSeq->_a[i - 1]; } pSeq->_a[0] = x; pSeq->_size++; } void SeqPopFront(SeqList* pSeq)// { assert(pSeq); size_t i = 1; for (i = 1; i < pSeq->_size; i++) { pSeq->_a[i - 1] = pSeq->_a[i]; } //pSeq->_a[pSeq->_size] = 0; pSeq->_size--; } void SeqInsert(SeqList* pSeq, size_t pos, DataType x)// { assert(pSeq); if (!CheckCapcity(pSeq)) { return; } size_t i = pSeq->_size; for (; i >= pos; i--) { pSeq->_a[i] = pSeq->_a[i - 1]; } pSeq->_a[pos-1] = x; pSeq->_size++; } void SeqErase(SeqList* pSeq, size_t pos)// { assert(pSeq); size_t i = pos; for (; i < pSeq->_size; i++) { pSeq->_a[i - 1] = pSeq->_a[i]; } //pSeq->_a[pSeq->_size] = 0; pSeq->_size--; } int SeqFind(SeqList* pSeq, DataType x)// { assert(pSeq); size_t i = 0; for (; i < pSeq->_size; i++) { if (pSeq->_a[i] == x) { return i + 1; } } return 0; } void SeqAt(SeqList* pSeq, size_t pos, DataType x)// { assert(pSeq); assert(pos <= pSeq->_size); pSeq->_a[pos - 1] = x; } void BubbleSort(SeqList* pSeq)// { assert(pSeq); size_t i, j; for (i = 0; i < pSeq->_size; i++) { int tmp = 0; for (j = 0; j < pSeq->_size - i - 1; j++) { DataType x; if (pSeq->_a[j]>pSeq->_a[j + 1]) { x = pSeq->_a[j]; pSeq->_a[j] = pSeq->_a[j + 1]; pSeq->_a[j + 1] = x; tmp = 1; } } if (0 == tmp)// , return { return; } } } void SelectSort(SeqList* pSeq)// { assert(pSeq); size_t min = 0; size_t max = 0; size_t left = 0; size_t right = pSeq->_size - 1; while (left_a[min]>pSeq->_a[i]) { min = i; } if (pSeq->_a[max] < pSeq->_a[i]) { max = i; } } DataType x = pSeq->_a[min]; pSeq->_a[min] = pSeq->_a[left]; pSeq->_a[left] = x; if (max == left) { max = min; } x = pSeq->_a[max]; pSeq->_a[max] = pSeq->_a[right]; pSeq->_a[right] = x;// , , left++; right--; } } int BinarySearch(SeqList* pSeq,DataType x)// { assert(pSeq); int left = 0, right = pSeq->_size - 1; while (left <= right) { int mid = ((right - left) / 2) + left; if (pSeq->_a[mid] > x) { right = mid - 1; } if (pSeq->_a[mid] < x) { left = mid + 1; } else { return mid + 1;// , , return mid } } return 0; } int main() { SeqList s; SeqInit(&s); SeqPushBack(&s, 1); SeqPushBack(&s, 2); SeqPushBack(&s, 3); SeqPushBack(&s, 4); SeqPushBack(&s, 5); SeqPushFront(&s, 6); SeqPrint(&s); /*SeqPopBack(&s); SeqPrint(&s); system("pause"); SeqPushFront(&s, 9); SeqPrint(&s); system("pause"); SeqPopFront(&s); SeqPrint(&s);*/ SeqInsert(&s, 3, 9); SeqPrint(&s); SeqErase(&s, 5); SeqPrint(&s); printf(" %d !
(0 !!!)
", SeqFind(&s, 9)); SeqAt(&s, 1, 1); SeqPrint(&s); SelectSort(&s); printf(" :"); BubbleSort(&s); SeqPrint(&s); printf("
"); printf(" %d !
(0 !!!)
", BinarySearch(&s, 9)); SeqDestory(&s); SeqPrint(&s); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기