CS13002 Programming and Data Structures | Section 3/C, Spring 2003--2004 |
Assignment 7 : Solution
The program
#include <stdio.h> #define MAX_SIZE 32 char store[MAX_SIZE]; int tos1, tos2; void initStack ( int option ) { int i; if (option == 0) { tos1 = -2; tos2 = -1; } else { tos1 = -1; tos2 = MAX_SIZE; } for (i=0; i<MAX_SIZE; ++i) store[i] = '_'; } void printStack ( ) { int i; for (i=0; i<MAX_SIZE; ++i) printf("%c",store[i]); printf("\n"); } void pushStack ( int which , char what , int option ) { if (option == 0) { if (which == 1) { tos1 += 2; if (tos1 >= MAX_SIZE) { fprintf(stderr,"Error: Overflow in Stack 1.\n"); exit(1); } else store[tos1] = what; } else if (which == 2) { tos2 += 2; if (tos2 >= MAX_SIZE) { fprintf(stderr,"Error: Overflow in Stack 2.\n"); exit(1); } else store[tos2] = what; } else { fprintf(stderr,"Error: Undefined stack identifier.\n"); exit(1); } } else { if (which == 1) { ++tos1; if (tos1 >= tos2) { fprintf(stderr,"Error: Overflow in stack.\n"); exit(1); } else store[tos1] = what; } else if (which == 2) { --tos2; if (tos1 >= tos2) { fprintf(stderr,"Error: Overflow in stack.\n"); exit(1); } else store[tos2] = what; } else { fprintf(stderr,"Error: Undefined stack identifier.\n"); exit(1); } } } void popStack ( int which , int option ) { if (option == 0) { if (which == 1) { if (tos1 < 0) { fprintf(stderr,"Error: Underflow in Stack 1.\n"); exit(1); } else { store[tos1] = '_'; tos1 -= 2; } } else if (which == 2) { if (tos2 < 0) { fprintf(stderr,"Error: Underflow in Stack 2.\n"); exit(1); } else { store[tos2] = '_'; tos2 -= 2; } } else { fprintf(stderr,"Error: Undefined stack identifier.\n"); exit(1); } } else { if (which == 1) { if (tos1 < 0) { fprintf(stderr,"Error: Underflow in Stack 1.\n"); exit(1); } else { store[tos1] = '_'; --tos1; } } else if (which == 2) { if (tos2 >= MAX_SIZE) { fprintf(stderr,"Error: Underflow in Stack 2.\n"); exit(1); } else { store[tos2] = '_'; ++tos2; } } else { fprintf(stderr,"Error: Undefined stack identifier.\n"); exit(1); } } } int main () { int i, option, which, action; char what; srand((unsigned int)time(NULL)); printf("Enter strategy -- 0 (odd-even) or 1 (colliding) : "); scanf("%d",&option); initStack(option); i = 0; while (1) { ++i; printf("Iteration %3d : ", i); fflush(stdout); /* Initially make push. Then make push twice as likely as pop. */ if (i <= 8) action = 1; else action = rand() % 3; /* Make Stack 1 twice more active than Stack 2 */ which = (rand() % 3) ? 1 : 2; if (action) { /* Push */ what = ((which == 1) ? 'a' : 'A') + (rand() % 26); printf("Push %c in stack %d. New stack : ",what,which); fflush(stdout); pushStack(which,what,option); } else { /* Pop */ printf("Pop in stack %d. New stack : ",which); fflush(stdout); popStack(which,option); } printStack(); } }
Output
Enter strategy -- 0 (odd-even) or 1 (colliding) : 0 Iteration 1 : Push p in stack 1. New stack : p_______________________________ Iteration 2 : Push i in stack 1. New stack : p_i_____________________________ Iteration 3 : Push o in stack 1. New stack : p_i_o___________________________ Iteration 4 : Push x in stack 1. New stack : p_i_o_x_________________________ Iteration 5 : Push z in stack 1. New stack : p_i_o_x_z_______________________ Iteration 6 : Push r in stack 1. New stack : p_i_o_x_z_r_____________________ Iteration 7 : Push n in stack 1. New stack : p_i_o_x_z_r_n___________________ Iteration 8 : Push f in stack 1. New stack : p_i_o_x_z_r_n_f_________________ Iteration 9 : Pop in stack 1. New stack : p_i_o_x_z_r_n___________________ Iteration 10 : Push s in stack 1. New stack : p_i_o_x_z_r_n_s_________________ Iteration 11 : Push j in stack 1. New stack : p_i_o_x_z_r_n_s_j_______________ Iteration 12 : Push W in stack 2. New stack : pWi_o_x_z_r_n_s_j_______________ Iteration 13 : Pop in stack 1. New stack : pWi_o_x_z_r_n_s_________________ Iteration 14 : Push X in stack 2. New stack : pWiXo_x_z_r_n_s_________________ Iteration 15 : Push g in stack 1. New stack : pWiXo_x_z_r_n_s_g_______________ Iteration 16 : Push x in stack 1. New stack : pWiXo_x_z_r_n_s_g_x_____________ Iteration 17 : Pop in stack 2. New stack : pWi_o_x_z_r_n_s_g_x_____________ Iteration 18 : Pop in stack 1. New stack : pWi_o_x_z_r_n_s_g_______________ Iteration 19 : Push t in stack 1. New stack : pWi_o_x_z_r_n_s_g_t_____________ Iteration 20 : Pop in stack 1. New stack : pWi_o_x_z_r_n_s_g_______________ Iteration 21 : Push e in stack 1. New stack : pWi_o_x_z_r_n_s_g_e_____________ Iteration 22 : Pop in stack 1. New stack : pWi_o_x_z_r_n_s_g_______________ Iteration 23 : Push R in stack 2. New stack : pWiRo_x_z_r_n_s_g_______________ Iteration 24 : Push p in stack 1. New stack : pWiRo_x_z_r_n_s_g_p_____________ Iteration 25 : Push I in stack 2. New stack : pWiRoIx_z_r_n_s_g_p_____________ Iteration 26 : Push r in stack 1. New stack : pWiRoIx_z_r_n_s_g_p_r___________ Iteration 27 : Push h in stack 1. New stack : pWiRoIx_z_r_n_s_g_p_r_h_________ Iteration 28 : Push u in stack 1. New stack : pWiRoIx_z_r_n_s_g_p_r_h_u_______ Iteration 29 : Push C in stack 2. New stack : pWiRoIxCz_r_n_s_g_p_r_h_u_______ Iteration 30 : Push v in stack 1. New stack : pWiRoIxCz_r_n_s_g_p_r_h_u_v_____ Iteration 31 : Pop in stack 1. New stack : pWiRoIxCz_r_n_s_g_p_r_h_u_______ Iteration 32 : Pop in stack 1. New stack : pWiRoIxCz_r_n_s_g_p_r_h_________ Iteration 33 : Push f in stack 1. New stack : pWiRoIxCz_r_n_s_g_p_r_h_f_______ Iteration 34 : Push Y in stack 2. New stack : pWiRoIxCzYr_n_s_g_p_r_h_f_______ Iteration 35 : Push n in stack 1. New stack : pWiRoIxCzYr_n_s_g_p_r_h_f_n_____ Iteration 36 : Pop in stack 1. New stack : pWiRoIxCzYr_n_s_g_p_r_h_f_______ Iteration 37 : Push D in stack 2. New stack : pWiRoIxCzYrDn_s_g_p_r_h_f_______ Iteration 38 : Push t in stack 1. New stack : pWiRoIxCzYrDn_s_g_p_r_h_f_t_____ Iteration 39 : Push E in stack 2. New stack : pWiRoIxCzYrDnEs_g_p_r_h_f_t_____ Iteration 40 : Push O in stack 2. New stack : pWiRoIxCzYrDnEsOg_p_r_h_f_t_____ Iteration 41 : Push x in stack 1. New stack : pWiRoIxCzYrDnEsOg_p_r_h_f_t_x___ Iteration 42 : Push k in stack 1. New stack : pWiRoIxCzYrDnEsOg_p_r_h_f_t_x_k_ Iteration 43 : Push f in stack 1. New stack : Error: Overflow in Stack 1. Enter strategy -- 0 (odd-even) or 1 (colliding) : 0 Iteration 1 : Push p in stack 1. New stack : p_______________________________ Iteration 2 : Push w in stack 1. New stack : p_w_____________________________ Iteration 3 : Push i in stack 1. New stack : p_w_i___________________________ Iteration 4 : Push m in stack 1. New stack : p_w_i_m_________________________ Iteration 5 : Push q in stack 1. New stack : p_w_i_m_q_______________________ Iteration 6 : Push i in stack 1. New stack : p_w_i_m_q_i_____________________ Iteration 7 : Push d in stack 1. New stack : p_w_i_m_q_i_d___________________ Iteration 8 : Push l in stack 1. New stack : p_w_i_m_q_i_d_l_________________ Iteration 9 : Pop in stack 2. New stack : Error: Underflow in Stack 2. Enter strategy -- 0 (odd-even) or 1 (colliding) : 1 Iteration 1 : Push j in stack 1. New stack : j_______________________________ Iteration 2 : Push x in stack 1. New stack : jx______________________________ Iteration 3 : Push U in stack 2. New stack : jx_____________________________U Iteration 4 : Push r in stack 1. New stack : jxr____________________________U Iteration 5 : Push K in stack 2. New stack : jxr___________________________KU Iteration 6 : Push z in stack 1. New stack : jxrz__________________________KU Iteration 7 : Push m in stack 1. New stack : jxrzm_________________________KU Iteration 8 : Push b in stack 1. New stack : jxrzmb________________________KU Iteration 9 : Pop in stack 2. New stack : jxrzmb_________________________U Iteration 10 : Pop in stack 1. New stack : jxrzm__________________________U Iteration 11 : Pop in stack 2. New stack : jxrzm___________________________ Iteration 12 : Pop in stack 1. New stack : jxrz____________________________ Iteration 13 : Pop in stack 1. New stack : jxr_____________________________ Iteration 14 : Push n in stack 1. New stack : jxrn____________________________ Iteration 15 : Push D in stack 2. New stack : jxrn___________________________D Iteration 16 : Push c in stack 1. New stack : jxrnc__________________________D Iteration 17 : Push H in stack 2. New stack : jxrnc_________________________HD Iteration 18 : Pop in stack 1. New stack : jxrn__________________________HD Iteration 19 : Push G in stack 2. New stack : jxrn_________________________GHD Iteration 20 : Push f in stack 1. New stack : jxrnf________________________GHD Iteration 21 : Pop in stack 1. New stack : jxrn_________________________GHD Iteration 22 : Push V in stack 2. New stack : jxrn________________________VGHD Iteration 23 : Push f in stack 1. New stack : jxrnf_______________________VGHD Iteration 24 : Pop in stack 2. New stack : jxrnf________________________GHD Iteration 25 : Push n in stack 1. New stack : jxrnfn_______________________GHD Iteration 26 : Pop in stack 2. New stack : jxrnfn________________________HD Iteration 27 : Pop in stack 1. New stack : jxrnf_________________________HD Iteration 28 : Push z in stack 1. New stack : jxrnfz________________________HD Iteration 29 : Push G in stack 2. New stack : jxrnfz_______________________GHD Iteration 30 : Push a in stack 1. New stack : jxrnfza______________________GHD Iteration 31 : Push X in stack 2. New stack : jxrnfza_____________________XGHD Iteration 32 : Push w in stack 1. New stack : jxrnfzaw____________________XGHD Iteration 33 : Push L in stack 2. New stack : jxrnfzaw___________________LXGHD Iteration 34 : Push S in stack 2. New stack : jxrnfzaw__________________SLXGHD Iteration 35 : Push q in stack 1. New stack : jxrnfzawq_________________SLXGHD Iteration 36 : Pop in stack 1. New stack : jxrnfzaw__________________SLXGHD Iteration 37 : Push m in stack 1. New stack : jxrnfzawm_________________SLXGHD Iteration 38 : Push Y in stack 2. New stack : jxrnfzawm________________YSLXGHD Iteration 39 : Push z in stack 1. New stack : jxrnfzawmz_______________YSLXGHD Iteration 40 : Push h in stack 1. New stack : jxrnfzawmzh______________YSLXGHD Iteration 41 : Push y in stack 1. New stack : jxrnfzawmzhy_____________YSLXGHD Iteration 42 : Push N in stack 2. New stack : jxrnfzawmzhy____________NYSLXGHD Iteration 43 : Pop in stack 1. New stack : jxrnfzawmzh_____________NYSLXGHD Iteration 44 : Push i in stack 1. New stack : jxrnfzawmzhi____________NYSLXGHD Iteration 45 : Push c in stack 1. New stack : jxrnfzawmzhic___________NYSLXGHD Iteration 46 : Push s in stack 1. New stack : jxrnfzawmzhics__________NYSLXGHD Iteration 47 : Push X in stack 2. New stack : jxrnfzawmzhics_________XNYSLXGHD Iteration 48 : Push q in stack 1. New stack : jxrnfzawmzhicsq________XNYSLXGHD Iteration 49 : Push v in stack 1. New stack : jxrnfzawmzhicsqv_______XNYSLXGHD Iteration 50 : Push a in stack 1. New stack : jxrnfzawmzhicsqva______XNYSLXGHD Iteration 51 : Push l in stack 1. New stack : jxrnfzawmzhicsqval_____XNYSLXGHD Iteration 52 : Push l in stack 1. New stack : jxrnfzawmzhicsqvall____XNYSLXGHD Iteration 53 : Push h in stack 1. New stack : jxrnfzawmzhicsqvallh___XNYSLXGHD Iteration 54 : Pop in stack 1. New stack : jxrnfzawmzhicsqvall____XNYSLXGHD Iteration 55 : Push k in stack 1. New stack : jxrnfzawmzhicsqvallk___XNYSLXGHD Iteration 56 : Push x in stack 1. New stack : jxrnfzawmzhicsqvallkx__XNYSLXGHD Iteration 57 : Push q in stack 1. New stack : jxrnfzawmzhicsqvallkxq_XNYSLXGHD Iteration 58 : Push F in stack 2. New stack : jxrnfzawmzhicsqvallkxqFXNYSLXGHD Iteration 59 : Pop in stack 2. New stack : jxrnfzawmzhicsqvallkxq_XNYSLXGHD Iteration 60 : Push A in stack 2. New stack : jxrnfzawmzhicsqvallkxqAXNYSLXGHD Iteration 61 : Pop in stack 1. New stack : jxrnfzawmzhicsqvallkx_AXNYSLXGHD Iteration 62 : Push z in stack 1. New stack : jxrnfzawmzhicsqvallkxzAXNYSLXGHD Iteration 63 : Push v in stack 1. New stack : Error: Overflow in stack. Enter strategy -- 0 (odd-even) or 1 (colliding) : 1 Iteration 1 : Push C in stack 2. New stack : _______________________________C Iteration 2 : Push z in stack 1. New stack : z______________________________C Iteration 3 : Push n in stack 1. New stack : zn_____________________________C Iteration 4 : Push h in stack 1. New stack : znh____________________________C Iteration 5 : Push k in stack 1. New stack : znhk___________________________C Iteration 6 : Push q in stack 1. New stack : znhkq__________________________C Iteration 7 : Push e in stack 1. New stack : znhkqe_________________________C Iteration 8 : Push a in stack 1. New stack : znhkqea________________________C Iteration 9 : Push t in stack 1. New stack : znhkqeat_______________________C Iteration 10 : Push z in stack 1. New stack : znhkqeatz______________________C Iteration 11 : Push f in stack 1. New stack : znhkqeatzf_____________________C Iteration 12 : Pop in stack 1. New stack : znhkqeatz______________________C Iteration 13 : Push x in stack 1. New stack : znhkqeatzx_____________________C Iteration 14 : Push Z in stack 2. New stack : znhkqeatzx____________________ZC Iteration 15 : Pop in stack 2. New stack : znhkqeatzx_____________________C Iteration 16 : Push e in stack 1. New stack : znhkqeatzxe____________________C Iteration 17 : Push q in stack 1. New stack : znhkqeatzxeq___________________C Iteration 18 : Push t in stack 1. New stack : znhkqeatzxeqt__________________C Iteration 19 : Push v in stack 1. New stack : znhkqeatzxeqtv_________________C Iteration 20 : Push r in stack 1. New stack : znhkqeatzxeqtvr________________C Iteration 21 : Push Q in stack 2. New stack : znhkqeatzxeqtvr_______________QC Iteration 22 : Push v in stack 1. New stack : znhkqeatzxeqtvrv______________QC Iteration 23 : Pop in stack 1. New stack : znhkqeatzxeqtvr_______________QC Iteration 24 : Pop in stack 2. New stack : znhkqeatzxeqtvr________________C Iteration 25 : Pop in stack 1. New stack : znhkqeatzxeqtv_________________C Iteration 26 : Pop in stack 2. New stack : znhkqeatzxeqtv__________________ Iteration 27 : Push v in stack 1. New stack : znhkqeatzxeqtvv_________________ Iteration 28 : Push k in stack 1. New stack : znhkqeatzxeqtvvk________________ Iteration 29 : Push i in stack 1. New stack : znhkqeatzxeqtvvki_______________ Iteration 30 : Pop in stack 1. New stack : znhkqeatzxeqtvvk________________ Iteration 31 : Pop in stack 2. New stack : Error: Underflow in Stack 2.