顺序表头插头删尾插尾删以及任意位置的插入删除和顺序表中的查找

简介: 顺序表头插头删尾插尾删以及任意位置的插入删除和顺序表中的查找

今天,话不多说,直接上代码


//这里对函数进行测试
#include"Seqlist.h"
#include<assert.h>
#include<stdlib.h>
//测试尾插
void TestSeqlist1()
{
  SeqList s;
  SeqlistInit(& s);
  SeqlistPushBack(&s, 1);
  SeqlistPushBack(&s, 2);
  SeqlistPushBack(&s, 3);
  SeqlistPushBack(&s, 4);
  SeqlistPushBack(&s, 5);
  SeqlistPushBack(&s, 6);
  SeqlistPrint(&s);
  SeqlistPopBack(&s);
  SeqlistPopBack(&s);
  SeqlistPopBack(&s);
  SeqlistPrint(&s);
  SeqlistPushFront(&s, -1); 
  SeqlistPrint(&s);
  SeqlistPopFront(&s);
  SeqlistPopFront(&s);
  SeqlistPrint(&s);
  SeqlistDestory(&s);
  int pos = SeqlistFind(&s, 5);
  if (pos != -1)
  {
    SeqlistErase(&s, pos);
  }
  SeqlistPrint(&s);
  SeqlistDestory(&s);
 }
int main()
{
  TestSeqlist1();
  return  0;
}
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//这个文件中写的是函数的申明
//顺序表其实就是数组,且有效数组在数组中必须是连续的
//动态顺序表实现
typedef int SlDATAType;
typedef    struct Seqlist
{
  SlDATAType* p;
  int size;//有效数据的个数
  int  capacity;//容量空间大小
}SL,SeqList;
//尾插
//尾删
//头插
//头删
void SeqlistInit(SL *ps);
void SeqlistDestory(SL* ps);
void SeqlistPrint(  SL* ps);
void SeqlistCheckCapacity(SL* ps);
void SeqlistPushBack(SL* ps, SlDATAType x);
void SeqlistPushBack(   SL* ps, SlDATAType x); 
void SeqlistPopBack(   SL* ps);
void SeqlistPushFront(   SL* ps, SlDATAType x);
void SeqlistPopFront(  SL* ps);
//任意位置的插入和删除
void SeqlistInsert(  SL* ps,int pos,SlDATAType x);
void SeqlistErase(  SL* ps,int pos);
int SeqlistFind(SL* psl,SlDATAType x);
int SeqlistSort(SL* psl);
int SeqlistBinaryFind(SL* psl, SlDATAType x);
#include"Seqlist.h"//这里写函数的定义
typedef int SlDATAType;
void SeqlistInit(SL* ps)
{
  ps->p = (SlDATAType*)malloc(sizeof(SlDATAType) * 4);
  if (ps->p == NULL)
  {
    printf("申请内存失败\n");
    exit(-1);
  }
  ps->size= 0;
  ps->capacity = 4;
}
void SeqlistDestory(SL* ps)
{
  free(ps->p);
  ps->p = NULL;
  ps->size = ps->capacity = 0;
}
void SeqlistPrint(SL* ps)
{
  assert(ps);
    for (int i = 0; i < ps->size; ++i)
    {
      printf("%d", ps->p[i]);
    }
    printf("\n");
}
void SeqlistCheckCapacity(SL* ps)
{
  //如果满了需要增容
  if (ps->size == ps->capacity)
  {
    ps->capacity *= 2;
    ps->p = (SlDATAType*)realloc(ps->p, sizeof(SlDATAType) * ps->capacity);
    if (ps->p == NULL)
    {
      printf("扩容失败");
      exit(-1);
    }
  }
}
void SeqlistPushBack( SL* ps, SlDATAType x)
{
  assert(ps);
  SeqlistCheckCapacity(ps);
  ps->p[ps->size] = x;
  ps->size++;
}
void SeqlistPopBack(SL* ps)
{
  assert(ps);
  //ps->p[ps->size - 1] = 0;
  ps->size--;
}
void SeqlistPushFront(SL* ps, SlDATAType x)
{
  assert(ps);
  SeqlistCheckCapacity(ps);
  int end = ps->size - 1;
  while(end >= 0)
  {
    ps->p[end + 1] = ps->p[end];
    --end;
  }
  ps->p[0] = x;
  ps->size++;
}
void SeqlistPopFront(SL* ps)
{
  assert(ps);
  int start = 0;
  while (start < ps->size - 1)
  {
    ps->p[start] = ps->p[start + 1];
    ++start;
  }
  ps->size--;
}
//size_t就是unsigned int 
//任意位置的插入和删除
void SeqlistInsert(SL* ps, int pos, SlDATAType x)
{
  assert(ps);
  assert(&pos<ps->size&&pos>=0);
  SeqlistCheckCapacity(ps);
  int end = ps->size - 1;
  while (end>=pos)
  {
    ps->p[end + 1] = ps->p[end];
    --end;
  }
  ps->p[pos] = x;
  ps->size++;
}
void SeqlistErase(SL* ps, int pos)
{
  assert(ps);
  assert(pos < ps->size&& pos >= 0);
  int start = pos;
  while (start < ps->size - 1)
  {
    ps->p[start] = ps->p[start + 1];
    ++start;
  }
  ps->size--;
}
//顺序表中的查找
int SeqlistFind(SL* ps, SlDATAType x)
{
  assert(ps);
  int i = 0;
  while (i < ps->size)
  {
    if (ps->p[i] == x)
    {
      return i;
    }
    ++i;
  }
  return -1;
}


今天就分享到这里,我们下期再见啦

相关文章
|
4月前
|
存储 算法 编译器
顺序表的实现(头插、尾插、头删、尾删、查找、删除、插入)
顺序表的实现(头插、尾插、头删、尾删、查找、删除、插入)
|
4月前
双链表的插入,删除以及遍历
双链表的插入,删除以及遍历
41 6
|
4月前
|
存储
单链表相关操作(插入,删除,查找)
单链表相关操作(插入,删除,查找)
42 4
|
3月前
|
算法
数据结构和算法学习记录——线性表之单链表(下)-头插函数、尾删函数、头删函数、查找函数、pos位置插入&删除数据、单链表销毁
数据结构和算法学习记录——线性表之单链表(下)-头插函数、尾删函数、头删函数、查找函数、pos位置插入&删除数据、单链表销毁
55 0
|
3月前
|
算法
数据结构和算法学习记录——线性表之双向链表(下)-头插函数、头删函数、查找函数、pos位置之前插入结点、pos位置删除结点及其复用、销毁链表函数
数据结构和算法学习记录——线性表之双向链表(下)-头插函数、头删函数、查找函数、pos位置之前插入结点、pos位置删除结点及其复用、销毁链表函数
22 0
|
4月前
(数据结构)单链表 —— 尾插,尾删,头插,头删,查找,插入,删除。
数据结构单链表 —— 尾插,尾删,头插,头删,查找,插入,删除
35 0
|
4月前
|
存储
数据结构:图文详解单链表的各种操作(头插法,尾插法,任意位置插入,删除节点,查询节点,求链表的长度,清空链表)
数据结构:图文详解单链表的各种操作(头插法,尾插法,任意位置插入,删除节点,查询节点,求链表的长度,清空链表)
524 0
|
4月前
|
存储
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
动态顺序表基本操作--头插头删尾插尾删
动态顺序表基本操作--头插头删尾插尾删
46 0
|
4月前
|
存储 算法 C++
链表基础知识(二、双向链表头插、尾插、头删、尾删、查找、删除、插入)
链表基础知识(二、双向链表头插、尾插、头删、尾删、查找、删除、插入)