Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct Map {
- uint id;
- char* value;
- struct Map *next;
- } map;
- map* head = NULL;
- void add_elem(uint key, char* str){
- if (head == NULL){
- map* new = (map*) malloc (sizeof(map));
- head = new;
- new->id = key;
- new->value = (char*) malloc (strlen(str) + 1);
- strcpy(new->value, str);
- new->next = NULL;
- return;
- }
- map *tmp = head, *h = head;
- head = head->next;
- while (head){
- tmp = head;
- head = head->next;
- }
- map* new = (map*) malloc (sizeof(map));
- tmp->next = new;
- head = new;
- new->id = key;
- new->value = (char*) malloc (strlen(str) + 1);
- strcpy(new->value, str);
- new->next = NULL;
- head = h;
- }
- void pop(uint id){
- map* h = head;
- if (head == NULL){
- printf("Error!\n");
- exit(1);
- }
- map* tmp = head;
- head = head->next;
- if (head == NULL){
- head = tmp;
- free(head);
- return;
- }
- if (tmp->id == id){
- free(tmp);
- return;
- }
- while (head && head->id != id){
- tmp = head;
- head = head->next;
- }
- tmp->next = head->next;
- free(head);
- head = h;
- return;
- }
- char* get_value(uint id){
- map* h = head;
- map* tmp = head;
- if (tmp->id == id){
- head = h;
- return tmp->value;
- }
- head = head->next;
- while (head){
- if (head->id == id){
- head = h;
- return tmp->next->value;
- }
- tmp = head;
- head = head->next;
- }
- printf("id incorrect!\n");
- head = h;
- return NULL;
- }
- void check_map(){
- map* h = head;
- while (head){
- printf("Key: %d, value: %s\n", head->id, head->value);
- head = head->next;
- }
- head = h;
- }
- int main(){
- add_elem(23, "Hello world for this dict");
- add_elem(12, "ultron is so-so");
- add_elem(34, "Avengers are good");
- add_elem(33, "vision isnt create yet");
- check_map();
- pop(12);
- printf("\n");
- check_map();
- pop(23);
- printf("\n");
- check_map();
- printf("\n");
- add_elem(123, "Work map?");
- add_elem(234, "seems to work!");
- check_map();
- printf("\n");
- printf("Value:::: %s\n", get_value(33));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement