2.3.3算法2.18 2.19

#include<stdio.h>

typedef struct DuLNode{
  int data;
  struct DuLNode *prior;
  struct DuLNode *next;
}DuLNode,*DuLinkList;

// it return a pointer of the ith element in the double linked list
DuLinkList GetElemP_DuL(DuLinkList L,int i){
  DuLinkList p;int j;
  p=L;
  for(j=1;j<=i;j++){
    p=p->next;
  }
  return p;
}

int main(){
  //first,we construct a double linked list, it value is 1 2 3
  DuLinkList L,A,B,C,p;

  L=(DuLinkList)malloc(sizeof(DuLNode));
  A=(DuLinkList)malloc(sizeof(DuLNode));
  B=(DuLinkList)malloc(sizeof(DuLNode));
  C=(DuLinkList)malloc(sizeof(DuLNode));

  L->next=A;L->prior=C;  A->data=1;A->prior=L;A->next=B; B->data=2;B->prior=A;B->next=C; C->data=3;C->prior=B;C->next=L;
  p=L->next;//p point to A

  ListInsert_DuL(L,2,5);

  int e;
  ListDelete_DuL(L,3,e);


  while(p && p!=L){
  printf("%d\n",p->data);
  p=p->next;
  }


  return 1;
}
//insert one number before ith number
int ListInsert_DuL(DuLinkList L,int i, int e){
  DuLinkList s;
  DuLinkList p;
  p=GetElemP_DuL(L,i);
  if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) return 0;
  s->data=e;
  s->prior=p->prior;p->prior->next=s;
  s->next=p;p->prior=s;
  return 1;
}

//delete the i-th number in the double linked list
int ListDelete_DuL(DuLinkList L,int i,int e){
  DuLinkList p;
  if(!(p=GetElemP_DuL(L,i))) return 0;
  e=p->data;
  p->prior->next=p->next;
  p->next->prior=p->prior;
  free(p);
  return 1;
}















 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值