静态链表的基本操作
静态链表的结构
typedef struct
{
char date;
int next;
} * NodePtr, Node;
typedef struct
{
NodePtr node;
int * used;
} * list, lnode;
静态链表的初始化
list initlist ( )
{
list temphead= ( list) malloc ( sizeof ( lnode) * initlen) ;
temphead-> node= ( NodePtr) malloc ( sizeof ( Node) * initlen) ;
temphead-> used = ( int * ) malloc ( sizeof ( int ) * initlen) ;
temphead-> node-> date= '\0' ;
temphead-> node-> next= - 1 ;
temphead-> used[ 0 ] = 1 ;
int i;
for ( i= 1 ; i< initlen; i++ )
{
temphead-> used[ i] = 0 ;
}
return temphead;
}
打印静态链表
void printlist ( list parahead)
{
int p= 0 ;
while ( p!= - 1 )
{
printf ( "%c" , parahead-> node[ p] . date ) ;
p= parahead-> node[ p] . next;
}
printf ( "\n" ) ;
}
在指定位置插入元素
void insert ( list parahead, char ch, int position)
{
int i;
int p= 0 ;
for ( i= 1 ; i< position; i++ )
{
p= parahead-> node[ p] . next;
if ( p== - 1 )
{
printf ( "插入位置%d大于当前链表的长度\n" , position) ;
return ;
}
}
int q= - 1 ;
for ( i= 1 ; i< initlen; i++ )
{
if ( parahead-> used[ i] == 0 )
{
q= i;
parahead-> used[ q] = 1 ;
break ;
}
}
if ( q== - 1 )
{
printf ( "链表已满,不能插入元素\n" ) ;
return ;
}
parahead-> node[ q] . date = ch;
parahead-> node[ q] . next= parahead-> node[ p] . next ;
parahead-> node[ p] . next= q;
}
删除指定位置的元素
void deletenode1 ( list parahead, int position)
{
int i, p= 0 ;
for ( i= 1 ; i< position; i++ )
{
p= parahead-> node[ p] . next ;
if ( p== - 1 )
{
printf ( "删除位置大于当前链表的实际长度\n" ) ;
return ;
}
}
int q= parahead-> node[ p] . next ;
parahead-> node[ p] . next = parahead-> node[ q] . next ;
parahead-> used[ q] = 0 ;
}
删除指定位置的节点
void deletenode2 ( list parahead, char ch)
{
int p= 0 ;
while ( parahead-> node[ p] . next != - 1 && parahead-> node[ parahead-> node[ p] . next ] . date!= ch)
{
p= parahead-> node[ p] . next ;
}
if ( parahead-> node[ p] . next == - 1 )
{
printf ( "链表中没有%c,无法删除\n" , ch) ;
return ;
}
int q= parahead-> node[ p] . next ;
parahead-> node[ p] . next = parahead-> node[ q] . next ;
parahead-> used[ q] = 0 ;
}
全部代码
# include <stdio.h>
# include <stdlib.h>
# define initlen 20
typedef struct
{
char date;
int next;
} * NodePtr, Node;
typedef struct
{
NodePtr node;
int * used;
} * list, lnode;
list initlist ( )
{
list temphead= ( list) malloc ( sizeof ( lnode) * initlen) ;
temphead-> node= ( NodePtr) malloc ( sizeof ( Node) * initlen) ;
temphead-> used = ( int * ) malloc ( sizeof ( int ) * initlen) ;
temphead-> node-> date= '\0' ;
temphead-> node-> next= - 1 ;
temphead-> used[ 0 ] = 1 ;
int i;
for ( i= 1 ; i< initlen; i++ )
{
temphead-> used[ i] = 0 ;
}
return temphead;
}
void printlist ( list parahead)
{
int p= 0 ;
while ( p!= - 1 )
{
printf ( "%c" , parahead-> node[ p] . date ) ;
p= parahead-> node[ p] . next;
}
printf ( "\n" ) ;
}
void insert ( list parahead, char ch, int position)
{
int i;
int p= 0 ;
for ( i= 1 ; i< position; i++ )
{
p= parahead-> node[ p] . next;
if ( p== - 1 )
{
printf ( "插入位置%d大于当前链表的长度\n" , position) ;
return ;
}
}
int q= - 1 ;
for ( i= 1 ; i< initlen; i++ )
{
if ( parahead-> used[ i] == 0 )
{
q= i;
parahead-> used[ q] = 1 ;
break ;
}
}
if ( q== - 1 )
{
printf ( "链表已满,不能插入元素\n" ) ;
return ;
}
parahead-> node[ q] . date = ch;
parahead-> node[ q] . next= parahead-> node[ p] . next ;
parahead-> node[ p] . next= q;
}
void deletenode1 ( list parahead, int position)
{
int i, p= 0 ;
for ( i= 1 ; i< position; i++ )
{
p= parahead-> node[ p] . next ;
if ( p== - 1 )
{
printf ( "删除位置大于当前链表的实际长度\n" ) ;
return ;
}
}
int q= parahead-> node[ p] . next ;
parahead-> node[ p] . next = parahead-> node[ q] . next ;
parahead-> used[ q] = 0 ;
}
void deletenode2 ( list parahead, char ch)
{
int p= 0 ;
while ( parahead-> node[ p] . next != - 1 && parahead-> node[ parahead-> node[ p] . next ] . date!= ch)
{
p= parahead-> node[ p] . next ;
}
if ( parahead-> node[ p] . next == - 1 )
{
printf ( "链表中没有%c,无法删除\n" , ch) ;
return ;
}
int q= parahead-> node[ p] . next ;
parahead-> node[ p] . next = parahead-> node[ q] . next ;
parahead-> used[ q] = 0 ;
}
void test ( )
{
list tempList = initlist ( ) ;
printlist ( tempList) ;
insert ( tempList, 'H' , 1 ) ;
insert ( tempList, 'e' , 2 ) ;
insert ( tempList, 'l' , 3 ) ;
insert ( tempList, 'l' , 4 ) ;
insert ( tempList, 'o' , 5 ) ;
printlist ( tempList) ;
printf ( "Deleting 'e'.\r\n" ) ;
deletenode2 ( tempList, 'e' ) ;
printf ( "Deleting 'a'.\r\n" ) ;
deletenode2 ( tempList, 'a' ) ;
printf ( "Deleting 'o'.\r\n" ) ;
deletenode2 ( tempList, 'o' ) ;
printlist ( tempList) ;
printf ( "删除第2个元素\r\n" ) ;
deletenode1 ( tempList, 2 ) ;
printf ( "删除第1个元素\r\n" ) ;
deletenode1 ( tempList, 1 ) ;
printlist ( tempList) ;
insert ( tempList, 'x' , 1 ) ;
printlist ( tempList) ;
}
int main ( )
{
test ( ) ;
return 0 ;
}
代码运行测试