码迷,mamicode.com
首页 > 其他好文 > 详细

线性表的应用

时间:2016-10-01 00:27:25      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

 

线性表的应用

标签:

原文地址:http://www.cnblogs.com/chang1203/p/5925034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!