กองซ้อน

กองซ้อน
ความสำคัญของลำดับFILO (First In Last Out)
การซ้ำกันของสมาชิกอนุญาตให้ซ้ำกันได้
เวลาที่ใช้ในการเข้าถึงPUSH/POP
เหมือนกับการวางจานซ้อนกัน วางเพิ่มหรือยกออกทำได้เฉพาะจานบนสุดเท่านั้น

กองซ้อน หรือ สแต็ก (อังกฤษ: Stack) หมายถึง แบบชนิดข้อมูลนามธรรมที่มีลักษณะการเรียงลำดับข้อมูล ในการเข้า-ออกในลักษณะเข้าก่อนออกทีหลัง FILO (First In Last Out) กล่าวคือข้อมูลที่เข้าใหม่ ๆ จะได้ออกก่อน คล้ายกองที่ทับถมซึ่งสิ่งที่เข้ามาใหม่จะอยู่ด้านบน ๆ จึงเรียกว่า กองซ้อน (stack) กองซ้อนมีการดำเนินการพื้นฐานเพียง 3 อย่าง ได้แก่ push, pop และ top กองซ้อน โดยที่การ push คือการใส่ข้อมูลลงไปในกองซ้อน ซึ่งจะกระทำได้หากกองซ้อนยังว่างอยู่ หากไม่มีที่ว่างในกองซ้อนเหลืออยู่หรือกองซ้อนเต็ม กองซ้อนนั้นจะอยู่ในสภาวะล้นหรือมากเกินเก็บ (overflow) การ pop คือการนำข้อมูลออกจากส่วนบนสุดของกองซ้อน นอกจากนี้ การ pop จะเผยข้อมูลที่ถูกผิดอยู่ก่อนหน้า หรือทำให้กองซ้อนว่างได้ แต่ถ้ากองซ้อนนั้นว่างอยู่แล้ว การ pop จะทำให้อยู่ในสภาวะน้อยเกินเก็บ (underflow) (นั่นคือ ไม่มีข้อมูลให้นำออกแล้ว) การ top กองซ้อน จะดึงข้อมูลที่อยู่บนสุดและส่งค่านั้นให้ผู้ใช้โดยที่ไม่ได้ลบทิ้งไป การ top กองซ้อนอาจทำให้กองซ้อนอยู่ในสภาวะน้อยเกินเก็บได้เช่นกัน หากกองซ้อนว่างอยู่แล้ว

กองซ้อนจึงเป็นวิธีการจัดการเข้า-ออกของข้อมูลอีกแบบหนึ่ง เป็นโครงสร้างข้อมูลที่นำมาใช้ในการทำงานของโปรแกรมคอมพิวเตอร์หลายประการ อาทิการสร้าง subroutine การเรียงลำดับนิพจน์ ฯลฯ

จุดเด่นของกองซ้อน

กองซ้อนมีจุดเด่นในการจัดการการเข้า-ออกของข้อมูล ใช้เก็บข้อมูลที่ต้องการจัดเรียงเป็นระบบ โดยพิจารณาข้อมูลที่มาใหม่ๆก่อน จึงทำให้สะดวกต่อการจัดการข้อมูลซึ่งต้องการเรียงลำดับใหม่ หรือการย้อนกลับไปจากข้อมูลใหม่ไปข้อมูลเก่า เช่น subroutine, การเรียงลำดับนิพจน์

บริการที่มักจะมี

  • การเก็บเข้ากองซ้อน (Push)
  • การดึงข้อมูลบนสุดของกองซ้อน (Pop)
  • การดูข้อมูลบนสุดของกองซ้อน (Top)
  • ทำกองซ้อนให้ว่าง ตรวจสอบความว่างของกองซ้อน (Empty)

ความเร็วที่ใช้ในการทำงาน

การทำงานของกองซ้อนนั้นไม่ซับซ้อน เนื่องจากไม่ต้องมีการไล่พิจารณาสมาชิกทุกตัว เป็นเพียงแต่การพิจารณาข้อมูลบนสุดของกองซ้อน จึงทำให้ความเร็วในการทำงานของกองซ้อนเป็นค่าคงที่ (O(1))

วิธีการสร้างกองซ้อน

การสร้างกองซ้อนอาจใช้แถวลำดับประกอบกับจำนวนเต็ม ที่เก็บดัชนีของข้อมูลบนสุดของกองซ้อน (Stack Pointer หรือ Top of Stack) หรือใช้รายการโยงโดยการเก็บข้อมูลที่ใหม่ๆบนตัวแรกสุดของรายการ

การเก็บข้อมูลใหม่เข้าในกองซ้อนเรียกว่า PUSH ทำได้โดยการเพิ่มค่าดัชนีบนสุดของกองซ้อนไปหนึ่ง (increment) และเก็บข้อมูลใหม่ไว้ในดัชนีนั้น หรือเพิ่มเข้าไปที่ตัวแรกสุดของรายการโยง การนำข้อมูลตัวบนสุดของกองซ้อนออกเรียกว่า POP ทำได้โดยการคืนค่าข้อมูลที่เก็บไว้ในแถวลำดับ ดัชนีบนสุดและลดค่าดัชนีบนสุดลงมาอีกหนึ่ง (decrement) หรือเอาตัวแรกสุดของรายการโยงออก สำหรับ การหาตัวบนสุด (Top) นั้นก็ได้มาจากการคืนค่าข้อมูลในดัชนีบนสุดหรือข้อมูลตัวแรกสุดของรายการโยงนั้นเอง

ดูเพิ่ม

Read other articles:

Ця стаття є частиною Проєкту:Населені пункти України (рівень: невідомий) Портал «Україна»Мета проєкту — покращувати усі статті, присвячені населеним пунктам та адміністративно-територіальним одиницям України. Ви можете покращити цю статтю, відредагувавши її, а на стор...

 

Віталій Вовк — президент і засновник «Всеукраїнської відкритої ліги змішаних єдиноборств Реал Файт Промоушен» (англ. Ukrainian open league of MMA Real Fight Promotion). Віталій Вовк також заснував серію турнірів по ММА «West Fight».[1] Біографія Віталій Вовк народився 7 липня 1972 року в місті Л

 

40°26′36″N 79°56′30″W / 40.44333°N 79.94167°W / 40.44333; -79.94167 Scotch'n'Soda TheatreFounded1937Type501(c)(3)LocationCarnegie Mellon UniversityOriginsThe White Friars ClubArea served PittsburghKey peopleBoard Of DirectorsWebsiteScotch'n'Soda Theatre Scotch'n'Soda is a student-run theatre organization that resides on the campus of Carnegie Mellon University. Its initial dedication was the creation and production of original musicals, but has now taken to...

Not to be confused with China Movie Channel. State-owned film enterprise in the People's Republic of China China Film Group Corporation中国电影集团公司The studio of China Film Group in Yangsong, Huairou District, BeijingTypeState-owned enterpriseTraded asSSE: 600977IndustryCinemaFoundedFebruary 1999HeadquartersBeijing, ChinaKey peopleHan Sanping, Zhang QiangProductsfilmParentCentral Propaganda Department of the Chinese Communist PartySubsidiariesChina Film AnimationWebsitewww.zgdygf.c...

 

هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (نوفمبر 2023) في 1 مارس عام 1815، هرب نابليون بونابرت من سجنه في جزيرة إلبا وأطلق محاولة لاستعادة إمبراطوريته. تعهد اتحاد القوى الأوروبية بمنعه من ذلك. خلال الفترة المعروفة ...

 

American politician Robert SpellanePortrait of Robert SpellaneMember of the Massachusetts House of Representatives from the 13th Worcester DistrictIn office2001–2011Preceded byHarriette L. ChandlerSucceeded byJohn J. Mahoney Personal detailsBorn (1970-03-05) March 5, 1970 (age 53)Worcester, MassachusettsPolitical partyDemocraticResidence(s)Worcester, MassachusettsAlma materRoanoke CollegeOccupationPolitician Robert P. Spellane (born March 5, 1970 in Worcester, Massachusetts) is an Amer...

Arthur Foss Arthur Foss in her slip at the Historic Ships Wharf at Lake Union Park, March 2021. History United States Name Wallowa (1889–1934) Arthur Foss (1934–1942) Dohasan (1942–1945 Navy) Arthur Foss (1945–1964) Theodore Foss (1964–1970) Arthur Foss (1970–present) Owner Oregon Railway and Navigation Company (1889–1898) Pacific Clipper Line (1898–1904) Puget Sound Mill and Timber Company (1904–1929) Merrill & Ring Logging Company (1929) Foss Launch & Tug Company (...

 

Railway station in Richmond, Melbourne, Victoria, Australia West RichmondPTV commuter rail stationSouthbound view from Platform 2 in June 2018General informationLocationMuir Street,Richmond, Victoria 3121City of YarraAustraliaCoordinates37°48′54″S 144°59′29″E / 37.8150°S 144.9914°E / -37.8150; 144.9914Owned byVicTrackOperated byMetro TrainsLine(s) Mernda Hurstbridge Distance3.66 kilometres fromSouthern CrossPlatforms2 sideTracks2Connections BusConstructionS...

 

Raising and breeding insects as livestock Farming of crickets in Thailand. Insect farming is the practice of raising and breeding insects as livestock, also referred to as minilivestock or micro stock. Insects may be farmed for the commodities they produce (like silk, honey, lac or insect tea), or for them themselves; to be used as food, as feed, as a dye, and otherwise. Farming of popular insects Silkworms Silkworms, the caterpillars of the domestic silkmoth, are kept to produce silk, an ela...

Anonymous Old English poem based on the biblical Book of Daniel DanielThe first page of Daniel in MS Junius 11.Author(s)anonymousLanguageOld EnglishState of existencetwo poems, A and B; possibly unfinishedManuscript(s)Junius manuscriptGenrepoetryVerse formalliterativeLength764 linesSubjectbased on the Book of DanielPersonagesDaniel, Three Youths, Nebuchadnezzar II Daniel is an anonymous Old English poem based loosely on the Biblical Book of Daniel, found in the Junius Manuscript. The author a...

 

Weekly South Korean music chart by Billboard magazine South Korea Songs is a music record chart in South Korea compiled by Billboard since May 7, 2022, and is a replacement for the Billboard K-pop Hot 100 chart that was discontinued one week prior. The chart is updated every Tuesday on Billboard's website. It is part of Billboard's Hits of the World chart collection, ranking the top 25 songs weekly in more than 40 countries around the globe.[1] The first number-one song on the...

 

Deutsch-Tschechische Industrie- und Handelskammer(Česko-německá obchodní a průmyslová komora) (DTIHK / ČNOPK) Gründung 1993 Sitz Prag Geschäftsstelle Václavské náměstí 40, 110 00 Praha 1 Zweck Pflege und Erweiterung der deutsch-tschechischen Wirtschaftsbeziehungen Vorsitz Milan Šlachta (Präsident) Geschäftsführung Bernard Bauer (Geschäftsführendes Vorstandsmitglied) Personen Vorstand der DTIHK Mitglieder 660 (2022) Website tschechien.ahk.de Die Deutsch-Tschechische Industr...

此條目需要补充更多来源。 (2020年7月14日)请协助補充多方面可靠来源以改善这篇条目,无法查证的内容可能會因為异议提出而被移除。致使用者:请搜索一下条目的标题(来源搜索:中国人民抗日军事政治大学 — 网页、新闻、书籍、学术、图像),以检查网络上是否存在该主题的更多可靠来源(判定指引)。 中国人民抗日军事政治大学校训团结、紧张、严肃、活泼创办...

 

2010 Indian filmKanimozhiKanimozhi theatrical posterDirected bySripathy RangasamyWritten bySripathy RangasamyProduced by T. Siva Sona Heiden Starring Jai Shazahn Padamsee Vijay Vasanth CinematographyP. ChidamparamEdited by Praveen K. L. N. B. Srikanth Music bySatish ChakravarthyProductioncompanyAmma CreationsDistributed byUniq ProductionsRelease date 26 November 2010 (2010-11-26) Running time115 minutesCountryIndiaLanguageTamilBudget₹30 million (US$380,000)[1] Kanimoz...

 

For other uses, see Kamra (surname). Town in Punjab, PakistanKamra Kalan کامرہTownKamraTop: A lake located in Kamra Below: A road located in KamraNickname: City of AviationKamra KalanCoordinates: 33°51′N 72°24′E / 33.850°N 72.400°E / 33.850; 72.400CountryPakistanProvincePunjabDistrictAttockTehsilAttockElevation312 m (1,024 ft)Time zoneUTC+5 (PST)Websitehttp://www.cbkamra.gov.pk/en Kamra (Urdu: کامرہ) is a town located in Attock District...

This article includes a list of general references, but it lacks sufficient corresponding inline citations. Please help to improve this article by introducing more precise citations. (September 2017) (Learn how and when to remove this template message) Ignacy MatuszewskiMinister of Finance of PolandIn office14 April 1929 – 26 May 1931PresidentIgnacy MościckiPreceded byGabriel CzechowiczSucceeded byJan PiłsudskiConstituencyBezpartyjny Blok Współpracy z Rządem Personal detailsBo...

 

Railway station in Ichinomiya, Aichi Prefecture, Japan This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Kaimei Station – news · newspapers · books · scholar · JSTOR (November 2011) (Learn how and when to remove this template message) Kaimei Station開明駅Kaimei Station in June 2008General informationLocationNagora Kaimei Ich...

 

Status of the French language in Laos Bilingual Lao-French sign in front of government ministry building in Pakse. French is spoken by a significant minority in Laos. Laos has the second largest Francophone community in Southeast Asia after Vietnam and ahead of Cambodia. French is used as an administrative language and is also widely present in commerce, and is also studied by over a third of students in Laos.[1] Consequently, the language enjoys a healthier status in Laos compared to...

Mitski discographyMitski in 2013Studio albums7Music videos22EPs3Singles23 American recording artist Mitski has released seven studio albums, three extended plays, and 23 singles. Mitski's sixth studio album, Laurel Hell, proved to be her most successful album to date, peaking at number 5 on the US Billboard 200. She would follow up with her seventh studio album, The Land Is Inhospitable and So Are We, which included her most commercially successful single, My Love Mine All Mine. Studio albums...

 

Google Cardboard Шлем для Google Cardboard Описание Тип технология виртуальной реальности Разработчик Google Сайт Официальный сайт  Медиафайлы на Викискладе Google Cardboard (рус. «Картон») — стандарт компании Google в области виртуальной реальности, в основе которого лежит шлем, главной част...

 

Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!