logo

C++의 정방향 목록 | 세트 2(기능 조작)

C++의 정방향 목록 | 세트 1(소개 및 중요 기능) 이 문서에서는 추가 기능에 대해 설명합니다. 삽입 및 삭제 이외의 정방향 목록에서 사용할 수 있는 작업 중 일부는 다음과 같습니다. 

1. 병합() :- 이 기능은 하나의 전달 목록을 다른 목록과 병합하는 데 사용됩니다. 두 목록이 모두 정렬되면 반환된 결과 목록도 정렬됩니다. 



2. 연산자 '=' :- 이 연산자는 하나의 전달 목록을 다른 목록에 복사합니다. 이 경우 만들어진 복사본은 딥 카피(Deep Copy)입니다. 

CPP
// C++ code to demonstrate the working of  // merge() and operator=  #include    #include  using namespace std;  int main()  {   // Initializing 1st forward list   forward_list<int> flist1 = {1 2 3};     // Declaring 2nd forward list   forward_list<int> flist2;     // Creating deep copy using '='   flist2 = flist1;     // Displaying flist2   cout << 'The contents of 2nd forward list'  ' after copy are : ';   for (int &x : flist2)   cout << x << ' ';   cout << endl;     // Using merge() to merge both list in 1   flist1.merge(flist2);     // Displaying merged forward list   // Prints sorted list   cout << 'The contents of forward list '  'after merge are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;     return 0;  }  

산출:

tkinter 버튼
The contents of 2nd forward list after copy are : 1 2 3 The contents of forward list after merge are : 1 1 2 2 3 3 

시간 복잡도: 오(1)



보조 공간: 오(1)
  3. 종류() :- 이 기능은 정방향 목록을 정렬하는 데 사용됩니다. 

4. 고유한() :- 이 함수는 여러 번 나타나는 숫자를 삭제하고 고유한 요소가 있는 정방향 목록을 반환합니다. 이 함수를 성공적으로 실행하려면 정방향 목록을 정렬해야 합니다. 

CPP
// C++ code to demonstrate the working of  // sort() and unique()  #include    #include // for sort() and unique()  using namespace std;  int main()  {   // Initializing 1st forward list   forward_list<int> flist1 = {1 2 3 2 3 3 1};   // Sorting the forward list using sort()   flist1.sort();   // Displaying sorted forward list   cout << 'The contents of forward list after '  'sorting are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;   // Use of unique() to remove repeated occurrences   flist1.unique();   // Displaying forward list after using unique()   cout << 'The contents of forward list after '  'unique operation are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;   return 0;  }  

산출:



The contents of forward list after sorting are : 1 1 2 2 3 3 3 The contents of forward list after unique operation are : 1 2 3 

시간 복잡도: 오(1)

리디마 티와리

보조 공간: 오(1)
 

5. 뒤집다() :- 이 함수는 정방향 목록을 반전하는 데 사용됩니다.

문자열 메소드

6. 교환() :- 이 함수는 한 전달 목록의 내용을 다른 내용으로 바꿉니다. 

CPP
// C++ code to demonstrate the working of  // reverse() and swap()  #include    #include // for reverse() and swap()  using namespace std;  int main()  {   // Initializing 1st forward list   forward_list<int> flist1 = {1 2 3};   // Initializing 2nd forward list   forward_list<int> flist2 = {4 5 6};   // Using reverse() to reverse 1st forward list   flist1.reverse();   // Displaying reversed forward list   cout << 'The contents of forward list after'  ' reversing are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl << endl;   // Displaying forward list before swapping   cout << 'The contents of 1st forward list '  'before swapping are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;   cout << 'The contents of 2nd forward list '  'before swapping are : ';   for (int &x : flist2)   cout << x << ' ';   cout << endl;   // Use of swap() to swap the list   flist1.swap(flist2);   // Displaying forward list after swapping   cout << 'The contents of 1st forward list '  'after swapping are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;   cout << 'The contents of 2nd forward list '  'after swapping are : ';   for (int &x : flist2)   cout << x << ' ';   cout << endl;   return 0;  }  

산출:

The contents of forward list after reversing are : 3 2 1 The contents of 1st forward list before swapping are : 3 2 1 The contents of 2nd forward list before swapping are : 4 5 6 The contents of 1st forward list after swapping are : 4 5 6 The contents of 2nd forward list after swapping are : 3 2 1 

시간 복잡도: 오(1)

보조 공간: 오(1)
 

7. 분명한() :- 이 함수는 정방향 목록의 내용을 지웁니다. 이 함수 이후에는 전달 목록이 비어 있게 됩니다.

  8. 비어 있는() :- 이 함수는 목록이 비어 있으면 true를 반환하고, 그렇지 않으면 false를 반환합니다. 

csma와 csma CD
CPP
// C++ code to demonstrate the working of  // clear() and empty()  #include    #include // for clear() and empty()  using namespace std;  int main()  {   // Initializing forward list   forward_list<int> flist1 = {1 2 3};     // Displaying forward list before clearing   cout << 'The contents of forward list are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;     // Using clear() to clear the forward list   flist1.clear();     // Displaying list after clear() performed   cout << 'The contents of forward list after '  << 'clearing are : ';   for (int &x : flist1)   cout << x << ' ';   cout << endl;     // Checking if list is empty   flist1.empty() ? cout << 'Forward list is empty' :   cout << 'Forward list is not empty';     return 0;  }  

산출:

The contents of forward list are : 1 2 3 The contents of forward list after clearing are : Forward list is empty

시간 복잡도: 오(1)

보조 공간: 오(1)
Forward_list의 최근 기사