大话数据结构第四章学习
1.栈(stack)的基本定义与进出栈
栈的基本性质:
1)栈是限定仅在表尾进行插入和删除的线性表
我们将允许插入的一段称为栈顶(top),另一端称为栈尾(bottom),不含任何数据元素的栈称为空栈。
2)栈的插入称为入栈,删除称为出栈。其操作性质是后进先出,使用头插法。
进栈出栈的实现:
#include <stdio.h>
#include <stdlib.h>
typedef struct A {
int number;
struct A * next;
} NODE, * LIST;
LIST chushihua ( ) {
LIST head;
head = ( NODE * ) malloc ( sizeof ( NODE) ) ;
head-> next = NULL ;
return head;
}
LIST pushstack ( LIST head) {
int x;
printf ( "请输入要插入栈顶的元素" ) ;
scanf ( "%d" , & x) ;
NODE * s;
s = ( NODE * ) malloc ( sizeof ( NODE) ) ;
s-> number = x;
s-> next = head-> next;
head-> next = s;
return head;
}
LIST deletestack ( LIST head) {
NODE * s;
s = ( NODE * ) malloc ( sizeof ( NODE) ) ;
s = head-> next;
head-> next = s-> next;
free ( s) ;
return head;
}
void output ( LIST head) {
printf ( "栈中元素有:" ) ;
NODE * p;
p = head-> next;
while ( p) {
printf ( "%d " , p-> number) ;
p = p-> next;
}
printf ( "\n" ) ;
}
main ( ) {
int flag;
LIST head;
head = chushihua ( ) ;
pushstack ( head) ;
stw:
printf ( "0:结束录入结束1:继续录入元素2:删除栈顶元素3:显示所有元素[ ]\b\b" ) ;
scanf ( "%d" , & flag) ;
switch ( flag) {
case 1 :
pushstack ( head) ;
goto stw;
break ;
case 2 :
deletestack ( head) ;
goto stw;
break ;
case 3 :
system ( "cls" ) ;
output ( head) ;
goto stw;
break ;
case 0 :
exit ( 0 ) ;
break ;
default :
system ( "cls" ) ;
printf ( "输入有误,请重新输入!" ) ;
goto stw;
break ;
}
}
2.队列的链式储存、入队、出队
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode * next;
} QNode, * QueuePtr;
typedef struct
{
QueuePtr front, rear;
} LinkQueue;
Status EnQueue ( LinkQueue * Q, QElemType e)
{
QueuePtr s= ( QueuePtr) malloc ( sizeof ( QNode) ) ;
if ( ! s)
exit ( OVERFLOW) ;
s-> data= e;
s-> next= NULL ;
Q-> rear-> next= s;
Q-> rear= s;
return OK;
}
Status DnQueue ( LinkQueue * Q, QElemType * e)
{
QueuePtr p;
if ( Q-> front== Q-> rear)
return ERROR;
p= Q-> front-> next;
* e= p-> data;
Q-> front-> next= p-> next;
if ( Q-> rear== p)
Q-> rear= Q-> front;
free ( p) ;
return OK;
}
3.斐波那契数列(递归)
#include <stdio.h>
int f ( int n ) ;
int main ( ) {
int n;
scanf ( "%d" , & n) ;
printf ( "%d\n" , f ( n) ) ;
return 0 ;
}
int f ( int n ) {
long s;
if ( n == 0 )
return 0 ;
if ( n == 1 )
return 1 ;
if ( n == 2 )
return 2 ;
s = f ( n - 1 ) + f ( n - 2 ) ;
return s;
}
4.用栈实现四则运算
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node * next;
} Node;
typedef struct stack {
Node * top;
int count;
} Stack;
int InitStack ( Stack * l) {
l-> top = NULL ;
l-> count = 0 ;
return 1 ;
}
int chack ( Stack * s) {
return ( s-> count == 0 ) ? 1 : 0 ;
}
int Push ( Stack * s, int e) {
Node * p = ( Node * ) malloc ( sizeof ( Node) ) ;
p-> data = e;
p-> next = s-> top;
s-> top = p;
s-> count++ ;
return 1 ;
}
int GetTop ( Stack * s) {
if ( NULL == s-> top) {
return 0 ;
}
return ( s-> top-> data) ;
}
int judge ( char s) {
switch ( s) {
case '(' :
return 3 ;
case '*' :
case '/' :
return 2 ;
case '+' :
case '-' :
return 1 ;
default :
return 0 ;
}
}
int fall_back ( Stack * s) {
int e;
if ( NULL == s-> top) {
return 0 ;
}
Node * p = s-> top;
e = p-> data;
s-> top = p-> next;
free ( p) ;
s-> count-- ;
return e;
}
main ( ) {
Stack num, sig;
char str[ 100 ] = { 0 } ;
int i = 0 , tmp = 0 , j;
InitStack ( & num) ;
InitStack ( & sig) ;
printf ( "Please Input Operator :\n" ) ;
scanf ( "%s" , str) ;
while ( str[ i] != '\0' || chack ( & sig) != 1 ) {
if ( str[ i] >= '0' && str[ i] <= '9' ) {
tmp = tmp * 10 + str[ i] - '0' ;
i++ ;
if ( str[ i] < '0' || str[ i] > '9' ) {
Push ( & num, tmp) ;
tmp = 0 ;
}
} else {
if ( ( chack ( & sig) == 1 ) || ( GetTop ( & sig) == '(' && str[ i] != ')' )
|| judge ( str[ i] ) > judge ( GetTop ( & sig) ) ) {
Push ( & sig, str[ i] ) ;
i++ ;
continue ;
}
if ( GetTop ( & sig) == '(' && str[ i] == ')' ) {
fall_back ( & sig) ;
i++ ;
continue ;
}
if ( ( str[ i] == '\0' && chack ( & sig) != 1 ) || ( str[ i] == ')' && GetTop ( & sig) != '(' )
|| judge ( str[ i] ) <= judge ( GetTop ( & sig) ) ) {
switch ( fall_back ( & sig) ) {
case '+' :
Push ( & num, fall_back ( & num) + fall_back ( & num) ) ;
break ;
case '-' :
j = fall_back ( & num) ;
Push ( & num, fall_back ( & num) - j) ;
break ;
case '*' :
Push ( & num, fall_back ( & num) * fall_back ( & num) ) ;
break ;
case '/' :
j = fall_back ( & num) ;
Push ( & num, fall_back ( & num) / j) ;
}
continue ;
}
}
}
printf ( "%d" , fall_back ( & num) ) ;
printf ( "\n" ) ;
return 0 ;
}
最小栈
typedef struct MinStack {
int val;
int min;
struct MinStack * next;
} MinStack, * pMinStack;
MinStack* minStackCreate ( ) {
pMinStack stack = ( pMinStack) malloc ( sizeof ( MinStack) ) ;
stack-> next = NULL ;
return stack;
}
void minStackPush ( MinStack* obj, int valu) {
pMinStack temp = ( pMinStack) malloc ( sizeof ( MinStack) ) ;
temp-> val = valu;
if ( obj-> next == NULL ) {
temp-> min = valu;
obj -> min = 0 ;
} else {
temp-> min = ( ( temp-> val) > ( obj-> next-> min) ) ? ( obj-> next-> min) : ( temp-> val) ;
}
temp-> next = obj-> next;
obj-> next = temp;
}
void minStackPop ( MinStack* obj) {
pMinStack temp = obj-> next;
obj-> next = obj-> next-> next;
free ( temp) ;
temp = NULL ;
}
int minStackTop ( MinStack* obj) {
return obj-> next-> val;
}
int minStackGetMin ( MinStack* obj) {
return obj-> next-> min;
}
void minStackFree ( MinStack* obj) {
pMinStack temp;
while ( obj-> next!= NULL )
{
temp = obj-> next;
obj-> next = obj-> next-> next;
free ( temp) ;
}
free ( obj) ;
obj = NULL ;
}
有效括号
typedef struct ZHAN {
char ch;
struct ZHAN * next;
} NODE, * LIST;
LIST chushihua ( ) {
LIST head = NULL ;
head = ( NODE * ) malloc ( sizeof ( NODE) ) ;
head-> next = NULL ;
return head;
}
void in ( LIST head, char a)
{
NODE * s;
s = ( NODE * ) malloc ( sizeof ( NODE) ) ;
s-> ch = a;
s-> next = head-> next;
head-> next = s;
}
void tui ( LIST head)
{
NODE * s;
s = ( NODE * ) malloc ( sizeof ( NODE) ) ;
s= head-> next;
head-> next= s-> next;
free ( s) ;
}
bool isValid ( char * s)
{
LIST head;
head = chushihua ( ) ;
int n= strlen ( s) , j= 0 , i;
for ( i= 0 ; i< n; i++ )
{
if ( s[ i] == '{' || s[ i] == '[' || s[ i] == '(' )
in ( head, s[ i] ) ;
else
{
if ( head-> next== NULL )
return false;
if ( s[ i] == '}' && head-> next-> ch!= '{' )
return false;
if ( s[ i] == ']' && head-> next-> ch!= '[' )
return false;
if ( s[ i] == ')' && head-> next-> ch!= '(' )
return false;
else
tui ( head) ;
}
}
if ( head-> next== NULL )
return true;
else
return false;
}
合并两个有序链表
struct ListNode* mergeTwoLists ( struct ListNode* l1, struct ListNode* l2)
{
if ( l1== NULL )
return l2;
if ( l2== NULL )
return l1;
struct ListNode * head= ( struct ListNode* ) malloc ( sizeof ( struct ListNode) ) ;
struct ListNode * p;
p= head;
while ( l1!= NULL && l2!= NULL )
{
if ( l1-> val< l2-> val)
{
p-> next= l1;
l1= l1-> next;
p= p-> next;
}
else
{
p-> next= l2;
l2= l2-> next;
p= p-> next;
}
}
if ( l1!= NULL )
p-> next= l1;
else if ( l2!= NULL )
p-> next= l2;
return head-> next;
}
买股票的最佳时机
int maxProfit ( int * prices, int pricesSize)
{
int i, min= prices[ 0 ] , max= prices[ 0 ] , n= 0 ;
for ( i= 1 ; i< pricesSize; i++ )
{
if ( prices[ i] < min)
{
min= prices[ i] ;
max= prices[ i] ;
}
else
if ( prices[ i] > max)
{
max= prices[ i] ;
}
if ( max- min> n)
n= max- min;
}
if ( n<= 0 )
return 0 ;
else
return n;
}