节点结构体的创建
typedef int data_t ;
typedef struct node {
data_t data;
struct node * next;
} linknode, * linklist;
操作函数
linklist node_create ( void ) ;
void head_insert ( linklist L, data_t arg) ;
void tail_insert ( linklist L, data_t arg) ;
void head_delete ( linklist L) ;
void tail_delete ( linklist L) ;
void post_delete ( linklist L, int postition) ;
void list_sort ( linklist L) ;
linklist list_clear ( linklist L) ;
void list_show ( linklist L) ;
创建一个新节点
linklist node_create ( void ) {
linklist L;
L = ( linklist) malloc ( sizeof ( linknode) ) ;
if ( L == NULL ) {
perror ( "malloc" ) ;
return ;
}
L-> next = NULL ;
L-> data = 0 ;
return L;
}
从头部插入一个节点
void head_insert ( linklist L, data_t arg) {
if ( L-> next == NULL ) {
printf ( "head_insert failed:list is empty\n" ) ;
return ;
}
linklist temp;
if ( ( temp = ( linklist) malloc ( sizeof ( linknode) ) ) == NULL ) {
perror ( "malloc in head_insert" ) ;
return ;
}
L-> data++ ;
temp-> next = L-> next;
L-> next = temp;
temp-> data = arg;
}
从尾部插入一个节点
void tail_insert ( linklist L, data_t arg) {
linklist temp;
linklist temp2;
temp2 = L;
if ( ( temp = ( linklist) malloc ( sizeof ( linknode) ) ) == NULL ) {
perror ( "malloc in tail_insert()" ) ;
return ;
}
while ( temp2-> next != NULL ) {
temp2 = temp2-> next;
}
L-> data++ ;
temp2-> next = temp;
temp-> data = arg;
temp-> next = NULL ;
}
从头部删除一个节点
void head_delete ( linklist L) {
if ( L-> next == NULL ) {
printf ( "head_delete failed:list is empty\n" ) ;
return ;
}
linklist temp;
linklist temp2;
temp = L-> next;
L-> next = L-> next-> next;
free ( temp) ;
temp = NULL ;
L-> data-- ;
}
从尾部删除一个节点
void tail_delete ( linklist L) {
if ( L-> next == NULL ) {
printf ( "delete failed:list is empty\n" ) ;
return ;
}
linklist temp;
linklist temp2;
temp = L;
while ( temp-> next-> next != NULL ) {
temp = temp-> next;
}
temp2 = temp-> next;
free ( temp2) ;
temp-> next = NULL ;
L-> data-- ;
}
删除指定位置的节点
void post_delete ( linklist L, int postition) {
if ( L-> next == NULL ) {
printf ( "post_delete failed:list is empty\n" ) ;
return ;
}
if ( postition <= 0 || postition > L-> data) {
printf ( "post_delete failed:postition is out of limit\n" ) ;
return ;
}
linklist temp;
linklist temp2;
int i;
temp = L;
for ( i = 0 ; i < postition - 1 ; i++ ) {
temp = temp-> next;
}
temp2 = temp-> next;
temp-> next = temp-> next-> next;
free ( temp2) ;
L-> data-- ;
}
排序
void list_sort ( linklist L) {
linklist p = NULL ;
linklist q = NULL ;
linklist temp = NULL ;
int i, j;
for ( i = 0 ; i < L-> data - 1 ; i++ ) {
temp = L;
p = temp-> next;
q = p-> next;
for ( j = 0 ; j < L-> data - i - 1 ; j++ ) {
if ( p-> data > q-> data) {
p-> next = q-> next;
q-> next = p;
temp-> next = q;
}
temp = temp-> next;
p = temp-> next;
q = p-> next;
}
}
}
清空并释放链表
linklist list_clear ( linklist L) {
linklist p = NULL ;
if ( L == NULL )
return NULL ;
p = L;
while ( L != NULL ) {
p = L;
L = L-> next;
free ( p) ;
}
printf ( "\nclear successfully\n" ) ;
return NULL ;
}
遍历链表
void list_show ( linklist L) {
if ( L-> next == NULL ) {
printf ( "show failed : list is empty\n" ) ;
return ;
}
linklist temp;
temp = L;
while ( temp-> next != NULL ) {
temp = temp-> next;
printf ( "%d " , temp-> data) ;
}
puts ( "" ) ;
}
完整程序
# include <stdio.h>
# include <malloc.h>
typedef int data_t ;
typedef struct node {
data_t data;
struct node * next;
} linknode, * linklist;
linklist node_create ( void ) ;
void head_insert ( linklist L, data_t arg) ;
void tail_insert ( linklist L, data_t arg) ;
void head_delete ( linklist L) ;
void tail_delete ( linklist L) ;
void post_delete ( linklist L, int postition) ;
void list_sort ( linklist L) ;
linklist list_clear ( linklist L) ;
void list_show ( linklist L) ;
int main ( ) {
linklist H = NULL ;
int buf[ 128 ] = { 0 } ;
int i = 0 , j = 0 ;
H = node_create ( ) ;
printf ( "input : " ) ;
while ( 1 ) {
j = scanf ( "%d" , & buf[ i] ) ;
tail_insert ( H, buf[ i] ) ;
if ( getchar ( ) == '\n' ) break ;
i++ ;
}
printf ( "numbers : " ) ;
list_show ( H) ;
list_sort ( H) ;
printf ( "sort : " ) ;
list_show ( H) ;
return 0 ;
}
linklist node_create ( void ) {
linklist L;
L = ( linklist) malloc ( sizeof ( linknode) ) ;
if ( L == NULL ) {
perror ( "malloc" ) ;
return ;
}
L-> next = NULL ;
L-> data = 0 ;
return L;
}
void head_insert ( linklist L, data_t arg) {
if ( L-> next == NULL ) {
printf ( "head_insert failed:list is empty\n" ) ;
return ;
}
linklist temp;
if ( ( temp = ( linklist) malloc ( sizeof ( linknode) ) ) == NULL ) {
perror ( "malloc in head_insert" ) ;
return ;
}
L-> data++ ;
temp-> next = L-> next;
L-> next = temp;
temp-> data = arg;
}
void tail_insert ( linklist L, data_t arg) {
linklist temp;
linklist temp2;
temp2 = L;
if ( ( temp = ( linklist) malloc ( sizeof ( linknode) ) ) == NULL ) {
perror ( "malloc in tail_insert()" ) ;
return ;
}
while ( temp2-> next != NULL ) {
temp2 = temp2-> next;
}
L-> data++ ;
temp2-> next = temp;
temp-> data = arg;
temp-> next = NULL ;
}
void head_delete ( linklist L) {
if ( L-> next == NULL ) {
printf ( "head_delete failed:list is empty\n" ) ;
return ;
}
linklist temp;
linklist temp2;
temp = L-> next;
L-> next = L-> next-> next;
free ( temp) ;
temp = NULL ;
L-> data-- ;
}
void tail_delete ( linklist L) {
if ( L-> next == NULL ) {
printf ( "delete failed:list is empty\n" ) ;
return ;
}
linklist temp;
linklist temp2;
temp = L;
while ( temp-> next-> next != NULL ) {
temp = temp-> next;
}
temp2 = temp-> next;
free ( temp2) ;
temp-> next = NULL ;
L-> data-- ;
}
void post_delete ( linklist L, int postition) {
if ( L-> next == NULL ) {
printf ( "post_delete failed:list is empty\n" ) ;
return ;
}
if ( postition <= 0 || postition > L-> data) {
printf ( "post_delete failed:postition is out of limit\n" ) ;
return ;
}
linklist temp;
linklist temp2;
int i;
temp = L;
for ( i = 0 ; i < postition - 1 ; i++ ) {
temp = temp-> next;
}
temp2 = temp-> next;
temp-> next = temp-> next-> next;
free ( temp2) ;
L-> data-- ;
}
void list_sort ( linklist L) {
linklist p = NULL ;
linklist q = NULL ;
linklist temp = NULL ;
int i, j;
for ( i = 0 ; i < L-> data - 1 ; i++ ) {
temp = L;
p = temp-> next;
q = p-> next;
for ( j = 0 ; j < L-> data - i - 1 ; j++ ) {
if ( p-> data > q-> data) {
p-> next = q-> next;
q-> next = p;
temp-> next = q;
}
temp = temp-> next;
p = temp-> next;
q = p-> next;
}
}
}
linklist list_clear ( linklist L) {
linklist p = NULL ;
if ( L == NULL )
return NULL ;
p = L;
while ( L != NULL ) {
p = L;
L = L-> next;
free ( p) ;
}
printf ( "\nclear successfully\n" ) ;
return NULL ;
}
void list_show ( linklist L) {
if ( L-> next == NULL ) {
printf ( "show failed : list is empty\n" ) ;
return ;
}
linklist temp;
temp = L;
while ( temp-> next != NULL ) {
temp = temp-> next;
printf ( "%d " , temp-> data) ;
}
puts ( "" ) ;
}