#include
#include
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*linklist;
// 头插法建立单链表
void HeadInsert(linklist &L){
int x;
L = (linklist)malloc(sizeof(LNode)); //申请头节点
L->next = NULL;
cout<<"请输入链表每一项,当输入为999时完成链表:";
cin>>x;
while (x!=999) {
LNode *s = new LNode;
s->data=x;
s->next = L->next;
L->next = s;
cin>>x;
}
}
//尾插法建立单链表
void Tailinsert(linklist &L){
LNode *p;
int x;
L = (linklist)malloc(sizeof(LNode));
p=L;
cout<<"请输入链表每一项,当输入为999时完成链表:";
cin>>x;
while (x!=999) {
LNode *s =new LNode;
s->data =x;
p->next = s;
p = s;
cin>>x;
}
p->next=NULL;
}
//按序号查找节点值
LNode* GetElem(linklist L,int i){
int j= 1;
LNode *p = L->next;
if (i==0) {
return L;
}
if (i<1) {
return NULL;
}
while (p&&jnext;
j++;
}
return p;
}
//按值查找
LNode* GetElemByint(linklist &L,int x){
LNode *p=L->next;
while (p!=NULL&&p->data!=x) {
p=p->next;
}
return p;
}
//插入节点(前插)先查找前驱再插入
void Insertlink(linklist &L,int i,int x){
LNode *p = GetElem(L, i-1);
LNode *s = new LNode;
s->next = p->next;
p->next = s;
s->data=x;
}
//删除节点
void DelatePoint(linklist &L,int i){
LNode *p = GetElem(L, i-1);
LNode *s =new LNode;
s=p->next;
p->next=s->next;
free(s);
}
int GetLength(linklist L){
LNode *p = L->next;
int count = 0;
while (p!=NULL) {
p=p->next;
count++;
}
return count;
}
// 打印输出链表
void Printflinklist(linklist L){
LNode *p = L->next;
while (p!=NULL) {
cout<data<<" ";
p = p->next;
}
cout<