[GH-ISSUE #2223] Hello, I have a problem where the lines cannot match and the stack is messed up. #47785

Closed
opened 2026-04-28 05:19:19 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @FennikzZ on GitHub (Jan 27, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/2223

Originally assigned to: @bmizerany on GitHub.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node {
    char c;
    struct Node* next;
} Node;

Node* head = NULL;
Node* tail = NULL;
int count = 0;

void appendList(char ch) {
    Node* n = (Node*)malloc(sizeof(Node)); 
    if (n == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    n->next = NULL;
    n->c = ch;

    if (head == NULL) {
        head = n;
    } else {
        tail->next = n;
    }

    tail = n;
    count++;
}

void printList() {
    Node* current = head;

    while (current != NULL) {
        printf("%c", current->c);
        current = current->next;
    }
    printf("\n");
}
void destroyList(){
    Node *ptr;
    while(count>0){
        ptr=head;
        head=head->next;
        count--;
        free(ptr);
    }
    head = NULL;
    tail = NULL;
}


typedef struct nd {
    char c;
    struct nd *next;
} node;
node *top = NULL;

void push(char x) {
    node *n = malloc(sizeof(node));
    if (n == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    n->next = top;
    top = n;
    n->c = x;
}

char pop() {
    char p;
    node *n;
    if (top == NULL) {
        printf("Stack is empty\n");
        return 0;
    }
    n = top;
    top = top->next;
    p = n->c;
    free(n);
    if(p != '('){
    appendList(p);
    }
    return p;
}

char stackTop() {
    if (top == NULL) {
        return 0;
    } else {
        return top->c;
    }
}

void printStack() {
    node* temp = top;

    while (temp != NULL) {
        printf("%c ", temp->c);
        temp = temp->next;
    }
    printf("\t\t");
}

int prec(char c) {
    if (c == '^')
        return 3;
    else if (c == '/' || c == '*')
        return 2;
    else if (c == '+' || c == '-')
        return 1;
    else
        return -1;
}

char associativity(char c) {
    if (c == '^')
        return 'R';
    return 'L'; 
}
void infixToPostfix(){
    char ch;
    int step = 1;
 
    printf("Infix : ");

    while ((ch = getchar()) != '\n') {
        if(step == 1){
            printf("\n%-15s%-15s%-15s%-15s\n\n","STEP","SYMBOL","STACK","OUTPUT");
        }
        if (isdigit(ch) || isalpha(ch)) {
            appendList(ch);
        }  else if (ch == '(') {
            push(ch);
        }  else if (ch == ')') {
            while (stackTop() != '(') {
                pop();
            }
            pop(); 
        }  else {
            while (top != NULL && (prec(ch) < prec(stackTop()) || (prec(ch) == prec(stackTop()) && associativity(ch) == 'L'))) {                       
                pop();
            }
            push(ch);
        }
        printf("%2d\t\t%c\t\t", step, ch);
        printStack();
        printList();
        step++;
    }
    while (top != NULL) {
        pop();
    }
    printf("%2d\t\t\t\t\t\t", step);
    printList();
    printf("\n");
    printf("Postfix : ");
    printList();
    printf("___________________________________________________________\n\n");
}
void main() {
    printf("Infix to Postfix converter.\n");
    while (1){
        infixToPostfix();
        destroyList();
    }
}
Originally created by @FennikzZ on GitHub (Jan 27, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/2223 Originally assigned to: @bmizerany on GitHub. ``` #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct Node { char c; struct Node* next; } Node; Node* head = NULL; Node* tail = NULL; int count = 0; void appendList(char ch) { Node* n = (Node*)malloc(sizeof(Node)); if (n == NULL) { printf("Memory allocation failed\n"); return; } n->next = NULL; n->c = ch; if (head == NULL) { head = n; } else { tail->next = n; } tail = n; count++; } void printList() { Node* current = head; while (current != NULL) { printf("%c", current->c); current = current->next; } printf("\n"); } void destroyList(){ Node *ptr; while(count>0){ ptr=head; head=head->next; count--; free(ptr); } head = NULL; tail = NULL; } typedef struct nd { char c; struct nd *next; } node; node *top = NULL; void push(char x) { node *n = malloc(sizeof(node)); if (n == NULL) { printf("Memory allocation failed\n"); return; } n->next = top; top = n; n->c = x; } char pop() { char p; node *n; if (top == NULL) { printf("Stack is empty\n"); return 0; } n = top; top = top->next; p = n->c; free(n); if(p != '('){ appendList(p); } return p; } char stackTop() { if (top == NULL) { return 0; } else { return top->c; } } void printStack() { node* temp = top; while (temp != NULL) { printf("%c ", temp->c); temp = temp->next; } printf("\t\t"); } int prec(char c) { if (c == '^') return 3; else if (c == '/' || c == '*') return 2; else if (c == '+' || c == '-') return 1; else return -1; } char associativity(char c) { if (c == '^') return 'R'; return 'L'; } void infixToPostfix(){ char ch; int step = 1; printf("Infix : "); while ((ch = getchar()) != '\n') { if(step == 1){ printf("\n%-15s%-15s%-15s%-15s\n\n","STEP","SYMBOL","STACK","OUTPUT"); } if (isdigit(ch) || isalpha(ch)) { appendList(ch); } else if (ch == '(') { push(ch); } else if (ch == ')') { while (stackTop() != '(') { pop(); } pop(); } else { while (top != NULL && (prec(ch) < prec(stackTop()) || (prec(ch) == prec(stackTop()) && associativity(ch) == 'L'))) { pop(); } push(ch); } printf("%2d\t\t%c\t\t", step, ch); printStack(); printList(); step++; } while (top != NULL) { pop(); } printf("%2d\t\t\t\t\t\t", step); printList(); printf("\n"); printf("Postfix : "); printList(); printf("___________________________________________________________\n\n"); } void main() { printf("Infix to Postfix converter.\n"); while (1){ infixToPostfix(); destroyList(); } } ```
GiteaMirror added the needs more info label 2026-04-28 05:19:19 -05:00
Author
Owner

@bmizerany commented on GitHub (Mar 11, 2024):

@FennikzZ Will you please help me understand the issue? Is the output from ollama? If so, what are the minimum number of steps to reproduce (please include specific commands)? Also, if you have some logs and can share them, please do. They can found under ~/.ollama/logs/server.log

It would also be helpful to know your ARCH, OS, etc.

<!-- gh-comment-id:1989079555 --> @bmizerany commented on GitHub (Mar 11, 2024): @FennikzZ Will you please help me understand the issue? Is the output from ollama? If so, what are the minimum number of steps to reproduce (please include specific commands)? Also, if you have some logs and can share them, please do. They can found under `~/.ollama/logs/server.log` It would also be helpful to know your ARCH, OS, etc.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#47785