线性表中插入元素

算法2.4.写这个算法花了很长一段时间,唉,看来对线性表还是不够理解,继续努力,下面是代码
【线性表中插入元素】

#include using namespace std; #define maxsize 10 #define sizeincrement 5 typedef int elemtype; typedef struct { elemtype *elem; elemtype aa[maxsize]; int length; int listsize; }sqlist; int main() { sqlist *la,*newbase; int *p,*q; int i,len,pos,value; la=(sqlist *)malloc(maxsize *sizeof(sqlist)); cout<<"链表a的长度"<>len; cout<<"输入元素"<>la->aa[i]; cout<<"输入插入的元素和位置"<>value>>pos; if(pos<1||pos>len) cout<<"错误"<maxsize) { newbase=(sqlist *)realloc(la->elem,(maxsize+sizeincrement) *sizeof(sqlist)); if(!newbase) {cout<<"内存分配失败"<listsize=len-1; q=&(la->aa[pos-1]); for(p=&(la->aa[la->listsize]); p>=q; --p) {//cout<<*(p+1)<listsize; for(i=0; i<=la->listsize; ++i) cout<aa[i]<<" "; cout<




转载于:https://www.cnblogs.com/javaspring/archive/2011/08/30/2656473.html

    推荐阅读