双链表元素的删除和插入(VS2010测…

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <conio.h>

using namespace std;


typedef struct student

{

int data;

struct student *next;

struct student *pre;

}dnode;


//建立双链表

dnode *creat()

{

dnode *head, *p, *s;

int x, cycle 1;

head (dnode *)malloc(sizeof(dnode));

head;

while(cycle)

{

printf("\nplease input the data: ");

scanf("%d",&x);

if(x != 0)

{

(dnode *)malloc(sizeof(dnode));

s->data x;

printf("\n %d",s->data);

p->next s;

s->pre p;

s;

}

else

cycle 0;

}

head head->next;

head->pre NULL;

p->next NULL;

printf("\n  yyy  %d",head->data);

return (head);

}

//链表测长

int length(dnode *head)

{

int 0;

dnode *p;

head;

while(p != NULL)

{

p->next;

n++;

}

return (n);

}

//链表打印

void print(dnode *head)

{

dnode *p;

int n;

=length(head);

printf("\nNow,These %d records are :\n",n);

head;

if(head != NULL)   //空链表

{}

while(p != NULL)

{

printf("  uuu  %d  \n",p->data);

p->next;

}

}

//双链表删除节点

dnode *del(dnode *head, int x)

{

dnode *p1, *p2;

    p1 head;

while((x != p1->data) && (p1->next != NULL))  //元素x不是首节点&&没到链尾

{

p1 p1->next;

}


if(x p1->data)

{

if(p1 == head)         //被删除节点为首节点

{

head head->next;

head->pre NULL;

free(p1);

}

else if(p1->next == NULL)

{

p1->pre->next NULL;

free(p1);

}

else

{

p1->next->pre p1->pre;

p1->pre->next p1->next;

}

}

else

printf("\n%d cound not been found",x);

return (head);

}

//双链表插入节点(在第k个节点后插入)

dnode *insert(dnode *head, int k, int x)

{

dnode *p0, *p1;

int i, L;

length(head);

p1 head;


p0 (dnode *)malloc(sizeof(dnode));   //创建新节点

p0->data x;


if(k 0)

printf("\nout of bounds");


for(i=1; (i<k)&&p1; i++)

{

p1 p1->next;

}


if(k)

{

if(k == L)

{

p1->next p0;

p0->pre p1;

p0->next NULL;

}

else

{

p0->next p1->next;

p0->pre p1;

p1->next p0;

p1->next->pre p0;

}

}

else

{

p0->next head;

p0->pre NULL;

p1->pre p0;

head p0;

}

return (head);

}


int main(int, char**, char**)

{

dnode *a, *head_del, *head_ins;

creat();

length(a);

print(a);


printf("\nPlease enter the data which you want to delete: ");

int x;

scanf("%d",&x);

head_del del(a, x);

print(head_del);

printf("\nPlease enter the number which you want to insert: ");

int k, y;

scanf("%d",&k);

scanf("%d",&y); 

printf("\nk %d\ny %d\n", k, y);

head_ins insert(head_del, k, y);

print(head_ins);


return 0;



<script type="text/javascript" id="wumiiRelatedItems"> </script>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值