2021-05-11

利用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 }
这个数据可以使用Python进行解析和处理。可以按照以下步骤进行: 1. 将数据分割成每个数据项。 ``` data_items = data.split(',') ``` 2. 对于每个数据项,将其按#分割成四个小项,并存储到一个列表中。 ``` data_list = [] for item in data_items: item_list = item.split('#') data_list.append(item_list) ``` 3. 对于每个小项,进行相应的类型转换。 ``` for item in data_list: item[0] = int(item[0]) # 题号转换为整数 item[1] = datetime.datetime.strptime(item[1], '%Y-%m-%d %H:%M:%S') # 时间转换为datetime类型 if item[2] != '': # 操作类型转换为整数 item[2] = int(item[2]) item[3] = str(item[3]) # 科目转换为字符串类型 ``` 4. 可以按照需要对数据进行进一步处理,如按照题号、时间等进行排序、筛选等操作。 完整的Python代码如下: ``` import datetime data = '''1#2021-05-18 14:31:55##初级会计实务,2#2021-05-18 14:31:57#12#初级会计实务,2#2021-05-18 14:32:08##初级会计实务,3#2021-05-18 14:32:09#12#初级会计实务,4#2021-05-18 14:32:34#12#初级会计实务,4#2021-05-18 14:32:45##初级会计实务,5#2021-05-18 14:32:46#12#初级会计实务,5#2021-05-18 14:32:57##初级会计实务,6#2021-05-18 14:33:00#12#初级会计实务,7#2021-05-18 14:33:44#12#初级会计实务,7#2021-05-18 14:34:42##初级会计实务,8#2021-05-18 14:34:43#12''' # 将数据分割成每个数据项 data_items = data.split(',') # 对于每个数据项,将其按#分割成四个小项,并存储到一个列表中 data_list = [] for item in data_items: item_list = item.split('#') data_list.append(item_list) # 对于每个小项,进行相应的类型转换 for item in data_list: item[0] = int(item[0]) # 题号转换为整数 item[1] = datetime.datetime.strptime(item[1], '%Y-%m-%d %H:%M:%S') # 时间转换为datetime类型 if item[2] != '': # 操作类型转换为整数 item[2] = int(item[2]) item[3] = str(item[3]) # 科目转换为字符串类型 print(data_list) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值