王道链表题1-4
# include <iostream>
# include <bits/stdc++.h>
using namespace std; typedef int Elemtype;
typedef struct Lnode { Elemtype data; struct Lnode * next;
} Lnode, * Linklist;
int a[ 4 ] = { 1 , 2 , 3 , 4 } ;
int n= 4 ;
void buildlist ( Lnode * L) { Lnode * s, * r= L; r-> data= a[ 0 ] ; if ( n== 1 ) r-> next= NULL ; else for ( int i= 1 ; i< n; i++ ) { s= ( Lnode* ) malloc ( sizeof ( Lnode) ) ; s-> data= a[ i] ; r-> next= s; r= r-> next; } L= ( Linklist) malloc ( sizeof ( Lnode) ) ; L-> next= NULL ; }
Linklist list_headinsert ( Linklist & L) { Lnode * s; int x; L= ( Linklist) malloc ( sizeof ( Lnode) ) ; L-> next= NULL ; scanf ( "%d" , & x) ; while ( x!= 9999 ) { s= ( Lnode* ) malloc ( sizeof ( Lnode) ) ; s-> data= x; s-> next= L-> next; L-> next= s; scanf ( "%d" , & x) ; } return L;
}
Linklist List_TailInsert ( Linklist & L) { int x; L= ( Linklist) malloc ( sizeof ( Linklist) ) ; Lnode * s, * r= L; scanf ( "%d" , & x) ; while ( x!= 9999 ) { s= ( Lnode* ) malloc ( sizeof ( Lnode) ) ; s-> data= x; r-> next= s; r= s; scanf ( "%d" , & x) ; } r-> next= NULL ; return L;
} void disp ( Lnode * L) { Lnode * s= L; while ( s) { cout<< ( s-> data) << " " ; s= s-> next; } cout<< endl;
} void deletex ( Lnode * & L, int x) { if ( L== NULL ) return ; Lnode * p; if ( L-> data= x) { p= L; L= L-> next; free ( p) ; deletex ( L, x) ; } else deletex ( L-> next, x) ; }
void del_x_1 ( Linklist & L, Elemtype x) { Lnode * p= L-> next, * pre= L, * q; while ( p!= NULL ) { if ( p-> data== x) { q= p; p= p-> next; pre-> next= p; free ( q) ; } else { pre= p; p= p-> next; } }
}
void R_Print ( Lnode * & L) { Lnode * p= L; if ( p!= NULL ) { R_Print ( L-> next) ; printf ( "%d " , p-> data) ; }
}
void del_min ( Linklist & L) { Lnode * pre= L, * p= L-> next, * q= L-> next; while ( q!= NULL ) { if ( ( q-> data) < ( p-> next-> data) ) { p= pre; pre= q; q= q-> next; } else { pre= q; q= q-> next; } } q= p-> next; p-> next= p-> next-> next; free ( q) ;
} int main ( ) { Lnode list; Lnode * L= & list; List_TailInsert ( L) ; del_min ( L) ; disp ( L) ; return 0 ;
}