{"id":29226,"date":"2023-01-06T09:50:00","date_gmt":"2023-01-06T04:20:00","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/"},"modified":"2023-01-06T09:50:00","modified_gmt":"2023-01-06T04:20:00","slug":"solved-push-pop-stack-operation-in-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/","title":{"rendered":"[Solved] Push pop stack operation in C [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48071158\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48071158\" data-parentid=\"48070800\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here you have again some things wrong. Before you create new threads with<br \/>\nsimilar content, stick to one thread.<\/p>\n<p>In your code you also never check if <code>malloc<\/code> fails. It&#8217;s always better to do that. For simplicity I&#8217;ve omitted these checks in my suggestions. <\/p>\n<p>1.<br \/>\nWhy do you do <code>S1[0].top=-1;<\/code> in <code>makeEmpty<\/code>? <code>create<\/code> already does that and writing in that way (instead of <code>S1-&gt;top = -1<\/code>) makes the code hard to interpret, because it could mean that you are treating the result of <code>create<\/code> as an array of <code>Stack<\/code>s. It&#8217;s not wrong, it makes harder for us to interpret your intentions.<\/p>\n<pre><code>Stack makeEmpty(void)\n{\n    Stack S1=create(100);\n    return S1;\n}\n<\/code><\/pre>\n<p>is enough.<\/p>\n<p>2.<br \/>\nIn <code>push<\/code> and <code>pop<\/code> you have to check first if the operations are valid. That<br \/>\nmeans:<\/p>\n<ul>\n<li>for <code>push<\/code>: check that you still have space to do the operation<\/li>\n<li>for <code>pop<\/code>: check that you have at least one element on the stack.<\/li>\n<\/ul>\n<p>The code could be:<\/p>\n<pre><code>int isFull(Stack S)\n{\n    return (S-&gt;size - 1 == S-&gt;top);\n}\n\nint push(char X, Stack S)\n{\n    if(isFull(S))\n        return 0;\n\n    S-&gt;array[++S-&gt;top]=X;\n    return 1;\n}\n\nint pop(Stack S, char *val)\n{\n    if(isEmpty(S))\n        return 0;\n\n    *val = S-&gt;array[S-&gt;top--];\n    return 1;\n}\n<\/code><\/pre>\n<p>I changed the signatures of these functions to let the user know <strong>if<\/strong> the<br \/>\noperation were successful. Otherwise you have no idea and you end up with<br \/>\nundefined values. In main your should change the <code>pop<\/code> calls as well:<\/p>\n<pre><code>char el;\npop(S1, &amp;el);\n<\/code><\/pre>\n<p>3.<br \/>\nWhat do you want <code>deleteStack<\/code> to do: reset the stack or free the memory? You<br \/>\nare mixing those in a bad way.<\/p>\n<p>If you want to reset (meaning to pop all elements at once), then all you have<br \/>\nto do is set <code>top = -1<\/code>. You don&#8217;t have to free <code>array<\/code>. If you do, then you<br \/>\nneed to reallocate memory for <code>array<\/code> again.<\/p>\n<p>When using <code>free<\/code> you can only pass to <code>free<\/code> the same pointer<br \/>\nyou&#8217;ve got from <code>malloc<\/code>\/<code>calloc<\/code>\/<code>realloc<\/code>. In your code you are passing a<br \/>\n8 bit integer as a pointer address and freeing that, that will make your<br \/>\nprogram crash 100% of the time.<\/p>\n<pre><code>void deleteStack(Stack S)\n{\n    S-&gt;top = -1;\n}\n<\/code><\/pre>\n<p>If you want to free the memory<\/p>\n<pre><code>void freeStack(Stack S)\n{\n    free(S-&gt;array);\n    free(S);\n}\n<\/code><\/pre>\n<p><strong>but<\/strong> that also means that you cannot access the stack any more. Note that I<br \/>\nchanged the name of the function to make it more clear its intention.<\/p>\n<p>If you use my suggestions, you have to change them in <code>main<\/code> as well,<br \/>\nspecially the <code>pop<\/code>s.<\/p>\n<hr>\n<p><strong>EDIT<\/strong>:<br \/>\nSebivor&#8217;s quote from the comments:<\/p>\n<blockquote>\n<p>Don&#8217;t hide levels of pointer indirection behind <code>typedef<\/code><\/p>\n<\/blockquote>\n<p>Yes, that also makes the code hard to read. In my suggestions I didn&#8217;t change that, but I definitely would change<\/p>\n<pre><code>typedef struct stack Stack;\n<\/code><\/pre>\n<p>and in every function that has <code>Stack S<\/code> as argument change it to <code>Stack *S<\/code>.  (See also Is it a good idea to typedef pointers \u2014 the short answer is &#8216;No&#8217;.)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Push pop stack operation in C [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here you have again some things wrong. Before you create new threads with similar content, stick to one thread. In your code you also never check if malloc fails. It&#8217;s always better to do that. For simplicity I&#8217;ve omitted these checks in my suggestions. 1. Why do you do S1[0].top=-1; in makeEmpty? create already &#8230; <a title=\"[Solved] Push pop stack operation in C [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\" aria-label=\"More on [Solved] Push pop stack operation in C [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,324,1738,458],"class_list":["post-29226","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-c","tag-strcmp","tag-struct"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Push pop stack operation in C [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Push pop stack operation in C [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here you have again some things wrong. Before you create new threads with similar content, stick to one thread. In your code you also never check if malloc fails. It&#8217;s always better to do that. For simplicity I&#8217;ve omitted these checks in my suggestions. 1. Why do you do S1[0].top=-1; in makeEmpty? create already ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T04:20:00+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Push pop stack operation in C [closed]\",\"datePublished\":\"2023-01-06T04:20:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\"},\"wordCount\":405,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"c++\",\"strcmp\",\"struct\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\",\"name\":\"[Solved] Push pop stack operation in C [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-06T04:20:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Push pop stack operation in C [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Push pop stack operation in C [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Push pop stack operation in C [closed] - JassWeb","og_description":"[ad_1] Here you have again some things wrong. Before you create new threads with similar content, stick to one thread. In your code you also never check if malloc fails. It&#8217;s always better to do that. For simplicity I&#8217;ve omitted these checks in my suggestions. 1. Why do you do S1[0].top=-1; in makeEmpty? create already ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/","og_site_name":"JassWeb","article_published_time":"2023-01-06T04:20:00+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Push pop stack operation in C [closed]","datePublished":"2023-01-06T04:20:00+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/"},"wordCount":405,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","c++","strcmp","struct"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/","name":"[Solved] Push pop stack operation in C [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-06T04:20:00+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-push-pop-stack-operation-in-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Push pop stack operation in C [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29226"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29226\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}