Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. /*TODO:
  6.  *  account for indexes greater than list size
  7.  */
  8.  
  9.  
  10. typedef struct linkedlist {
  11.     int data;
  12.     struct linkedlist *next;
  13. } LinkedList_t;
  14.  
  15. LinkedList_t *createLinkedList(int d)
  16. {
  17.     LinkedList_t *p = malloc(sizeof(LinkedList_t));
  18.     p->data = d;
  19.     p->next = NULL;
  20.  
  21.     return p;
  22. }
  23.  
  24. int push(LinkedList_t *h, int d)
  25. {
  26.     // Add to the top of the list
  27.     if(h == NULL)
  28.         return -1;
  29.  
  30.     LinkedList_t *k = h;   
  31.     while(k->next != NULL) {
  32.         k = k->next;
  33.     }
  34.     k->next = createLinkedList(d);
  35.     return 0;
  36. }
  37.  
  38. int pop(LinkedList_t *h)
  39. {
  40.     // Delete last entry
  41.     if(h == NULL)
  42.         return -1;
  43.  
  44.     if(h->next == NULL)
  45.         free(h);
  46.  
  47.     // get to second to last entry, then free
  48.     LinkedList_t *k = h;
  49.     while(k->next->next != NULL) {
  50.         k = k->next;
  51.     }
  52.     free(k->next);
  53.     k->next = NULL;
  54.     return 0;
  55. }
  56.  
  57. int search(LinkedList_t *h, int st)
  58. {
  59.     LinkedList_t *k = h;
  60.     int i = 0;
  61.     while(k->data != st) {
  62.         k = k->next;
  63.         i++;
  64.     }
  65.  
  66.     return i;
  67. }
  68.  
  69. int deleteByIndex(LinkedList_t *h, int index)
  70. {
  71.     LinkedList_t *k = h;
  72.     if(index == 0) {
  73.         k = k->next;
  74.         *h = *k;
  75.         return 0;
  76.     }
  77.  
  78.     for(int i = 0; i < index - 1; i++) {
  79.         k = k->next;
  80.     }
  81.  
  82.     free(k->next);
  83.  
  84.     k->next = k->next->next;
  85.     return 0;
  86. }
  87.  
  88. void setDataByIndex(LinkedList_t *h, int index, int s)
  89. {
  90.     LinkedList_t *k = h;
  91.     for(int i = 0; i < index; i++) {
  92.         k = k->next;
  93.     }
  94.  
  95.     k->data = s;
  96. }
  97.  
  98. int getDataByIndex(LinkedList_t *h, int index)
  99. {
  100.     LinkedList_t *k = h;
  101.     for(int i = 0; i < index; i++) {
  102.         k = k->next;
  103.     }
  104.  
  105.     return k->data;
  106. }
  107.  
  108. void printList(LinkedList_t *h)
  109. {
  110.     LinkedList_t *t = h;
  111.     while(t != NULL) {
  112.         printf("%d\n", t->data);
  113.         t = t->next;
  114.     }
  115. }
  116.  
  117. int main()
  118. {
  119.     LinkedList_t *head = createLinkedList(1);      
  120.     push(head, 2);
  121.     push(head, 3);
  122.     push(head, 4);
  123.  
  124.     printList(head);
  125.     puts("");
  126.     setDataByIndex(head, 1, 10);
  127.     printList(head);
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement