Advertisement
DMG

Singly linked list in C with struct as element

DMG
Dec 17th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Node ListNode;
  5. typedef struct FootballPlayer ListElement;
  6.  
  7. #define MAX_LENGTH 50
  8.  
  9. struct FootballPlayer {
  10.        int number;
  11.        // G - goalkeeper, D - defender, M - midfielder, F - forward
  12.        char position;
  13.        char name[MAX_LENGTH];
  14. };
  15.  
  16. struct Node {
  17.        ListElement element;
  18.        ListNode *next;
  19. };
  20.  
  21. void initialize_list (ListNode **head) {
  22.      *head = NULL;
  23. }
  24.  
  25. void add_element (ListNode **head, ListElement element_data) {
  26.      ListNode *new_node;
  27.      new_node = (ListNode *)malloc(sizeof(ListNode));
  28.      new_node -> element = element_data;
  29.      new_node -> next = NULL;
  30.      
  31.      if (*head == NULL) {
  32.         *head = new_node;
  33.         return;
  34.      }
  35.      
  36.      ListNode *current;
  37.      current = *head;
  38.      
  39.      while (current -> next != NULL) {
  40.            current = current -> next;
  41.      }
  42.      
  43.      current -> next = new_node;
  44. }
  45.  
  46. // Brisanje je izvedeno po broju igraca - if uslov
  47. void delete_element (ListNode **head, int element_data_number) {
  48.      ListNode *current;
  49.      ListNode *previous;
  50.      
  51.      previous = *head;
  52.      current = *head;
  53.      
  54.      while (current != NULL) {
  55.            if (current -> element.number == element_data_number) {
  56.                    if (previous != current) {
  57.                        previous -> next = current -> next;
  58.                    }
  59.                    else {
  60.                        *head = current -> next;
  61.                    }
  62.                    
  63.                    free(current);
  64.                    return;
  65.            }
  66.            
  67.            previous = current;
  68.            current = current -> next;
  69.      }
  70. }
  71.  
  72. void print_list (ListNode **head) {
  73.      ListNode *current;
  74.      current = *head;
  75.      
  76.      int i = 0;
  77.      
  78.      while (current != NULL) {
  79.            printf("Singly_linked_list[%d] == %d (%c) - %s \n", i, current -> element.number, current -> element.position, current -> element.name);
  80.            ++i;
  81.            current = current -> next;
  82.      }
  83.      
  84.      printf("\n");
  85. }
  86.  
  87. int main()
  88. {
  89.     ListNode *head;
  90.     initialize_list(&head);
  91.    
  92.     ListElement elem;
  93.     elem.number = 4;
  94.     elem.position = 'D';
  95.     strcpy(elem.name, "Sergio Ramos");
  96.     add_element(&head, elem);
  97.    
  98.     elem.number = 23;
  99.     elem.position = 'M';
  100.     strcpy(elem.name, "Isco Alarcon");
  101.     add_element(&head, elem);
  102.    
  103.     elem.number = 1;
  104.     elem.position = 'G';
  105.     strcpy(elem.name, "Iker Casillas");
  106.     add_element(&head, elem);
  107.  
  108.     print_list(&head);
  109.  
  110.     delete_element(&head, 1);
  111.  
  112.     print_list(&head);
  113.    
  114.     elem.number = 7;
  115.     elem.position = 'F';
  116.     strcpy(elem.name, "Cristiano Ronaldo");
  117.     add_element(&head, elem);
  118.    
  119.     elem.number = 12;
  120.     elem.position = 'D';
  121.     strcpy(elem.name, "Marcelo");
  122.     add_element(&head, elem);
  123.    
  124.     print_list(&head);
  125.  
  126.     system("PAUSE");
  127.     return 0;  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement