顺序表1
#include <iostream>
using namespace std;#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef int ElemType;
struct SqList
{
ElemType* elem;
int length;
int listsize;
};
int InitList(SqList& La)
{
La.elem=new ElemType[LIST_INIT_SIZE];
if(La.elem==0)
return 0;
else
{
La.length=0;
La.listsize=LIST_INIT_SIZE;
return 1;
}
}
int main()
{
SqList L;
int i;
i=InitList(L);
if(i==1)
{
cout<<"初始化后,L.elem="<<L.elem<<endl;
cout<<"L.length="<<L.length<<endl;
cout<<"L.listsize"<<L.listsize<<endl;
}
return 0;
}
顺序表2
#include <iostream>
using namespace std;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef int ElemType;
struct SqList
{
ElemType* elem;
int length;
int listsize;
};
int InitList(SqList& La)
{
La.elem=new ElemType[LIST_INIT_SIZE];
if(La.elem==0)
return 0;
else
{
La.length=0;
La.listsize=LIST_INIT_SIZE;
return 1;
}
}
int main()
{
SqList L;
int i;
i=InitList(L);
if(i==1)
{
cout<<"L.elem="<<L.elem<<endl;
cout<<"L.length="<<L.length<<endl;
cout<<"L.listsize"<<L.listsize<<endl;
}
for(int j=0;j<=9;j++)
{
L.elem[j]=j+1;
}
for(int k=0;k<=9;k++)
{
cout<<L.elem[k]<<" ";
}
cout<<endl;
return 0;
}
逆序创建带头结点的链表
#include <iostream>
using namespace std;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef int ElemType;
struct SqList
{
ElemType* elem;
int length;
int listsize;
};
int InitList(SqList& La)
{
La.elem=new ElemType[LIST_INIT_SIZE];
if(La.elem==0)
return 0;
else
{
La.length=0;
La.listsize=LIST_INIT_SIZE;
return 1;
}
}
int main()
{
SqList L;
int i;
i=InitList(L);
if(i==1)
{
cout<<"L.elem="<<L.elem<<endl;
cout<<"L.length="<<L.length<<endl;
cout<<"L.listsize"<<L.listsize<<endl;
}
for(int j=0;j<=9;j++)
{
L.elem[j]=j+1;
}
for(int k=0;k<=9;k++)
{
cout<<L.elem[k]<<" ";
}
cout<<endl;
return 0;
}
顺序表删除
#include <iostream>
#include<malloc.h>
using namespace std;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef int ElemType;
struct SqList
{
ElemType* elem;
int length;
int listsize;
};
int InitList(SqList &La)
{
La.elem=new ElemType[LIST_INIT_SIZE];
if(La.elem==0)
return 0;
else
{
La.length=0;
La.listsize=LIST_INIT_SIZE;
return 1;
}
}
int ListInsert(SqList &La,int i,ElemType e)
{
ElemType* newbase;
ElemType* q;
ElemType* p;
if((i<1)||(i>La.length+1))
return 0;
if(La.length>=La.listsize)
{
newbase=(ElemType *)realloc((La).elem,(La.listsize+LISTINCREMENT)*sizeof(ElemType));
if(newbase==0)
return 0;
La.elem=newbase;
La.listsize=La.listsize+LISTINCREMENT;
}
for(int j=La.length;j>=i;j--)
{
La.elem[j]=La.elem[j-1];
}
La.elem[i-1]=e;
(La.length)++;
return 1;
}
int ListDelete(SqList &La,int i,ElemType &e1)
{
if(i<=0||i>La.length)
return 0;
e1=La.elem[i-1];
for(int k=i-1;k<=La.length-2;k++)
{
La.elem[k]=La.elem[k+1];
}
(La.length)--;
return 1;
}
int main()
{
SqList L;
ElemType e;
int i;
int j,k;
i=InitList(L);
cout<<"初始化L后L.elem="<<L.elem<<endl;
cout<<"L.length="<<L.length<<endl;
cout<<"L.listsize="<<L.listsize<<endl;
for(int h=1;h<=10;h++)
{
ListInsert(L,h,h);
}
for(int x=0;x<=9;x++)
{
cout<<L.elem[x]<<" ";
}
cout<<endl;
if(ListDelete(L,5,e)==1)
cout<<"删除的元素是"<<e<<endl;
return 0;
}