upt-pt-labs/lab12/3.c

94 lines
1.9 KiB
C
Raw Permalink Normal View History

2016-05-20 06:29:02 +00:00
#include <stdio.h>
#include <stdlib.h>
/* Single Linked list related code */
typedef struct _selist * Slist;
struct _selist {
int el;
Slist next;
};
int head(Slist lst) {
return lst->el;
}
Slist tail(Slist lst) {
return lst->next;
}
2016-05-20 06:40:09 +00:00
Slist init() {
Slist dummy = malloc(sizeof(struct _selist));
dummy->next = dummy;
return dummy;
}
void insert(int element, Slist current) {
2016-05-20 06:29:02 +00:00
Slist head = current;
2016-05-20 06:40:09 +00:00
// chestia asta merge pana la ultimul element, si ramane acolo.
// daca-i head n-are treaba.
2016-05-20 06:29:02 +00:00
while(current->next != head) {
current = current->next;
}
2016-05-20 06:40:09 +00:00
// creaza un nod nou, la next si schimba adresa nodului curent pe next.
2016-05-20 06:29:02 +00:00
current->next = malloc(sizeof(struct _selist));
2016-05-20 06:40:09 +00:00
// se muta pe nod-ul creat
2016-05-20 06:29:02 +00:00
current = current->next;
2016-05-20 06:40:09 +00:00
// baga elementu si face ca nodu sa pointeze spre head.
2016-05-20 06:29:02 +00:00
current->el = element;
current->next = head;
}
void print_list(Slist list) {
printf("SLIST PRINT\n");
Slist head = list;
list = list->next;
while (list != head) {
printf("%d ", list->el);
list = list->next;
}
printf("\n");
}
size_t size(Slist list) {
size_t elements = 0;
Slist head = list;
list = list->next;
while (list != head) {
elements += 1;
list = list->next;
}
return elements;
}
void delete_middle(Slist list) {
unsigned long middle = size(list) / 2;
Slist prev = list;
list = list->next;
for (int i = 0; i <= middle; i++) {
if (i + 1 == middle) {
prev = list;
}
if (i == middle) {
prev->next = list->next;
free(list);
}
list = list->next;
}
}
int main(void) {
2016-05-20 06:40:09 +00:00
Slist head = init();
2016-05-20 06:29:02 +00:00
insert(1, head);
insert(2, head);
insert(3, head);
insert(4, head);
insert(5, head);
2016-05-20 06:40:09 +00:00
insert(6, head);
2016-05-20 06:29:02 +00:00
delete_middle(head);
print_list(head);
return 0;
}