Advertisement
Guest User

Untitled

a guest
May 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.     int value;
  6.     struct node *prev;
  7.     struct node *next;
  8. }node;
  9.  
  10. node *head=NULL;
  11. node *last=NULL;
  12. node *current=NULL;
  13.  
  14. int empty(){
  15.     if(head==NULL)return 1;
  16.     return 0;
  17. }
  18. int size(){
  19.     int licznik=0;
  20.     current=head;
  21.     while(current!=NULL){
  22.         licznik++;
  23.         current=current->next;
  24.     }
  25.     return licznik;
  26. }
  27. void clear(){ //POPRAWIC NA GLOBALNA!!!!!!!!
  28.  
  29.     while(head!=NULL){
  30.         node *tmp=head;
  31.         head=head->next;
  32.         free(tmp);
  33.     }
  34.     last=head;
  35. }
  36.  
  37. void push_front(int t)
  38. {
  39.     node *tmp=NULL;
  40.     tmp=malloc(sizeof(node));
  41.     if(head==NULL && last == NULL)
  42.     {
  43.         head=tmp;
  44.         last=tmp;
  45.         tmp->value=t;
  46.         tmp->next=NULL;
  47.         tmp->prev=NULL;
  48.     }
  49.     else {
  50.         tmp->value = t;
  51.         tmp->next = head;
  52.         tmp->prev = NULL;
  53.         head->prev = tmp;
  54.         head = tmp;
  55.     }
  56. }
  57.  
  58. void push_back(int t)// nie dziala
  59. {
  60.     node *tmp=NULL;
  61.     tmp=malloc(sizeof(node));
  62.     tmp->value=t;
  63.     if(head==NULL && last == NULL)
  64.     {
  65.         head=tmp;
  66.         last=tmp;
  67.         tmp->next=NULL;
  68.         tmp->prev=NULL;
  69.     }
  70.     else {
  71.         tmp->next = NULL;
  72.         tmp->prev = last;
  73.         last->next = tmp;
  74.         last = tmp;
  75.     }
  76. }
  77. void remove1(int t)
  78. {
  79.     node *guardian=malloc(sizeof(node));
  80.     guardian->next=head;
  81.     node *p=guardian;
  82.     node *q=head;
  83.     while(q!=NULL)
  84.     {
  85.         if(q->value==t)
  86.         {
  87.             p->next=q->next;
  88.             q->next->prev=p;
  89.             free(q);
  90.             q=p->next;
  91.         }
  92.         else{
  93.             p=q;
  94.             q=q->next;
  95.         }
  96.     }
  97.     head=guardian->next;
  98.     p=guardian;
  99.     while(p->next!=NULL)p=p->next;
  100.     if(p==guardian)last=NULL;
  101.     else last=p;
  102. }
  103. int front(){
  104.     if(head==NULL)return -1;
  105.     return head->value;
  106. }
  107. int back()
  108. {
  109.     if(last==NULL)return -1;
  110.     return last->value;
  111. }
  112. void pop_front()
  113. {
  114.     node *tmp=head;
  115.     if(head==NULL) return;
  116.     head=head->next;
  117.     free(tmp);
  118. }
  119. void pop_back()
  120. {
  121.     node *tmp=last;
  122.     if(last==NULL)return;
  123.     last=last->prev;
  124.     free(tmp);
  125. }
  126. void print()
  127. {
  128.     current=head;
  129.     while(current!=NULL)
  130.     {
  131.         printf("%d ",current->value);
  132.         current->next;
  133.     }
  134. }
  135. int main() {
  136.     printf("Hello, World!\n");
  137.     for(int i=1;i<5;i++)
  138.     {
  139.         push_front(i);
  140.     }
  141.     push_back(6);
  142.     remove1(4);
  143.     while(head!=NULL)
  144.     {
  145.         printf("%d ",head->value);
  146.         head=head->next;
  147.     }
  148.  
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement