#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define ms(a) memset(a,0,sizeof(a))
typedef int Elementtype;
struct node{
Elementtype element;
node* next=NULL;
};
typedef node* LIST;
typedef node* position;
/* End
返回最后一个元素的位置
*/
position End(LIST L){
position p=L;
while(p->next!=NULL){
p=p->next;
}
return p;
}
/* Insert
在 p 处下一个位置插入元素 x
*/
void Insert(Elementtype x,position p){
position tem;
tem=p->next;
p->next=new node;
p->next->element=x;
p->next->next=tem;
}
/* Delete
删除位于 p 后面一个位置的元素
*/
void Delete(position p){
position t;
if(p->next!=NULL){
t=p->next;
p->next=t->next;
delete t;
}
}
/* Locate
返回 L 中 x 的位置
*/
position Locate(Elementtype x,LIST L){
position p=L;
if(p->next!=NULL){
if(p->next->element==x){
return p;
}
else{
p=p->next;
}
}
return p;
}
/* Makenull
将 L 置空
*/
position Makenull(LIST &L){
L=new node;
L->next=NULL;
return L;
}
void Print(LIST L){
position p=L->next;
if(p==NULL){
cout<<"List is empty."<<endl;
}
else{
while(p!=NULL){
cout<<p->element<<" ";
p=p->next;
}
cout<<endl;
}
}
int main(){
LIST L=new node;
Insert(1,L);
Insert(2,L);
Insert(3,L);
Insert(4,L);
Print(L);
Delete(L);
Print(L);
cout<<Locate(2,L)<<endl;
cout<<End(L);
return 0;
}
线性表——指针实现
最新推荐文章于 2023-04-27 09:43:59 发布