SHARE
TWEET

Untitled

a guest Mar 26th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6.     int val;
  7.     node *next;
  8. };
  9. void _CreateList ( node *&first);//tworzy listê
  10. void _CoutList (node *first);//wypisuje listê
  11. int _ListLength (node *first);//zwraca d³ugoœæ listy
  12.  
  13. void Insert (node *&head, int val){
  14.     if(head==NULL){//jeśli lista jest pusta
  15.         head=new node;
  16.         head->val=val;
  17.         head->next=NULL;
  18.         return;
  19.     }
  20.  
  21.     node *prev=NULL;
  22.     node *cur=head;
  23.     node *tmp=new node;
  24.     tmp->val=val;
  25.     tmp->next=NULL;
  26.  
  27.     if(head->val > val) {//wstawianie na początek
  28.         tmp->next=head;
  29.         head=tmp;
  30.         return;
  31.     }
  32.     while (cur != NULL && cur->val < val){
  33.         prev=cur;
  34.         cur=cur->next;
  35.     }
  36.     if(cur!=NULL && val == cur->val) return;
  37.     else {
  38.     prev->next=tmp;
  39.     tmp->next=cur;
  40.     }
  41. }
  42.  
  43.  
  44. int FindDistinct (int *A, int n){
  45.     int res;
  46.     node *list=NULL;
  47.     for(int i=0;i<n;i++){
  48.         Insert(list,abs(A[i]));
  49.     }
  50.     res=_ListLength(list);
  51.  
  52.     return res;
  53. }
  54.  
  55.  
  56. int main()
  57. {
  58.     int n;
  59.     cin>>n;
  60.     int A[n];
  61.     for(int i=0;i<n;i++) cin>>A[i];
  62.  
  63.     cout<<FindDistinct(A,n);
  64.     return 0;
  65. }
  66. int _ListLength(node *first){
  67.     int length=0;
  68.     while (first!=NULL){
  69.         length++;
  70.         first=first->next;
  71.     }
  72.     return length;
  73. }
  74. void _CreateList ( node *&first){
  75.     int len;
  76.     cin>>len;
  77.     node *p;
  78.     for(int i=0;i<len;i++){
  79.         int x;
  80.         cin>>x;
  81.         p= new node;
  82.         p->val=x;
  83.         p->next=first;
  84.         first=p;
  85.     }
  86. }
  87. void _CoutList (node *first){
  88.     while ( first !=NULL)
  89.     {
  90.         cout<<first->val<<" ";
  91.         first=first->next;
  92.     }
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top