C语言实现顺序表

本文介绍了一个简单的序列列表数据结构的实现及其基本操作,包括初始化、插入、删除等,并提供了完整的C语言代码示例。适用于初学者理解数据结构及C语言编程。



#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++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值