try.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. ** Written by David Gerdes US Army Construction Engineering Research Lab
  3. ** April 1992
  4. ** Copyright 1992 USA-CERL All rights reserved.
  5. **
  6. */
  7. /*
  8. ** takes 1st command line argument and stuffs each letter of it into
  9. ** a linked list. then prints it back out to stdout.
  10. ** If a second argument is specified, the first argument is put in the
  11. ** list backwards.
  12. */
  13. #include <stdio.h>
  14. #include <grass/linkm.h>
  15. struct link
  16. {
  17. char let;
  18. struct link *next;
  19. };
  20. int main(int argc, char *argv[])
  21. {
  22. register int i;
  23. VOID_T *head;
  24. struct link List, *tmp, *p;
  25. int rev = 0;
  26. if (argc < 2)
  27. fprintf(stderr, "Usage: %s str [rev]\n", argv[0]), exit(1);
  28. if (argc > 2)
  29. rev = 1;
  30. List.next = NULL;
  31. List.let = ' ';
  32. head = (VOID_T *) link_init(sizeof(struct link));
  33. for (i = 0; argv[1][i]; i++) {
  34. tmp = (struct link *)link_new(head);
  35. tmp->let = argv[1][i];
  36. if (rev)
  37. add_link_rev(&List, tmp);
  38. else
  39. add_link(&List, tmp);
  40. }
  41. dumplist(&List);
  42. p = List.next;
  43. while (p->next != NULL) {
  44. tmp = p->next;
  45. link_dispose(head, p);
  46. p = tmp;
  47. }
  48. link_cleanup(head);
  49. exit(0);
  50. }
  51. int add_link_rev(struct link *List, struct link *link)
  52. {
  53. struct link *p;
  54. p = List->next;
  55. List->next = link;
  56. link->next = p;
  57. }
  58. int add_link(struct link *List, struct link *link)
  59. {
  60. struct link *p;
  61. p = List;
  62. while (p->next != NULL)
  63. p = p->next;
  64. p->next = link;
  65. link->next = NULL;
  66. }
  67. int dumplist(struct link *List)
  68. {
  69. struct link *p;
  70. p = List->next;
  71. while (p != NULL) {
  72. putchar(p->let);
  73. p = p->next;
  74. }
  75. putchar('\n');
  76. }