mihainan

Nan Mihai - Structuri de date (pt Adriana)

Apr 8th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. typedef struct linie{
  2.     struct linie *prev;
  3.     char data;
  4.     struct linie *next;
  5. }linie;
  6.  
  7. typedef linie* Linie;
  8.  
  9. typedef struct text{
  10.     struct text *prev;
  11.     Linie data;
  12.     struct text *next;
  13. }text;
  14.  
  15. typedef text* Text;
  16.  
  17. typedef struct _backup{
  18.     Text* data;
  19.     int capacity;
  20.     int size;
  21. }* Backup;
  22.  
  23. Backup initBackup()
  24. {
  25.     Backup nou;
  26.     nou = (Backup) malloc(sizeof(struct _backup));
  27.     nou->capacity = 10;
  28.     nou->data = (Text *) malloc(nou->capacity*sizeof(Text));
  29.     nou->size = 0;
  30.     return nou;
  31. }
  32.  
  33. void ensureCapacity(Backup s, int minCapacity)
  34. {
  35.     if(s->capacity < minCapacity)
  36.     {
  37.         s->data = (Text*) realloc(s->data, minCapacity*sizeof(Text));
  38.         s->capacity = minCapacity;
  39.     }
  40. }
  41.  
  42.  
  43.  
  44. Text top(Backup s)
  45. {
  46.     if(s->size == 0)
  47.     {
  48.         return NULL;
  49.     }
  50.     else
  51.     {
  52.         return s->data[s->size-1];
  53.     }
  54. }
  55.  
  56. /** 7. Intoarce elementul din varful stivei si il elimina din stiva. Intoarce 0 daca stiva e goala */
  57. Text pop(Backup s)
  58. {
  59.     Text c;
  60.     if(s->size == 0)
  61.     {
  62.         return NULL;
  63.     }
  64.     else
  65.     {
  66.         c = s->data[s->size-1];
  67.         s->size--;
  68.         return c;
  69.     }
  70.    
  71. }
  72.  
  73. void push(Backup s, Text value)
  74. {
  75.     Text nou, tmp;
  76.     tmp = value;
  77.     if(tmp != NULL)
  78.     {
  79.         nou = initText(tmp->data);
  80.         while(tmp->next != NULL)
  81.         {
  82.             tmp = tmp->next;
  83.             nou = addLast2(nou, tmp->data);
  84.         }
  85.         if(s->size < s->capacity)
  86.         {
  87.             s->data[s->size] = nou;
  88.             s->size++;
  89.         }
  90.         else
  91.         {
  92.             ensureCapacity(s, 2*s->size);
  93.             s->data[s->size] = nou;
  94.             s->size++;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment