数据结构基本结构:顺序表




#include<stdio.h>
#define MAX 100

char List[MAX] ;

/*
 * Initializes the list.
 * */
void InitializeList()
{
    int i;
    for( i=0;i<10;i++)
    {
        List[i] = 'A'+i;
    }
}

/***************Gets the element count in array.******************/
int GetListLength()
{
    int length = 0;
    char *p = List;
   
    while((*p)!='/0')
    {
        p++;
        length++;
    }
   
    return length;       
}

/*
 * Shows all elements in the List.
 * */
void ShowData()
{
    char *p = List;
   
    while((*p)!='/0')
    {
        printf("%c/t",*p);   
        p++;
    }
    printf("/n");
}

/*
 *  Adds a new element to the array.
 *
 * */
void AddElement(char data, int index)
{
    int i;
    int length = GetListLength();
   
    //Checks whether the list is full now.
    if(length == MAX - 1)
    {
        printf("The List is full now");
        return;
    }
   
    if((index<0) || (index>length))
    {
        printf("The insert index is inlegal");
        return;
    }

    for(i=length - 1;i>=index;i--)
    {
        List[i+1] = List[i];   
    }
    List[index] = data;
   
}

/*
 * Deletes element by the given index.
 * */
void DeleteElement(int index)
{
    int i;
   
    int length = GetListLength();
   
    //Checks whether the list is full now.
    if(length == 0)
    {
        printf("The List is empty now");
        return;
    }
   
    if((index<0) || (index>length - 1))
    {
        printf("The delete index is inlegal");
        return;
    }
   
    for(i=index;i<length;i++)
    {
        List[i] = List[i+1];
    }
    List[i] = '/0';
       
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值