编辑代码

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct LinearList/*定义线性表结构*/
{
    int *list;       /*存线性表元素*/
    int size;        /*存线性表长度*/
    int Maxsize;     /*存list数组元素的个数*/
};
typedef struct LinearList LIST;
void InitList(LIST *L,int ms)/*初始化线性表*/
{
    if((L->list=(int*)malloc(ms*sizeof(int)))==NULL)
       {
           printf("内存申请错误");
             exit(1);
       }
       L->size=0;
    L->Maxsize=ms;
}
int InsertList(LIST *L,int item,int rc)/*item记录值;rc插入位置*/
{
    int i;
    if(L->size==L->Maxsize)/*线性表已满*/
       return -1;
    if(rc<0)
        rc=0;
    if(rc>L->size)
        rc=L->size;
    for(i=L->size-1;i>=rc;i--)/*将线性表元素后移*/
       L->list[i+=1]=L->list[i];
       L->list[rc]=item;
       L->size++;
       return 0;
}
void OutputList(LIST *L)/*输出线性表元素*/
{
    int i;
    for(i=0;i<L->size;i++)
        printf("%d",L->list[i]);
        printf("\n");
}
int FindList(LIST *L,int item)/*查找线性元素,返回值>=0为元素的位置,返回-1为没找到*/
{
    int i;
    for(i=0;i<L->size;i++)
        if(item==L->list[i])
        return i;
    return -1;
}
int DeleteList1(LIST *L,int item)/*删除 指定元素值得线性表记录,返回值为>=0为删除成功*/
{
    int i,n;
    for(i=0;i<L->size;i++)
        if(item==L->list[i])
        break;
    if(i<L->size)
    {
        for(n=i;n<L->size-1;n++)
            L->list[n]=L->list[n+1];
        L->size--;
        return i;
    }
    return -1;
}
int DeleteList2(LIST *L,int rc)/*删除指定位置的线性表记录*/
{
    int i,n;
    if(rc<0||rc>=L->size)
        return -1;
    for(n=rc;n<L->size-1;n++)
    L->list[n]=L->list[n+1];
    L->size--;
    return 0;
}
int main()
{
    LIST LL;
       int i,r;
       InitList(&LL,10);
       printf("list addr=%p\tsize=%d\tMaxsize=%d\n",LL.list,LL.list,LL.Maxsize);
 
       while(1)
       {
           printf("请输入元素值,输入0结束插入操作:");
           fflush(stdin);/*清空标准输入缓冲区*/
           scanf("%d",&i);
           if(i==0)
            break;
           printf("请输入插入位置:");
           scanf("%d",&r);
           InsertList(&LL,i,r-1);
           printf("线性表为:");
           OutputList(&LL);
       }
       while(1)
       {
           printf("请输入查找元素值,输入0结束查找操作:");
           fflush(stdin);/*清空标准输入缓冲区*/
           scanf("%d ",&i);
           if(i==0)
            break;
           r=FindList(&LL,i);
           if(r<0)
            printf("没有找到\n");
           else
             printf("有符合条件的元素,位置为:%d\n",r+1);
       }
       while(1)
       {
           printf("请输入删除元素值,输入0结束查找操作:");
           fflush(stdin);/*清楚标准缓存区*/
           scanf("%d",&i);
           if(i==0)
            break;
           r=DeleteList1(&LL,i);
            if(i<0)
                printf("没有找到\n");
            else{
                printf("有符合条件的元素,位置为:%d\n线性表为:",r+1);
                OutputList(&LL);
            }
        }
        while(1)
       {
           printf("请输入删除元素位置,输入0结束查找操作:");
           fflush(stdin);/*清楚标准输入缓冲区*/
           scanf("%d",&r);
           if(r==0)
            break;
           i=DeleteList2(&LL,r-1);
           if(i<0)
            printf("位置越界\n");
           else
           {
               printf("线性表为:");
               OutputList(&LL);
           }
       }
}