📄 list.h
字号:
#include<iostream.h>
const ok=1;
const error=0;
const overlow=0;
const underlow=0;
const yes=1;
const no=0;
typedef struct LNode
{ int data;
struct LNode *next;
} LNode,* LinkList;
void creat_sqlist(LinkList &L,int n) //创建线形表
{ L= NULL; int i; LNode *p; //生成头指针
for (i=n;i>0;--i) //从n到1
{ p=new LNode; //生成新结点
cin>>p->data; //逆序输入anan-1 …. a1
p->next=L; L=p;}
}
int ListLength_L( LinkList L ) //求长度
{ //L为链表的头指针,本函数返回 L 所指链表的长度
LNode *p; int k=0;
p=L;
while (p) { k++; p=p->next; } //k计非空结点数
return k;
} // ListLength_L
int getelem_sq(LinkList L,int i,int e) //取元素
{ LNode *p; int k;
p=L;
for(k=1;k<=i;k++)
p=p->next;
e=p->data;
return e;
}
int require_qianji(LinkList L,int i) //求前继
{ LNode *p; int k,e;
p=L;
if ( i==1)
return error;
else
{ for(k=1;k<=i-1;k++)
p=p->next;
e=p->data;
return e;}
} // LocateElem_Sq
int require_houji(LinkList L,int i) //求后继
{ LNode *p; int k,e;
p=L;
for(k=1;k<=i+1;k++)
p=p->next;
e=p->data;
return e;
} // LocateElem_Sq
LNode* LocateElem_L( LinkList L,int e ) //查找
{ LNode *p;
p=L;
while ( p && p->data != e ) p=p->next;
return p;
} // LocateElem_L
int ListInsert_L(LinkList &L,int i,LNode *s) //插入
{ LNode *p;int j=1;
if (i==1) {s->next=L;L=s;} //在表头插入
else
{ p=L;
while(p->next && j<i-1){p=p->next;++j;}
if(!p||j>i-1) return error;
s->next=p->next; p->next=s;
return ok;}
}//ListInsert_L
int ListDelete_L(LinkList &L,int i,int &e) //删除
{ LNode *p,*q; int j=1;
if (i==1) {q=L;L=L->next;}
else
{ p=L;
while (p->next &&j<i-1) //指针P定位到ai的前驱结点
{ p=p->next;++j;}
q=p->next; p->next=q->next;
} //修改指针
e=q->data; delete q; //释放结点空间
return ok;
}
int ListEmpty(LinkList L) //判定空表
{ LNode *p;
p=L;
if(p=NULL)
return yes;
else
return no;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -