동적 순서 표 생 성 및 간단 한 조작
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;
}
,
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.