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;
- }
- }
- int main() {
- freopen("data.txt", "r", stdin);
- List odd, even;
- init(&odd);
- init(&even);
- do {
- int x;
- if (scanf("%d", &x) != 1) {
- break;;
- }
- if (x & 1) {
- addEnd(&odd, x);
- } else {
- addEnd(&even, x);
- }
- //break;
- } while (!feof(stdin));
- puts("odd:");
- printList(&odd);
- puts("even:");
- printList(&even);
- freeList(&odd);
- freeList(&even);//*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement