Advertisement
TAHMID37

linklist_b

Jul 8th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1.  
  2. /*  TAHMID RAHMAN
  3.     DAMIAN FOREVER
  4.      MATH LOVER
  5.     NEVER GIVE UP
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. #define pi acos(-1.0)
  10. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  11. #define ll long long
  12. #define pb push_back
  13. #define fi first
  14. #define se second
  15. #define in insert
  16. #define mp make_pair
  17. #define GCD(a,b) __gcd(a,b);
  18. #define endl "\n"
  19. #define FRU freopen("out.txt","w",stdout)
  20. #define FRO freopen("in.txt","r",stdin)
  21. #define INFLL 9223372036854775807
  22. #define debug 0
  23. #define MAXN  100001
  24. #define ar array
  25. const int mxN=2e5;
  26. const int MOD=1e9+7;
  27. bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
  28. //Don't hesitate to ask me if you don't understand my code.......Happy coding,Tahmid...;
  29. struct node{
  30.    int data;
  31.    struct node *next;
  32.  
  33. };
  34.  
  35. struct node* head=NULL;
  36. struct node* cur=NULL;
  37.  
  38. void in_sert(int n,int data)
  39. {
  40.    struct node* temp=(struct node*)malloc(sizeof(struct node));
  41.    temp->data=data;
  42.    temp->next=NULL;
  43.    if(n==1)
  44.    {
  45.       // temp->next=head;
  46.        head=temp;
  47.        return;
  48.    }
  49.    struct node* temp1=head;
  50.    for(int i=0;i<n-2;i++)
  51.    {
  52.        temp1=temp1->next;
  53.    }
  54.    temp->next=temp1->next;
  55.    temp1->next=temp;
  56. }
  57.  
  58. void dele_te(int n)
  59. {
  60.   struct node* tempx=head;
  61.    if(n==1)
  62.    {
  63.        head=tempx->next;
  64.        free(tempx);
  65.        return ;
  66.    }
  67.    int i;
  68.    for(i=0;i<n-2;i++)
  69.    {
  70.       tempx=tempx->next;
  71.    }
  72.    struct node* tempxx=tempx->next;
  73.    tempx->next=tempxx->next;
  74.    free(tempxx);
  75.  
  76. }
  77.  
  78. int length()
  79. {
  80.     ll len=0;
  81.     struct node*p=head;
  82.     while(p!=NULL)
  83.         {
  84.             len++;
  85.             p=p->next;
  86.         }
  87.     return len;
  88. }
  89.  
  90. void print()
  91. {
  92.     struct node* ptr=head;
  93.     while(ptr!=NULL)
  94.     {
  95.         printf("%d ",ptr->data);
  96.         ptr=ptr->next;
  97.     }
  98. }
  99. bool isempty()
  100. {
  101.     return (head==NULL);
  102. }
  103.  
  104. int main()
  105. {
  106.  
  107.     ll t;
  108.     cin>>t;
  109.     while(t--)
  110.     {
  111.         string s,p="P",d="D";
  112.         int n,m;
  113.         cin>>s;
  114.         if(s==p)
  115.         {
  116.                  if(isempty()==1)
  117.                   cout<<"The linked list is empty."<<endl;
  118.                  else
  119.                    print();
  120.               cout<<endl;
  121.  
  122.         }
  123.         else if(s==d)
  124.         {
  125.             cin>>n;
  126.             ll x=length();
  127.  
  128.             if(x<n)
  129.             {
  130.                 cout<<"There is no such node."<<endl;
  131.             }
  132.             else if(x==0)
  133.                 cout<<"There is no such node."<<endl;
  134.             else
  135.             dele_te(n);
  136.         }
  137.         else
  138.         {
  139.             cin>>n>>m;
  140.            ll  xx=length();
  141.             if(xx+1<n)
  142.               cout<<"There is no such node."<<endl;
  143.             else
  144.             in_sert(n,m);
  145.         }
  146.     }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement