前提・実現したいこと
発生している問題・エラーメッセージ
コンパイルは通ったが実行しようとすると何もメッセージが出ず終了する。
該当のソースコード
C
1//スタック機能を用いた整合性判定プログラム 2//左括弧ならスタックにpush 右括弧ならpopし同種類の括弧か調べる 3//今回のデータのマックスは20にする 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7 8typedef struct node{ 9 char data; 10 struct node *next; 11} Node; 12 13typedef struct { 14 Node *head; 15 Node *crnt; 16} List; 17 18typedef struct { 19 int max; 20 int num; 21 List stk; 22} Stack; 23 24static Node *AllocNode(void){ 25 return (Node *)calloc(1, sizeof(Node)); 26} 27 28int push(Stack *s, char x){ 29 if(s -> num >= s -> max){ 30 return -1; 31 } 32 33 if(s -> num == 0){ 34 s -> stk.head = AllocNode();//お盆の一番上がhead 35 } 36 else{ 37 s -> stk.crnt = AllocNode(); 38 s -> stk.crnt -> next = s -> stk.head;//headを移動 39 s -> stk.head = s -> stk.crnt; 40 } 41 s -> stk.head -> data = x; 42 s -> num++; 43 return 0; 44} 45 46int pop(Stack *s, char *x){ 47 Node *node; 48 49 if(s -> num == 0){ 50 return -1; 51 } 52 53 node = s -> stk.head; 54 55 while(node != NULL){ 56 if(x == "]"){ 57 if(&node -> data == "["){ 58 s -> stk.crnt -> next = node -> next; 59 } 60 } 61 if(x == "}"){ 62 if(&node -> data == "{"){ 63 s -> stk.crnt -> next = node -> next; 64 } 65 } 66 if(x == ")"){ 67 if(&node -> data == "("){ 68 s -> stk.crnt -> next = node -> next; 69 } 70 } 71 s -> stk.crnt = node; 72 node = node -> next; 73 } 74 75 *x = s -> stk.crnt -> data; 76 77 s -> num--; 78 free(node); 79 return 0; 80} 81 82void display(Stack *s){ 83 Node *node; 84 node = s -> stk.head; 85 86 while(node != NULL){ 87 printf("%c\n", node -> data); 88 node = node -> next; 89 } 90} 91int main(){ 92 Stack *s; 93 int i; 94 int result = 0; 95 char data[50]; 96 s -> max = 20; 97 s -> num = 0; 98 99 printf("data?\n"); 100 scanf("%s",data); 101 102 for(i = 0; data[i] != NULL; ++i){ 103/////////////////////////1////////////////////////////////// 104 if(&data[i] == "{" || &data[i] == "[" || &data[i] == "("){//push 105 result = push(s, data[i]); 106 } 107/////////////////////////2////////////////////////////////// 108 if(&data[i] == "}" || &data[i] == "]" || &data[i] == ")"){//pop 109 result = pop(s, &data[i]); 110 } 111 112 113 } 114 115 if(s -> stk.head != NULL || result == -1){ 116 printf("不整合\n"); 117 } 118 else{ 119 printf("整合\n"); 120 } 121 free(s); 122 return 0; 123} 124
試したこと
1と2どちらかコメントアウトすると実効できるが両方コメントアウトしないと失敗する。
補足情報(FW/ツールのバージョンなど)
bcc32
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。