Guest User

Untitled

a guest
Nov 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import "stdio.h";
  2. import "stdlib.h";
  3.  
  4. type SList struct {
  5. value int;
  6. next *SList;
  7. };
  8.  
  9. func SListNode(value int) *SList
  10. {
  11. var n *SList = stdlib.malloc(sizeof(type SList));
  12. n.value = value;
  13. n.next = nil;
  14. return n;
  15. }
  16.  
  17. func SListPush(head **SList, value int)
  18. {
  19. if *head == nil {
  20. *head = SListNode(value);
  21. return;
  22. }
  23.  
  24. n := SListNode(value);
  25. n.next = *head;
  26. *head = n;
  27. }
  28.  
  29. func SListPop(head **SList) (value int)
  30. {
  31. if *head != nil {
  32. cur := *head;
  33. value = cur.value;
  34. *head = cur.next;
  35. stdlib.free(cur);
  36. }
  37. }
  38.  
  39. func main(argc int, argv **byte) int
  40. {
  41. var list *SList;
  42. for i := 0; i < 5; i++ {
  43. SListPush(&list, i*10);
  44. }
  45. for i := 0; i < 5; i++ {
  46. v := SListPop(&list);
  47. stdio.printf("%d\n", v);
  48. }
  49. return 0;
  50. }
Add Comment
Please, Sign In to add comment