html模版C++問題
大大們好,小弟剛學完C++,目前正在刷leetcode練習。今天遇到了一題,感覺有點卡住,希望大大們能解惑

題目:Design your implementation of the linked list.You can choose to use the singly linked list or the doubly linked list.A node in a singly linked list should have two attributes: val and next.val is the value of the current node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list.Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.

addAtHead(val) : Add a node of value val before the first element of the linked list.After the insertion, the new node will be the first node of the linked list.

addAtTail(val) : Append a node of value val to the last element of the linked list.

addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list.

If index equals to the length of linked list, the node will be appended to the end of linked list.If index is greater than the length, the node will not be inserted.deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:MyLinkedList linkedList = new MyLinkedList();linkedList.addAtHead(1);linkedList.addAtTail(3);linkedList.addAtIndex(1, 2);??// linked list becomes 1->2->3linkedList.get(1);????????????// returns 2linkedList.deleteAtIndex(1);??// now the linked list is 1->3linkedList.get(1);????????????// returns 3

以上是題目,然後我有用vector寫出來;只是想請教,要是不利用標準樣板來寫而是用new和delete來寫的話,要怎麼寫呢?

用vector自己寫的:

然後另一個想請教的是,建構式代表什麼意思;通常是怎麼撰寫建構式的呢?雖然讀完了C++的用書,但裡面沒怎麼提關於建構式的相關說明和用法。希望大大們能不令賜教。
自學,
Constructor就是你在宣告物件時有沒有什麼要預先執行的指令(例如參數初始化等)如果不寫建構子編譯器會給你一個預設建構子Defaultconstructor就是宣告後不做任何事情036F12924A6FE485
arrow
arrow

    nelirifex 發表在 痞客邦 留言(0) 人氣()