ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

顺序表静态实现

2021-07-05 20:02:05  阅读:162  来源: 互联网

标签:InsertList 顺序 静态 ee 元素 ii 实现 int LL


一、步骤

1、定义一个结构体

#include <stdio.h>
#include <string.h>

#define MAXSIZE 100       // 顺序表的最大长度

typedef int ElemType;     // 自定义顺序表的数据元素为整数。

typedef struct
{
  ElemType data[MAXSIZE];   // 用数组存储顺序表中的元素
  unsigned int length;      // 顺序表中元素的个数
}SeqList,*PSeqList;

int main()
{
  return 0;
}

2、创建一个顺序表


int main()
{
  SeqList LL;     // 创建顺序表
  return 0;
}

3、创建一个数据元素


int main()
{
  SeqList LL;     // 创建顺序表
  ElemType ee;    // 创建一个数据元素。
  return 0;
}

4、初始化顺序表

void InitList(PSeqList LL)   
void ClearList(PSeqList LL)


int main()
{
  SeqList LL;     // 创建顺序表
  ElemType ee;    // 创建一个数据元素。
  InitList(&LL);  // 初始化顺序表。
  return 0;
}

******************************初始化顺序表*******************************************************
// 初始化顺序表
void InitList(PSeqList LL)
{
  ClearList(LL); // 清空顺序表。
}
******************************初始化顺序表*******************************************************


******************************清空顺序表。*******************************************************
// 清空顺序表。
void ClearList(PSeqList LL)
{
  if (LL == NULL) return;  // 检查空指针。

  LL->length=0;  // 表长置为0。
  memset(LL->data,0,sizeof(ElemType)*MAXSIZE);  // 数组元素清0。
}
******************************清空顺序表*******************************************************

5、在表中插入元素

int  InsertList(PSeqList LL, unsigned int ii, ElemType *ee);   

int main()
{
  SeqList LL;     // 创建顺序表
  ElemType ee;    // 创建一个数据元素。
  InitList(&LL);  // 初始化顺序表。
  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  return 0;
}

// 在顺序表LL的第ii个位置插入元素ee,返回值:0-失败;1-成功。
int InsertList(PSeqList LL, unsigned int ii, ElemType *ee)
{
  if ( (LL == NULL) || (ee == NULL) ) return 0;   // 检查空指针。

  if (LL->length >= MAXSIZE)
  {
    printf("顺序表已满,不能插入。\n"); return 0;
  }

  // 判断插入位置是否合法
  if ( (ii < 1) || (ii > LL->length+1) )
  {
    printf("插入位置(%d)不合法,应该在(%d-%d)之间。\n",ii,1,LL->length+1); return 0;
  }

  // 注意,元素后移只能用循环,不能用以下注释掉的方法,当元素是结构体时,以下方法不稳定。
  // if ( ii < LL->length+1)
  //    memcpy(LL->data+ii,LL->data+ii-1,(LL->length-ii+1)*sizeof(ElemType));

  // 把ii和ii之后的元素后移。
  int kk;
  for (kk=LL->length;kk>=ii;kk--)
  {
    // memcpy(LL->data+kk,LL->data+kk-1,sizeof(ElemType));  // 采用memcpy是为了支持ee为结构体的情况。
    memcpy(&LL->data[kk],&LL->data[kk-1],sizeof(ElemType));  // 用数组的形式也可以。
 	
  }

  //memcpy(LL->data+ii-1,ee,sizeof(ElemType));  // 采用memcpy是为了支持ee为结构体的情况。
  memcpy(&LL->data[ii-1],ee,sizeof(ElemType));  // 用数组的形式也可以。
  LL->length++;       // 表的长度加1。

  return 1;
}

6、 打印顺序表中全部的元素。


void PrintList(PSeqList LL);

int main()
{
  SeqList LL;     // 创建顺序表

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。
	
  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

	
  return 0;
}



// 打印顺序表中全部的元素。
void PrintList(PSeqList LL)
{
  if (LL == NULL) return;   // 检查空指针。

  if (LL->length == 0) { printf("表为空。\n"); return; }

  int kk;
  for (kk = 0; kk < LL->length; kk++)
  {
    // printf("%-3d", *(LL->data+kk));  // 如果元素ee为结构体,这行代码要修改。
    printf("%-3d", LL->data[kk]);  // 用数组的形式也可以。
  }

  printf("\n");
}

7、在表头插入元素

// 在顺序表LL的头结点插入元素ee,返回值:0-失败;1-成功。
int  PushFront(PSeqList LL, ElemType *ee);

int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  return 0;
}


// 在顺序表LL的头结点插入元素ee,返回值:0-失败;1-成功。
int  PushFront(PSeqList LL, ElemType *ee)
{
  return InsertList(LL,1,ee);
}

8、表尾插入元素(12)

// 在顺序表LL的尾结点插入元素ee,返回值:0-失败;1-成功。
int  PushBack(PSeqList LL, ElemType *ee);

int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  printf("表尾插入元素(12)\n");
  ee=12; PushBack(&LL, &ee);
  PrintList(&LL);

  return 0;
}

// 在顺序表LL的尾结点插入元素ee,返回值:0-失败;1-成功。
int PushBack(PSeqList LL, ElemType *ee)
{
  return InsertList(LL,LL->length+1,ee);
}

9、删除第7个元素

int  DeleteElem(PSeqList LL, unsigned int ii);  

int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  printf("表尾插入元素(12)\n");
  ee=12; PushBack(&LL, &ee);
  PrintList(&LL);

  printf("删除表中第7个元素。\n");
  DeleteElem(&LL,7); 
  PrintList(&LL);


  return 0;
}



// 删除顺序表LL中的第ii个元素,返回值:0-位置ii不合法;1-成功。
int DeleteElem(PSeqList LL, unsigned int ii)
{
  if (LL == NULL) return 0;   // 检查空指针。

  // 判断删除位置ii是否合法
  if ( (ii < 1) || (ii > LL->length) )
  {
    printf("删除位置(%d)不合法,应该在(%d-%d)之间\n",ii,1,LL->length); return 0;
  }

  // 把ii之后的元素前移。
  int k;
  for (k=ii;k<=LL->length;k++)
  {
    // memcpy(LL->data+k-1,LL->data+k,sizeof(ElemType));  // 采用memcpy是为了支持ee为结构体的情况。
    memcpy(&LL->data[k-1],&LL->data[k],sizeof(ElemType));   // 用数组的形式也可以。
  }

  LL->length--;       // 表的长度减1。

  return 1;
}

10、删除末尾元素


// 删除顺序表LL中尾元素,返回值:0-位置ii不合法;1-成功。
int PopBack(PSeqList LL);
int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  printf("表尾插入元素(12)\n");
  ee=12; PushBack(&LL, &ee);
  PrintList(&LL);

  printf("删除表中第7个元素。\n");
  DeleteElem(&LL,7); 
  PrintList(&LL);

  printf("删除表尾元素。\n");
  PopBack(&LL); PrintList(&LL);
  return 0;
}


// 删除顺序表LL中尾元素,返回值:0-位置ii不合法;1-成功。
int PopBack(PSeqList LL)
{
  return DeleteElem(LL, LL->length);
}

11、获取第n个元素的值

// 获取顺序表中第ii个元素的值,存放在ee中,返回值:0-失败;1-成功。
int  GetElem(PSeqList LL, unsigned int ii, ElemType* ee);  

int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  printf("表尾插入元素(12)\n");
  ee=12; PushBack(&LL, &ee);
  PrintList(&LL);

  printf("删除表中第7个元素。\n");
  DeleteElem(&LL,7); 
  PrintList(&LL);

  printf("删除表尾元素。\n");
  PopBack(&LL); PrintList(&LL);

  GetElem(&LL,3,&ee);
  printf("第3个元素的值是%d。\n",ee);

  return 0;
}



// 获取顺序表中第ii个元素的值,存放在ee中,返回值:0-失败;1-成功。
int  GetElem(PSeqList LL, unsigned int ii, ElemType* ee)
{
  if ( (LL == NULL) || (ee == NULL) ) return 0;   // 检查空指针。
  
  // 判断位置ii是否合法
  if ( (ii < 1) || (ii > LL->length) ) return 0;

  //memcpy(ee,LL->data+ii-1,sizeof(ElemType));  // 采用memcpy是为了支持ee为结构体的情况。
  memcpy(ee,&LL->data[ii-1],sizeof(ElemType));   // 用数组的形式也可以。

  return ;
}

12、获取元素值所在的位置

// 查找ee在顺序表LL中的位置,返回值:0-元素ee在表LL中不存在,>0元素ee在表LL中的位置。
int  LocateElem(PSeqList LL, ElemType *ee);

int main()
{
  SeqList LL;     // 创建顺序表。

  ElemType ee;    // 创建一个数据元素。

  InitList(&LL);  // 初始化顺序表。

  printf("在表中插入元素(1、2、3、4、5、6、7、8、9、10)。\n");
  ee=1;  InsertList(&LL, 1, &ee);
  ee=2;  InsertList(&LL, 1, &ee);
  ee=3;  InsertList(&LL, 1, &ee);
  ee=4;  InsertList(&LL, 1, &ee);
  ee=5;  InsertList(&LL, 1, &ee);
  ee=6;  InsertList(&LL, 1, &ee);
  ee=7;  InsertList(&LL, 1, &ee);
  ee=8;  InsertList(&LL, 1, &ee);
  ee=9;  InsertList(&LL, 1, &ee);
  ee=10; InsertList(&LL, 1, &ee);

  PrintList(&LL);

  printf("在表头插入元素(11)\n");
  ee=11; PushFront(&LL, &ee);
  PrintList(&LL);

  printf("表尾插入元素(12)\n");
  ee=12; PushBack(&LL, &ee);
  PrintList(&LL);

  printf("删除表中第7个元素。\n");
  DeleteElem(&LL,7); 
  PrintList(&LL);

  printf("删除表尾元素。\n");
  PopBack(&LL); PrintList(&LL);

  GetElem(&LL,3,&ee);
  printf("第3个元素的值是%d。\n",ee);


  ee=8;
  printf("元素值为8的位置是=%d\n",LocateElem(&LL,&ee));

  return 0;
}


// 查找ee在顺序表LL中的位置,返回值:0-元素ee在表LL中不存在,>0元素ee在表LL中的位置。
int LocateElem(PSeqList LL, ElemType *ee)
{
  if ( (LL == NULL) || (ee == NULL) ) return 0;   // 检查空指针。

  int kk;

  for (kk = 0; kk < LL->length; kk++)
  {
    // 如果元素ee为结构体,这行代码要修改。
    // if (*(LL->data+kk) == *ee) return kk+1; // 在表中对应序号应为kk+1。
    if (LL->data[kk] == *ee) return kk+1; // 用数组的形式也可以。
  }
    
  return 0;
}

标签:InsertList,顺序,静态,ee,元素,ii,实现,int,LL
来源: https://www.cnblogs.com/lnterpreter/p/14972214.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有