双向链表插入与删除

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct node
{
int data;
struct node *pre;
struct node *next;
}dnode;

/*
双向链表删除结点
*/
dnode *Del_Node(dnode *Head, int num)
{
dnode *p1,*p2;

// 遍历到num结点
while( num != p1->data && p1->next != NULL)
{
p1 = p1->next;
}
// 找到了数据域为data的结点
if(num == p1->data)
{
if( p1 == Head )			// 要删除的结点是头结点
{
free(p1);
}
else if(p1->next == NULL)	// 要删除的是最后一个结点
{
p1->pre->next = NULL;
free(p1);
}
else						// 中间结点
{
p1->next->pre = p1->pre;
p1->pre->next = p1->next;
free(p1);
}
}
else
{
printf("%d cound not been found",num);

}
}

/*
双向链表插入结点
*/
dnode *Insert_Sort(dnode *Head, int num)
{
dnode *p0,*p1;
p0 = (dnode *)malloc( sizeof(dnode) );
p0->next = NULL;
p0->pre  = NULL;
p0->data = num;

if(p1 == NULL)
{
}

// 遍历
while( (p0->data > p1->data) && (p1->next != NULL) )
{
p1 = p1->next;
}

if(p0->data <= p1->data)
{
if(Head == p1)			// 头结点
{
p0->next = p1;
p1->pre  = p0;
}
else
{
p1->pre->next = p0;
p0->next      = p1;
p0->pre       = p1->pre;
p1->pre       = p0;
}
}
else						// 最后一个结点
{
p1->next = p0;
p0->pre  = p1;
p0->next = NULL;
}

}

/* print list item->name */
{
dnode *temp = Head;

while(temp)
{
printf("-------- %d\n",temp->data);
temp = temp->next;
}
}

void main()
{
dnode *Head = NULL;
int i = 0;

while( i++ < 10 )
{
}

printf("------------------------------\n");

}

11-29

05-06 2万+
08-22 3083
01-29 9179
07-28 140
08-18 3573
03-27 3035
10-23 72
04-26 4133
10-03 8933
02-01 4396
02-18 544
08-20 3556
03-23 101
02-16 172
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客