Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zamfiroiu committed Mar 29, 2018
1 parent 57d8e83 commit 5721fc3
Show file tree
Hide file tree
Showing 2 changed files with 388 additions and 0 deletions.
242 changes: 242 additions & 0 deletions 1051_seminar5.cpp
@@ -0,0 +1,242 @@
//#include<iostream>
//struct Book {
// char* title;
// int numberOfPages;
//};
//
//struct node {
// Book info;
// node* next;
//};
//
//node* insertAtBegin(node* head, Book book) {
// node* newNode = (node*)malloc(sizeof(node));
// newNode->info.title = (char*)malloc(sizeof(char)*(strlen(book.title) + 1));
// strcpy(newNode->info.title, book.title);
// newNode->info.numberOfPages = book.numberOfPages;
// newNode->next = head;
// return newNode;
//}
//
//Book createBook(const char* title, int numberOfPages) {
// Book book;
// book.title = (char*)malloc(sizeof(char)*(strlen(title) + 1));
// strcpy(book.title, title);
// book.numberOfPages = numberOfPages;
//
// return book;
//}
//
//node* insertAtEnd(node* head, Book book) {
// node* newNode = (node*)malloc(sizeof(node));
// newNode->info.title = (char*)malloc(sizeof(char)*(strlen(book.title) + 1));
// strcpy(newNode->info.title, book.title);
// newNode->info.numberOfPages = book.numberOfPages;
// newNode->next = NULL;
// if (head) {
// node*p = head;
// while (p->next) {
// p = p->next;
// }
// p->next = newNode;
// }
// else {
// head = newNode;
// }
// return head;
//}
//
//void displayBook(Book book) {
// printf("Book %s has %d pages\n", book.title, book.numberOfPages);
//}
//
//void displayList(node* head) {
// node* p = head;
// while (p) {
// displayBook(p->info);
// p = p->next;
// }
//}
//
//node* deleteList(node* head) {
// while (head) {
// free(head->info.title);
// node* temp = head;
// head = head->next;
// free(temp);
// }
// return head;
//}
//
//node* insertSorted(node* head, Book book) {
// if (head) {
// if (book.numberOfPages < head->info.numberOfPages) {
// head = insertAtBegin(head, book);
// }
// else {
// node*p = head;
// while (p->next && p->next->info.numberOfPages < book.numberOfPages) {
// p = p->next;
// }
// p->next = insertAtBegin(p->next, book);
// }
// }
// else {
// head = insertAtBegin(NULL, book);
// }
// return head;
//}
//
//void pushStack(node* *stack, Book info) {
// *stack = insertAtEnd(*stack, info);
//}
//
//Book popStack(node* *stack) {
// node* p = *stack;
// while (p && p->next && p->next->next) {
// p = p->next;
// }
// if (p && p->next) {
// Book book = createBook(p->next->info.title, p->next->info.numberOfPages);
// free(p->next->info.title);
// free(p->next);
// p->next = NULL;
// return book;
// }
// else {
// if (p) {
// Book book = createBook(p->info.title, p->info.numberOfPages);
// free(p->info.title);
// free(p);
// *stack = NULL;
// return book;
// }
// else {
// Book book;
// book.title = NULL;
// book.numberOfPages = -1;
// return book;
// }
// }
//}
//
//int isEmpty(node* stack) {
// return stack ? 0 : 1;
//}
//
//void pushQueue(node* * queue, Book info) {
// *queue = insertAtBegin(*queue, info);
//}
//
//Book popQueue(node** queue) {
// return popStack(queue);
//}
//
//node* filterBooksInAQueue(node* *stack, int limit) {
// node* resultQueue = NULL;
// node* tempStack = NULL;
// while (*stack) {
// Book book = popStack(stack);
// if (book.numberOfPages > limit) {
// pushStack(&tempStack,book);
// }
// else {
// pushQueue(&resultQueue, book);
// }
// free(book.title);
// }
//
// while (tempStack) {
// Book book = popStack(&tempStack);
// pushStack(stack, book);
// free(book.title);
// }
// return resultQueue;
//}
//
//node* filterBooks(node* stack, int limit) {
// node*tempStack = NULL;
// while (stack) {
// Book book = popStack(&stack);
// if (book.numberOfPages > limit) {
// pushStack(&tempStack, book);
// }
// else {
// displayBook(book);
// }
// free(book.title);
// }
// while (tempStack) {
// Book book = popStack(&tempStack);
// pushStack(&stack, book);
// free(book.title);
// }
// return stack;
//}
//
//
//
//void main() {
// node* stack = NULL;
//
// pushStack(&stack, createBook("Morometii", 400));
// pushStack(&stack, createBook("Ion", 200));
// pushStack(&stack, createBook("Moara cu noroc", 160));
// pushStack(&stack, createBook("Baltagul", 170));
// //
// ////memory leak
// //if (!isEmpty(stack)) {
// // displayBook(popStack(&stack));
// //}
//
// //while (stack) {
// // Book book = popStack(&stack);
// // displayBook(book);
// // free(book.title);
// //}
//
// //node* queue = NULL;
// //pushQueue(&queue, createBook("Morometii", 400));
// //pushQueue(&queue, createBook("Ion", 200));
// //pushQueue(&queue, createBook("Moara cu noroc", 160));
// //pushQueue(&queue, createBook("Baltagul", 170));
//
// //printf("Queue:\n");
// //while (queue) {
// // Book book = popQueue(&queue);
// // displayBook(book);
// // free(book.title);
// //}
//
// /*stack = filterBooks(stack, 200);
// printf("The result stack:\n");
// while (stack) {
// Book book = popStack(&stack);
// displayBook(book);
// free(book.title);
// }*/
//
// node* resultQueue = filterBooksInAQueue(&stack, 200);
// printf("Queue:\n");
// while (resultQueue) {
// Book book = popQueue(&resultQueue);
// displayBook(book);
// free(book.title);
// }
//
// printf("The result stack:\n");
// while (stack) {
// Book book = popStack(&stack);
// displayBook(book);
// free(book.title);
// }
//
// //node*p = (node*)malloc(sizeof(node));
// //free(p);
// //p = NULL;
//
// node** p;
// p = (node**)malloc(sizeof(node*));
// (*p) = NULL;
//
//}
146 changes: 146 additions & 0 deletions 1051_seminar6.cpp
@@ -0,0 +1,146 @@
#include<iostream>
struct Clothes {
int code;
char* color;
};

struct node {
Clothes info;
node*next;
};

struct HashTable {
node** vector;
int size;
};

Clothes createClothes(char* color, int code) {
Clothes c;
c.code = code;
c.color = (char*)malloc(sizeof(char)*(strlen(color) + 1));
strcpy(c.color, color);
return c;
}

void printClothes(Clothes c) {
printf("%d.The color is %s\n", c.code, c.color);
}

int getHashCode(int code, int size) {
return code%size;
}

node* createNode(Clothes info, node* next) {
node* newNode = (node*)malloc(sizeof(node));
newNode->info = createClothes(info.color, info.code);
newNode->next = next;
return newNode;
}

node* insertAtTheEnd(node* head, Clothes info) {
node* newNode = createNode(info, NULL);
if (head) {
node* p = head;
while (p->next) {
p = p->next;
}
p->next = newNode;
return head;
}
else {
return newNode;
}
}

HashTable initializeHashTable(int size) {
HashTable hTable;
hTable.vector = (node**)malloc(sizeof(node*)*size);
for (int i = 0; i < size; i++) {
hTable.vector[i] = NULL;
}
hTable.size = size;
return hTable;
}

int insertInTheHashTable(HashTable hTable, Clothes c) {
if (hTable.vector) {
int hashCode = getHashCode(c.code, hTable.size);
if (hTable.vector[hashCode]) {
//we have a collision;
hTable.vector[hashCode] = insertAtTheEnd(hTable.vector[hashCode], c);
return hashCode;
}
else {
hTable.vector[hashCode] = createNode(c, NULL);
return hashCode;
}
}
else {
return -1;
}
}

void printHashTable(HashTable ht) {
for (int i = 0; i < ht.size; i++) {
node*p = ht.vector[i];
while (p) {
printClothes(p->info);
p = p->next;
}
printf("\n");
}
}

Clothes findByCode(HashTable ht, int code) {
if (ht.vector) {
int position = getHashCode(code, ht.size);
node* p = ht.vector[position];
while (p && p->info.code != code) {
p = p->next;
}
if (p) {
return p->info;
}
}
Clothes c;
c.code = -1;
c.color = NULL;
return c;
}

void freeHashTable(HashTable * ht) {
for (int i = 0; i < ht->size; i++)
{
if (ht->vector[i]) {
node*p = ht->vector[i];
while (p) {
free(p->info.color);
node*aux = p;
p = p->next;
free(aux);
}
}
}
free(ht->vector);
ht->size = 0;
ht->vector = NULL;
}

void main() {
HashTable hTable;
hTable = initializeHashTable(5);
int position = insertInTheHashTable(hTable, createClothes("Red", 1));
insertInTheHashTable(hTable, createClothes("Green", 3));
insertInTheHashTable(hTable, createClothes("Blue", 6));

printHashTable(hTable);

Clothes c = findByCode(hTable, 3);

printClothes(c);

freeHashTable(&hTable);
//we don't need to delete it
//because we have a shallow copy on the return of findByCode()
//free(c.color);
}

0 comments on commit 5721fc3

Please sign in to comment.