数据结构练习题
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW - 2
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
} LNode, * LinkList; void CreateList_L ( LinkList & L, int n) {
int i; LinkList p, q; L= ( LinkList) malloc ( sizeof ( LNode) ) ; L-> next= NULL ; q= L; for ( i= n; i> 0 ; -- i) { p= ( LinkList) malloc ( sizeof ( LNode) ) ; scanf ( "%d" , & p-> data) ; p-> next= NULL ; q-> next= p; q= p; }
} Status OutputList_L ( LinkList L) { LinkList p= L-> next; if ( ! p) return ERROR; while ( p) { printf ( "%d " , p-> data) ; p= p-> next; } printf ( "\n" ) ; return OK;
} Status GetElem_L ( LinkList & L, int i, ElemType & e) { int j; LinkList p; p= L-> next; j= 1 ; while ( p&& j< i) { p= p-> next; ++ j; } if ( ! p|| j> i) return ERROR; e= p-> data; return OK;
} Status ListDelete_L ( LinkList & L, int i, ElemType & e) { LinkList p, q; int j; p= L; j= 0 ; while ( p-> next&& j< i- 1 ) { p= p-> next; ++ j; } if ( ! ( p-> next) || j> i- 1 ) return ERROR; q= p-> next; p-> next= q-> next; e= q-> data; free ( q) ; return OK;
} int main ( void ) { LinkList L; int n, i, j, a= 0 , b= 0 , cnt= 0 ; scanf ( "%d" , & n) ; CreateList_L ( L, n) ; for ( i= 0 ; i< n- cnt- 1 ; i++ ) { GetElem_L ( L, i+ 1 , a) ; for ( j= i+ 1 ; j< n- cnt; ) { GetElem_L ( L, j+ 1 , b) ; if ( a== b) { ListDelete_L ( L, j+ 1 , b) ; cnt++ ; } else j++ ; } } printf ( "%d\n" , cnt) ; OutputList_L ( L) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW - 2
typedef int ElemType;
typedef int Status;
typedef struct LNode { ElemType data; struct LNode * llnode, * rlnode;
} LNode, * LinkList; void CreateList_L ( LinkList & L, int n) { int i; LinkList p, q; L= ( LinkList) malloc ( sizeof ( LNode) ) ; L-> llnode= L; L-> rlnode= L; q= L; for ( i= n; i> 0 ; -- i) { p= ( LinkList) malloc ( sizeof ( LNode) ) ; scanf ( "%d" , & p-> data) ; p-> rlnode= L; p-> llnode= q; q-> rlnode= p; q= p; }
} Status OutputList_L ( LinkList L) { LinkList p= L-> rlnode; if ( ! p) return ERROR; while ( p!= L) { printf ( "%d " , p-> data) ; p= p-> rlnode; } printf ( "\n" ) ; return OK;
} Status GetElem_L ( LinkList & L, int i, ElemType & e) { int j; LinkList p; p= L-> rlnode; j= 1 ; while ( p&& j< i) { p= p-> rlnode; ++ j; } if ( ! p|| j> i) return ERROR; e= p-> data; return OK;
} Status ListDelete_L ( LinkList & L, ElemType e) { LinkList p, q; p= L-> rlnode; while ( p!= L) { if ( p-> data== e) { p-> llnode-> rlnode= p-> rlnode; p-> rlnode-> llnode= p-> llnode; q= p; p= p-> rlnode; free ( q) ; } else p= p-> rlnode; } return OK;
} int main ( void ) { LinkList L; int n, x; scanf ( "%d" , & n) ; CreateList_L ( L, n) ; scanf ( "%d" , & x) ; ListDelete_L ( L, x) ; OutputList_L ( L) ; return 0 ;
}
# include <stdio.h>
# define MaxSize 50
typedef int ElemType;
typedef struct { ElemType data[ MaxSize] ; int length;
} SqList; void InitList ( SqList & L)
{ L. length= 0 ;
} ; void CreateList ( SqList & L, int n) { int i; for ( i= 0 ; i< n; i++ ) scanf ( "%d" , & L. data[ i] ) ; L. length= n;
} int ListEmpty ( SqList L) { return ( L. length== 0 ) ;
} int ListLength ( SqList L) { return ( L. length) ;
} void DispList ( SqList L) { int i; if ( L. length== 0 ) return ; for ( i= 0 ; i< L. length; i++ ) printf ( "%d " , L. data[ i] ) ;
} int GetElem ( SqList L, int i, ElemType & e) { if ( i< 1 || i> L. length) return 0 ; e= L. data[ i- 1 ] ; return 1 ;
} int LocateElem ( SqList L, ElemType e) { int i= 1 ; while ( i<= L. length&& L. data[ i- 1 ] != e) i++ ; if ( i<= L. length) return i; else return 0 ;
} int ListInsert ( SqList & L, int i, ElemType e) { int j; if ( i< 1 || i> L. length+ 1 ) return 0 ; for ( j= L. length- 1 ; j>= i- 1 ; j-- ) L. data[ j+ 1 ] = L. data[ j] ; L. data[ i- 1 ] = e; ++ L. length; return 1 ;
} int ListDelete ( SqList & L, int i, ElemType & e)
{ int j; if ( i< 1 || i> L. length) return 0 ; e= L. data[ i- 1 ] ; for ( j= i- 1 ; j< L. length- 1 ; j++ ) L. data[ j] = L. data[ j+ 1 ] ; -- L. length; return 1 ;
} int ListReverse ( SqList & L) { int tmp; for ( int i= 0 ; i< L. length/ 2 ; i++ ) { tmp= L. data[ i] ; L. data[ i] = L. data[ L. length- i- 1 ] ; L. data[ L. length- i- 1 ] = tmp; } return 0 ;
} int main ( void ) { int n; SqList L; InitList ( L) ; scanf ( "%d" , & n) ; CreateList ( L, n) ; ListReverse ( L) ; DispList ( L) ; return 0 ;
}
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <malloc.h> typedef struct _Node { int value; struct _Node * next;
} Node, * List; void print_list ( List list)
{ Node* p = list-> next; while ( p) { printf ( "%d " , p-> value) ; p = p-> next; }
} void free_list ( List list)
{ Node* p = list-> next, * q; while ( p) { q = p; p = p-> next; free ( q) ; }
} void remove_duplicates ( List list)
{ Node * p, * q; p= list-> next; q= p-> next; while ( q) { if ( q-> value== p-> value) { p-> next= q-> next; free ( q) ; q= p-> next; } else { p= p-> next; q= p-> next; } }
}
List create_list ( )
{ List list = ( Node* ) malloc ( sizeof ( Node) ) ; list-> next = NULL ; int v; Node* prev = list; while ( scanf ( "%d" , & v) > 0 ) { Node* node = ( Node* ) malloc ( sizeof ( Node) ) ; node-> value = v; node-> next = NULL ; prev-> next = node; prev = node; } return list;
} int main ( int argc, char * * argv)
{ List list = create_list ( ) ; remove_duplicates ( list) ; print_list ( list) ; free_list ( list) ; return 0 ;
}
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h> typedef struct _Node { int value; struct _Node * next;
} Node, * List;
List create_list ( )
{ List list = ( Node* ) malloc ( sizeof ( Node) ) ; list-> next = NULL ; int v; Node* prev = list; while ( scanf ( "%d" , & v) > 0 ) { Node* node = ( Node* ) malloc ( sizeof ( Node) ) ; node-> value = v; node-> next = NULL ; prev-> next = node; prev = node; } return list;
} void print_rev_kth ( List list, int k)
{ Node * prior= list-> next; Node * node= list-> next; for ( int i= 0 ; i< k&& prior!= NULL ; i++ ) { prior= prior-> next; } while ( node!= NULL && prior!= NULL ) { node= node-> next; prior= prior-> next; } if ( node!= NULL ) { printf ( "%d\n" , node-> value) ; }
} int main ( int argc, char * * argv)
{ int k; scanf ( "%d" , & k) ; List list = create_list ( ) ; print_rev_kth ( list, k) ; return EXIT_SUCCESS;
}
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW - 2
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
typedef int Status;
typedef char SElemType;
typedef struct { SElemType * base; SElemType * top; int stacksize;
} SqStack; Status InitStack ( SqStack & S) {
S. base = ( SElemType* ) malloc ( STACK_INIT_SIZE* sizeof ( SElemType) ) ; if ( ! S. base) exit ( OVERFLOW) ; S. top= S. base; S. stacksize= STACK_INIT_SIZE; return OK;
} Status StackEmpty ( SqStack & S) { if ( S. top== S. base) return TRUE; else return FALSE;
} Status GetTop ( SqStack & S, SElemType & e) { if ( S. top == S. base) return ERROR; else e= * S. top; return OK;
} Status Push ( SqStack & S, SElemType e) { if ( S. top- S. base >= S. stacksize) { S. base = ( SElemType* ) realloc ( S. base, ( S. stacksize + STACKINCREMENT) * sizeof ( SElemType) ) ; if ( ! S. base) exit ( OVERFLOW) ; S. top= S. base + S. stacksize; S. stacksize+= STACKINCREMENT; } S. top++ ; * S. top= e; S. stacksize++ ; return OK;
} Status Pop ( SqStack & S, SElemType & e) { if ( S. top== S. base) return ERROR; else { e= * S. top; S. top-- ; S. stacksize-- ; return OK; }
} Status Correct ( SElemType str[ ] ) { SqStack S; InitStack ( S) ; int i, state= 1 , sum= 0 ; SElemType e; for ( i= 0 ; str[ i] != '\0' ; i++ ) { switch ( str[ i] ) { case '(' : Push ( S, str[ i] ) ; state++ ; break ; case '[' : Push ( S, str[ i] ) ; state++ ; break ; case ')' : if ( * S. top== '(' ) { Pop ( S, e) ; state-- ; sum++ ; } else { Push ( S, str[ i] ) ; state++ ; } break ; case ']' : if ( * S. top== '[' ) { Pop ( S, e) ; state-- ; sum++ ; } else { Push ( S, str[ i] ) ; state++ ; } break ; default : break ; } if ( ! state) break ; } if ( StackEmpty ( S) && state== 1 ) return sum; else return ERROR;
} int main ( void ) { SElemType str[ 100 ] ; char ch; int i= 0 , sum; while ( ( ch= getchar ( ) ) != '@' ) { if ( ch== '(' || ch== ')' || ch== '[' || ch== ']' ) str[ i++ ] = ch; } sum= Correct ( str) ; if ( Correct ( str) == ERROR) printf ( "no\n" ) ; else printf ( "%d\n" , sum) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
typedef int Status; int main ( void ) { char ch; int a[ 30 ] = { 0 } ; while ( ( ch= getchar ( ) ) != '$' ) { getchar ( ) ; if ( a[ ch- 'a' ] == 0 ) { printf ( "%c " , ch) ; a[ ch- 'a' ] = 1 ; } else continue ; }
}
# include <iostream>
# include <cstdlib>
# include <stack>
# include <ctype.h>
using namespace std;
stack< int > s; int main ( void ) { char ch; int num= 0 , x, y, p= - 1 ; while ( ( ch= getchar ( ) ) != '#' ) { if ( isdigit ( ch) ) { num*= 10 ; num+= ch- '0' ; p= 1 ; } else if ( isspace ( ch) ) { if ( p== 1 ) { s. push ( num) ; num= 0 ; p= - 1 ; } else continue ; } else { y= s. top ( ) ; s. pop ( ) ; x= s. top ( ) ; s. pop ( ) ; switch ( ch) { case '+' : s. push ( x+ y) ; break ; case '-' : s. push ( x- y) ; break ; case '*' : s. push ( x* y) ; break ; case '/' : s. push ( x/ y) ; break ; default : break ; } p= 0 ; } } printf ( "%d\n" , s. top ( ) ) ; return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) { int n, top = 0 , t = 1 , ans = 0 ; cin >> n; int * a = new int [ n + 10 ] , * stk = new int [ n + 10 ] , * pop_t = new int [ n + 10 ] ; for ( int i = 1 ; i <= n; ++ i) cin >> a[ i] ; for ( int i = 1 ; i <= n; ++ i) cin >> pop_t[ i] ; for ( int i = 1 ; i <= n; ++ i) { stk[ ++ top] = a[ i] ; while ( top != 0 && stk[ top] == pop_t[ t] ) { top-- , t++ , ans++ ; } } if ( top != 0 ) { cout << 0 << "\n" ; } else { cout << ans << "\n" ; } return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) { int n, t = 0 ; int * res = new int [ 101 ] ; cin >> n; while ( n<= 0 ) { printf ( "Error!\n" ) ; cin>> n; } while ( n != 0 ) { res[ ++ t] = n % 8 ; n /= 8 ; } for ( int i = t; i >= 1 ; -- i) cout << res[ i] ; cout << "\n" ; return 0 ;
}
# include <iostream>
# include <ctype.h>
using namespace std;
int main ( ) { char str[ 1001 ] ; int top= 0 ; char ch; while ( ( ch= getchar ( ) ) != '\n' ) { if ( ch== '(' || ch== ')' || ch== '[' || ch== ']' || ch== '{' || ch== '}' ) { if ( top== 0 ) { if ( ch== '(' || ch== '[' || ch== '{' ) str[ top++ ] = ch; else { printf ( "0\n" ) ; return 0 ; } } else { switch ( ch) { case '(' : case '[' : case '{' : str[ top++ ] = ch; break ; case ')' : if ( str[ top- 1 ] == '(' ) { str[ top- 1 ] = '\0' ; top-- ; } else str[ top++ ] = ch; break ; case ']' : if ( str[ top- 1 ] == '[' ) { str[ top- 1 ] = '\0' ; top-- ; } else str[ top++ ] = ch; break ; case '}' : if ( str[ top- 1 ] == '{' ) { str[ top- 1 ] = '\0' ; top-- ; } else str[ top++ ] = ch; break ; default : break ; } } } else continue ; } if ( top== 0 ) printf ( "1\n" ) ; else printf ( "0\n" ) ; return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) { int n = 0 , top1 = 0 , top2 = 0 ; char str[ 1001 ] ; int stk1[ 1001 ] , stk2[ 1001 ] ; while ( ( str[ n++ ] = getchar ( ) ) != '#' ) ; n -= 1 ; for ( int i = 0 ; i < n; ++ i) { if ( str[ i] >= 'A' && str[ i] <= 'Z' ) { stk2[ ++ top2] = str[ i] ; continue ; } if ( str[ i] == '+' || str[ i] == '-' ) { while ( top1 != 0 && stk1[ top1] != - 5 ) { stk2[ ++ top2] = stk1[ top1] ; top1-- ; } stk1[ ++ top1] = ( str[ i] == '+' ? - 1 : - 3 ) ; } if ( str[ i] == '*' || str[ i] == '/' ) { while ( top1 != 0 && stk1[ top1] != - 5 && stk1[ top1] != - 1 && stk1[ top1] != - 3 ) { stk2[ ++ top2] = stk1[ top1] ; top1-- ; } stk1[ ++ top1] = ( str[ i] == '*' ? - 2 : - 4 ) ; } if ( str[ i] == '(' ) stk1[ ++ top1] = - 5 ; if ( str[ i] == ')' ) { while ( top1 != 0 && stk1[ top1] != - 5 ) { stk2[ ++ top2] = stk1[ top1] ; top1-- ; } top1-- ; } } while ( top1 != 0 ) { stk2[ ++ top2] = stk1[ top1] ; top1-- ; } for ( int i = 1 ; i <= top2; ++ i) { if ( stk2[ i] >= 0 ) cout << ( char ) stk2[ i] ; if ( stk2[ i] == - 1 ) cout << "+" ; if ( stk2[ i] == - 2 ) cout << "*" ; if ( stk2[ i] == - 3 ) cout << "-" ; if ( stk2[ i] == - 4 ) cout << "/" ; } cout << "\n" ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# define MAXSTRLEN 255
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW - 2
typedef unsigned char SString[ MAXSTRLEN+ 1 ] ;
typedef int Status; Status Index ( SString S, SString T, int pos) { int i= pos; int j= 1 ; while ( i<= S[ 0 ] && j<= T[ 0 ] ) { if ( S[ i] == T[ j] ) { ++ i; ++ j; } else { i= i- j+ 2 ; j= 1 ; } } if ( j> T[ 0 ] ) return i- T[ 0 ] ; else return 0 ;
} Status StrDelete ( SString & S, int pos, int len) { int i; if ( pos< 1 || pos+ len- 1 > S[ 0 ] || len< 0 ) return FALSE; for ( i= pos+ len; i<= S[ 0 ] ; i++ ) { S[ i- len] = S[ i] ; } S[ 0 ] -= len; return 0 ;
} int main ( void ) { SString a, b; int i= 1 , j= 1 , pos; char ch; while ( ( ch= getchar ( ) ) != '\n' ) { a[ i++ ] = ch; } a[ 0 ] = i- 1 ; while ( ( ch= getchar ( ) ) != '\n' ) { b[ j++ ] = ch; } b[ 0 ] = j- 1 ; while ( ( pos= Index ( a, b, 1 ) ) != 0 ) { StrDelete ( a, pos, b[ 0 ] ) ; } for ( i = 1 ; i <= a[ 0 ] ; i++ ) { printf ( "%c" , a[ i] ) ; } printf ( "\n" ) ; return 0 ;
}
# include <stdio.h>
# include <string.h>
int main ( )
{ char s1[ 100 ] ; gets ( s1) ; char s2[ 100 ] ; gets ( s2) ; int len1, len2; len1= strlen ( s1) ; len2= strlen ( s2) ; int i, j, k; int flag= 1 ; int f; while ( flag) for ( i= 0 ; i< len1; i++ ) { flag= 0 ; if ( s1[ i] == s2[ 0 ] ) { f= 1 ; for ( j= i, k= 0 ; k< len2; j++ , k++ ) if ( s1[ j] != s2[ k] ) { f= 0 ; break ; } if ( f) { flag= 1 ; for ( ; j< len1; j++ , i++ ) s1[ i] = s1[ j] ; for ( ; i< len1; i++ ) s1[ i] = '\0' ; break ; } } len1= strlen ( s1) ; } puts ( s1) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct TreeNode { char val; struct TreeNode * left; struct TreeNode * right;
} TreeNode;
TreeNode* createTreeNode ( char val) { TreeNode* node = ( TreeNode* ) malloc ( sizeof ( TreeNode) ) ; node-> val = val; node-> left = NULL ; node-> right = NULL ; return node;
}
TreeNode* strToTree ( const char * * str) { if ( * * str == '\0' || * * str == ',' ) return NULL ; TreeNode* root = createTreeNode ( * ( ( * str) ++ ) ) ; if ( * ( * str) == '(' ) { ( * str) ++ ; root-> left = strToTree ( str) ; if ( * ( * str) == ',' ) { ( * str) ++ ; root-> right = strToTree ( str) ; } ( * str) ++ ; } return root;
}
void collectPaths ( TreeNode* node, char * path, int pathIndex) { if ( node == NULL ) return ; path[ pathIndex] = node-> val; pathIndex++ ; if ( node-> left == NULL && node-> right == NULL ) { for ( int i = pathIndex - 1 ; i >= 0 ; i-- ) { printf ( "%c " , path[ i] ) ; } printf ( "\n" ) ; } else { collectPaths ( node-> left, path, pathIndex) ; collectPaths ( node-> right, path, pathIndex) ; }
}
void deleteTree ( TreeNode* node) { if ( node == NULL ) return ; deleteTree ( node-> left) ; deleteTree ( node-> right) ; free ( node) ;
} int main ( ) { char input[ 1024 ] ; scanf ( "%s" , input) ; const char * str = input; TreeNode* root = strToTree ( & str) ; char path[ 1024 ] ; collectPaths ( root, path, 0 ) ; deleteTree ( root) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct TreeNode { char val; struct TreeNode * left; struct TreeNode * right;
} TreeNode;
TreeNode* createTreeNode ( char val) { TreeNode* node = ( TreeNode* ) malloc ( sizeof ( TreeNode) ) ; node-> val = val; node-> left = NULL ; node-> right = NULL ; return node;
}
TreeNode* strToTree ( const char * * str) { if ( * * str == '\0' || * * str == ',' ) return NULL ; TreeNode* root = createTreeNode ( * ( ( * str) ++ ) ) ; if ( * ( * str) == '(' ) { ( * str) ++ ; root-> left = strToTree ( str) ; if ( * ( * str) == ',' ) { ( * str) ++ ; root-> right = strToTree ( str) ; } ( * str) ++ ; } return root;
}
int isSymmetric ( TreeNode* left, TreeNode* right) { if ( left == NULL && right == NULL ) return 1 ; if ( left == NULL || right == NULL ) return 0 ; return isSymmetric ( left-> left, right-> right) && isSymmetric ( left-> right, right-> left) ;
}
int isSymmetricTree ( TreeNode* root) { if ( root == NULL ) return 1 ; return isSymmetric ( root-> left, root-> right) ;
}
void deleteTree ( TreeNode* node) { if ( node == NULL ) return ; deleteTree ( node-> left) ; deleteTree ( node-> right) ; free ( node) ;
} int main ( ) { char input[ 1024 ] ; scanf ( "%s" , input) ; const char * str = input; TreeNode* root = strToTree ( & str) ; int result = isSymmetricTree ( root) ; printf ( "%s\n" , result ? "true" : "false" ) ; deleteTree ( root) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
typedef struct BiTNode
{ int data; int cs; struct BiTNode * lchild , * rchild;
} BiTNode, * BiTree;
typedef struct
{ BiTree * base; BiTree * top; int stacksize;
} SqStack; typedef struct QNode
{ BiTree data; struct QNode * next;
} QNode, * QueuePtr; typedef struct
{ QueuePtr front; QueuePtr rear;
} LinkQueue; int InitQueue ( LinkQueue & Q)
{ Q. front= Q. rear= ( QueuePtr) malloc ( sizeof ( QNode) ) ; if ( ! Q. front) exit ( - 1 ) ; Q. front-> next= NULL ; return 1 ;
}
int EnQueue ( LinkQueue& Q, BiTree e)
{ QueuePtr p= ( QueuePtr) malloc ( sizeof ( QNode) ) ; if ( ! p) exit ( - 2 ) ; p-> data= e; p-> next= NULL ; Q. rear-> next= p; Q. rear= p; return 1 ;
}
int DeQueue ( LinkQueue & Q, BiTree & e)
{ if ( Q. front== Q. rear) return - 1 ; QueuePtr p= Q. front-> next; e= p-> data; Q. front-> next= p-> next; if ( Q. rear== p) Q. rear= Q. front; free ( p) ; return 1 ;
} int InitStack ( SqStack & S)
{ S. base= ( BiTree* ) malloc ( STACK_INIT_SIZE* sizeof ( BiTree) ) ; if ( ! S. base) exit ( - 1 ) ; S. top= S. base; S. stacksize= STACK_INIT_SIZE; return 1 ;
} int Push ( SqStack & S, BiTree e)
{ if ( S. top- S. base>= S. stacksize) { S. base= ( BiTree* ) realloc ( S. base, ( S. stacksize+ STACKINCREMENT) * sizeof ( BiTree) ) ; if ( ! S. base) exit ( - 1 ) ; S. top= S. base+ S. stacksize; S. stacksize+= STACKINCREMENT; } * S. top++ = e; return 1 ;
}
int Pop ( SqStack& S, BiTree & e)
{ if ( S. top== S. base) return - 1 ; e= * -- S. top; return 1 ;
}
int GetTop ( SqStack S, BiTree & e)
{ if ( S. top== S. base) return 0 ; e= * ( S. top- 1 ) ; return 1 ;
}
void CreatBiNode ( BiTree & T, int integer, int cs)
{ T= ( BiTNode * ) malloc ( sizeof ( BiTNode) ) ; if ( T== NULL ) exit ( - 1 ) ; T-> data= integer; T-> cs= cs; T-> rchild= NULL ; T-> lchild= NULL ;
}
int ShuOrFu ( char c)
{ if ( ( c== '(' ) || ( c== ')' ) || ( c== ',' ) ) return 1 ; else return 0 ;
}
void CreatBiTree ( BiTree & T, char * s)
{ int integer, cs= 1 ; SqStack S; InitStack ( S) ; integer= s[ 0 ] - '0' ; CreatBiNode ( T, integer, cs) ; BiTree p= T, q= NULL ; char c; for ( int i= 1 ; i< strlen ( s) - 1 ; i++ ) { if ( ( ShuOrFu ( s[ i] ) == 0 ) && ( ShuOrFu ( s[ i+ 1 ] ) == 1 ) ) integer= s[ i] - '0' ; if ( ShuOrFu ( s[ i] ) == 1 ) { c= s[ i] ; if ( c== '(' || c== ',' ) continue ; } if ( ( ShuOrFu ( s[ i] ) == 0 ) && ( ShuOrFu ( s[ i+ 1 ] ) == 0 ) ) { integer= ( s[ i] - '0' ) * 10 + ( s[ i+ 1 ] - '0' ) ; i= i+ 1 ; } if ( c== '(' ) { cs++ ; CreatBiNode ( p-> lchild, integer, cs) ; GetTop ( S, q) ; if ( q!= p) Push ( S, p) ; p= p-> lchild; } else if ( c== ')' ) { Pop ( S, p) ; cs-- ; } else if ( c== ',' ) { Pop ( S, p) ; CreatBiNode ( p-> rchild, integer, cs) ; Push ( S, p-> rchild) ; p= p-> rchild; } else break ; }
} void ACTraverse ( BiTree T)
{ LinkQueue Q; InitQueue ( Q) ; BiTree a[ 100 ] ; EnQueue ( Q, T) ; int i= 0 ; while ( Q. front!= Q. rear) { if ( Q. front-> next-> data-> rchild!= NULL ) EnQueue ( Q, Q. front-> next-> data-> rchild) ; if ( Q. front-> next-> data-> lchild!= NULL ) EnQueue ( Q, Q. front-> next-> data-> lchild) ; DeQueue ( Q, a[ i] ) ; i++ ; } int k= a[ i- 1 ] -> cs; for ( int j= i- 1 ; j>= 0 ; j-- ) { if ( k!= a[ j] -> cs) printf ( "\n" ) ; printf ( "%d " , a[ j] -> data) ; k= a[ j] -> cs; }
}
void PreOrder ( BiTree T)
{ if ( T) { printf ( "%d " , T-> data) ; PreOrder ( T-> lchild) ; PreOrder ( T-> rchild) ; }
} int main ( )
{ BiTree T= NULL ; char s[ 100 ] ; gets ( s) ; CreatBiTree ( T, s) ; ACTraverse ( T) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# include <string.h> # define MAX_SIZE 26 char preOrder[ MAX_SIZE] ;
int preIndex = 0 ;
void buildTree ( const char * inOrder, int inStart, int inEnd, const char * postOrder, int postStart, int postEnd) { if ( inStart > inEnd) return ; char root = postOrder[ postEnd] ; preOrder[ preIndex++ ] = root; int rootIndex = - 1 ; for ( int i = inStart; i <= inEnd; i++ ) { if ( inOrder[ i] == root) { rootIndex = i; break ; } } int leftTreeSize = rootIndex - inStart; buildTree ( inOrder, inStart, rootIndex - 1 , postOrder, postStart, postStart + leftTreeSize - 1 ) ; buildTree ( inOrder, rootIndex + 1 , inEnd, postOrder, postStart + leftTreeSize, postEnd - 1 ) ;
} int main ( ) { char inOrder[ MAX_SIZE] ; char postOrder[ MAX_SIZE] ; scanf ( "%s %s" , inOrder, postOrder) ; buildTree ( inOrder, 0 , strlen ( inOrder) - 1 , postOrder, 0 , strlen ( postOrder) - 1 ) ; for ( int i = 0 ; i < preIndex; i++ ) { printf ( "%c" , preOrder[ i] ) ; } printf ( "\n" ) ; return 0 ;
}
# include <iostream>
using namespace std;
const int MAX = 32767 ; class Dijkstra {
private : int vexnum; int arcnum; bool * in_s; int * D; int * * g; public : void create ( ) ; void shortest_path ( ) ; } ; void Dijkstra :: create ( )
{ cin >> vexnum >> arcnum; in_s = new bool [ vexnum] ( ) ; D = new int [ vexnum] ; g = new int * [ vexnum] ; ; for ( int i = 0 ; i < vexnum; i++ ) { g[ i] = new int [ vexnum] ; for ( int j = 0 ; j < vexnum; j++ ) { g[ i] [ j] = MAX; } } int temp; for ( int i = 0 ; i < vexnum; i++ ) for ( int j = 0 ; j < vexnum; j++ ) { cin >> temp; if ( temp != 0 && temp != MAX) g[ i] [ j] = temp; } for ( int i = 0 ; i < vexnum; i++ ) { D[ i] = g[ 0 ] [ i] ; }
} void Dijkstra :: shortest_path ( )
{ in_s[ 0 ] = true ; for ( int v = 0 ; v < vexnum- 1 ; v++ ) { int j = 0 ; while ( in_s[ j] ) j++ ; for ( int i = j + 1 ; i < vexnum; i++ ) if ( ! in_s[ i] && D[ i] < D[ j] ) j = i; in_s[ j] = true ; for ( int k = 0 ; k < vexnum; k++ ) if ( ! in_s[ k] && ( D[ j] + g[ j] [ k] < D[ k] ) ) D[ k] = D[ j] + g[ j] [ k] ; } cout << endl; for ( int i = 0 ; i < vexnum; i++ ) { if ( D[ i] != MAX) cout << D[ i] << " " ; }
} int main ( ) { Dijkstra d; d. create ( ) ; d. shortest_path ( ) ;
}
# include <stdio.h>
# include <stdlib.h>
# include <limits.h>
# define MAX_N 100
void floyd ( int graph[ ] [ MAX_N] , int n) { int i, j, k; for ( k = 0 ; k < n; k++ ) { for ( i = 0 ; i < n; i++ ) { for ( j = 0 ; j < n; j++ ) { if ( graph[ i] [ k] != 32767 && graph[ k] [ j] != 32767 ) { if ( graph[ i] [ j] > graph[ i] [ k] + graph[ k] [ j] ) { graph[ i] [ j] = graph[ i] [ k] + graph[ k] [ j] ; } } } } }
}
void findFarthest ( int graph[ ] [ MAX_N] , int n, int * max_distances) { int i, j; for ( i = 0 ; i < n; i++ ) { int max_dist = 0 ; for ( j = 0 ; j < n; j++ ) { if ( graph[ j] [ i] > max_dist) { max_dist = graph[ j] [ i] ; } } max_distances[ i] = max_dist; }
}
int findBestVertex ( int * max_distances, int n) { int min_max_distance = INT_MAX; int best_vertex = 0 ; for ( int i = 0 ; i < n; i++ ) { if ( max_distances[ i] < min_max_distance) { min_max_distance = max_distances[ i] ; best_vertex = i; } } return best_vertex;
} int main ( ) { int n, m; int graph[ MAX_N] [ MAX_N] ; scanf ( "%d %d" , & n, & m) ; for ( int i= 0 ; i< n; i++ ) { for ( int j= 0 ; j< n; j++ ) { scanf ( "%d" , & graph[ i] [ j] ) ; } } floyd ( graph, n) ; int max_distances[ MAX_N] ; findFarthest ( graph, n, max_distances) ; int best_vertex = findBestVertex ( max_distances, n) ; printf ( "%d" , best_vertex) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
# include <limits.h> # define MAX_N 100
# define INF INT_MAX
void floyd ( int graph[ ] [ MAX_N] , int n) { for ( int k = 0 ; k < n; k++ ) { for ( int i = 0 ; i < n; i++ ) { for ( int j = 0 ; j < n; j++ ) { if ( graph[ i] [ k] != INF && graph[ k] [ j] != INF && graph[ i] [ j] > graph[ i] [ k] + graph[ k] [ j] ) { graph[ i] [ j] = graph[ i] [ k] + graph[ k] [ j] ; } } } }
}
int findMax ( int graph[ ] [ MAX_N] , int n) { int max = - 1 ; for ( int i = 0 ; i < n; i++ ) { for ( int j = 0 ; j < n; j++ ) { if ( i != j && graph[ i] [ j] != INF && graph[ i] [ j] > max) { max = graph[ i] [ j] ; } } } return max;
} int main ( ) { int n, m; int graph[ MAX_N] [ MAX_N] ; scanf ( "%d %d" , & n, & m) ; for ( int i = 0 ; i < n; i++ ) { for ( int j = 0 ; j < n; j++ ) { if ( i == j) { graph[ i] [ j] = 0 ; } else { graph[ i] [ j] = INF; } } } for ( int i = 0 ; i < n; i++ ) { for ( int j = 0 ; j < n; j++ ) { scanf ( "%d" , & graph[ i] [ j] ) ; if ( graph[ i] [ j] == 0 && i != j) { graph[ i] [ j] = INF; } } } floyd ( graph, n) ; int max = findMax ( graph, n) ; printf ( "%d\n" , max) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
typedef struct TreeNode { int val; struct TreeNode * left; struct TreeNode * right;
} TreeNode;
TreeNode* insert ( TreeNode* root, int val) { if ( root == NULL ) { TreeNode* newNode = ( TreeNode* ) malloc ( sizeof ( TreeNode) ) ; newNode-> val = val; newNode-> left = NULL ; newNode-> right = NULL ; return newNode; } if ( val < root-> val) { root-> left = insert ( root-> left, val) ; } else if ( val > root-> val) { root-> right = insert ( root-> right, val) ; } return root;
}
int height ( TreeNode* root) { if ( root == NULL ) return 0 ; int leftHeight = height ( root-> left) ; int rightHeight = height ( root-> right) ; return ( leftHeight > rightHeight? leftHeight : rightHeight) + 1 ;
}
int isBalanced ( TreeNode* root) { if ( root == NULL ) return 1 ; int leftHeight = height ( root-> left) ; int rightHeight = height ( root-> right) ; if ( abs ( leftHeight - rightHeight) > 1 ) return 0 ; return isBalanced ( root-> left) && isBalanced ( root-> right) ;
}
void freeTree ( TreeNode* root) { if ( root == NULL ) return ; freeTree ( root-> left) ; freeTree ( root-> right) ; free ( root) ;
} int main ( ) { int n; scanf ( "%d" , & n) ; TreeNode* root = NULL ; for ( int i = 0 ; i < n; i++ ) { int val; scanf ( "%d" , & val) ; root = insert ( root, val) ; } if ( isBalanced ( root) ) { printf ( "true\n" ) ; } else { printf ( "false\n" ) ; } freeTree ( root) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
typedef struct TreeNode { int val; struct TreeNode * left; struct TreeNode * right;
} TreeNode, * Tree;
TreeNode* insert ( TreeNode* root, int val) { if ( root == NULL ) { TreeNode* newNode = ( TreeNode* ) malloc ( sizeof ( TreeNode) ) ; newNode-> val = val; newNode-> left = NULL ; newNode-> right = NULL ; return newNode; } if ( val < root-> val) { root-> left = insert ( root-> left, val) ; } else if ( val > root-> val) { root-> right = insert ( root-> right, val) ; } return root;
} void findKey ( Tree & root, int key) { if ( root== NULL ) { return ; } if ( root-> right!= NULL ) { findKey ( root-> right, key) ; } if ( root-> val>= key) { printf ( "%d " , root-> val) ; } if ( root-> left!= NULL ) { findKey ( root-> left, key) ; }
} int main ( void ) { int n, k; scanf ( "%d" , & n) ; Tree root = NULL ; for ( int i = 0 ; i < n; i++ ) { int val; scanf ( "%d" , & val) ; root = insert ( root, val) ; } scanf ( "%d" , & k) ; findKey ( root, k) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
typedef struct TreeNode { int val; struct TreeNode * left; struct TreeNode * right;
} TreeNode, * Tree;
TreeNode* insert ( TreeNode* root, int val) { if ( root == NULL ) { TreeNode* newNode = ( TreeNode* ) malloc ( sizeof ( TreeNode) ) ; newNode-> val = val; newNode-> left = NULL ; newNode-> right = NULL ; return newNode; } if ( val < root-> val) { root-> left = insert ( root-> left, val) ; } else if ( val > root-> val) { root-> right = insert ( root-> right, val) ; } return root;
} void deleteKey ( Tree & root, int key) { if ( root== NULL ) { return ; } if ( root-> val== key) { root-> left= NULL ; return ; } if ( root-> left!= NULL ) deleteKey ( root-> left, key) ; if ( root-> right!= NULL ) deleteKey ( root-> right, key) ;
} void coutTree ( Tree root) { if ( root== NULL ) return ; printf ( "%d" , root-> val) ; if ( root-> left!= NULL || root-> right!= NULL ) printf ( "(" ) ; else return ; coutTree ( root-> left) ; printf ( "," ) ; coutTree ( root-> right) ; printf ( ")" ) ;
} int main ( void ) { int n, k; scanf ( "%d" , & n) ; Tree root = NULL ; for ( int i = 0 ; i < n; i++ ) { int val; scanf ( "%d" , & val) ; root = insert ( root, val) ; } scanf ( "%d" , & k) ; deleteKey ( root, k) ; coutTree ( root) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
bool isMaxHeap ( int * arr, int n) { for ( int i= n/ 2 - 1 ; i>= 0 ; i-- ) { int leftChild= 2 * i+ 1 ; int rightChild= 2 * i+ 2 ; if ( leftChild< n&& arr[ leftChild] > arr[ i] ) return false ; if ( rightChild< n&& arr[ rightChild] > arr[ i] ) return false ; } return true ;
}
int main ( void ) { int n; scanf ( "%d" , & n) ; int arr[ n] ; for ( int i= 0 ; i< n; i++ ) { scanf ( "%d" , & arr[ i] ) ; } if ( isMaxHeap ( arr, n) ) printf ( "true" ) ; else printf ( "false" ) ; return 0 ;
}
# include <stdio.h>
# include <stdlib.h>
int sort ( int * arr, int left, int right) { int i = left; int j = right; int temp = arr[ left] ; while ( i != j) { while ( temp <= arr[ j] && i < j) j-- ; while ( temp >= arr[ i] && i < j) i++ ; if ( i < j) { int t = arr[ i] ; arr[ i] = arr[ j] ; arr[ j] = t; } } arr[ left] = arr[ i] ; arr[ i] = temp; return i;
} void quick_sort ( int * arr, int left, int right) { int * stack = ( int * ) malloc ( sizeof ( int ) * right) ; for ( int i = 0 ; i < right; i++ ) stack[ i] = - 1 ; int count = 0 ; if ( left < right) { stack[ count++ ] = left; stack[ count++ ] = right; } while ( count) { int r_pop = stack[ count- 1 ] ; int l_pop = stack[ count- 2 ] ; stack[ count - 1 ] = stack[ count - 2 ] = - 1 ; count -= 2 ; int i = sort ( arr, l_pop, r_pop) ; if ( l_pop < i - 1 ) { stack[ count++ ] = l_pop; stack[ count++ ] = i - 1 ; } if ( r_pop > i + 1 ) { stack[ count++ ] = i + 1 ; stack[ count++ ] = r_pop; } } free ( stack) ; stack = NULL ;
} int main ( ) { int n; scanf ( "%d" , & n) ; int array[ n] ; for ( int i= 0 ; i< n; i++ ) { scanf ( "%d" , & array[ i] ) ; } quick_sort ( array, 0 , sizeof ( array) / sizeof ( int ) - 1 ) ; for ( int i = 0 ; i < sizeof ( array) / sizeof ( int ) ; i++ ) { printf ( "%d " , array[ i] ) ; }
}
# include <stdio.h>
void insertSort ( int array[ ] , int n) { int temp; for ( int i = 1 ; i < n; i++ ) { int low = 0 ; int hight = i- 1 ; temp = array[ i] ; while ( hight>= low) { int mid = ( low + hight ) / 2 ; if ( array[ mid] < temp) { hight = mid - 1 ; } else { low = mid + 1 ; } } for ( int j = i- 1 ; j > hight; j-- ) { array[ j+ 1 ] = array[ j] ; } array[ hight+ 1 ] = temp; }
} int main ( void ) { int n; scanf ( "%d" , & n) ; int a[ n] ; for ( int i= 0 ; i< n; i++ ) { scanf ( "%d" , & a[ i] ) ; } insertSort ( a, n) ; for ( int i = 0 ; i < n; i++ ) { printf ( "%d " , a[ i] ) ; } return 0 ;
}