Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. typedef struct str
  5. {
  6. int val;
  7. struct str* next;
  8. } str;
  9.  
  10. str* copy(str* el)
  11. {
  12. str* cpy = malloc(sizeof(str));
  13. *cpy = *el;
  14. return cpy;
  15. }
  16.  
  17. str* new_element(int v)
  18. {
  19. str* cpy = malloc(sizeof(str));
  20. cpy->val = v;
  21. return cpy;
  22. }
  23.  
  24. str* add(str* head, str* el)
  25. {
  26. if(head != NULL)
  27. {
  28. str *a, *b, *c;
  29. a = b = c = head;
  30. do{
  31. a = b;
  32. b = c;
  33. c = c->next;
  34. }while(c!=head);
  35. a->next = el;
  36. el->next = b;
  37. return head;
  38. }
  39. else
  40. {
  41. el->next = el;
  42. return el;
  43. }
  44. }
  45.  
  46. void main()
  47. {
  48. str head = {2, &head};
  49. str head2 = {5, NULL};
  50. str *list = &head;
  51. str *curr = list;
  52. char a; int i = 0;
  53. while(1)
  54. {
  55. a = getch();
  56. if(a == 'q')
  57. {
  58. printf("[%d] ", curr->val);
  59. curr = curr->next;
  60. }else
  61. {
  62. printf("\n");
  63. list = add(list, new_element(i));
  64. curr = list;
  65. }
  66. // printf("PRESSED '%c'", a);
  67. i++;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement