#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 100
int top = -1;
typedef struct _Node{
char word[10];
_Node *next;
}SNode;
bool IsFull(int capacity){
if(top >= capacity - 1){
return true;
}else{
return false;
}
}
bool IsEmpty(){
if(top == -1){
return true;
}else{
return false;
}
}
void Push(SNode stack[], SNode value, int capacity){
if(IsFull(capacity) == true){
printf("\nStack is full. Overflow condition!");
}else{
++top;
stack[top] = value;
}
}
void Pop(){
if(IsEmpty() == true){
printf("\nStack is empty. Underflow condition!");
}else{
top --;
}
}
SNode Top(SNode stack[]){
return stack[top];
}
int Size(){
return top + 1;
}
int main()
{
int capacity = 100;
int top = -1;
SNode stack[capacity];
char str[100];
int len, i, index;
printf("Enter any string: ");
fflush(stdin);
gets(str);
len = strlen(str);
SNode noteItem;
index = 0;
for(i=0; i < len; i++)
{
if(str[i] == ' ')
{
Push(stack, noteItem, capacity);
if(Size() > 1)
{
stack[Size() - 2].next = &stack[Size() - 1];
}
memset(noteItem.word, '\0', sizeof noteItem.word);
index = 0;
}
else
{
noteItem.word[index] = str[i];
index ++;
}
}
if(str[len - 1] != ' ')
{
Push(stack, noteItem, capacity);
}
printf("Original string: %s\n", str);
printf("Reverse ordered words: ");
while(Size() > 0)
{
SNode s = Top(stack);
if(i > 0)
{
printf(" ");
}
printf("%s", s.word);
Pop();
}
return 0;
}
Giúp mình đưa bài vào function
code này phải của bạn viết không? tiêu đề của topic này là ý gì? bạn đang gặp khó khăn gì?
1 Like