二、线性表的顺序存储(附)

1.双链表

初始化

typedef struct Dnode{
ElemType data;
struct Dnode *prior,*next;
}Dnode,*DLinklist;

bool InitDLinklist(DLinklist &L)
{
L=(Dnode *)malloc(sizeof(Dnode));
if(L==null)
	return false;
L-prior=null;
L->next=null;
return true;
}

插入
插入需要考虑,如果在尾部插入,则如何处理尾结点,next指向null

//在p结点后插入s结点
bool InsertNextDnode(DNode *p,Dnode *s)
{
if(s==null||p=null)
return false;
//if(p->next==null)//若p无后继结点
if(p->next==null)//尾结点   ????
	{
	s->next=P->next;
	p->prior=s;
	}
s-next=p->next;
p->next->prior=s;//如果是尾结点,error
s->prior=p;
p->next=s;
return true;
}

删除

//删除p结点的后继结点q
bool DeleteNextDnode(Dnode *p)
{
Dnode *q;//指向被删除结点
if(p->next==null||p=null)
	return false;//尾结点不可以删
q=p->next;
q->next->prior=p;
p->next=q->next;
free(q);
return true;
}

2.循环单链表

typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode,* Linklist;
//初始化
bool InitList(Linklist &L)
{
L=(Lnode *)malloc(sizeof(Lnode));
if(L==null)
return false;
L->next=L;
return true;
}

//判表空
if(L->next==L)
return true;//表空

3.循环双链表

typedef InitDLinklist(DLinklist &L)
{
ElemType data;
struct Dnode *prior,*next;
}Dnode,*DLinklist;
//初始化
bool InitDLinklist(DLinklist &L)
{
L=(Lnode *)malloc(sizeof(Lnode));
if(L==null)
return null;
L->next=L;
L->prior=L;
return ture;
}
//判表空
if(L->next=L)
return true;

//插入;在p后插入q
bool InsertNextDnode(Dnode *p,Dnode *q)
{
//与双链表不同无需考虑边界
}

4.静态链表

分配连续的一整片空间,插入,删除等只需要修改指针,不需要移动元素,以next==-1为结束标志。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只爱圣女果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值