Advertisement
Mukit1234

Untitled

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