Last active
December 29, 2016 10:01
-
-
Save hygull/5a6e6e5a3f0d37f68c24baf7847832c2 to your computer and use it in GitHub Desktop.
To help the manager(from practice part of hackerearth) to list out the costs for food packages. created by hygull - https://repl.it/EyC3/1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Creation date : 29/12/2016. | |
| Problem's link : https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/tutorial/ | |
| Aim of program : To help the manager to list out the costs for food packages. | |
| Coded by : rishikesh Agrawani. | |
| */ | |
| package main | |
| import "fmt" | |
| //A structure that denotes the food package | |
| type FoodPackage struct { | |
| costOfPackage int | |
| next *FoodPackage | |
| } | |
| func CustomerQuery(pileTop *FoodPackage) (*FoodPackage, *FoodPackage) { //Type-1 Query | |
| if pileTop == nil { | |
| return nil, nil | |
| } | |
| topPackage := pileTop | |
| pileTop = pileTop.next | |
| topPackage.next = nil | |
| return pileTop, topPackage | |
| } | |
| func ChefQuery(pileTop *FoodPackage, cost int) *FoodPackage { //Type-2 Query | |
| newFoodPack := new(FoodPackage) | |
| newFoodPack.costOfPackage = cost | |
| if pileTop == nil { | |
| pileTop = newFoodPack //New food package added to pile of food packages | |
| } else { | |
| newFoodPack.next = pileTop | |
| pileTop = newFoodPack | |
| } | |
| return pileTop | |
| } | |
| func main() { | |
| var queries, queryType, cost int | |
| var pileTop, poppedFoodPack *FoodPackage | |
| var costsSlice []interface{} | |
| fmt.Scanf("%d", &queries) //No. of queries | |
| for i := 0; i < queries; i++ { | |
| fmt.Scanf("%d", &queryType) //1 or 2 | |
| if queryType == 1 { | |
| pileTop, poppedFoodPack = CustomerQuery(pileTop) | |
| if poppedFoodPack == nil { | |
| costsSlice = append(costsSlice, "No Food") //A message if no food package is available | |
| } else { | |
| costsSlice = append(costsSlice, poppedFoodPack.costOfPackage) | |
| } | |
| } else { | |
| fmt.Scanf("%d", &cost) | |
| pileTop = ChefQuery(pileTop, cost) | |
| } | |
| } | |
| for _, cost := range costsSlice { | |
| fmt.Println(cost) | |
| } | |
| } | |
| /*INPUT:- | |
| 6 | |
| 1 | |
| 2 5 | |
| 2 7 | |
| 2 9 | |
| 1 | |
| 1 | |
| */ | |
| /*OUTPUT:- | |
| No Food | |
| 9 | |
| 7 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment