#pragma once
#include<stdio.h>
#include<assert.h>
#include<string.h>
#define MAX_SIZE 5
typedef int DataType;
typedef struct SeqList
{
DataType array[MAX_SIZE ];
size_t size;
}SeqList;
void PrintSeqList(SeqList * pSeq);
void InitSeqList(SeqList * pSeq);
void PushBack(SeqList * pSeq, DataType x);
void PopBack(SeqList * pSeq);
void PushFront(SeqList * pSeq, DataType x);
void Popfront(SeqList * pSeq);
void Insert(SeqList *pSeq, rsize_t pos, DataType x);
int Find(SeqList *pSeq, DataType x);
int Remove(SeqList * pSeq, DataType x);
void RemoveAll(SeqList * pSeq, DataType x);
void printSeqList(SeqList * pSeq);
void InitSeqList(SeqList * pSeq)
{
memset( pSeq->array, 0, sizeof (DataType)* MAX_SIZE);
pSeq->size = 0;
}
void PushBack(SeqList * pSeq, DataType x )
{
assert(pSeq );
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full");
return;
}
pSeq->array[pSeq ->size++] = x;
}
void PopBack(SeqList * pSeq)
{
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty");
return;
}
pSeq->array[--pSeq ->size ] = 0;
}
void PrintSeqList(SeqList * pSeq)
{
int i = 0;
assert(pSeq );
for (; i < pSeq ->size; ++i)
{
printf( "%d ", pSeq ->array[i]);
}
printf( "\n");
}
void PushFront(SeqList * pSeq, DataType x )
{
int begin = pSeq ->size - 1;
assert(pSeq );
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full\n");
return ;
}
for (; begin >= 0; --begin)
{
pSeq->array[begin + 1] = pSeq ->array[begin];
}
pSeq->array[0] = x ;
++ pSeq->size;
}
void Popfront(SeqList * pSeq)
{
int begin = 1;
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty");
return;
}
for (; begin < pSeq ->size; ++begin)
{
pSeq->array[begin - 1] = pSeq ->array[begin];
}
-- pSeq->size;
}
void Insert(SeqList *pSeq, rsize_t pos , DataType x)
{
int begin = pSeq ->size - 1;
assert(pSeq );
assert(pos <= pSeq->size);
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full\n");
return;
}
for (; begin >= pos ; --begin)
{
pSeq->array[begin + 1] = pSeq ->array[begin];
}
pSeq->array[pos ] = x;
++ pSeq->size;
}
int Find(SeqList *pSeq, DataType x )
{
int i = 0;
assert(x );
for (; i < pSeq ->size; i++)
{
if (pSeq ->array[i] == x)
{
return i;
}
return -1;
}
}
void Erase(SeqList * pSeq, size_t pos )
{
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty\n");
return;
}
assert(pos < pSeq->size);
int begin = pos + 1;
for (; begin < pSeq ->size; ++begin)
{
pSeq->array[begin - 1] = pSeq ->array[begin];
}
--begin;
}
int Remove(SeqList * pSeq, DataType x )
{
int pos;
assert(pSeq );
pos = Find( pSeq, x );
if (pos != -1)
{
Erase( pSeq, pos);
}
return pos;
}
void RemoveAll(SeqList * pSeq, DataType x )
{
int pos;
assert(pSeq );
pos = Find( pSeq, x );
while (pos != -1)
{
Erase( pSeq, pos);
pos = Find( pSeq, x );
}
}
int BinarySearch(SeqList * pSeq, DataType x )
{
assert(pSeq );
int left = 0;
int right = pSeq ->size;
while (left < right)
{
int mid = (right + left) / 2;
if (x >pSeq->array[mid])
{
left = mid + 1;
}
else if (x < pSeq->array[mid])
{
right = mid - 1;
}
else
return mid;
}
}
C语言实现顺序表
原创
©著作权归作者所有:来自51CTO博客作者wx63118e2bb7416的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【数据结构】C语言实现顺序栈
【数据结构】第三章——栈、队列和数组详细介绍通过C语言实现顺序栈
数据结构 C语言 顺序栈 -
1.顺序表的实现——C语言
顺序表各种操作的代码实现
顺序表 冒泡排序 算法 随机数 -
利用C语言实现动态顺序表
利用C语言实现
C语言 动态 capacity -
数据结构--顺序表(C语言实现)
数据结构中以C语言为基础实现顺序表
数据结构 C语言 -
顺序栈C语言实现
“`#include <stdio.h>#define MAXSIZE 10001#define ELEMTYPE int
c语言 typedef struct 栈 数据结构 -
【c语言实现动态顺序表的内存扩容】
函数中,我们首先初始化一个容量为 5 的动态顺序表,然后连续插入 6 个元素,超出容量后会自动进行扩容。最后打印动态顺
程序人生 c语言 virtualenv 开发语言 动态顺序表 -
顺序队列的C语言实现
#include <stdio.h>#define MAXSIZE 101#define ELEMTYPE int#define QUEUE_EMPTY -10000ty
队列 struct typedef c语言 #define -
C语言实现ArrayList (线性表顺序存储结构)
数据结构C/C++实现,ArrayList ,动态数组,线性表顺序存储
数据结构C/C++实现ArrayLis -
C语言实现顺序表的增删查改以及排序
实现线性表的基本操作与冒泡排序线性表,现则排序线性表以及测试代码
C语言 基本操作 线性表 -
mysql 关联查询后右侧表不走索引
数据库表关联的三种方式 NESTED LOOP 对于被连接的数据子集较小的情况,nested loop连接是个较好的选择。nested loop就是扫描一个表,每读到一条记录,就根据索 引去另一个表里面查找,没有索引一般就不会是 nested loops。 一般在nested l
mysql 关联查询后右侧表不走索引 oracle 数据表关联 hash 杂谈