Postgraduate Review (4)-stack operations

Source: Internet
Author: User

I finally finished the stack, but I am still too lazy.

The main algorithm is 1) number hexadecimal conversion 2) parentheses matching 3) Row editing

Let's take a look at the maze first ....

# Include <stdio. h>

# Include <stdlib. h>
# Define Max size 100
# Define stack_increase 10
Typedef char elemtype;
Typedef struct
{
Elemtype * base;
Elemtype * top;
Int stacksize;
} Sstack;
Void Init (sstack * s)
{
S-> base = (elemtype *) malloc (maxsize * sizeof (elemtype ));
If (! S-> Base)
Exit (0 );
S-> Top = s-> base;
S-> stacksize = maxsize;
}
Elemtype gettop (sstack S)
{
If (S. base = S. Top)
Printf ("No element! \ N ");
Else
Return (* (-- S. Top ));
}
Void push (sstack * s, elemtype X)
{
If (S-> top-S-> base> = s-> stacksize)
{
S-> base = (elemtype *) realloc (S-> base, (S-> stacksize + stack_increase) * sizeof (elemtype ));
If (! S-> Base)
Exit (0 );
Else
S-> stacksize + = stack_increase;

}

* (S-> top) = x;
S-> top ++;

}
Elemtype POP (sstack * s)
{
Elemtype C;
If (S-> Top = s-> base)
Exit (0 );
Else
{
C = gettop (* s );
S-> top --;
Return C;
}

}
Void clearstack (sstack * s)
{
While (! Stackempty (* s ))
{
Pop (s );
}
}
Void show (sstack S)
{
While (S. Top! = S. Base)
{
Printf ("% 5c", * (S. Base ));
S. Base ++;
}
Printf ("\ n ");

}
// Instance, decimal input, and octal output
// N = (N Div d) * D + N mod d
Void conversion ()
{
Int I;
Sstack S;
Init (& S );
Printf ("Please input a decimal numbel: \ n ");
Scanf ("% d", & I );
While (I)
{
Push (& S, I % 8 );
I = I/8;
}
While (S. Top! = S. Base)
{
Printf ("% d", gettop (s ));
S. Top --;
}
Printf ("\ n ");
}
Int stackempty (sstack S)
{
If (S. Top = S. Base) return 1;
Else return 0;
}
// Matching brackets. When left parentheses are added to the stack, the stack starts and ends with empty stacks.
Int match (char * exp)
{
Sstack S;
Init (& S );
Int I = 0, j = 1, B = 1;
Printf ("Exp: % C \ n", exp [3]);
Char c = NULL;
While (exp [I]! = '\ 0' & B = 1)
{
// Printf ("exp [I]: % C \ n", exp [I]);
If (exp [I] = '(') Push (& S, exp [I]);
If (exp [I] = ')')
{
C = POP (& S );
If (C! = '(') B = 0;
}

I ++;
}
Return (B & stackempty (s ));

}

Void lineedit ()
{
Sstack S;
Init (& S );
Char CH = getchar ();
While (Ch! = EOF)
{
While (Ch! = EOF & Ch! = '\ N ')
{
Switch (CH)
{
Case '#': Pop (& S); break;
Case '@': clearstack (& S); break;
Default: Push (& S, CH); break;
}
Ch = getchar ();
}
Show (s );
// Do some action to transmit the data
Clearstack (& S );
If (Ch! = EOF) CH = getchar ();
}
// Destroystack ();
}
Main ()
{
Lineedit ();
Char * Ex = "(())";
Printf ("EX: % C \ n", ex [3]);
Printf ("is matched? % D ", match (Ex ));
Sstack S;
Init (& S );
Printf ("is empty? % D ", stackempty (s ));
Printf ("Push three! \ N ");
Push (& S, 'A ');
Push (& S,'s ');
Push (& S, 'w ');

Show (s );
Printf ("pop one! \ N ");
Pop (& S );
Show (s );
Printf ("decimal input, octal output \ n ");
Conversion ();

Gettop (s );
Printf ("Hello! \ N ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.