Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Author: Jessica de Leeuw, deleeuwj
- //Assignment 8 Quesion 1
- #include <stdio.h> // imports IO library
- #include <stdlib.h> // imports IO library
- #include <string.h> // imports IO library
- #define MAX_LEN 101
- typedef struct node {
- char val[MAX_LEN];
- struct node * next;
- } node_t;
- void push(node_t * top, char * str);
- char * pop(node_t * top, char pop_val[MAX_LEN]);
- int main(void) {
- char str[MAX_LEN] = "*+AB-CD";
- int len = strlen(str);
- node_t * top = NULL;
- top = malloc(sizeof(node_t));
- for (int i = len; i > 0; i--) {
- char pop_val1[MAX_LEN];
- char pop_val2[MAX_LEN];
- if (str[i] == '*' || str[i] == '-' || str[i] == '+' || str[i] == '/') {
- pop(top, pop_val1);
- pop(top, pop_val2);
- }
- else {
- char char_str[2];
- char_str[0] = str[i];
- char_str[1] = '\0';
- push(top, char_str);
- }
- //printf("%c\n", str[i]);
- }
- return 0;
- }
- void push(node_t * top, char str[MAX_LEN]) {
- node_t * head = malloc(sizeof(node_t));
- if (head != NULL) {
- head->next = top;
- strcpy(head->val, str);
- // head->val = str;
- top = head;
- }
- else printf("%s could not be inserted. No memory available.\n", str);
- }
- char * pop(node_t * top, char pop_val[MAX_LEN]) {
- if (top->val != NULL) {
- node_t * tmp = top;
- strcpy(pop_val, top->val);
- strcpy(top->val, top->next->val);
- // pop_val = top->val;
- // top->val = top->next->val;
- free(tmp);
- return pop_val;
- }
- else {
- printf("The stack is empty.\n");
- return "\0";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement