cicular linked list

 avatar
unknown
actionscript
2 years ago
1.9 kB
8
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 *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);
    if(head==NULL){
        head=temp=start;
        start->next=NULL;
    }
    else{
        temp->next=start;
        temp=start;
        start->next=head;
        
        
        
        
    }
    
}

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


}

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