|
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define Maxsize 10
- typedef int Elemtype; //将int定义为Elemtype
- typedef struct{
- Elemtype *elem;
- int length;
- int listsize;
- } sqlist;
- //初始化一个顺序表
- //参数L:sqlist类型的指针
- void initSqlist(sqlist *L)
- {
- L->elem=(int *)malloc(Maxsize*sizeof(Elemtype));
- if(!L->elem) exit(0);
- L->length=0;
- L->listsize=Maxsize;
- }
- void insertElem(sqlist *L,int i,Elemtype item) //向第i个位置上插入item
- {
- //Elemtype *base,*insertPtr,*p;
- Elemtype *insertPtr,*p;
- Elemtype *base;
- if(i<1||i>L->length+1) exit(0);
- if(L->length>=L->listsize)
- {
- [color=Red] base=(Elemtype*)realloc(L->elem,(L->listsize+10)*sizeof(Elemtype));[/color]
- if(!base) exit(0);
- L->elem=base;
- // L->elem=(int *)realloc(L->elem,20*sizeof(int));
- L->listsize=L->listsize+10;
- }
- insertPtr=&(L->elem[i-1]);
- for(p=&(L->elem[L->length+1]);p>insertPtr;p--)
- *(p+1)=*p;
- *insertPtr=item;
- L->length++;
- }
- void delElem(sqlist *L,int i) //删除第i个元素
- {
- Elemtype *delItem,*q;
- if(i<1||i>L->length) exit(0);
- delItem=&(L->elem[i-1]);
- q=L->elem+L->length-1;
- for(++delItem;delItem<=q;++delItem)
- *(delItem-1)=* delItem;
- L->length--;
- }
- void main()
- {
- sqlist l;
- int i;
- initSqlist(&l);
- printf("biaozhi1\n");
- for(i=0;i<15;i++)
- { insertElem(&l,i+1,i+1);
- printf("biaozhi2\n");
- }
- printf("\nThe content of the list is \n");
- for(i=0;i<l.length;i++)
- printf("%d",l.elem[i]);
- delElem(&l,5);
- printf("\nDelete the fifth element\n");
- for(i=0;i<l.length;i++)
- printf("%d",l.elem[i]);
- printf("\n");
- }
复制代码 这个程序在VC上可以通过 但gcc却通不过
找了好多资料都说是指针没初始化 我觉得我的没这个问题啊 问题锁定再realloc这个函数上 |
|