fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NR_NOTE 10
  5.  
  6. struct Elem {
  7. char nota;
  8. struct Elem* next; // adresa elementului urmator
  9. };
  10.  
  11. typedef struct Elem Node;
  12.  
  13. void addAtBeginning(Node** head, char v) {
  14. Node *newNode = (Node*)malloc(sizeof(Node));
  15. newNode->nota = v;
  16. newNode->next = *head;
  17. *head = newNode;
  18. }
  19.  
  20. void addAtEnd(Node** head, char v) {
  21. Node *aux = *head;
  22. Node *newNode = (Node*)malloc(sizeof(Node));
  23. newNode->nota = v;
  24.  
  25. if (*head == NULL)
  26. addAtBeginning(&*head, v);
  27. else {
  28. while (aux->next != NULL)
  29. aux = aux->next;
  30. aux->next = newNode;
  31. newNode->next = NULL;
  32. }
  33. }
  34.  
  35. void print(Node* head) {
  36. while (head != NULL) {
  37. printf("%c ", head->nota);
  38. head = head->next;
  39. }
  40. }
  41.  
  42. Node* cautareSecventa(Node* head, char secventa[]) {
  43. Node *iter;
  44. iter = head;
  45. if (iter != NULL) { //parcurgere lista
  46. while (iter->next != NULL) {
  47. if (iter->nota == secventa[0] && iter->next->nota == secventa[1]) {
  48. Node *iter2 = iter->next->next; //parcurgere pentru gasirea ultimei note
  49. while (iter2->next != NULL) {
  50. if (iter2->nota == secventa[2]) {
  51. return iter;
  52. }
  53. iter2 = iter2->next;
  54. }
  55. }
  56. iter = iter->next;
  57. }
  58. }
  59. return NULL;
  60. }
  61.  
  62. void adaugareRefren(Node** head, Node* headRefren, char endRefren) {
  63. Node *iter;
  64. for (int i = 0; i < 2; i++) {
  65. iter = headRefren;
  66. if (iter != NULL) {
  67. while (iter->next != NULL) {
  68. addAtEnd(head, iter->nota);
  69. if (iter->nota == endRefren) {
  70. break;
  71. }
  72. iter = iter->next;
  73. }
  74. }
  75. }
  76. }
  77.  
  78. void eliberareMem(Node** head) {
  79. Node *headcopy;
  80. while (*head!=NULL) {
  81. headcopy = (*head)->next;
  82. free(*head);
  83. *head = headcopy;
  84. }
  85. *head=NULL;
  86. }
  87.  
  88. int main() {
  89.  
  90. //Subpunctul a
  91. char v[] = {'D', 'D', 'G', 'G', 'A', 'F', 'C', 'D', 'F', 'C'};
  92. int i;
  93. Node *CapLista = NULL;
  94. for (i = NR_NOTE - 1; i >= 0; i--) {
  95. addAtBeginning(&CapLista, v[i]);
  96. }
  97. print(CapLista);
  98. printf("\n");
  99.  
  100. //Subpunctul b
  101. char s[] = {'G','A','D'};
  102. Node *Refren = cautareSecventa(CapLista, s);
  103. if (Refren != NULL) {
  104. //Subpunctul c
  105. adaugareRefren(&CapLista, Refren, s[2]);
  106. print(CapLista);
  107. printf("\n");
  108. } else printf("Nu exista secventa \n");
  109.  
  110. eliberareMem(&CapLista);
  111. return 0;
  112. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
D D G G A F C D F C 
D D G G A F C D F C G A F C D G A F C D