1. 程式人生 > >(C/C++) Link List

(C/C++) Link List

利用C++寫一個基本的 Link list 練習,功能包含 pint list、CreatList、Insert、Delete、Reverse、Search、Clear、GetLen。

先建立相關的Class ListNode、LinkedList

 1 class LinkedList;  // 需要先宣告
 2 class ListNode{
 3 public:
 4     int data;
 5     ListNode *next;
 6 public:
 7     ListNode():data(0), next(0){};
 8     ListNode(int a):data(a), next(0
){}; 9 friend class LinkedList; 10 }; 11 12 13 class LinkedList{ 14 public: 15 // int size; // size是用來記錄Linked list的長度, 非必要 16 ListNode *first; 17 public: 18 LinkedList() :first(0){}; 19 void PrintList(); 20 void CreateList(int *arr, int len); 21 void Push_front(int
x); 22 void Push_back(int x); 23 void Delete(int x); 24 void Clear(); 25 void Reverse(); 26 int ListLen(); 27 ListNode * ListSearch(int x); 28 void Insert(int x, int data); 29 };

首先是Creat List,給一個arry利用Link list串在一起。

 1 void LinkedList::CreateList(int *arr, int len)
2 { 3 if (arr == NULL || len == 0) 4 return; 5 6 ListNode * previous = new ListNode(); 7 for (int i = 0; i < len; i++) 8 { 9 ListNode * newNode = new ListNode(); 10 newNode->data = arr[i]; 11 if (i == 0) 12 first = newNode; 13 else 14 previous->next = newNode; 15 newNode->next = NULL; 16 previous = newNode; 17 } 18 }

PrintList : 印出所有的 List

 1 void LinkedList::PrintList()
 2 {
 3     if (first == 0)
 4     {
 5         cout << "List is empty.\n";
 6         return;
 7     }
 8     
 9     ListNode * current = first;
10     while (current != 0)
11     {
12         cout << current->data << "  ";
13         current = current->next;
14     }
15     cout << endl;
16 }

Insert List : 把新的node插在x之後

 1 void LinkedList::Insert(int x, int data)
 2 {
 3     ListNode * current = first;
 4     ListNode * previous = new ListNode();
 5     ListNode * newNode = new ListNode();
 6     while (current)
 7     {
 8         if (current->data == x)
 9         {
10             newNode->data = data;
11             previous->next = newNode;
12             newNode->next = current;
13             break;
14         }
15         else
16         {
17             previous = current;
18             current = current->next;
19         }
20     }
21 
22 }

Delete : 刪除特定 node

 1 void LinkedList::Delete(int x)
 2 {
 3     ListNode * current = first, *previous = 0;
 4 
 5     while (current != 0 && current->data != x) {
 6         previous = current;
 7         current = current->next;
 8     }
 9 
10     if (current == 0) {
11         std::cout << "There is no " << x << " in list.\n";
12     }
13     else if (current == first) {
14         first = current->next;
15         delete current;
16         current = 0;
17     }
18     else {
19         previous->next = current->next;
20         delete current;
21         current = 0;
22     }
23 }

Reverse : 反轉 list

 1 void LinkedList::Reverse()
 2 {
 3     if (first == 0 || first->next == 0)
 4         return;
 5 
 6     ListNode *previous = 0, *current = first, *preceding = first->next;
 7 
 8     while (preceding != 0)
 9     {
10         current->next = previous;
11         previous = current;
12         current = preceding;
13         preceding = preceding->next;
14     }
15     current->next = previous;
16     first = current;
17 }

Search node : 尋找特定node

 1 ListNode * LinkedList::ListSearch(int x)
 2 {
 3     if (first == 0)
 4     {
 5         printf("List is empty\n");
 6         return NULL;
 7     }
 8     ListNode * newNode = first;
 9     while (newNode)
10     {
11         if (newNode->data != x)
12             newNode = newNode->next;
13         else
14             return newNode;
15     }
16     printf("not found Node!!\n");
17     return NULL;
18 }

ListLen : 回傳List長度

 1 int LinkedList::ListLen()
 2 {
 3     if (first == 0)    return NULL;
 4     int index = 0;
 5     ListNode * newNode = first;
 6     while (newNode)
 7     {
 8         newNode = newNode->next;
 9         index++;
10     }
11     return index;
12 }

Push_front/back : 插入節點在頭/尾

 1 void LinkedList::Push_front(int x)
 2 {
 3     ListNode *newNode = new ListNode(x);
 4     newNode->next = first;
 5     first = newNode;
 6 }
 7 
 8 void LinkedList::Push_back(int x)
 9 {
10     ListNode * newNode = new ListNode(x);
11     if (first == 0)
12     {
13         first = newNode;
14         return;
15     }
16     ListNode * current = first;
17     while (current->next != 0)
18     {
19         current = current->next;
20     }
21     current->next = newNode;
22 }