Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node ListNode;
- typedef struct FootballPlayer ListElement;
- #define MAX_LENGTH 50
- struct FootballPlayer {
- int number;
- // G - goalkeeper, D - defender, M - midfielder, F - forward
- char position;
- char name[MAX_LENGTH];
- };
- struct Node {
- ListElement element;
- ListNode *next;
- };
- void initialize_list (ListNode **head) {
- *head = NULL;
- }
- void add_element (ListNode **head, ListElement element_data) {
- ListNode *new_node;
- new_node = (ListNode *)malloc(sizeof(ListNode));
- new_node -> element = element_data;
- new_node -> next = NULL;
- if (*head == NULL) {
- *head = new_node;
- return;
- }
- ListNode *current;
- current = *head;
- while (current -> next != NULL) {
- current = current -> next;
- }
- current -> next = new_node;
- }
- // Brisanje je izvedeno po broju igraca - if uslov
- void delete_element (ListNode **head, int element_data_number) {
- ListNode *current;
- ListNode *previous;
- previous = *head;
- current = *head;
- while (current != NULL) {
- if (current -> element.number == element_data_number) {
- if (previous != current) {
- previous -> next = current -> next;
- }
- else {
- *head = current -> next;
- }
- free(current);
- return;
- }
- previous = current;
- current = current -> next;
- }
- }
- void print_list (ListNode **head) {
- ListNode *current;
- current = *head;
- int i = 0;
- while (current != NULL) {
- printf("Singly_linked_list[%d] == %d (%c) - %s \n", i, current -> element.number, current -> element.position, current -> element.name);
- ++i;
- current = current -> next;
- }
- printf("\n");
- }
- int main()
- {
- ListNode *head;
- initialize_list(&head);
- ListElement elem;
- elem.number = 4;
- elem.position = 'D';
- strcpy(elem.name, "Sergio Ramos");
- add_element(&head, elem);
- elem.number = 23;
- elem.position = 'M';
- strcpy(elem.name, "Isco Alarcon");
- add_element(&head, elem);
- elem.number = 1;
- elem.position = 'G';
- strcpy(elem.name, "Iker Casillas");
- add_element(&head, elem);
- print_list(&head);
- delete_element(&head, 1);
- print_list(&head);
- elem.number = 7;
- elem.position = 'F';
- strcpy(elem.name, "Cristiano Ronaldo");
- add_element(&head, elem);
- elem.number = 12;
- elem.position = 'D';
- strcpy(elem.name, "Marcelo");
- add_element(&head, elem);
- print_list(&head);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement