国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

數據結構順序表操作示例

2020-01-26 15:34:38
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#include<stdio.h>
#include<malloc.h>
#define maxsize 1024
typedef char datatype;
typedef struct
{
 datatype data[maxsize];
 int last;
}sequenlist;


/*在第I個元素前插入數據x,元素從0開始計數*/
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);
}


/*刪除第i個元素,元素從0開始計數*/
int dellist(sequenlist *L,int i)
{
 if((i<0)||(i>L->last))
 {printf("error,please input the right 'i'");
 return 0;}
 else
  {
   for(;i<L->last ;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<n;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;i<L->last ;i++)
 {
  printf("data[%d]=",i);
  printf("%c/n",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'));
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 新乐市| 永善县| 丘北县| 广州市| 建阳市| 调兵山市| 闸北区| 报价| 华阴市| 遵化市| 临夏市| 甘南县| 六枝特区| 常山县| 开阳县| 岳普湖县| 婺源县| 宁阳县| 枣庄市| 理塘县| 高雄市| 马尔康县| 阳原县| 石嘴山市| 黄龙县| 赞皇县| 当涂县| 兴隆县| 日照市| 永州市| 高邑县| 长子县| 新竹市| 新源县| 庆城县| 越西县| 彰化县| 望都县| 奈曼旗| 南和县| 阿拉善盟|