Advertisement
Guest User

KVProjektni#2

a guest
Dec 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node{
  5.     int data;
  6.     struct Node* prev;
  7.     struct Node* next;
  8. };
  9.  
  10. struct Node* head;
  11.  
  12. struct Node* newNode(int x){
  13.     struct Node* new=(struct Node*)malloc(sizeof(struct Node));
  14.     new->data=x;
  15.     new->prev=NULL;
  16.     new->next=NULL;
  17.     return new;
  18. }
  19.  
  20.  
  21. void printList(){
  22.     struct Node* temp=head;
  23.     while(temp != NULL){
  24.         printf("%d\t",temp->data);
  25.         temp=temp->next;
  26.     }
  27.     printf("\n");
  28. }
  29.  
  30. //dodavanje na pocetak
  31.  
  32. void InsertAtHead(int x){
  33.     struct Node* new= newNode(x);
  34.     if(head==NULL)
  35.     {
  36.         head=new;
  37.         return;
  38.     }
  39.     head->prev=new;
  40.     new->next=head;
  41.     head=new;
  42. }
  43.  
  44. //dodavanje na kraj
  45.  
  46. void InsertAtTail(int x){
  47.  
  48.     struct Node* temp=head;
  49.     struct Node* new=newNode(x);
  50.     if(head==NULL){
  51.         head=new;
  52.         return;
  53.     }
  54.     while(temp->next != NULL){
  55.         temp=temp->next;//otisao na zadnji node
  56.     }
  57.     temp->next=new;
  58.     new->prev=temp;
  59. }
  60.  
  61.  
  62.  
  63. void deleteNodes(struct Node** head_ref){
  64.     if(*head_ref==NULL)
  65.         return;
  66.     struct Node* temp=*head_ref;
  67.     struct Node* next;
  68.     while(temp!=NULL){
  69.         next=temp->next;
  70.         free(temp);
  71.         temp=next;
  72.     }
  73.     *head_ref=NULL;
  74.  
  75. }
  76.  
  77.  
  78.  
  79. void deleteNodePos(struct Node** head_ref,int n){
  80.     if(*head_ref==NULL)
  81.         return;
  82.  
  83.     struct Node* current=*head_ref;
  84.     int i;
  85.     for(i=1;current!=NULL && i<n;i++){
  86.         current=current->next;
  87.  
  88.         if(current == NULL)
  89.             return;
  90.     }
  91.     if(current->next!=NULL)
  92.         current->next->prev=current->prev;
  93.  
  94.     if(current->prev!=NULL)
  95.         current->prev->next=current->next;
  96.     free(current);
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103. int main(){
  104.  
  105.     InsertAtHead(1);
  106.     InsertAtTail(2);
  107.     InsertAtTail(3);
  108.     InsertAtTail(4);
  109.     printList();
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement