栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构顺序表操作示例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构顺序表操作示例

复制代码 代码如下:
#include
#include
#define maxsize 1024
typedef char datatype;
typedef struct
{
 datatype data[maxsize];
 int last;
}sequenlist;



int insert(sequenlist *L,datatype x,int i)
{
 int j;
 if(L->last==maxsize-1)
 {
  printf("overflow");
  return 0;
 }
 else if((i<0)||(i>L->last))
 {
  printf("error,please input the right 'i'");
  return 0;
 }
 else
 {
  for(j=L->last;j>=i;j--)
  {
   L->data[j+1]=L->data[j];
   L->data[i]=x;
   L->last=L->last +1;
  } 
 }
  return(1);
}



int dellist(sequenlist *L,int i)
{
 if((i<0)||(i>L->last))
 {printf("error,please input the right 'i'");
 return 0;}
 else
  {
   for(;ilast ;i++)
    L->data[i]=L->data[i+1];
    L->last =L->last-1;
    return(1);
  }
}



void createlist(sequenlist *L)
{
 int n,i;
 char tmp;
 printf("请输入元素个数:n");
 scanf("%d",&n);
 for(i=0;i {
  printf("data[%d]=",i);
  fflush(stdin);
  scanf("%c",&tmp);
  L->data[i] =tmp;
 }
 L->last=n-1;
 printf("/n");
}



void printflist(sequenlist *L)
{
 int i;
 for(i=0;ilast ;i++)
 {
  printf("data[%d]=",i);
  printf("%cn",L->data [i]);
 }
}


main()
{
 sequenlist *L;
 char cmd,x;
 int i;
 L=(sequenlist *)malloc(sizeof(sequenlist));  
 createlist(L);
 printflist(L);
 do
 {
  printf("i,I...插入n");
  printf("d,D...删除n");
  printf("q,Q...退出n");

 do
 {
  fflush(stdin);
  scanf("%c",&cmd);
 }while((cmd!='d')&&(cmd!='D')&&(cmd!='q')&&(cmd!='Q')&&(cmd!='i')&&(cmd!='I'));
 switch(cmd)
 {
  case 'i':
  case 'I':
   printf("请输入你要插入的数据:");
   fflush(stdin);
   scanf("%c",&x);
   printf("请输入你要插入的位置:");
   scanf("%d",&i);
   insert(L,x,i);
   printflist(L);
   break;

  case 'd':
  case 'D':
   printf("请输入你要删除的元素的位置:");
   fflush(stdin);
   scanf("%d",&i);
   dellist(L,i);
   printflist(L);
   break;
 }
 }while((cmd!='q')&&(cmd!='Q'));
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号