Kolorwanie:
Status:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Item {
	float price;
	int size;
	char *name;
	char *brandName;
} Item;

typedef struct Node {
	Item *item;
	struct Node *next;
	struct Node *prev;
} Node;

typedef struct List {
	Node *first;
	Node *last;
} List;

Item *createItem(float price, int size, char *name, char *brandName) {
	Item *item = (Item *) malloc(sizeof(Item));
	item->price = price;
	item->size = size;
	item->name = name;
	item->brandName = brandName;
	return item;
}

/*
void addToStart(List *list, Item *item) {
	Node *new = malloc(sizeof(Node));
	new->data = data;
	new->prev = NULL;
	list->first->prev = new;
	new->next = list->first;
	list->first = new;
}
*/

void addToEnd(List *list, Item *item) {
	Node *new = malloc(sizeof(Node));
	new->item = item;
	new->next = NULL;
	list->last->next = new;
	new->prev = list->last;
	list->last = new;
}


void addAtIndex(List *list, Item *item, unsigned index) {
	Node *current = list->first;
	int i=0;
	if(!current) return;
	while(current && i<index) {
		if(i == index-1) {
			Node *new = (Node *) malloc(sizeof(Node));
			new->item = item;
			new->prev = current->prev;
			current->prev->next = new;
			current->prev = new;
			new->next = current;
			break;
		} else if(current == list->last) {
			addToEnd(list, item);
			break;
		} else {
			current = current->next;
			i++;
		}
	}
}


Item *getByName(List *list, char *name) {
	Node *current= list ->first;
	if(!current) return NULL;
	while(current) {
	if(strcmp(current->item->name, name)==0){
		return current->item;
	} else {
		current=current->next;
	}
	}
	return NULL;
}

void printItem(Item *item) {
	printf("%f, %d, %s, %s\n", item->price, item->size, item->name, item->brandName);	
}

void printList(List *list) {
	Node *current = list->first;
	if(!current) return;
	while (current) {	
		printItem(current->item);
		current = current->next;
	}

}

void removeElement(List *list, char *name) {
	Node *current = list->first;
	Node *temp = current;
	while (current) {
		if (strcmp(current->item->name, name)==0) {
			if (current == list->first) {
				current->next->prev = NULL;
				list->first = current->next;
				free(current);

			} else if (current == list->last) {
				current->prev->next = NULL;
				list->last = current->prev;
				free(current);

			} else {
				current->prev->next = current->next;
				current->next->prev = current->prev;
				free(current);

			}
			return;
		} else {
			current = current->next;
		}
	}
}
int equals(Item *item1, Item *item2) {
        return \
                item1->price == item2->price && \
                item1->size == item2->size && \
                strcmp(item1->name, item2->name) && \
                strcmp(item1->brandName, item2->brandName);
}
Item *getFromUser(){
	float f;
	int g; 
	b=malloc(sizeof(char));
	m=malloc(sizeof(char));
	char b[255];
	char m[255];

	printf("podaj cene ;");

	scanf("%f" , &f);
	printf("podaj rozmiar;");

	scanf("%d", &g);
	printf("podaj nazwe");
	scanf("%s", b);
	printf("podaj marke");
	scanf("%s", m);
	return createItem(f,g,b,m);
}
 void saveToFile (List *list,**first){
	 FILE *fp;
	 if ((fp=fopen("plik.txt", "w"))!=NULL)   
    {
        printf("Utworzono plik tekstowy\n");

    
        Node *current=*first;
        while (current!=NULL)
        {
            fprintf(ft, "%s\t%s\n", current->price, current->size , current -> name , current->brandName);
            current=current->next;
        }

        fclose(fp);

    }
    else
    {
        printf("Plik nie utworzono pliku tekstowego");
		}
 }
 
	


int main(void) {
	//to mozna wpakowac do funkcji ktore dodaja do listy
	List *list = malloc(sizeof(List));
	list->first = malloc(sizeof(Node));
	list->last = list->first;
	list->first->item = createItem(1.0, 1, "test", "test");

	addToEnd(list, createItem(2.0, 2, "test2", "test2"));
	addToEnd(list, createItem(2.0, 3, "test2", "test3"));
	addToEnd(list, createItem(2.0, 6, "test2", "test4"));
	addAtIndex(list, createItem(10.0, 5, "dshgoids2", "test5"), 2);
	printList(list);


	addToEnd(list, getFromUser());

	printf("\n");
	printList(list);
	return 0;
}


Copyrights 2014-2020 © Wklejaj.pl Wszelkie prawa zastrzeżone.