#include<iostream>
using namespace std;
#define ElemType int
#define Status int
#define OK 1;
#define ERROR 0;
struct Node{
ElemType data;
struct Node *next;
};
Status GetElem(Node HeadPoint,int i,ElemType *e){
Node *pos=HeadPoint.next;
for(int j=0;j<i;j++){
pos=pos->next;
}
if(pos->next==NULL){
return ERROR;
}
*e=pos->data;
return OK;
}//查找节点
Status Insert(Node *HeadPoint,int i,ElemType e){
Node *pos=HeadPoint->next;
Node *NewNode=new Node();
NewNode->data=e;
for(int j=1;j<i;j++){
pos=pos->next;
}
if(pos->next==NULL){
return ERROR;
}
NewNode->next=pos->next;
pos->next=NewNode;
return OK;
}//插入节点,在第i个节点后插入
Status Delete(Node *HeadPoint,int i,ElemType *e){
Node *pos=HeadPoint->next;
for(int j=1;j<i-1;j++){
pos=pos->next;
}
if(pos->next==NULL){
return ERROR;
}
*e=pos->next->data;
pos->next=pos->next->next;
delete pos->next;
return OK;
}//删除节点
Status CreateHead(Node *HeadPoint,int i){
for(int j=0;j<i;j++){
Node *NewNode=new Node();
NewNode->data=j*j;
NewNode->next=HeadPoint->next;
HeadPoint->next=NewNode;
}
return OK;
}//头插法创建链表
Status CreatTail(Node *HeadPoint,int i){
Node *pos=HeadPoint;
while(pos->next!=NULL){
pos=pos->next;
}
for(int j=0;j<i;j++){
Node *NewNode=new Node();
NewNode->data=j*j;
NewNode->next=pos->next;
pos->next=NewNode;
pos=NewNode;
}
return OK;
}//尾插法创建链表
Status ClearList(Node *HeadPoint){
Node *pos=HeadPoint->next;
Node *node=new Node();
while(pos->next!=NULL){
node=pos->next;
delete pos;
pos=node;
}
HeadPoint->next=NULL;
return OK;
}//清空单链表
int main(){
Node *HeadPoint=new Node();\
HeadPoint->next=NULL;
//头指针
CreateHead(HeadPoint,10);
int e;
ClearList(HeadPoint);
getchar();
// GetElem(*HeadPoint,6,&e);
// cout<<e<<endl;
// Insert(HeadPoint,3,100);
// Insert(HeadPoint,8,200);
// getchar();
// Delete(HeadPoint,12,&e);
// cout<<e<<endl;
// Delete(HeadPoint,10,&e);
// cout<<e<<endl;
// CreatTail(HeadPoint,10);
// getchar();
return 0;
}