Problem C: 顺序表基本运算(线性表)


Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 35   Solved: 11
[ Submit][ Status][ Web Board]

Description

编写一个程序,实现顺序表的各种基本运算(假设顺序表的元素类型为char),主函数已给出,请补充每一种方法。

 

1、初始化顺序表L;

2、采用尾插法依次插入元素a,b,c,d,e;

3、输出顺序表L;

4、输出顺序表L的长度;

5、判断顺序表是否为空;

6、输出顺序表L的第三个元素;

7、输出元素a的位置;

8、在第四个元素位置插入元素f;

9、输出顺序表L;

10、删除L的第三个元素;

11、输出顺序表L;

12、释放顺序表L;

    

数据元素类型定义为

typedef char ElemType;

 

顺序表的定义为

typedef struct
{
    ElemType data[SizeMax];
    int length;
} SqList;
  
 

主函数:

int main()

{

    SqList *L;

    InitList(L);                            //初始化顺序表

    ElemType a,b,c,d,e;

    scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);

    Insert(L,a);

    Insert(L,b);

    Insert(L,c);

    Insert(L,d);

    Insert(L,e);                            //使用尾插法插入元素a,b,c,d,e

    Print(L);                               //输出顺序表

    PrintLength(L);                         //输出顺序表长度

    if(SqNull(L))

         printf("顺序表不为空\n");

    else printf("顺序表为空\n");            //判断顺序表是否为空

    PrintData(L,3);                         //输出第三个元素

    printf("元素a的位置:%d\n",Find(L,a));  //输出元素a的位置

    ElemType f;

    scanf("%c",&f);

    Insertinto(L,4,f);                      //将f插入到第四个位置

    Print(L);                               //输出顺序表

    Delete(L,3);                            //删除第三个元素

    Print(L);                               //输出顺序表

    free(L);                                //释放内存

    return 0;

}

Input

第一行输入五个元素a,b,c,d,e;接下来输入元素f;请根据题目编写算法。

Output

Sample Input

1 2 3 4 5
6

Sample Output

1 2 3 4 5
5
顺序表不为空
3
元素a的位置:1
1 2 3 6 4 5
1 2 6 4 5

HINT

请使用C++编译并提交

#include <stdio.h>
#include <stdlib.h>
#define SizeMax 10
typedef char ElemType;
typedef struct
{
     ElemType data[SizeMax];
     int length;
} SqList; void InitList(SqList *&L)
{
     L=(SqList *) malloc ( sizeof (SqList));
      L->length=0;
  
}
void Insert(SqList *&L,ElemType s)
{
  
     L->data[L->length]=s;
     L->length++;
}
void Print(SqList *&L)
{
     int i;
     for (i=0;i<L->length-1;i++)
         printf ( "%c " ,L->data[i]);
         printf ( "%c\n" ,L->data[L->length-1]);
}
void PrintLength(SqList *&L)
{
      printf ( "%d\n" ,L->length);
}
bool SqNull(SqList *&L)
{
     if (L->length) return 1;
     return 0;
  
}
void PrintData(SqList *&L, int n)
{
     printf ( "%c\n" ,L->data[n-1]);
}
bool Find(SqList *&L, char a)
{
     int i;
     for (i=0;i<L->length;i++)
         if (L->data[i]==a)
            return i+1;
}
void Insertinto(SqList *&L, int n, char f)
{
     int j;
         n--;
      for (j=L->length;j>n;j--)
      L->data[j]=L->data[j-1];
     L->data[n]=f;
     L->length++;
  
}
void Delete(SqList *&L, int n)
{
     int i;
   // if(n<1||n>L->length+1)
         //return 0;
         n--;
     for (i=n;i<L->length;i++)
         L->data[i]=L->data[i+1];
     L->length--;
     //return 1;
}
int main()
{
     SqList *L;
     InitList(L);                            //初始化顺序表
     ElemType a,b,c,d,e;
     scanf ( "%c %c %c %c %c%*c" ,&a,&b,&c,&d,&e);
     Insert(L,a);
     Insert(L,b);
     Insert(L,c);
     Insert(L,d);
     Insert(L,e);                            //使用尾插法插入元素a,b,c,d,e
     Print(L);                               //输出顺序表
     PrintLength(L);                         //输出顺序表长度
     if (SqNull(L))
          printf ( "顺序表不为空\n" );
     else printf ( "顺序表为空\n" );            //判断顺序表是否为空
     PrintData(L,3);                         //输出第三个元素
     printf ( "元素a的位置:%d\n" ,Find(L,a));  //输出元素a的位置
     ElemType f;
     scanf ( "%c" ,&f);
     Insertinto(L,4,f);                      //将f插入到第四个位置
     Print(L);                               //输出顺序表
     Delete(L,3);                            //删除第三个元素
     Print(L);                               //输出顺序表
     free (L);                                //释放内存
     return 0;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值