You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * linkedlist.c
  3. *
  4. * Created by Yigit Colakoglu on 07/06/2021.
  5. * Copyright yigit@yigitcolakoglu.com. 2021. All rights reserved.
  6. */
  7. #include "linkedlist.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #define RANDLEN 6
  12. LinkedList *linkedlistalloc(void){
  13. return (LinkedList *) malloc(sizeof(LinkedList));
  14. }
  15. int linkedlistfind(LinkedList *p, char *str) {
  16. int count = 0;
  17. while(p != NULL){
  18. if(!strcmp(p->data, str))
  19. return count;
  20. count++;
  21. p = p->next;
  22. }
  23. return -1;
  24. }
  25. LinkedList *linkedlistadd(LinkedList *p, char *data){
  26. if(p == NULL){
  27. p = linkedlistalloc();
  28. p->next = NULL;
  29. p->data = data;
  30. }else
  31. p->next = linkedlistadd(p->next, data);
  32. return p;
  33. }
  34. char rstr[RANDLEN+1];
  35. char *randstr(){
  36. char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  37. int n = RANDLEN;
  38. while((--n) > -1){
  39. size_t index = (double) rand()/RAND_MAX * (sizeof charset - 1);
  40. rstr[n] = charset[index];
  41. }
  42. return rstr;
  43. }
  44. void linkedlistprint(LinkedList *p, FILE *out, char* payload){
  45. int random = 0;
  46. if(!payload){
  47. random = 1;
  48. payload = randstr();
  49. }
  50. if(p != NULL){
  51. (p->data == NULL) ? fprintf(out, "NULL=NULL") : fprintf(out, "%s=%s", p->data, payload);
  52. (p->next == NULL) ? : fprintf(out, "%c",'&');
  53. if(random)
  54. payload = NULL;
  55. linkedlistprint(p->next, out, payload);
  56. }
  57. }