Advertisement
TAHMID37

linklist_c

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