双方向チェーンテーブルの指定ノードを削除
3665 ワード
// doubleLink.cpp : 。
//
/*
, 。 , 。
。
*/
/*
, ,
vs2008 。
*/
#include "stdafx.h"
#include
#include
#include
struct node {
int value;
struct node *priv;
struct node *next;
};
/* */
struct node* create_double_link();
/* */
struct node* delete_double_link(struct node* head,int value);
void print_double_link(struct node* head);
struct node* create_double_link()
{
struct node* head=NULL;
struct node* pcurrent=NULL;
struct node*ptemp=NULL;
int num=0,total=0;
do
{
printf("please input the number:
");
scanf("%d",&total);
if( total == 0 )
printf("sorry,the number is error!
");
else
printf("
");
}while( total == 0 );
while(numvalue=num;
}
if( num == 1)
{
head=pcurrent;
pcurrent->priv=NULL;
}
else
{
ptemp->next=pcurrent;
pcurrent->priv=ptemp;
}
ptemp=pcurrent;
}
ptemp->next=NULL;
return (head);
}
struct node* delete_double_link(struct node* head,int value)
{
struct node* pcurrent=NULL;
pcurrent=head;
if( head==NULL )
{
printf("error:please check the data!
");
return(NULL);
}
else
{
while( (value!=pcurrent->value)&&(pcurrent->next!=NULL) )
pcurrent=pcurrent->next;
if(value==pcurrent->value)//find it
{
if( pcurrent==head )// is head node
{
if(pcurrent->next)
{
pcurrent->next->priv=NULL;
head=pcurrent->next;
}
}
else if(pcurrent->next==NULL)//wear
{
pcurrent->priv->next=NULL;
}
else
{
pcurrent->priv->next=pcurrent->next;
pcurrent->next->priv=pcurrent->priv;
}
free(pcurrent);
printf("the num have been deletea is: %d
",value);
}
else
{
printf("can not find the num: %d
",value);
}
}
return(head);
}
void print_double_link(struct node* head)
{
struct node* ptemp;
if( head == NULL)
{
printf("error:have no data!
");
return;
}
ptemp=head;
while(ptemp)
{
printf("num: %d
",ptemp->value);
ptemp=ptemp->next;
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct node* head,ptemp;
int num=0;
head=create_double_link();
print_double_link(head);
printf("please input the delete num:
");
scanf("%d",&num);
head=delete_double_link(head, num);
print_double_link(head);
Sleep(10000);
return 0;
}