Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node{
- int data;
- struct Node* prev;
- struct Node* next;
- };
- struct Node* head;
- struct Node* newNode(int x){
- struct Node* new=(struct Node*)malloc(sizeof(struct Node));
- new->data=x;
- new->prev=NULL;
- new->next=NULL;
- return new;
- }
- void printList(){
- struct Node* temp=head;
- while(temp != NULL){
- printf("%d\t",temp->data);
- temp=temp->next;
- }
- printf("\n");
- }
- //dodavanje na pocetak
- void InsertAtHead(int x){
- struct Node* new= newNode(x);
- if(head==NULL)
- {
- head=new;
- return;
- }
- head->prev=new;
- new->next=head;
- head=new;
- }
- //dodavanje na kraj
- void InsertAtTail(int x){
- struct Node* temp=head;
- struct Node* new=newNode(x);
- if(head==NULL){
- head=new;
- return;
- }
- while(temp->next != NULL){
- temp=temp->next;//otisao na zadnji node
- }
- temp->next=new;
- new->prev=temp;
- }
- void deleteNodes(struct Node** head_ref){
- if(*head_ref==NULL)
- return;
- struct Node* temp=*head_ref;
- struct Node* next;
- while(temp!=NULL){
- next=temp->next;
- free(temp);
- temp=next;
- }
- *head_ref=NULL;
- }
- void deleteNodePos(struct Node** head_ref,int n){
- if(*head_ref==NULL)
- return;
- struct Node* current=*head_ref;
- int i;
- for(i=1;current!=NULL && i<n;i++){
- current=current->next;
- if(current == NULL)
- return;
- }
- if(current->next!=NULL)
- current->next->prev=current->prev;
- if(current->prev!=NULL)
- current->prev->next=current->next;
- free(current);
- }
- int main(){
- InsertAtHead(1);
- InsertAtTail(2);
- InsertAtTail(3);
- InsertAtTail(4);
- printList();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement