Advertisement
Mukit1234

Untitled

Oct 26th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5.     int data;
  6.     struct Node *prev;
  7.     struct Node *next;
  8. }*head,*p;
  9. void createlist(int n);
  10. void displayAccending();
  11. void Delete();
  12. int main()
  13. {
  14.     int n;
  15.     printf("Enter the number of nodes: ");
  16.     scanf("%d",&n);
  17.     createlist(n);
  18.     printf("Data in the list From 1st to last:\n");
  19.     displayAccending();
  20.     Delete();
  21.     printf("Data in the list From 1st to last:\n");
  22.     displayAccending();
  23.     return 0;
  24.  
  25. }
  26. void createlist(int n)
  27. {
  28.     struct Node *temp;
  29.     head=NULL;
  30.     p=NULL;
  31.     int data, i;
  32.     head= (struct Node*)malloc(sizeof(struct Node));
  33.     printf("Input Data for node 1 : ");
  34.     scanf("%d",&data);
  35.     head->data=data;
  36.     head->prev=NULL;
  37.     head->next=NULL;
  38.     p=head;
  39.     for(i=2; i<=n; i++)
  40.     {
  41.         temp= (struct Node*)malloc(sizeof(struct Node));
  42.         printf("Input data for node no %d:",i);
  43.         scanf("%d",&data);
  44.         temp->data=data;
  45.         temp->prev=p;
  46.         temp->next=NULL;
  47.         p->next=temp;
  48.         p=p->next;
  49.  
  50.     }
  51.  
  52. }
  53. void displayAccending()
  54. {
  55.     struct Node *temp;
  56.     temp=head;
  57.     while(temp!=NULL)
  58.     {
  59.         printf("%d\n",temp->data);
  60.         temp=temp->next;
  61.     }
  62. }
  63. void Delete()
  64. {
  65.     struct Node *temp1,*temp2;
  66.     int pos,i;
  67.     temp1=head;
  68.     printf("Enter node number for Delete: ");
  69.     scanf("%d",&pos);
  70.     if (pos==1)
  71.     {
  72.         head= temp1->next;
  73.         temp1->next->prev= temp1->prev;
  74.         free(temp1);
  75.         return ;
  76.     }
  77.     for (i=1; i<pos-1; i++)
  78.         temp1=temp1->next;
  79.     temp2=temp1->next;
  80.     temp1->next=temp2->next;
  81.  
  82.  
  83.     free(temp2);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement