1. Implementation principle of stack
#include <stdio.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int main(void)
{
push('a');
push('b');
push('c');
while(!is_empty())
{
putchar(pop());
}
printf("\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
2. Application of stack-----Reverse Polish notation
#include <stdio.h>
#include <string.h>
int stack[512];
int top = 0;
void push(int c);
int pop(void);
int is_empty(void);
int main(void)
{
char a[100];
int n;
int i;
int n1, n2;
printf("Please enter a reverse polish expression:\n");
gets(a);
n = strlen(a);
for(i = 0; i < n; i++)
{
if((a[i] >= '0') && (a[i] <= '9'))
push(a[i] - '0');
else
{
n2 = pop();
n1 = pop();
switch(a[i])
{
case '+':
push(n1 + n2);
break;
case '-':
push(n1 - n2);
break;
case '*':
push(n1 * n2);
break;
}
}
}
printf("result = %d\n", pop());
return 0;
}
void push(int c)
{
stack[top++] = c;
}
int pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
3. Application of stack----converting infix notation to suffix notation
#include <stdio.h>
#include <string.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int main(void)
{
char str[100];
int i, len;
printf("Please enter a calcuate expression:");
gets(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == '(')
continue;
else if((str[i] >= '0') && (str[i] <= '9'))
printf("%c", str[i]);
else if((str[i] == '+') || (str[i] == '-') || (str[i] == '*'))
push(str[i]);
else if(str[i] == ')')
printf("%c", pop());
}
printf("\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
4. Stack application----bracket matching
#include <stdio.h>
#include <string.h>
int stack[512];
int top = 0;
void push(int c);
int pop(void);
int is_empty(void);
int main(void)
{
char str[100];
int i, len;
printf("Please enter a calculate expression:");
gets(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == '(')
push(i);
else if(str[i] == ')')
printf("%d %d\n", pop(), i);
}
return 0;
}
void push(int c)
{
stack[top++] = c;
}
int pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
5. Stack application----palindrome determination
#include <stdio.h>
#include <string.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int is_palindrom(char *pt);
int main(void)
{
char str[100];
printf("Please enter a string:");
gets(str);
if(is_palindrom(str))
printf("str is a palindrom.\n");
else
printf("str is not a palindrom.\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
int is_palindrom(char *pt)
{
int len, i;
len = strlen(pt);
for(i = 0; i < len/2; i++)
push(pt[i]);
if(len % 2 == 1)
i++;
while(!is_empty())
{
if(pop() == pt[i])
i++;
else
return 0;
}
return 1;
}