顺序表的简单实现
#include<iostream>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
using namespace std;
typedef int Status;
typedef struct
{
int *elem;
int length;
}Sqlist;
Status InitList(Sqlist &L)
{
L.elem = new int[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length = 0;
return OK;
}
Status GetElem(Sqlist L,int i,int &e)
{
if(i<1||i>L.length) return ERROR;
e = L.elem[i-1];
return OK;
}
Status LocateElem(Sqlist L,int e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return ERROR;
}
Status ListInsert(Sqlist &L,int i,int e)
{
if(i<1 || i>L.length+1) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1] = L.elem[j];
L.elem[i-1] = e;
++L.length;
return OK;
}
Status ListDelete(Sqlist &L,int i)
{
if(i<1 || i>L.length) return ERROR;
for(int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;
}
void ClearList(Sqlist &L)
{
L.length=0;
}
void DestroyList(Sqlist &L)
{
if(L.elem) delete L.elem;
}
Status ListLength(Sqlist L)
{
return(L.length);
}
int main()
{
int m,n,x,a,b,c,d,f,g,h,o,p,q;
cout <<"简单实现顺序表:"<< endl;
cout << "------------------------------------------------" << endl;
Sqlist L;
m=InitList(L);
if(m)
cout <<"顺序表创建成功!"<< endl;
else
cout <<"顺序表创建失败!"<<endl;
cout << "------------------------------------------------" << endl;
cout <<"请输入你想存储的个数n和n个数的值:";
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> x;
ListInsert(L,i,x);
}
cout << "------------------------------------------------" << endl;
cout <<"现在顺序表为:"<< endl;
for(int i=0;i<L.length;i++)
{
cout << L.elem[i] <<" ";
}
cout << endl;
cout << "------------------------------------------------" << endl;
cout <<"请输入需要查找位置的值:";
cin >> b;
m = LocateElem(L,b);
if(m)
{
cout << "查找成功!"<< endl;
cout << "此值位置为:"<< m <<endl;
}
else
cout << "查找失败!"<<endl;
cout << "------------------------------------------------" << endl;
cout<<"请输入需要插入的值及位置:";
cin >> c >> d;
m=ListInsert(L,d,c);
if(m)
{
cout <<"插入成功!"<<endl;
cout <<"现在顺序表为:"<<endl;
for(int i=0;i<L.length;i++)
{
cout << L.elem[i] <<" ";
}
cout <<endl;
}
else
cout << "插入失败!"<<endl;
cout << "------------------------------------------------" << endl;
cout <<"请输入需要删除第几个元素:";
cin >> g;
m = ListDelete(L,g);
if(m)
{
cout <<"删除成功!"<<endl;
cout<<"现在顺序表为:"<<endl;
for(int i=0;i<L.length;i++)
{
cout << L.elem[i] <<" ";
}
cout << endl;
}
else
cout <<"删除失败!"<< endl;
cout << "------------------------------------------------" << endl;
cout <<"请输入需要你要查找的元素:";
cin >> o;
m=LocateElem(L,o);
if(m)
{
cout <<"查找成功!"<<endl;
cout <<"这个元素在这个位置:"<< m <<endl;
}
else
cout <<"查找失败!" <<endl;
cout << "------------------------------------------------" << endl;
cout <<"请输入你取第几个元素:";
cin >> p;
m = GetElem(L,p,q);
if(m)
cout <<"这个元素是:"<< q <<endl;
else
cout <<"位置不合法,取值失败!"<<endl;
cout << "------------------------------------------------" << endl;
cout <<"清空顺序表"<<endl;
ClearList(L);
cout<<"顺序表已清空!"<<endl;
cout << "------------------------------------------------" <<endl;
cout <<"销毁顺序表"<<endl;
DestroyList(L);
cout <<"顺序表已销毁!"<<endl;
}
单链表的简单实现
#include<iostream>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
using namespace std;
typedef int Status;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
Status InitList(LinkList &L)
{
L=new LNode;
L->next = NULL;
return OK;
}
void CreateList_H(LinkList &L,int n)
{
L=new LNode;
L->next=NULL; int m;
cout <<"输入这几个元素:";
for(int i=0;i<n;++i)
{
LNode *p=new LNode;
cin >> m;
p->data=m;
p->next=L->next; L->next=p;
}
}
void CreatList_R(LinkList &L,int n)
{
L=new LNode;int m;
L->next=NULL;
LNode*r=L;
cout <<"输入这几个元素:";
for(int i=0;i<n;++i)
{
LNode*p=new LNode;
cin >>m;
p->data=m;
p->next=NULL;r->next=p;
r=p;
}
}
Status ListInsert(LinkList &L,int i,int e)
{
LNode*p=L;int j=0;
while(p&&(j<i-1))
{
p=p->next;++j;
}
if(!p||j>i-1) return ERROR;
LNode*s=new LNode;
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
int ListLength_L(LinkList L)
{
LinkList p;
p=L->next;
int i=0;
while(p)
{
i++;
p=p->next;
}
return i;
}
Status LocateElem(LinkList L,int e)
{
LNode*p=L->next;
int j=1;
while(p&&p->data!=e)
{
p=p->next;
j++;
}
return j;
}
Status GetElem(LinkList L,int i,int &e)
{
int j;
LNode*p=L->next;j=1;
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i) return ERROR;
e=p->data;
return OK;
}
Status ClearList(LinkList &L)
{
LNode *p,*q;
p=L->next;
while(p)
{
q=p->next;
delete p;
p=q;
}
L->next=NULL;
return OK;
}
Status ListDelete(LinkList &L,int i)
{
LNode *p=L;int j=0;
LNode *q;
while((p->next)&&(j<i-1))
{
p=p->next;
++j;
}
if(!(p->next)||(j>i-1)) return ERROR;
q=p->next;
p->next=q->next;
delete q;
return OK;
}
Status DestoryList_L(LinkList &L)
{
LinkList p;
while(L)
{
p=L;
L=L->next;
delete p;
}
return OK;
}
int main()
{
LinkList L;int x;
cout <<"单链表的简单实现:"<<endl<<endl;
cout <<"初始化链表:"<<endl;
x=InitList(L);
if(x)
cout <<"初始化成功!"<<endl;
else
cout <<"初始化失败!" ;
cout <<endl;
cout <<"前插法创建单链表:"<<endl;
cout <<"输入你想存入的元素个数:";
int n;
cin >> n;
CreateList_H(L,n);
cout <<"当前单链表元素为:"<<endl;
for(int i=1;i<=ListLength_L(L);i++)
{
int a;
GetElem(L,i,a);
cout <<a<<" ";
}
cout<<endl<<endl;
cout <<"后插法创建单链表:"<<endl;
cout <<"输入你想存入的元素个数:";
cin >> n;
CreatList_R(L,n);
cout <<"当前单链表元素为:"<<endl;
for(int i=1;i<=ListLength_L(L);i++)
{
int a;
GetElem(L,i,a);
cout <<a<<" ";
}
cout<<endl<<endl;
int j,g;
cout <<"输入你想插入的结点的位置和数值:";
cin >> j>>g;
x=ListInsert(L,j,g);
if(x)
cout <<"插入成功!"<<endl;
else
cout <<"插入失败!"<<endl;
cout<<endl;
int y;
cout<<"输入你想删除的元素的位置:";
cin >> y;
x=ListDelete(L,y);
if(x)
cout <<"删除成功!"<<endl;
else
cout <<"删除失败!"<<endl;
cout <<endl;
cout <<"当前单链表元素为:"<<endl;
for(int i=1;i<=ListLength_L(L);i++)
{
int a;
GetElem(L,i,a);
cout <<a<<" ";
}
cout<<endl<<endl;
cout<<"此时表长为:"<<ListLength_L(L)<<endl<<endl;
int e,c;
cout<<"输入你想取第几个值:";
cin >>c;
x=GetElem(L,c,e);
if(x)
{
cout<<"取值成功!"<<endl;
cout<<"该值为:"<<e<<endl;
}
else
cout<<"取值失败"<<endl;
cout<<endl;
cout<<"输入想查找的值:";
cin >>e;
cout<<"该值位置在:"<<LocateElem(L,e)<<endl<<endl;
cout <<"清空链表:"<<endl;
if(ClearList(L))
cout<<"已清除,"<<"现在的表长为:"<< ListLength_L(L)<<endl<<endl;
cout<<"销毁链表:"<<endl;
if(DestoryList_L(L))
cout <<"已销毁!"<<endl;
return 0;
}