# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <string.h>
typedef int elemtype;
typedef struct DNode
{
union
{
int data;
int length;
} ;
struct DNode * next;
struct DNode * prior;
} DNode, * PDNode;
void Init_list ( PDNode plist) ;
bool Insert_head ( PDNode plist, int val) ;
bool Insert_tail ( PDNode plist, int val) ;
bool Del_head ( PDNode plist) ;
bool Del_tail ( PDNode plist) ;
bool Insert_pos ( PDNode plist, int pos, int val) ;
bool Del_pos ( PDNode plist, int pos) ;
PDNode Del_val ( PDNode plist, int val) ;
PDNode prior ( PDNode plist, int val) ;
PDNode Get_next ( PDNode plist, int val) ;
bool IsEmpty ( PDNode plist) ;
int Get_length ( PDNode plist) ;
void Clear ( PDNode plist) ;
void Destory ( PDNode plist) ;
void Destory2 ( PDNode plist) ;
void Show ( PDNode plist) ;
void Init_list ( PDNode plist)
{
assert ( plist != NULL ) ;
plist-> next = NULL ;
plist-> prior = NULL ;
}
bool Insert_head ( PDNode plist, int val)
{
assert ( plist != NULL ) ;
DNode* pnewnode = ( DNode* ) malloc ( sizeof ( DNode) ) ;
assert ( pnewnode != NULL ) ;
pnewnode-> data = val;
pnewnode-> next = plist-> next;
pnewnode-> prior = plist;
if ( plist-> next != NULL )
{
plist-> next-> prior = pnewnode;
}
plist-> next = pnewnode;
return true;
}
bool Insert_pos2 ( PDNode plist, int pos, int val)
{
assert ( plist != NULL ) ;
if ( pos< 0 || pos> Get_length ( plist) ) return false;
DNode* s = ( DNode* ) malloc ( sizeof ( DNode) ) ;
DNode* p = plist;
assert ( s != NULL ) ;
int i = 0 ;
s-> data = val;
while ( i < pos)
{
p = p-> next;
i++ ;
}
s-> next = p-> next;
s-> prior = p;
p-> next-> prior = s;
p-> next = s;
return true;
}
bool Insert_tail ( PDNode plist, int val)
{
assert ( plist != NULL ) ;
DNode* pnewnode = ( DNode* ) malloc ( sizeof ( DNode) ) ;
assert ( pnewnode != NULL ) ;
pnewnode-> data = val;
DNode* p = plist;
while ( p-> next != NULL )
{
p = p-> next;
}
pnewnode-> next = p-> next;
pnewnode-> prior = p;
p-> next = pnewnode;
return true;
}
bool Del_head ( PDNode plist)
{
Del_pos ( plist, 0 ) ;
return true;
}
bool Del_tail ( PDNode plist)
{
Del_pos ( plist, Get_length ( plist) - 1 ) ;
return true;
}
bool Del_pos ( PDNode plist, int pos)
{
assert ( plist != NULL ) ;
DNode* p = plist;
int i = 0 ;
while ( i < pos)
{
p = p-> next;
i++ ;
}
DNode* q = p-> next;
p-> next = q-> next;
if ( q-> next != NULL )
{
q-> next-> prior = p;
}
free ( q) ;
q = NULL ;
return true;
}
PDNode Get_prior ( PDNode plist, int val)
{
assert ( plist != NULL ) ;
DNode* p = plist-> next;
int i = 0 ;
while ( p != NULL )
{
if ( p-> data == val)
{
return p-> prior;
break ;
}
else
p = p-> next;
}
return NULL ;
}
PDNode Get_next ( PDNode plist, int val)
{
assert ( plist != NULL ) ;
DNode* p = plist-> next;
int i = 0 ;
while ( p != NULL )
{
if ( p-> data == val)
{
return p-> next;
break ;
}
else
p = p-> next;
}
return NULL ;
}
PDNode Del_val ( PDNode plist, int val)
{
assert ( plist != NULL ) ;
DNode* p = Get_prior ( plist, val) ;
assert ( p != NULL ) ;
DNode* q = p-> next;
p-> next = q-> next;
if ( q-> next != NULL )
{
q-> next-> prior = p;
}
free ( q) ;
q = NULL ;
return NULL ;
}
bool IsEmpty ( PDNode plist)
{
return plist-> next = NULL ;
}
int Get_length ( PDNode plist)
{
int count = 0 ;
DNode* p = plist-> next;
while ( p != NULL )
{
p = p-> next;
count++ ;
}
return count;
}
void Clear ( PDNode plist)
{
DNode* p = plist-> next;
while ( p != NULL )
{
Del_pos ( plist, 0 ) ;
p = p-> next;
}
}
void Destory ( PDNode plist)
{
while ( plist-> next != NULL )
{
DNode* p = plist-> next;
plist-> next = p-> next;
free ( p) ;
}
plist-> next = NULL ;
}
void Show ( PDNode plist)
{
assert ( plist != NULL ) ;
DNode* p = plist-> next;
while ( p != NULL )
{
printf ( "%d " , p-> data) ;
p = p-> next;
}
printf ( "\n" ) ;
}
int main ( )
{
DNode ps;
Init_list ( & ps) ;
for ( int i = 0 ; i < 10 ; i++ )
{
Insert_tail ( & ps, i + 1 ) ;
}
Show ( & ps) ;
Insert_pos2 ( & ps, 2 , 100 ) ;
Show ( & ps) ;
printf ( "%d\n" , Get_length ( & ps) ) ;
Del_pos ( & ps, 3 ) ;
Show ( & ps) ;
Del_val ( & ps, 8 ) ;
Show ( & ps) ;
printf ( "%d\n" , Get_length ( & ps) ) ;
for ( int i = 0 ; i < 4 ; i++ )
{
Del_head ( & ps) ;
}
Show ( & ps) ;
printf ( "%d\n" , Get_length ( & ps) ) ;
Del_tail ( & ps) ;
Show ( & ps) ;
return 0 ;
}