线性表的应用

#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE  100
#define LISTINCREMENT  10
typedef struct{
    int *elem;
    int length;
    int listsize;
}SqList;
void InitList_Sq(SqList &L){
    L.elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
    //if(! L.elem)exit(OVERFLOW);
    L.length = 0;
    L.listsize = LISTINCREMENT;
    printf("初始化成功\n");
}//InitList_Sq
void ListInsert_Sq(SqList L,int i,int e){
    if(L.length>=L.listsize){
        int *newbase = (int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof
        (int));
        //if(!newbase)exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT; 
    } 
    int *q = &(L.elem[i-1]);
    for(int *p = &(L.elem[L.length - 1]);p>=q;--p)*(p+1) = *p;
    *q = e;
    ++L.length;
    printf("成功\n"); 
}//ListInsert_Sq
void ListDelete_Sq(SqList L,int i,int e){   
    int *p = &(L.elem[i-1]);
    e = *p;
    int *q = L.elem + L.length - 1;
    for(++p;p<=q;++p) *(p-1)=*p;
    --L.length;
    printf("你要删除的值:%d\n",e); 
}//ListDelete_Sq
void main(){
    int e=0;
    SqList L;
    int i=0;
    int k;
    printf("欢迎进入系统:\n");
    int j=1;
    //scanf("%d\n",&j);
    InitList_Sq(L);
    while(j==1){
    printf("插入值选1,删除选2.\n");
    printf("请选择你想要进行的操作: \n");
    scanf("%d", &k);
    if(k==1){
    printf("请输入你想要插入的位置:"); 
    scanf("%d", &i);
    printf("请输入你想要输入的数据: "); 
    scanf("%d", &e);
    ListInsert_Sq(L,i,e);
    k=0;j=0;
    printf("请选择是否继续,继续选择1,退出选择2:\n");
    scanf("%d", &j);
    }
    if(k==2){
    printf("请输入你想要删除的数据的位置: ");
    scanf("%d", &i);
    ListDelete_Sq(L,i,e);
    k=0;j=0;
    printf("请选择是否继续,继续选择1,退出选择2:\n");
    scanf("%d", &j);
    }
    }
    return 0;
}

 

posted @ 2016-09-30 22:00  旷野足迹  阅读(318)  评论(0编辑  收藏  举报