依次为顺序表,单链表,双链表:
#include<iostream.h>
class shun
{
int Size;
int *a;
int length;
public:
shun(int MaxSize)
{
a=new int[MaxSize+1];
length=0;
Size=MaxSize;
}
~shun()
{
delete[] a;
}
void charu(int x,int y)
{
if(x>length+1||x<1)
{
cout<<"插入位置异常"<<endl;
return;
}
if(length==Size)
{
cout<<"线性表已满"<<endl;
return;
}
for(int i=length;i>+x+1;i--)
a[i+1]=a[i];
a[x]=y;
length++;
}
void shanchu(int x)
{
if(x>length||x<1)
{
cout<<"删除位置异常"<<endl;
return;
}
if(length==0)
{
cout<<"线性表为空"<<endl;
return;
}
for(int i=x+1;i<=length;i++)
a[i-1]=a[i];
length--;
}
void chazhao(int x)
{
int y=0;
for(int i=1;i<=length;i++)
if(a[i]==x)
{
y=i;
break;
}
if(y==0)
cout<<"查找失败"<<endl;
cout<<"a[y]="<<a[y]<<endl;
}
};
int main()
{
shun a(10);
for(int i=1;i<=10;i++)
a.charu(i,i);
for(i=1;i<=10;i++)
a.chazhao(i);
for(i=1;i<=10;i++)
a.shanchu(1);
return 0;
}
#include <string>
using namespace std;
#include<iostream>
using namespace std;
struct node
{
string name;
int date;
node *next;
};
class dan
{
node *front;
node *rear;
public:
dan()
{
node *x=new node;
x->next=NULL;
front=rear=x;
}
~dan()
{
node *x;
while(front!=NULL)
{
x=front;
front=front->next;
delete x;
}
}
void charu(string z,string x,int y)
{
node *m=front->next;
while(m!=NULL)
{
if(m->name==z)
{
node *n=new node;
n->date=y;
n->name=x;
n->next=m->next;
m->next=n;
break;
}
m=m->next;
}
}
void charu(string x,int y)
{
node *z=new node;
z->name=x;
z->date=y;
z->next=NULL;
rear->next=z;
rear=z;
}
void shanchu(string x)
{
node *z=front;
node *y=front->next;
while(y!=NULL)
{
if(y->name==x)
{
z->next=y->next;
delete y;
break;
}
z=z->next;
y=y->next;
}
}
void chazhao(string x)
{
node *y=front->next;
while(y!=NULL)
{
if(y->name==x)
cout<<y->name<<"成绩为:"<<y->date<<endl;
y=y->next;
}
}
};
int main()
{
dan a;
a.charu("李雷",88);
a.charu("王红",99);
a.chazhao("李雷");
a.chazhao("王红");
a.shanchu("李雷");
a.chazhao("李雷");
a.charu("王红","李雷",88);
a.chazhao("李雷");
return 0;
}
#include <string>
using namespace std;
#include<iostream>
using namespace std;
struct node
{
string name;
int date;
node *next;
node *front;
};
class dan
{
node *front;
node *rear;
public:
dan()
{
node *x=new node;
x->front=NULL;
x->next=NULL;
front=rear=x;
}
~dan()
{
node *x;
while(front!=NULL)
{
x=front;
front=front->next;
delete x;
}
}
void qiancharu(string z,string x,int y)
{
node *m=front->next;
while(m!=NULL)
{
if(m->name==z)
{
node *n=new node;
n->date=y;
n->name=x;
n->next=m;
n->front=m->front;
m->front->next=n;
m->front=n;
break;
}
m=m->next;
}
}
void houcharu(string z,string x,int y)
{
node *m=front->next;
while(m!=NULL)
{
if(m->name==z)
{
if(m->next==NULL)
{
node *q=new node;
q->name=x;
q->date=y;
q->next=NULL;
q->front=rear;
rear->next=q;
rear=q;
break;
}
node *n=new node;
n->date=y;
n->name=x;
n->next=m->next;
n->front=m;
m->next->front=n;
m->next=n;
break;
}
m=m->next;
}
}
void charu(string x,int y)
{
node *z=new node;
z->name=x;
z->date=y;
z->next=NULL;
z->front=rear;
rear->next=z;
rear=z;
}
void shanchu(string x)
{
node *z=front;
node *y=front->next;
while(y!=NULL)
{
if(y->name==x)
{
z->next=y->next;
z->next->front=z;
delete y;
break;
}
z=z->next;
y=y->next;
}
}
void chazhao(string x)
{
node *y=front->next;
while(y!=NULL)
{
if(y->name==x)
cout<<y->name<<"成绩为:"<<y->date<<endl;
y=y->next;
}
}
};
int main()
{
dan a;
a.charu("李雷",88);
a.charu("王红",99);
a.chazhao("李雷");
a.chazhao("王红");
a.shanchu("李雷");
a.chazhao("李雷");
a.qiancharu("王红","李雷",88);
a.houcharu("王红","张开",66);
a.chazhao("李雷");
a.chazhao("张开");
return 0;
}