双链表


typedef struct DoubleLink{
 int data;
 DoubleLink *next;
 DoubleLink *pre;
    int length;
}Dnode;

Dnode *Create();
Dnode * Del(Dnode * H,int num);

 

#include <iostream>
#include <stdlib.h>
#include "DoubleLink.h"
using namespace std;

Dnode *Create()
{
 Dnode *H,*p,*s;
 int data,cycle = 1;
 H = (Dnode*)malloc(sizeof(Dnode));

 H->data = -1;
 H->pre = H;
 H->next = H;
 H->length = 0;

 p = H;

 while(cycle)
 {
  printf("please input data:");
  scanf("%d",&data);
  if(data>0)
  {
   s = (Dnode*)malloc(sizeof(Dnode));
   s->data = data;
   s->next = NULL;
   p->next = s;
   s->pre = p;
   p=s;
   H->length ++;
  }
  else cycle = 0;
 }
    return H;
}
Dnode * Del(Dnode * H,int num)
{
 Dnode *p ;
 p = H->next;
 while(num!=p->data&&p->next!=NULL)
  p = p->next ;
 if(num = p->data )
 { 
  if(p->next==NULL)
  { 
   p->pre->next = NULL;
   free(p);
  }
  else
  {
   p->pre->next = p->next ;
   p->next->pre = p->pre ;
   free(p);
  }
 }
 return H;
}
void main()
{
 Dnode *L,*p,*q;
 int num;
 L = Create();
    p = L->next;
    for(int i=0;i<L->length;i++)
 {
  printf("/nLink data is:%d/n",p->data);
  p = p->next;
 }
 scanf("%d",&num);
    q=Del(L,num);
 for(i=0;i<L->length;i++)
 {
  printf("/nLink data is:%d/n",q->next->data);
  q = q->next;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值