doubly linked list

 avatar
unknown
actionscript
2 years ago
2.2 kB
6
Indexable
/* Online C Compiler and Editor */
#include <stdio.h>
#include<stdlib.h>

void linked_list();
void delete_at_first();

void display();
     struct NODE{
         int data;
         struct NODE *pre;
         struct NODE *next;
     };
     typedef struct NODE s;
     s *head=NULL,*temp,*start,*ptr;
    int c,n=1,choice,e=1,i;
  
int main()
{
    while(n!=2){
      printf("for insert or delete a linked list \n 1.insert \n 2.delete\n 3.display\n 4.exit\n");
      scanf("%d",&c);
      
      switch(c){
          case 1:
          linked_list();
          break;
          
          case 2:
           delete_at_first();
          break;
          
          case 3:
          display();
          break;
          
          case 4:
          n=2;
          break;
          
         
          default :
          printf("enter key is wrong\n");
          break;
         }
    }
   
    
    return 0;
}
linked_list(){
    start=(s*)malloc(sizeof(s));
    printf("enter your element\n");
    scanf("%d",&start->data);
    start->next=NULL;
    start->pre=NULL;
    if(head==NULL){
        head=temp=start;
        
    }
    else{
        temp->next=start;
         start->pre=temp;
        temp=start;
        
        
        
    }
    
     ptr=head;
    printf("your linked list is-\n");
    while(ptr->next!=NULL){
        ptr=ptr->next;
    }
   while(ptr!=NULL){
       printf("%d\n",ptr->data);
       ptr=ptr->pre;
   }
}

void delete_at_first(){
    if(head==NULL){
        printf("linked list is empty\n");
    }
    else if (head->next==NULL){
        temp=head;
        head=NULL;
    }
    else{
    head=head->next;
    head->pre=NULL;
    printf("one element is deleted\n");
}
ptr=head;
    printf("your linked list is-\n");
    while(ptr->next!=NULL){
        ptr=ptr->next;
    }
   while(ptr!=NULL){
       printf("%d\n",ptr->data);
       ptr=ptr->pre;
   }

}

void display(){
    temp=head;
    printf("your linked list is-\n");
    while(temp!=NULL){
        printf("%d\n",temp->data);
        temp=temp->next;
    }
}
Editor is loading...