以下是我们老师写的添加节点,需要再写个删除节点的。要直接加在这个程序中的。谢谢!!
#include <stdio.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef char ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize ;
}SqList;
SqList * InitList()
{
SqList *l=NULL;
l->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!l->elem) {printf("malloc error!\n"); return NULL;}
l->length=0;
l->listsize=LIST_INIT_SIZE;
printf("ok!\n");
return l;
}
ListInsert_sq(SqList *l, int i, ElemType e)
{ ElemType *sq,*p,*q;
if ( i<1|| i> l->length+1) {printf("location overflow!\n"); return(0); }
if (l->length >= l->listsize)
{ sq = (ElemType *) realloc (l->elem, (l->listsize + LIST_INCREMENT)
* sizeof (ElemType));
if (!sq) {printf("malloc error!\n"); return 0;}
l->elem = sq;
l->listsize += LIST_INCREMENT;
}
p = & l->elem[l->length - 1];
q = & l->elem[i - 1];
for (p; p >= q; --p)
*(p+1) = *p;
*q = e;
l->length ++;
return 1;
}
ElemType GetElem(SqList *l,int i)
{
return l->elem[i-1];
}
int ListLength(SqList *l)
{ return l->length;
}
main()
{ int i,n;
SqList *l;
l=InitList();
ListInsert_sq(l,1,'C');
ListInsert_sq(l,2,'h');
ListInsert_sq(l,3,'i');
ListInsert_sq(l,4,'n');
ListInsert_sq(l,5,'a');
n=ListLength(l);
for(i=1;i<=n;i++)
printf("%c", GetElem(l,i));
printf("\n");
}