{"id":6386,"date":"2017-05-17T04:00:47","date_gmt":"2017-05-17T04:00:47","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=6386"},"modified":"2022-01-11T14:59:33","modified_gmt":"2022-01-11T14:59:33","slug":"c-stack-example-lifo-stack","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/c-stack-example-lifo-stack\/","title":{"rendered":"C++ Stack Example: LIFO Stack"},"content":{"rendered":"<p>Hello! In this guide, I want to tell you about one of the most widely used data structures in C++. This is a type called Stack.<\/p>\n<p>The stack is a data structure organized on the principle of LIFO (last in \u2013 first out). It\u2019s usually compared with a gun clip \u2013 the shooting starts from the last bullet. It looks like this:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/03\/lifo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6388\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/03\/lifo.png\" alt=\"\" width=\"391\" height=\"281\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/03\/lifo.png 391w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2017\/03\/lifo-300x216.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/a><\/p>\n<p>So, let\u2019s start the implementation:<!--more--><\/p>\n<p>First we should create our data structure:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nstruct number {<br \/>\n\tint value; \/\/ value of the element<br \/>\n\tnumber *next; \/\/ pointer to the next element<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Create two pointers of number type:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\nnumber *top = NULL, *p;<br \/>\n[\/code]<\/p>\n<p>Now assume that we will read the values from the text file:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\ndo {<br \/>\n\tp = new number; \/\/ create a new element<br \/>\n\tp-&gt;next = top; \/\/ create the pointer to the previous element<br \/>\n\tfile &gt;&gt; p-&gt;value; \/\/ read the value from the file<br \/>\n\ttop = p; \/\/ move the stack\u2019s head<br \/>\n} while (top-&gt;value); \/\/ repeat the cycle until the document is over<br \/>\nNow we are able to read our stack. Let\u2019s try to insert some value inside the stack:<br \/>\np = top; \/\/ use a temporary pointer for the head of the stack<br \/>\nwhile (p-&gt;value != 7 || p-&gt;next != NULL) { \/\/ move to the needed value<br \/>\n\tp = p-&gt;next;<br \/>\n}<br \/>\nnumber *new_el = new number; \/\/ create a new element<br \/>\nnew_el-&gt;value = 15; \/\/ set the value<br \/>\nnew_el-&gt;next = p-&gt;next; \/\/ create a pointer to the next element<br \/>\np-&gt;next = new_el; \/\/ create a pointer to the previous element<\/p>\n<p>Now let\u2019s try to delete some element:<br \/>\nwhile (p-&gt;value != 7 || p-&gt;next != NULL) { \/\/ move to the needed value<br \/>\n\tp = p-&gt;next;<br \/>\n}<br \/>\nnumber *p2 = p-&gt;next; \/\/ save the address of the element to the new pointer<br \/>\np-&gt;next = p-&gt;next-&gt;next; \/\/ assign it with the next element<br \/>\ndelete p2; \/\/ delete element<br \/>\n[\/code]<\/p>\n<p>Now we are able to create a dynamic data structure called stack, read it from the text file, insert some values inside of it and delete them.<\/p>\n<p>Thanks for the attention!<\/p>\n<blockquote><p><em>Our C stack example gives you the opportunity to complete your own homework successfully. But don&#8217;t hand in our sample as your own assignment. The goal of our example is to help you with your homework of a similar topic (by the way, you can check one more C++ sample <a href=\"https:\/\/assignmentshark.com\/blog\/parity-check-program-in-c\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>). If you can&#8217;t do your task, AssignmentShark.com can do it for you. Don&#8217;t leave incomplete tasks, as there are experts who can deal with them easily.<\/em><\/p>\n<p><em>Our service always remains safe \u2013 your personal data will be secure and never passed to third parties. Each student can use our service, as we have affordable prices. We are available 24\/7, so you can contact us any time you need. When you have a lot of work to do, leave it to our site. Our experts can deal with any topic and assignment of any difficulty.<\/em><\/p>\n<p><em>When you are placing an order on our site, mention your requirements and set the deadline. Testimonials on our site show that we do everything possible to satisfy our customers&#8217; needs. The job of our service is to provide <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">online assignment help<\/a> for students to solve their problems with homework.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this guide, I want to tell you about one of the most widely used data structures in C++. This is a type called Stack. The stack is a data structure organized on the principle of LIFO (last in \u2013 first out). It\u2019s usually compared with a gun clip \u2013 the shooting starts from [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-6386","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=6386"}],"version-history":[{"count":7,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6386\/revisions"}],"predecessor-version":[{"id":12979,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/6386\/revisions\/12979"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=6386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=6386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=6386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}