Trouble using push in stack - c

I'm learning data structures and I have a problem pushing data into my stack. Even though I have used the same push function early in the program it doesn't seem to push characters. The program should transform infix expressions to postfix
When given the number "1+2" it should return "12+", however it only returns "12"
Thanks in advice
bool revisar(char * inf, stack p){
inicializa(&p);
Nodo D;
char pos[MAX];
int i=0;
int j=0;
while(inf[i]!='\0')
{//WHILE
if(inf[i]>='0' && inf[i]<='9')
{
pos[j++]=inf[i++];
}
if(inf[i]=='(')
{
D.caracter=inf[i++];
push(&p,D);
}
if(inf[i]==')')
{
while(top(p).caracter!='(')
{
pos[j++]=pop(&p).caracter;
}
if(top(p).caracter=='(')
pop(&p);
i++;
}
else
{
if(inf[i]=='+'||inf[i]=='-'||inf[i]=='*'||inf[i]=='/')
{
if(empty(p)||top(p).caracter=='(')
{
D.cara
cter=inf[i++];
push(&p,D);
if(empty(p));
printf("NNNNNNOOOOOOOOOOOOOOOOOOOOOO"); **Here it prints that the stack is still empty....
}
else
{
if(menorpre(top(p).caracter,inf[i]))
{
D.caracter=inf[i++];
push(&p,D);
}
else
{
pos[j++]=pop(&p).caracter;
if(!empty(p))
{
if(top(p).caracter!='(')
pos[j++]=pop(&p).caracter;
}
else
{
D.caracter=inf[i++];
push(&p,D);
}
}
}
}
}
}
while(!empty(p)){
printf("ddd");
pos[j++]=pop(&p).caracter;
}
pos[j]='\0';
printf("\n \n");
printf("i=%d,j=%d",i,j);
printf("LA CADENA EN POSFIJO ES: ");
puts(pos);
}
My stack library
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include "pila.h"
void inicializa(stack *p ){
*p = NULL;
}
bool empty(stack p){
if (p==NULL){
return true;
}
else return false;
}
int push(stack * p, Nodo n){
stack nuevo = (stack)malloc(sizeof(Nodo));
if(nuevo == NULL){
return 300;
}
*nuevo = n;
if(empty(*p)){
nuevo->next = NULL;
}
else{
nuevo->next = *p;
*p = nuevo;
}
return 301;
}
Nodo pop(stack * p){
Nodo copy = *(*p);
free(*p);
*p = copy.next;
inicializa(&copy.next);
return copy;
}
Nodo top(stack p){
Nodo copy;
copy = *p;
copy.next = NULL;
return copy;
}
The header file
struct nodo{
char caracter;
int numero;
struct nodo * next;
};
typedef struct nodo Nodo;
typedef Nodo * stack;
void inicializa(stack *p);
bool empty(stack p);
Nodo top(stack p);
Nodo pop(stack *p);
int push(stack *p, Nodo n);

The short answer -- in this code:
int push(stack * p, Nodo n){
stack nuevo = (stack)malloc(sizeof(Nodo));
if(nuevo == NULL){
return 300;
}
*nuevo = n;
if(empty(*p)){
nuevo->next = NULL;
}
else{
nuevo->next = *p;
*p = nuevo;
}
return 301;
}
The *p = nuevo; should come after the else, not inside it. It applies regardless. The longer answer:
There are several problems with your stack code, including mismanagement of malloc'd memory. I suggest a simpler stack design, along the lines:
struct nodo {
char caracter;
struct nodo *next;
};
typedef struct nodo Nodo;
typedef Nodo *stack;
void inicializa(stack *p);
bool empty(stack p);
char top(stack p);
char pop(stack *p);
int push(stack *p, char c);
void inicializa(stack *p) {
*p = NULL;
}
bool empty(stack p) {
return (p == NULL);
}
int push(stack *p, char caracter) {
Nodo *nuevo = malloc(sizeof(*nuevo));
if (nuevo == NULL) {
return 300;
}
nuevo->caracter = caracter;
if (empty(*p)) {
nuevo->next = NULL;
} else {
nuevo->next = *p;
}
*p = nuevo;
return 301;
}
char pop(stack *p) {
Nodo *copy = *p;
*p = copy->next;
char caracter = copy->caracter;
free(copy);
return caracter;
}
char top(stack p) {
return p->caracter;
}
This requires a revision of revisar() along the lines:
void revisar(char *inf, stack p)
{
char caracter;
char pos[MAX];
int i = 0;
int j = 0;
inicializa(&p);
while (inf[i] != '\0')
{
if (inf[i] >= '0' && inf[i] <= '9')
{
pos[j++] = inf[i++];
}
else if(inf[i] == '(')
{
(void) push(&p, inf[i++]);
}
else if (inf[i] == ')')
{
while (top(p) != '(')
{
pos[j++] = pop(&p);
}
if (top(p) == '(')
{
(void) pop(&p);
}
i++;
}
else
{
if (inf[i] == '+' || inf[i] == '-' || inf[i] == '*' || inf[i] == '/')
{
if (empty(p) || top(p) == '(')
{
(void) push(&p, inf[i++]);
if (empty(p))
{
printf("NNNNNNOOOOOOOOOOOOOOOOOOOOOO"); // Here it prints that the stack is still empty....
}
}
else
{
if (menorpre(top(p), inf[i]))
{
(void) push(&p, inf[i++]);
}
else
{
pos[j++] = pop(&p);
if (!empty(p))
{
if (top(p) != '(')
{
pos[j++] = pop(&p);
}
}
else
{
(void) push(&p, inf[i++]);
}
}
}
}
}
}
while (!empty(p))
{
pos[j++] = pop(&p);
}
pos[j]='\0';
printf("\n \n");
printf("i=%d, j=%d", i, j);
printf("LA CADENA EN POSFIJO ES: ");
puts(pos);
}
In my testing, I was able to get revisar("1+2", p); to produce:
% ./a.out
ddd
i=3, j=3LA CADENA EN POSFIJO ES: 12+
%
Which I'm guessing is what you're after. If you have more questions about this, make sure to add the definition of menorpre() to your code above as it's the piece that's missing that can't be reconstructed.

Related

Code for parenthesis matching using stack made with double linked list isn't giving any output

I was given an assignment to write the code for parenthesis matching using Double Linked Lists. So, I made stack functions using the same and even wrote the parenthesis matching function with it. But somehow, even after removing all the errors, I'm not able to run the code, i.e. it's not showing any output
Here is my code
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node *next;
struct node *prev;
}*top = NULL;
int isFull()
{
struct node *n = (struct node *)malloc(sizeof(struct node));
if (n == NULL)
{
return 1;
}
else
{
return 0;
}
}
int isEmpty(struct node *top)
{
if (top == NULL)
{
return 1;
}
else
{
return 0;
}
}
struct node *push(struct node *top, char val)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if (isFull())
{
printf("Stack overflowed!\n");
}
else if (isEmpty(top))
{
ptr->data = val;
ptr->next = top;
ptr->prev = NULL;
top = ptr;
return top;
}
else
{
ptr->data = val;
top->prev = ptr;
ptr->next = top;
ptr->prev = NULL;
top = ptr;
return top;
}
}
char pop(struct node **top)
{
struct node *ptr = *top;
char x;
if (isEmpty(*top))
{
printf("Stack Underflowed!\n");
}
else
{
x = (*top)->data;
((*top)->next)->prev = NULL;
*top = ptr->next;
free(ptr);
return x;
}
}
void print(struct node *start)
{
struct node *ptr = start;
while (ptr->next != NULL)
{
printf("%c ", ptr->data);
ptr = ptr->next;
}
}
int match(char a, char b)
{
if (a == '{' && b == '}')
{
return 1;
}
if (a == '[' && b == ']')
{
return 1;
}
if (a == '(' && b == ')')
{
return 1;
}
return 0;
}
int parenthesis(struct node *top, char *str)
{
char popped_char;
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == '{' || str[i] == '[' || str[i] == '(')
{
top = push(top, str[i]);
}
else if (str[i] == '}' || str[i] == ']' || str[i] == ')')
{
popped_char = pop(&top);
if (!match(popped_char, str[i]))
{
return 0;
}
}
}
if (isEmpty(top))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
// struct node *top = (struct node *)malloc(sizeof(struct node));
// top = NULL;
char *str = (char*)malloc(100*sizeof(char));
printf("Enter the string\n");
scanf("%s", str);
// printf("%s", str);
// para(top, str);
if (parenthesis(top, str))
{
printf("Parenthesis Matched!\n");
}
else
{
printf("Parenthesis did not match!\n");
}
}
If I write {} or {()}, I was expecting to get the output as "Parenthesis did not match!", but my code isn't even running.

Popping a float into a char to use as ascii code gives off a crash

while (!isEmpty(s))
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
This is where the crash happens specifically at the *c line. When i display the stack it has a value of 43.00000 which means it contains the '+' in ASCI code.
I also used these 3 lines above this specific one in two loops which gave no error.
float pop(stack *s)
{
float x = s->top->data;
node *temp = s->top;
s->top = s->top->next;
free(temp);
return x;
}
Here is the pop function.
Also this works in vscode and the crashes happen in codeblocks only
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
typedef struct node
{
float data;
struct node *next;
} node;
typedef struct
{
node *top;
} stack;
node *newNode(float x)
{
node *n = malloc(sizeof(node));
n->data = x;
n->next = NULL;
return n;
}
stack *init()
{
stack *s = malloc(sizeof(stack));
s->top = NULL;
return s;
}
float pop(stack *s)
{
float x = s->top->data;
node *temp = s->top;
s->top = s->top->next;
free(temp);
return x;
}
void push(stack *s, float x)
{
node *n = newNode(x);
if (s->top == NULL)
s->top = n;
else
{
n->next = s->top;
s->top = n;
}
}
float peek(stack *s)
{
float x = s->top->data;
return x;
}
int isEmpty(stack *s)
{
return (s->top == NULL);
}
void display(stack *s)
{
stack *temp = init();
while (!isEmpty(s))
push(temp, pop(s));
while (!isEmpty(temp))
{
printf("%f ", peek(temp));
push(s, pop(temp));
}
printf("\n");
}
int priority(char op)
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
if (op == '^')
return 3;
else
return 0;
}
int isOperator(char op[5])
{
if (!strcmp(op, "+") || !strcmp(op, "-") || !strcmp(op, "*") || !strcmp(op, "/") || !strcmp(op, "^") || !strcmp(op, "(") || !strcmp(op, ")"))
return 1;
else
return 0;
}
int isDigit(char digit[20])
{
int i = 0;
if (digit[0] == '-')
i++;
for (i; digit[i] != '\0'; i++)
if (!isdigit(digit[i]) && digit[i] != '.')
return 0;
return 1;
}
char *infixToPostfix(char *infix)
{
char postfix[100] = "", tokens[20][2000], *temptok, *c;
int i = 0;
temptok = strtok(infix, " ");
strcpy(tokens[i++], temptok);
while (temptok != NULL)
{
temptok = strtok(NULL, " ");
if (temptok != NULL)
strcpy(tokens[i++], temptok);
}
int ntok = i;
stack *s = init();
for (i = 0; i < ntok; i++)
{
if (isDigit(tokens[i]) && strcmp(tokens[i], "-"))
{
strcat(postfix, tokens[i]);
strcat(postfix, " ");
}
else if (isOperator(tokens[i]))
{
if (isEmpty(s))
push(s, tokens[i][0]);
else if (tokens[i][0] == '(')
push(s, tokens[i][0]);
else if (tokens[i][0] == ')')
{
while (peek(s) != '(')
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
pop(s);
}
else
{
while (!isEmpty(s) && priority(peek(s)) >= priority(tokens[i][0]) && tokens[i][0] != '(')
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
push(s, tokens[i][0]);
}
}
}
display(s);
system("pause");
while (!isEmpty(s))
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
strcpy(infix, postfix);
return infix;
}
int main()
{
char infixExpr[256] = "";
printf("Enter an expression you want to evaluate or Ctrl+Z to exit: ");
while (fgets(infixExpr, 255, stdin) != NULL)
{
replaceNewLineBySpace(infixExpr);
char *postfix = infixToPostfix(infixExpr);
printf("Postfix : %s\n", postfix);
float result = evaluatePostfix(postfix);
printf("Result: %f\n", result);
system("pause");
// exit(1);
}
return 0;
}

Need help creating maxHeap function

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode {
int id;
struct listNode *next;
} ListNode;
typedef struct treeNode {
char *word;
char *key;
int freq;
ListNode *head;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
TreeNode *insertItem(TreeNode *root, char *gword);
void printTreeInorder(TreeNode *v);
void searchforexist(TreeNode *root, char *key);
int searchItem(TreeNode *root, char *word);
void Heap(TreeNode *arr[],TreeNode *root, int i);
void maxheap(TreeNode *arr[],int k);
void freeNodes(TreeNode *root);
#define MAX 25
int main() {
char in[MAX];
char word[MAX];
TreeNode *root = NULL;
int comp;
int f=0;
int t=0;
FILE *fp = fopen("input.txt", "r");
memset(word, 0, MAX);
if (fp != NULL) {
while (fscanf(fp, "%24s \n", word) != EOF) {
root = insertItem(root, word);//insert items
t++;
if (strcmp(word, "eof") == 0) break;
}
fclose(fp);
}
// User inputs word
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
while(comp!=0)
{
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
if(comp==1)
break;
f=searchItem(root,in);
printf("%d",f);
f=0;
printf("\n");
}
TreeNode *arr[t];
Heap(arr,root,t);// HEAPPPPPPPPPPPPPPPPPPPPPPPPPPPP
printTreeInorder(root);
printf("\n");
freeNodes(root);
return 0;
}
TreeNode *insertItem(TreeNode *root, char *gword) {
TreeNode *v = root;
TreeNode *pv = NULL;
while (v != NULL) {
pv = v;
int comp = strcmp(gword, v->word);
if (comp < 0) {
v = v->left;
} else if (comp > 0) {
v = v->right;
} else {
char *word = v->word;
searchforexist(root,v->word);
return root;
}
}
TreeNode *tmp = (TreeNode *)malloc(sizeof(TreeNode));
tmp->word = strdup(gword);
tmp->left = tmp->right = NULL;
tmp->freq = 1;
if (root != NULL) {
if (strcmp(gword, pv->word) < 0) {
pv->left = tmp;
} else {
pv->right = tmp;
}
} else
root = tmp;
return root;
}
void searchforexist(TreeNode *root, char *word) {
if(root == NULL) {
return;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
root->freq++;
} else {
searchforexist(comp < 0 ? root->left : root->right , word);
}
}
int searchItem(TreeNode *root, char *word)
{
if(root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
return root->freq;
} else {
searchItem(comp < 0 ? root->left : root->right , word);
}
}
void Heap(TreeNode *arr[],TreeNode *root, int i)
{
int k=0;
while(k<i)
{
if(root==NULL){
maxheap(arr,k);
}
arr[k]=root;
k++;
if (k=i){
maxheap(arr,k);
break;
}
Heap(arr,root->left,k);
Heap(arr,root->right,k);
}
}
void maxheap(TreeNode *arr[],int k)
{
int i;
int j;
for (i = 0; i < k; i++)
{
for (j = 0; j < k; j++)
{
if(arr[i]->freq>arr[j]->freq)
{
TreeNode *tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
for (i = 0; i < k; i++)
{
printf("%s %d",arr[i]->word,arr[i]->freq);
}
}
void printTreeInorder(TreeNode *v)
{
if (v==NULL) return;
printf("(");
printTreeInorder(v->left);
printf(")");
printf(" %.4s ", v->word);
printf("(");
printTreeInorder(v->right);
printf(")");
}
void freeNodes(TreeNode *root) {
if (root == NULL) {
return;
}
freeNodes(root->left);
freeNodes(root->right);
if(root->word != NULL) free(root->word);
if(root->key != NULL) free(root->key);
free(root);
return;
}
This program reads a file and puts all strings to a binary search tree. Repeating words are not added but the frequency counter increases (searchforexist). Then the user types a word and the program displays the frequency of the word typed.
The above works fine using any input file.
However im having trouble with the next steps of the assignment:
After that the hole tree is suppose to be copied in a maxheap based on the frequency of each word. The heap must be created using array with size equal to the elements of a tree. Every cell of said array must contain a pointer that points to a node in the binary search tree so we can still access the word inside and its frequency.
The user then inputs an int number and the programm prints all words that have frequency less that the number inputed by the user.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode {
int id;
struct listNode *next;
} ListNode;
typedef struct treeNode {
char *word;
char *key;
int freq;
ListNode *head;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
TreeNode *insertItem(TreeNode *root, char *gword);
void printTreeInorder(TreeNode *v);
void searchforexist(TreeNode *root, char *key);
int searchItem(TreeNode *root, char *word);
void freeNodes(TreeNode *root);
#define MAX 25
int main() {
char in[MAX];
char word[MAX];
TreeNode *root = NULL;
int comp;
int f=0;
FILE *fp = fopen("input.txt", "r");
memset(word, 0, MAX);
if (fp != NULL) {
while (fscanf(fp, "%24s \n", word) != EOF) {
root = insertItem(root, word);//insert items
if (strcmp(word, "eof") == 0) break;
}
fclose(fp);
}
// User inputs word
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
while(comp!=0)
{
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
if(comp==1)
break;
f=searchItem(root,in);
printf("%d",f);
f=0;
printf("\n");
}
//heapcreating here
printTreeInorder(root);
printf("\n");
freeNodes(root);
return 0;
}
TreeNode *insertItem(TreeNode *root, char *gword) {
TreeNode *v = root;
TreeNode *pv = NULL;
while (v != NULL) {
pv = v;
int comp = strcmp(gword, v->word);
if (comp < 0) {
v = v->left;
} else if (comp > 0) {
v = v->right;
} else {
char *word = v->word;
searchforexist(root,v->word);
return root;
}
}
TreeNode *tmp = (TreeNode *)malloc(sizeof(TreeNode));
tmp->word = strdup(gword);
tmp->left = tmp->right = NULL;
tmp->freq = 1;
if (root != NULL) {
if (strcmp(gword, pv->word) < 0) {
pv->left = tmp;
} else {
pv->right = tmp;
}
} else
root = tmp;
return root;
}
void searchforexist(TreeNode *root, char *word) {
if(root == NULL) {
return;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
root->freq++;
} else {
searchforexist(comp < 0 ? root->left : root->right , word);
}
}
int searchItem(TreeNode *root, char *word)
{
if(root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
return root->freq;
} else {
searchItem(comp < 0 ? root->left : root->right , word);
}
}
void printTreeInorder(TreeNode *v)
{
if (v==NULL) return;
printf("(");
printTreeInorder(v->left);
printf(")");
printf(" %.4s ", v->word);
printf("(");
printTreeInorder(v->right);
printf(")");
}
void freeNodes(TreeNode *root) {
if (root == NULL) {
return;
}
freeNodes(root->left);
freeNodes(root->right);
if(root->word != NULL) free(root->word);
if(root->key != NULL) free(root->key);
free(root);
return;
}
I and many of us make simple mistakes. Use the automated tools you have to increase your coding efficiency.
Save time, enable all compiler warnings - or use a better compiler.
warning: control reaches end of non-void function [-Wreturn-type]
searchItem() needs to return a value.
int searchItem(TreeNode *root, char *word) {
if (root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if (comp == 0) {
return root->freq;
} else {
// searchItem(comp < 0 ? root->left : root->right, word);
return searchItem(comp < 0 ? root->left : root->right, word);
}
}
Other problems may exist.

printing a pointer value

i have this issue while trying to print *p value while p is pointed to a nodo of the list (obviusly i would like to print the nodo.info value)
here is the code, hope u understand:
struct nodo {
int info;
struct nodo *prec;
struct nodo *succ;
} ;
typedef struct nodo nodo;
int main (void) { // just declaring my 3 nodos
struct nodo *p;
struct nodo anodo;
struct nodo bnodo;
struct nodo cnodo;
anodo.info = 200;
anodo.prec = NULL;
anodo.succ = NULL;
bnodo.info = 22;
bnodo.prec = NULL;
bnodo.succ = NULL;
cnodo.info = 2000;
cnodo.prec = NULL;
cnodo.succ = NULL;
anodo.succ = &bnodo;
bnodo.prec = &anodo;
bnodo.succ = &cnodo;
cnodo.prec = &bnodo;
p = &anodo;
printf("\n%d\n", checklist (p)); // calling function
return 0;
}
nodo *checklist (struct nodo *p) {
int j=0;
while (p != NULL) {
if (p->info >= p->succ->info) { //if first element is major or same than next
p=p->succ;
} else {
while (p != NULL) {
if (p->info >= p->succ->info) { //same
p=p->succ;
j++;
} else {
p = NULL;
}
}
}
}
while (j != 0) {
p = p->prec; //used a counter to get back to the first nodo in wich next was less than prev
j--;
}
return p;
}
feel free to ask any details
p = checklist (p);
if (p)
printf("\n%d\n", p->info);
You need to return the nodo.info then
int checklist (struct nodo *p) {
int j=0;
while (p != NULL) {
/* avoid this p->succ->info */
if (p->info >= p->succ->info) { //if first element is major or same than next
p = p->succ;
} else {
while (p != NULL) {
if (p->info >= p->succ->info) { //same
p = p->succ;
j++;
} else {
p = NULL;
}
}
}
}
while (j != 0) { p = p->prec; //used a counter to get back to the first nodo in wich next was less than prev
j--;
}
return p->info;
}
or in main
int info
struct nodo *found;
found = checklist(p);
if (found != NULL)
printf("\n%d\n", found->info);

Problems with this stack implementation

where is the mistake?
My code here:
typedef struct _box
{
char *dados;
struct _box * proximo;
} Box;
typedef struct _pilha
{
Box * topo;
}Stack;
void Push(Stack *p, char * algo)
{
Box *caixa;
if (!p)
{
exit(1);
}
caixa = (Box *) calloc(1, sizeof(Box));
caixa->dados = algo;
caixa->proximo = p->topo;
p->topo = caixa;
}
char * Pop(Stack *p)
{
Box *novo_topo;
char * dados;
if (!p)
{
exit(1);
}
if (p->topo==NULL)
return NULL;
novo_topo = p->topo->proximo;
dados = p->topo->dados;
free(p->topo);
p->topo = novo_topo;
return dados;
}
void StackDestroy(Stack *p)
{
char * c;
if (!p)
{
exit(1);
}
c = NULL;
while ((c = Pop(p)) != NULL)
{
free(c);
}
free(p);
}
int main()
{
int conjunto = 1;
char p[30], * v;
int flag = 0;
Stack *pilha = (Stack *) calloc(1, sizeof(Stack));
FILE* arquivoIN = fopen("L1Q3.in","r");
FILE* arquivoOUT = fopen("L1Q3.out","w");
if (arquivoIN == NULL)
{
printf("Erro na leitura do arquivo!\n\n");
exit(1);
}
fprintf(arquivoOUT,"Conjunto #%d\n",conjunto);
while (fscanf(arquivoIN,"%s", p) != EOF )
{
if (pilha->topo == NULL && flag != 0)
{
conjunto++;
fprintf(arquivoOUT,"\nConjunto #%d\n",conjunto);
}
if(strcmp(p, "return") != 0)
{
Push(pilha, p);
}
else
{
v = Pop(pilha);
if(v != NULL)
{
fprintf(arquivoOUT, "%s\n", v);
}
}
flag = 1;
}
StackDestroy(pilha);
return 0;
}
The Pop function returns the string value read from file.
But is not correct and i don't know why.
You're not allocating any storage for the strings pointed to by dados - you're just re-using one string buffer (p) and passing that around, so all your stack elements just point to this one string.

Resources