顺序表
单链表
双向链表
顺序表
#include <iostream>
#include <cmath>
#include <cstdlib>
#define MAX 100
using namespace std;
typedef struct
{
int *elem;
int length;
}sqlist;
bool initiallist(sqlist &l) //初始化
{
l.elem=new int[MAX];
if(!l.elem)
return false;
l.length=0; //初始化为空
return true;
}
bool searchlist(sqlist l,int e) //查找表中的数据元素
{
for(int i=0;i<l.length-1;i++)
{
if(l.elem[i]==e)
return true;
}
return false;
}
bool getlist(sqlist l,int i,int &e) //在表中取第i个数据元素的值
{
if(i<1||i>l.length)
return false;
e=l.elem[i-1];
return true;
}
bool insertlist(sqlist &l,int i,int e) //在表中把e插入第i个位置
{
if(i<1||i>l.length)
return false;
if(l.length==MAX) //注意表已经满的情况,不能后移
return false;
for(int j=l.length-1;j>=i-1;j--)
{
l.elem[j+1]=l.elem[j];
} //第i-1个位置也要移动
l.elem[i-1]=e; //移动过后原来的i-1位置是空的,要赋值
l.length++;
return true;
}
bool deletelist(sqlist &l,int i) //删除表中的第i个数据元素
{
if(i<1||i>l.length)
return false;
for(int j=i;j<=l.length-1;j++)
{
l.elem[j-1]=l.elem[j];
}
l.length--;
return true;
}
int main()
{
sqlist *l; //在主函数中定义表
searchlist(l,0);
insertlist(l,3,1);
deletelist(l,3);
return 0;
}
单链表
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef struct lnode
{
int data;
struct lnode *next;
}lnode,*linklist;
bool creatlist(linklist &l)
{
l=new lnode;
if(!l)
return false;
l->next=NULL;
return true;
}
int getvalue(linklist l,int i)
{
linklist p;
int j=0;
int value;
p=l;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return 0;
value=p->data;
return value;
}
int searchlist(linklist l,int e)
{
linklist p;
p=l;
while(p&&p->data!=e)
{
p=p->next;
}
if(!p)
return 0;
return 1;
}
int insertlist(linklist &l,int i,int e)
{
linklist p,q;
int j=0;
p=l;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i-1)
return 0;
q=new lnode;
q->next=p->next;
p->next=q;
return 1;
}
int deletelist(linklist &l,int i)
{
linklist p,q;
int j=0;
p=l;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>n-1)
return 0;
q=p->next;
p->next=q->next;
delete q;
return 1;
}
int inserlisthead(linklist &l)
{
linklist p;
int n,num;
l=new lnode; //创建头节点
l->next=NULL;
cout<<"进行头插法"<<endl;
cout<<"请输入要插入的数据数:"<<endl;
cin>>n;
while (n--)
{
cout<<"请输入要插入的数据(逆序):"<<endl;
cin>>num;
p=new lnode; //生成新的节点
p->data=num;
p->next=l->next;
l->next=p;
}
return 1;
}
int insertlisttail(linklist &l)
{
linklist p,r;
int n,num;
l=new lnode;
l->next=NULL;
r=l;
cout<<"进行尾插法"<<endl;
cout<<"请输入要插入的数据数: "<<endl;
cin>>n;
while (n--)
{
p=new lnode; //生成新的节点
cout<<"请输入要插入的数:"<<endl;
cin>>num;
p->data=num;
p->next=NULL;
r->next=p;
r=p;
}
return 1;
}
双向链表
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef struct doublelnode
{
int data;
struct doublelnode *prior,*next; //有两个指针,一个指向前驱,一个指向后继
}doublelnode,*doublelinklist;
bool initialdoublelist(doublelinklist &l)
{
l=new doublelnode;
if(!l)
return false;
l->next=l->prior=NULL;
return true;
}
//查找和取值操作和单链表一致,这里不再重复操作
int insertdoublelist(doublelinklist &l,int i,int e)
{
doublelinklist p,q;
int j=0;
p=l; //把头指针赋给p,不要忘记这一步操作
while (p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return 0;
q=new doublelnode;
q->data=e;
q->prior=p->prior;
p->prior->next=q;
q->next=p;
p->prior=q;
return 1;
}
int deletedoublelist(doublelinklist &l,int e)
{
doublelinklist p;
int j=0;
p=l;
while (p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return 0;
p->prior->next=p->next;
if(p->next)
p->next->prior=p->prior;
delete p; //删除操作不要忘
return 1;
}