Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // un element al listei
- typedef struct elem {
- int n; // informatia utila
- struct elem * next; // camp de inlantuire catre urmatorul element
- }
- elem;
- // aloca un nou element si ii seteaza campurile corespunzatoare
- elem * nou(int n, elem * next) {
- elem * e = (elem * ) malloc(sizeof(elem));
- if (!e) {
- printf("memorie insuficienta");
- exit(EXIT_FAILURE);
- }
- e -> n = n;
- e -> next = next;
- return e;
- }
- typedef struct {
- elem * first; // primul element din lista
- elem * last; // ultimul element din lista
- }
- List;
- void init(List * list) {
- list -> first = NULL;
- list -> last = NULL;
- }
- // adauga un element la inceputul listei
- void addStart(List * list, int n) {
- elem * first = list -> first;
- list -> first = nou(n, first);
- if (!first) { // lista initiala vida - trebuie setat si ultim pe elementul adaugat
- list -> last = list -> first;
- }
- }
- // adauga un element la sfarsitul listei
- void addEnd(List * list, int n) {
- elem * e = nou(n, NULL);
- if (list -> last) { // adaugare in lista nevida
- list -> last -> next = e;
- } else { // adaugare in lista vida
- list -> first = e;
- }
- list -> last = e;
- }
- void printList(List * list) {
- elem * crt;
- for (crt = list -> first; crt; crt = crt -> next) {
- printf("%d ", crt -> n);
- }
- putchar('\n');
- }
- // elibereaza memoria ocupata de o lista
- void freeList(List * list) {
- elem * p, * crt = list -> first;
- while (crt) {
- p = crt -> next;
- free(crt);
- crt = p;
- }
- }
- void sepElems(List *list, List *odd, List *even) {
- for (elem *it = list->first; it; it = it->next) {
- if (it->n & 1) {
- addEnd(odd, it->n);
- } else {
- addEnd(even, it->n);
- }
- }
- }
- int main() {
- List list, odd, even;
- init(&list);
- init(&odd);
- init(&even);
- int n;
- scanf("%d", &n);
- for (int x, i = 0; i < n; i++) {
- scanf("%d", &x);
- addEnd(&list, x);
- }
- sepElems(&list, &odd, &even);
- puts("odd:");
- printList(&odd);
- puts("even:");
- printList(&even);
- freeList(&list);
- freeList(&odd);
- freeList(&even);//*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement