C语言实现的顺序表
顺序表是用一段地址连续的存储单元依次存储数据元素的线性结构。顺序表可分为静态存储和动态存储,静态顺序表比较简单,数据空间固定,而动态顺序表可以动态增容,便于存放大量数据,现主要把动态的基本实现一下~此处的排序简单实现了一下,后面会整理出各种排序~~

#define MAX_SIZE 100
#define INIT_SIZE 3
typedef int DataType;
//顺序表的静态存储
typedef struct SeqList_s
{
DataType array[MAX_SIZE]; //数据段
size_t size; //数据的个数
}SeqList_s;
//顺序表的动态存储
typedef struct SeqList_d
{
DataType* array; //数据块指针
size_t size; //有效数据的个数
size_t capacity; //容量
}SeqList_d;
//检查容量,增容
void CheckCapacity(SeqList_d *pSeq)
{
assert(pSeq);
DataType* temp;
if (pSeq->size >= pSeq->capacity)
{
pSeq->capacity *= 2; //容量增至2倍
temp = (DataType*)realloc(pSeq->array, pSeq->capacity*sizeof(DataType));
if (NULL == temp)
{
return;//如果没有增容成功,返回
}
pSeq->array = temp;
}
else
{
return;
}
}
//初始化
int InitSeqList_d(SeqList_d* pSeq)
{
assert(pSeq);
pSeq->array = (DataType*)malloc(sizeof(DataType)*INIT_SIZE);
if (NULL == pSeq)
{
return 0; //未成功
}
pSeq->size = 0;
pSeq->capacity = INIT_SIZE;
return 1;
}
//查找
size_t Find(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
size_t pos = pSeq->size; //用pos记录x所在位置,若大于等于size,则说明未找到
for (size_t i = 0; i < pSeq->size; i++)
{
if (*(pSeq->array + i) == x)
{
pos = i;
break;
}
}
if (pos >= pSeq->size)
{
printf("未找到\n");
}
return pos;
}
//尾插
void PushBack(SeqList_d* pSeq, DataType x)
{
assert(pSeq != NULL);
CheckCapacity(pSeq);
pSeq->array[pSeq->size++] = x;
}
//插入
void Insert(SeqList_d* pSeq, size_t pos, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
if (pos > pSeq->size)
{
printf("插入位置有误!\n");
return;
}
DataType* temp = pSeq->array + pSeq->size - 1;
while (temp >= (pSeq->array + pos))
{
*(temp + 1) = temp;
temp--;
}
////也可以这样移动
//for (size_t i = pSeq->size - 1; i >= pos; i--)
//{
// pSeq->array[i+1] = pSeq->array[i];
//}
*(pSeq->array + pos) = x;
pSeq->size++;
}
//删除pos位置的数据
void Erase(SeqList_d* pSeq, size_t pos)
{
assert(pSeq);
if (pos < 0 || pos >= pSeq->size)
{
printf("要删除的位置有误!\n");
return;
}
for (size_t i = pos; i < pSeq->size; i++)
{
*(pSeq->array + i) = *(pSeq->array + i + 1);
}
pSeq->size--;
}
//删除找到的第一个x
void Remove(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
size_t pos = Find(pSeq, x);
if (pos < 0 || pos >= pSeq->size)
{
printf("没有x这个数据!\n");
return;
}
else
{
for (size_t i = pos; i < pSeq->size; i++)
{
*(pSeq->array + i) = *(pSeq->array + i + 1);
}
pSeq->size--;
}
}
//删除所有的x(有优化)
void RemoveAll(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
size_t first = 0, second = 0, count = 0;
while (second < pSeq->size)
{
if (*(pSeq->array + second) == x)
{
count++;
}
else
{
*(pSeq->array + first) = *(pSeq->array + second);
first++;
}
second++;
}
pSeq->size -= count;
}

用second指针遍历顺序表,用first指针记录删除后的顺序表。
当second指针不是指向所要删除的数据3时,second指向的数据赋给first指向的数据,first指针和second指针同时向后指;
当second指针指向所要删除的数据3时,first指针不动,count计数加1,second指针继续向后指。
这样就把数据删除了~~~这种方法比找到一个就挪动一次顺序表要高效一些。。。
//排序(从小到大)
void Sort(SeqList_d* pSeq)
{
assert(pSeq);
if (pSeq->size <= 0)
{
return;
}
DataType temp;
bool flag = true;
for (size_t i = 0; (i < pSeq->size - 1) && flag; i++) //进行size-1趟排序
{
flag = false;
for (size_t j = 0; j < pSeq->size - i - 1; j++)
{
if (*(pSeq->array + j) > *(pSeq->array + j + 1))
{
temp = *(pSeq->array + j);
*(pSeq->array + j) = *(pSeq->array + j + 1);
*(pSeq->array + j + 1) = temp;
flag = true;
}
}
}
}
//二分查找(非递归)
int BinarySearch(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
int left = 0, right = pSeq->size - 1;
int mid;
while (left <= right)
{
mid = left + (right - left) / 2; //这样算mid可以避免溢出
if (*(pSeq->array + mid) < x) //x在后边部分
{
left = mid + 1;
}
else if (*(pSeq->array + mid) > x) //x在前半部分
{
right = mid - 1;
}
else
{
return mid;
break;
}
}
return -1;
}
//二分查找(递归)
int _BinarySearch_R(SeqList_d* pSeq, int left, int right, DataType x)
{
assert(pSeq);
if (left <= right)
{
int mid = left + (right - left) / 2;
if (pSeq->array[mid] < x)
{
return _BinarySearch_R(pSeq, mid + 1, right, x);
}
else if (pSeq->array[mid] > x)
{
return _BinarySearch_R(pSeq, left, mid - 1, x);
}
else
{
return mid;
}
}
return -1;
}
int Binary_R(SeqList_d* pSeq, DataType x)
{
return _BinarySearch_R(pSeq, 0, pSeq->size - 1, x);
}
C语言实现的顺序表的更多相关文章
- 数据结构C语言版--动态顺序表的基本功能实现(二)
/* * 若各个方法结构体变量参数为: &L(即地址符加变量)则结构体变量访问结构成员变量时使用"." * 若为:*L(即取地址符加变量)则结构体变量访问结构体成员变量使用 ...
- C语言学习笔记-顺序表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...
- C语言版本:顺序表的实现
seqlist.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include<cstdio> #include<malloc.h> ...
- c语言描述的顺序表实现
//顺序表的实现:(分配一段连续地址给顺序表,像数组一样去操作) #include<stdio.h> #include<stdlib.h> #define OK 1 #defi ...
- C语言项目实现顺序表
#include <stdio.h> #include <stdlib.h> #include "test_顺序表声明.h" /* run this pro ...
- 数据结构C语言版--静态顺序表的基本功能实现(一)
/* * 功能:创建一个线性表,并输出 * 静态分配内存 */ #include<stdio.h> //stdio.h是C的标准I/O库 //#include<iostream> ...
- 【数据结构】之顺序表(Java语言描述)
之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...
- 【数据结构】之顺序表(C语言描述)
顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...
- C语言实现顺序表
C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...
随机推荐
- 数字信号处理与音频处理(使用Audition)
前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...
- List 排序
1:sort 这种实现是用朗姆达表达式实现.Comparison<T> 委托详见 http://msdn.microsoft.com/zh-cn/library/tfakywbh.aspx ...
- 高亮选中MEMO某一行
选中第5行 //转到指定行并选中这行的文本 procedure SelectLine(Memo1: TMemo; ln: Integer); begin Memo1.SelStart := SendM ...
- log4j.properties文件配置--官方文档
Default Initialization Procedure The log4j library does not make any assumptions about its environme ...
- (转载)Ant教程
ant教程(一) 写在所有之前 为了减少阅读的厌烦,我会使用尽量少的文字,尽量明白的表达我的意思,尽我所能吧.作为一个学习者,在我的文章中会存在各种问题,希望热心人指正.目录大概是这样 ant教程 ( ...
- 删除sd卡的文件
public static void deleteAllFile(){ String path = Environment.getExternalStorageDirectory().getAbsol ...
- jhipster
Client side: yeoman,grunt,bower,angularjs Server side: maven,spring,spring mvc rest,spring data jpa
- 通过Html5的postMessage和onMessage方法实现跨域跨文档请求访问
在项目中有应用到不同的子项目,通过不同的二级域名实现相互调用功能.其中一个功能是将播放器作为单独的二级域名的请求接口,其他项目必须根据该二级域名调用播放器.最近需要实现视频播放完毕后的事件触发,调用父 ...
- javascript 中的new操作符的理解
new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassic ...
- css定义多重背景动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style typ ...