Home » » Xuat Chuoi Ky Ky Tu Dao Nguoc Stack

Xuat Chuoi Ky Ky Tu Dao Nguoc Stack

Written By 1 on Thứ Bảy, 19 tháng 5, 2012 | 23:17


// XuatChuoiDaoNguoc.cpp : Defines the entry point for the console application.
// VD Nhap vao chuoi: ABCDEF  thi xuat nguoc la : FEDCBA

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#define TRUE 1
#define FALSE 0
#define MAX 100
typedef char Data;
typedef struct{
int top;
Data S[MAX];
} Stack;
void Push(Stack & st, Data x);
Data Pop(Stack &st);
void InitStack(Stack &st);
int IsEmpty(Stack st);
int IsFull(Stack st);
Data Top(Stack st);
void Push(Stack & st, Data x)
{
if (IsFull(st))
printf("\nStack is full!");
else
st.S[++st.top] = x;
}
Data Pop(Stack &st)
{
if (IsEmpty(st))
printf("\nStack is empty!");
else
return (st.S[st.top--]);
}
void InitStack(Stack &st)
{
st.top = -1;
}
int IsEmpty(Stack st)
{
if (st.top == -1)
return TRUE;
else
return FALSE;
}
int IsFull(Stack st)
{
if (st.top >= MAX)
return TRUE;
else
return FALSE;
}
Data Top(Stack st)
{
Data d;
if (IsEmpty(st))
printf("\n Stack is empty!");
else
d = st.S[st.top];
return d;
}
void main()
{
char s[MAX];
Stack st;
printf(" \n Nhap chuoi: ");
gets(s);
InitStack(st);
for(int i=0; i < strlen(s); i++)
Push(st, s[i]);
printf("\n Chuoi nguoc lai: ");
while (!IsEmpty(st))
printf("%c", Pop(st));

getch();
}

0 nhận xét:

Đăng nhận xét