Advertisement
Mukit1234

Untitled

Oct 21st, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node{
  4.  
  5. int data;
  6. struct Node *next;
  7. }*head;
  8.  
  9. void createlist(int n);
  10. void display();
  11. void middle();
  12.  
  13. int main()
  14. {
  15.     int n;
  16.     printf("Enter the number of nodes: ");
  17.     scanf("%d",&n);
  18.  
  19.     createlist(n);
  20.     printf("Data Entered in Linked list are:\n");
  21.     display();
  22.     middle();
  23.     return 0;
  24. }
  25. void createlist(int n)
  26. {
  27.     struct Node *p, *temp;
  28.     int data,i;
  29.     head= (struct Node*)malloc(sizeof(struct Node));
  30.     printf("Enter data for node 1: ");
  31.     scanf("%d",&data);
  32.     head->data=data;
  33.     head->next=NULL;
  34.     p=head;
  35.     temp=head;
  36.     for(i=2;i<=n;i++)
  37.     {
  38.         printf("Enter data for node %d: ",i);
  39.         scanf("%d",&data);
  40.         temp=(struct Node*)malloc(sizeof(struct Node));
  41.         temp->data=data;
  42.         temp->next=NULL;
  43.         p->next=temp;
  44.         p=p->next;
  45.     }
  46. }
  47. void display()
  48. {
  49.     struct Node* temp;
  50.     temp=head;
  51.     while(temp!=NULL)
  52.     {
  53.         printf("%d\n",temp->data);
  54.         temp=temp->next;
  55.     }
  56. }
  57. void middle()
  58. {
  59.     struct Node *p , *q;
  60.     p=head;
  61.     q=head;
  62.     while( q && q->next)
  63.     {
  64.         p=p->next;
  65.         q=q->next->next;
  66.     }
  67.     printf("Middle Element is : %d",p->data);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement