SHARE
TWEET

Doble connected spisoc

allekco Oct 17th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stddef.h>
  7.  
  8. typedef struct node{
  9.     int value;
  10.     struct node *early;
  11.     struct node *next;
  12. } node;
  13.  
  14. typedef struct { //for easy keaping all list
  15.     int size;
  16.     node *head;
  17.     node *tail;
  18. }str_list;
  19.  
  20. void insert_first (str_list *list, int value){
  21.     node *tmp = (node*) malloc(sizeof(node));
  22.     tmp -> value = value;
  23.     tmp -> next = list -> head;
  24.     tmp -> early = NULL;
  25.     if (list -> head) { //if not zero element in list
  26.         list -> head -> early = tmp;
  27.     }  
  28.    
  29.     if (list -> tail == NULL) {
  30.        list -> tail = tmp;
  31.     }
  32.     list -> head = tmp;
  33.     list -> size ++;
  34. }
  35.  
  36. void inserting (str_list *list, int value, int key){
  37.     node *tmp = (node*) malloc(sizeof(node));
  38.     tmp -> value = value;
  39.     int i = 1;
  40.     node *iter = list -> head;
  41.  
  42.     while ((iter -> value != key) && (i < list -> size)){
  43.         iter = iter -> next;
  44.         i++;
  45.     }
  46.  
  47.     if (iter -> value != key){
  48.         printf ("-1\n");
  49.         printf ("\n");
  50.     }
  51.     else {
  52.         node *iter2 = iter;
  53.         tmp -> early = iter;
  54.         tmp -> next = iter -> next;
  55.         iter -> next = tmp;
  56.         if (iter2 -> next != NULL){
  57.             iter2 = iter2 -> next;
  58.             iter2 -> early = tmp;
  59.         }
  60.         else{
  61.             list -> tail = tmp;
  62.         }
  63.  
  64.         list -> size ++;
  65.     }
  66.  
  67. }
  68.  
  69. int delete_first (str_list *list){
  70.     if (list -> head != NULL){
  71.         node *tmp = list -> head;
  72.         list -> head = list -> head -> next;
  73.         if (list -> head) { //it means that's not last elm
  74.             list -> head -> early = NULL;
  75.         }
  76.         if (tmp == list -> tail) {
  77.             list -> tail = NULL;
  78.         }
  79.  
  80.         free (tmp);
  81.         list -> size --;
  82.         return 1;
  83.     }
  84.     else{
  85.         printf ("UNDERFLOW\n"); //u want delete frow empty stack
  86.         return -1;
  87.     }
  88. }
  89.  
  90.  
  91. int delet (str_list *list, int key){
  92.     if (list -> head != NULL){
  93.         node *tmp;
  94.         int i = 1;
  95.         node *iter = list -> head;
  96.  
  97.         while ((iter -> value != key) && (i < list -> size)){
  98.             iter = iter -> next;
  99.             i++;
  100.         }
  101.  
  102.         if (iter -> value != key){
  103.             return -1;
  104.         }
  105.         else {
  106.             if (iter == list -> head){
  107.                 int del;
  108.                 del = delete_first (list);
  109.                 return del;
  110.             }
  111.             else{
  112.                 tmp = iter;
  113.                 node *iter2 = iter -> next;
  114.                 iter = iter -> early;
  115.                 iter -> next = iter2;
  116.                 if (iter2 != NULL){
  117.                     iter2 -> early = iter;
  118.                 }
  119.                 else{
  120.                     list -> tail = iter;   
  121.                 }
  122.                 free (tmp);
  123.                 list -> size --;
  124.                 }
  125.                 return 1;
  126.             }
  127.     }
  128.     else{
  129.         printf ("UNDERFLOW\n"); //u want delete frow empty stack
  130.         return -1;
  131.     }
  132.  
  133. }  
  134.  
  135. void search_ (str_list *list, int key){
  136.     node *iter = list -> head;
  137.     int i = 1;
  138.     while ((iter -> value != key) && (i < list -> size)){
  139.         iter = iter -> next;
  140.         i++;
  141.     }
  142.     if (iter -> value != key){
  143.         printf ("not found\n\n");
  144.     }
  145.     else {
  146.         printf ("element found with index number: %d \n\n", i);
  147.     }
  148. }
  149.  
  150.  
  151. void output_ (str_list *list){
  152.     if (list -> size == 1)
  153.         printf ("1 element: ");
  154.     else
  155.         printf ("%d elemens: ", list -> size);
  156.     node *iter = list -> head;
  157.     if (iter != NULL){
  158.         while (iter != NULL){
  159.             printf("%d ", iter->value);
  160.             iter = iter -> next;
  161.         }
  162.     }
  163.     else{
  164.         printf ("empty stac");
  165.     }
  166.     printf("\n");
  167.     printf("\n");
  168. }
  169.  
  170. int main (void){
  171.  
  172.     str_list *list = (str_list*) malloc(sizeof(str_list));
  173.     list->size = 0;
  174.     list->head = list->tail = NULL;
  175.  
  176.     FILE *mf;
  177.     printf ("opening file :");
  178.     mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\input_double.dat","r+");
  179.     if (mf == NULL)
  180.         printf ("error\n");
  181.     else printf ("done\n");
  182.     char action [12];
  183.     char insert[] = "INSERT";
  184.     char delete[] = "DELETE";
  185.     char search[] = "SEARCH";
  186.     int value, key;
  187.  
  188.     while (1){
  189.         if(fscanf(mf, "%s %d %d", action, &key, &value)==EOF) break;
  190.         puts (action);
  191.  
  192.         int res = strcmp (action, insert);
  193.         if (res == 0){
  194.             printf ("%d %d\n", key, value);
  195.             if (key == 0){
  196.                 insert_first (list, value);
  197.                 output_(list);
  198.             }
  199.             else{
  200.                 inserting (list, value, key); //key is elem after which we insert      
  201.                 output_(list); 
  202.             }
  203.         }
  204.         res = strcmp (action, delete);
  205.         if (res == 0){
  206.             printf ("%d\n", key);
  207.             int del;   
  208.             del = delet (list, key);
  209.             printf ("result: %d\n", del);
  210.             output_(list);     
  211.         }
  212.  
  213.         res = strcmp (action, search);
  214.         if (res == 0){
  215.             printf ("%d\n", key);  
  216.             search_ (list, key);
  217.         }  
  218.     }
  219.  
  220.     fclose (mf);
  221.     printf ("file closed\n");
  222.     return 0;
  223. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top