From 30315e6d295e5ad3ffc91494d92c100d3a642fd3 Mon Sep 17 00:00:00 2001 From: ergz Date: Fri, 30 Jun 2023 07:25:01 -0700 Subject: [PATCH] fixed, was just i was trying to get value for freed mem --- c/a.out | Bin 16944 -> 16976 bytes c/stack.c | 55 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/c/a.out b/c/a.out index 5d3aed0992800724758d5b3ec86041e3470d7733..301e664dd0e64acd4b04a1bb9fdeae882432c449 100755 GIT binary patch delta 1483 zcmY*Ze`r%z6u$SRwMlH2ywvP9+cbG)&_-=-UD{^dZ2MZHuTo`Wt2)>~hLsH!-ROQ4 zHrg^PdxWyqGe;Gfogj4RP20%xC6G=0lKIcm@T_9MoI*dhY44EY<;sgrAmN3tXQ4 zV&cWK!Hp-+A4_~aCxm&%wCZ!zI`d ze8Hop8r^E|=zD)aD){bB?c;}PD)}(Q&{HsC;JW1{2SugW8yElbFZ-`e)qYn$xm`Iz zon*Tn8-}VSr_x@sp0}*)>BVHNu&ivlkgTPaHJ$d6<%fo%C%b%D0rjU;?NW9Jp0yva z**7uV63@(7bc&foXO1{)5^Y&4caqY-BY9iuX-7)RW%MqTsxXNz>SULtH9b%3gLzsR zJe$=ga3-s*x`kRdbA)V3(H-JQW?L)gN%cBXJ$A-+mc9FE!H9!7tx21p+0M-xhFSW@ zi~b0sM^Lu3_O1D~j4@?%i#DmWwP^n+uPWM1o7z`Cf$mpYW3%{3?yUcjo(?Hq=w6gL z5Pf|&{e9^E$L|`(Frs_fFfxe6h*?Axu^+MOo?$S~*xQIv#G{Cfh|+z-*oL?cu^aIO z;t=A`h@*&a{cRZ65vLH#U@Tf1K8=-Dp(0yb5FmIKcN}*yaJe*GiLB`+3fB_c)MFVa zvLwf_n*_O&PXzW0&Avr@3Jct5K7;!tZUs8!2;TyS%e042Y{kdj0CdoPQ2ipF7I0KxaH6REFSie0eCy0?buJtuSVGgt!96 z;!>FEM_HJ$M&jnydbk&tJe!cE+%XWVrBEU?$J1#mv2aExT1wqg+yl{y_|OjA>qPL+ zpr=~mN2n%00cWbE>Jj8b%wkb17{4Lrx@2pO((~YIw}AcPXhEQ+IDF8K8nPeMd<~4e z1xk(7^1xi<{}Rj6a>-X~xc)4Mf in2B129URvO8Vt^-U_hz$6xUkhYj8!W5o^<`hrD}Hd9b>&{a`8XoW$A zK^&{2&$QTi1how8Ku2K;c5mYFpKQ)*3&J)Tv;Ji}P(nLh(>fe8+w=ic)!xjan29H!$t=)m#tuP&d1n2*l^UfSRska#$tCut{j_VbAJ!^hI4%0u4_ zUR`M(Et@$IkG$4%aM8Vew?jW%vZuUa?`A^OvGPlsn6%WvjQdaD+{LwuNA9Fl?Kk!4 z4R_X!sN4)(^&Fv(!ana4jtqI{e0m8cylO1kCNZyS*HrC?T+~1u3p4Fm0&`Nib)-L713RhP%D-UdC+0cF??KR0)HP=MYX{B=NJFwjb=UmAv#%aYW{Xz%s4~gHf6U#SV#Cxu4k~ z&sfH25t2sAGVY7*#ZFnpd%zF0-%fE_l+GdrA*cQm@`VN2Zijp)j82l1&@mg$U3{gX zA~1}XijZ-3ftBaF##%~u5Tfi$YS)xhQoE(}C|V(49htpp7?Y^-ieb#6ZbMx_RZ&M!BYDHPjrtPmHcAMf#!*XG4dZ##80rw} z3Dl2JD2qbjTmz>*ShYKq#_RpRAEf;cZ74rt0I8T%}*HzgL0^*CTuBEO>VM^wymdl=j0f zkAFgcgtwXl5~+fT=Evadu1Y!z`DTBz1A%T*P2BC+Xx)>ol*-q}CiHzR|$GTM_HE<>B z54U2>X4EU{%^QK-r^UJ^rckGQV)Yc3H$ANHj~|ru%X=0|q`u8?rnSLUoFjg=rPgEk mtp?i~J+nc+(V|Z2(`^Q&=V4ljIZ2G0J&;#ovvalue = value; - + n->next = NULL; return (n); } @@ -56,7 +56,7 @@ bool pop(Stack* s) { } void delete_stack(Stack* s) { - while (s->length > 0) { + while (s->head != NULL) { pop(s); } } @@ -74,6 +74,14 @@ void push(Stack* s, Node* n) { } } +int peek(Stack* s) { + if (s->length == 0) { + return -1; + } + + return s->head->value; +} + int main() { Stack* s = new_stack(); @@ -83,35 +91,44 @@ int main() { Node* d = new_node(13); push(s, a); - printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); push(s, b); - printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); push(s, c); - printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); push(s, d); - printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); - bool res = pop(s); - printf("the value at the top of the stack is: %d\n", s->head->value); - printf("the length of the stack is: %d\n", s->length); + // bool res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); - res = pop(s); - printf("the value at the top of the stack is: %d\n", s->head->value); - printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); - res = pop(s); - printf("the value at the top of the stack is: %d\n", s->head->value); - printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); - res = pop(s); - printf("the value at the top of the stack is: %d\n", s->head->value); - printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); + // res = pop(s); + // printf("the value at the top of the stack is: %d\n", peek(s)); + // printf("the length of the stack is: %d\n", s->length); - // delete_stack(s); + delete_stack(s); return (0); } \ No newline at end of file