数据结构----顺序表

1.顺序表的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static final int MAXLEN=100;                              //定义顺序表的最大长度
 
class DATA
{
    String key;                                      //结点的关键字
    String name;
    int age;
}
                                                          //定义结点
class SLType                                            //定义顺序表结构
{
    DATA[] ListData = new DATA[MAXLEN+1];                 //保存顺序表的结构数组
    int ListLen;                                                    //顺序表已存结点的数量
}

2.初始化顺序表

1
2
3
4
void SLInit(SLType SL)                        //初始化顺序表
{
    SL.ListLen=0;                                //初始化为空表
}

3.计算顺序表长度

1
2
3
4
int SLLength(SLType SL)
{
    return(SL.ListLen);            //返回顺序表的元素数量
}

4.插入结点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int SLInsert(SLType Sl,int n,DATA data)
{
    int i;
    if(SL.ListLen>=MAXLEN)                                  //顺序表结点数量已超过最大数量
    {
        System.out.print("顺序表已满,不能插入结点!\n");
        return 0;                                                //返回0表示插入不成功
    }
    if(n<1||n>SL.ListLen-1)                                  //插入结点序号不正确
    {
        System.out.print("插入元素序号错误,不能插入元素!\n");
        return 0;
    }
    for(i=SL.ListLen;i>=n;i--)                               //将顺序表中的数据向后移动
    {
        SL.ListData[i+1]=SL.ListData[i];
    }
    SL.ListData[n]=data;                                       //插入结点
    SL.ListLen++;                                              //顺序表结点数量增加1
    return 1;                                                    //成功插入,返回1
}

6.追加结点

1
2
3
4
5
6
7
8
9
10
int SLAdd(SLType SL,DATA data)            //增加元素到顺序表尾部
{
    if(SL.ListLen>=MAXLEN)                    //顺序表已满
    {
        System.out.print("顺序表已满,不能再添加结点了!\n");
        return 0;
    }
    SL.ListData[++SL.ListLen]=data;
    return 1;
}

7.删除结点int SLDelete(SLType SL,int n) //删除顺序表中的数据元素

1
2
3
4
<em id="__mceDel">{
    int i;
    if(n<1||n>SL.ListLen+1)          //删除结点序号不正确<br>  {<br>    System.out.print("删除结点序号错误,不能删除结点!\n");<br>    return 0;<br>  }<br>  for(i=n;i<SL.ListLen;i++)          //将顺序表中的数据向前移动<br>  {<br>    SL.ListData[i]=SL.ListData[i+1];<br>  }<br>  SL.ListLen--;                //顺序表元素数量减1<br>  return 1;<br>}<br>
</em>

8.按照序号查找结点

1
2
3
4
5
6
7
8
9
DATA SLFindByNum(SLType SL,int n)
{
    if(n<1||n>SL.ListLen+1)
    {
        System.out.print("结点序号错误,不能返回结点!\n");
        return 0;
    }
    return SL.ListData[n];
}

9.按照关键字查找结点

1
2
3
4
5
6
7
8
9
10
11
12
int SLFindByCont(SLType SL,String key)
{
    int i;
    for(i=1;i<=SL.ListLen;i++)
    {
        if(SL.ListData[i].key.compareTo(key)==0)
        {
            return i;
        }
    }
    return 0;
}

10.显示所有结点

1
2
3
4
5
6
7
8
int SLAll(SLType SL)
{
    int i;
    for(i=1;i<=SL.ListLen;i++)
    {
        System.out.print("(%s,%s,%d)\n",SL.ListData[i].key,SL.ListData[i].name,SL.ListData[i].age);
    }
}

  

  

      

 

posted on   Mathematics  阅读(204)  评论(0编辑  收藏  举报
努力加载评论中...

点击右上角即可分享
微信分享提示