adding list implementation
This commit is contained in:
@@ -73,13 +73,18 @@ variable p.
|
||||
|
||||
- Play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/doubly_lists/remove/index.html) to see how the remove works.
|
||||
|
||||
## 11.7 Special Cases of Remove
|
||||
## 11.6 Special Cases of Remove
|
||||
|
||||
- If p==head and p==tail, the single node in the list must be removed and both the head and tail pointer
|
||||
variables must be assigned the value NULL.
|
||||
- If p==head or p==tail, then the pointer adjustment code we just wrote needs to be specialized to removing
|
||||
the first or last node.
|
||||
|
||||
## 11.7 List Implementation
|
||||
|
||||
- We have a list implementation [here](list.h).
|
||||
- And we can test our list with this [program](list_main.cpp).
|
||||
|
||||
## 11.8 Leetcode Exercises
|
||||
|
||||
- [Leetcode problem 141: Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/). Solution: [p141_linkedlistcycle.cpp](../../leetcode/p141_linkedlistcycle.cpp)
|
||||
|
||||
@@ -59,7 +59,6 @@ template <class T>
|
||||
class dslist {
|
||||
public:
|
||||
typedef list_iterator<T> iterator;
|
||||
typedef unsigned int size_type;
|
||||
// default constructor
|
||||
dslist(){
|
||||
head = nullptr;
|
||||
@@ -102,7 +101,7 @@ class dslist {
|
||||
}
|
||||
}
|
||||
|
||||
size_type size(){
|
||||
unsigned int size(){
|
||||
return size_;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,8 @@ int main(){
|
||||
teams.push_back("wpi");
|
||||
teams.push_back("yale");
|
||||
teams.push_back("brown");
|
||||
teams.push_back("cornell");
|
||||
teams.push_back("colgate");
|
||||
teams.push_back("miami");
|
||||
teams.push_back("colorado");
|
||||
teams.push_back("harvard");
|
||||
|
||||
// we can use a type alias defined inside a class even if there is no object of that class. The type alias becomes part of the class's scope and can be used anywhere in your code where the class's scope is visible. although no object of dslist is created in this code, the type alias size_type is still accessible because it is part of the class's scope.
|
||||
dslist<std::string>::iterator itr;
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
@@ -26,58 +21,9 @@ int main(){
|
||||
|
||||
teams.pop_back();
|
||||
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
itr = teams.begin();
|
||||
itr++;
|
||||
teams.erase(itr);
|
||||
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
teams.push_front("harvard");
|
||||
teams.push_front("princeton");
|
||||
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
teams.pop_front();
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
dslist<double> ll;
|
||||
ll.push_back(2.5);
|
||||
ll.push_back(3.4);
|
||||
int i = 0;
|
||||
dslist<double>::iterator itr2 = ll.begin();
|
||||
for (i=0, itr2 = ll.begin(); itr2 != ll.end(); itr2++, i++){
|
||||
std::cout << "ll[" << i << "] is " << *itr2 << std::endl;
|
||||
}
|
||||
|
||||
// copy a list - calls copy constructor.
|
||||
dslist<double> u(ll);
|
||||
itr2 = u.begin();
|
||||
itr2++;
|
||||
u.insert(itr2, 6.5);
|
||||
u.insert(itr2, 4.8);
|
||||
for (i=0, itr2 = u.begin(); itr2 != u.end(); itr2++, i++){
|
||||
std::cout << "u[" << i << "] is " << *itr2 << std::endl;
|
||||
}
|
||||
|
||||
// equivalent to list<double> w(v), w is a copy of the elements in v.
|
||||
// we use the const keyword in front of a variable definition to indicate that the value of the variable cannot be changed after it is initialized.
|
||||
dslist<double> w = ll;
|
||||
for (i=0, itr2 = w.begin(); itr2 != w.end(); itr2++, i++){
|
||||
std::cout << "w[" << i << "] is " << *itr2 << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user