Advertisement
Ruhan_DIU

Linked List

Dec 4th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node *link;
  7. };
  8.  
  9. struct node *head=NULL;
  10. struct node *tail=NULL;
  11.  
  12. main(){
  13. int x,i;
  14. scanf("%d",&x);
  15.  
  16. for(i=0;i<x;i++){
  17. int value;
  18. scanf("%d",&value);
  19.  
  20. struct node *NODE=(struct node*)malloc(sizeof(struct node));
  21.  
  22. NODE->data=value;
  23. NODE->link=NULL;
  24.  
  25. if(head==NULL){
  26. head=NODE;
  27. tail=NODE;
  28. }else{
  29. tail->link=NODE;
  30. tail=NODE;
  31. }
  32. }
  33.  
  34. struct node *pt=head;
  35. int count=0;
  36.  
  37. while(pt->link != NULL|| count==0){
  38. if(pt->link == NULL){
  39. printf("%d ",pt->data);
  40. count++;
  41. break;
  42. }else{
  43. printf("%d ",pt->data);
  44. pt=pt->link;
  45. }
  46.  
  47. }
  48.  
  49. struct node *ptr=head;
  50. struct node *temp;
  51. int k=0;
  52. while(ptr->link != NULL){
  53. k++;
  54. if(k==3){
  55. break;
  56. }else{
  57. temp=ptr;
  58. ptr=ptr->link;
  59. }
  60. }
  61.  
  62. temp->link=ptr->link;
  63. free(ptr);
  64.  
  65. // struct node *NODE=(struct node*)malloc(sizeof(struct node));
  66.  
  67. // NODE->data=100;
  68. // NODE->link=NULL;
  69.  
  70. // NODE->link=ptr->link;
  71. // ptr->link=NODE;
  72.  
  73. struct node *ptt=head;
  74. int countt=0;
  75. printf("\n");
  76. while(ptt->link != NULL|| countt==0){
  77. if(ptt->link == NULL){
  78. printf("%d ",ptt->data);
  79. count++;
  80. break;
  81. }else{
  82. printf("%d ",ptt->data);
  83. ptt=ptt->link;
  84. }
  85.  
  86. }
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement