Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

a8

a guest Nov 18th, 2018 70 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. //Author: Jessica de Leeuw, deleeuwj
  2. //Assignment 8 Quesion 1
  3. #include <stdio.h> // imports IO library
  4. #include <stdlib.h> // imports IO library
  5. #include <string.h> // imports IO library
  6. #define MAX_LEN 101
  7.  
  8. typedef struct node {
  9.   char val[MAX_LEN];
  10.   struct node * next;
  11. } node_t;
  12.  
  13. void push(node_t * top, char * str);
  14. char * pop(node_t * top, char pop_val[MAX_LEN]);
  15.  
  16. int main(void) {
  17.  
  18.   char str[MAX_LEN] = "*+AB-CD";
  19.   int len = strlen(str);
  20.  
  21.   node_t * top = NULL;
  22.   top = malloc(sizeof(node_t));
  23.  
  24.   for (int i = len; i > 0; i--) {
  25.     char pop_val1[MAX_LEN];
  26.     char pop_val2[MAX_LEN];
  27.     if (str[i] == '*' || str[i] == '-' || str[i] == '+' || str[i] == '/') {
  28.       pop(top, pop_val1);
  29.       pop(top, pop_val2);
  30.     }
  31.     else {
  32.       char char_str[2];
  33.       char_str[0] = str[i];
  34.       char_str[1] = '\0';
  35.       push(top, char_str);
  36.     }
  37.     //printf("%c\n", str[i]);
  38.   }
  39.  
  40.   return 0;
  41.  
  42. }
  43.  
  44. void push(node_t * top, char str[MAX_LEN]) {
  45.  
  46.   node_t * head = malloc(sizeof(node_t));
  47.   if (head != NULL) {
  48.     head->next = top;
  49.     strcpy(head->val, str);
  50.     // head->val = str;
  51.     top = head;
  52.   }
  53.   else printf("%s could not be inserted. No memory available.\n", str);
  54.  
  55. }
  56.  
  57. char * pop(node_t * top, char pop_val[MAX_LEN]) {
  58.  
  59.   if (top->val != NULL) {
  60.     node_t * tmp = top;
  61.     strcpy(pop_val, top->val);
  62.     strcpy(top->val, top->next->val);
  63.     // pop_val = top->val;
  64.     // top->val = top->next->val;
  65.     free(tmp);
  66.     return pop_val;
  67.   }
  68.   else {
  69.     printf("The stack is empty.\n");
  70.     return "\0";
  71.   }
  72.  
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top