Linked List
A linked list is a data structure which is built from structures and pointers. It forms a chain of "nodes" with pointers representing the links of the chain and holding the entire thing together. 
struct Node{
int data_;
Node* next_;
}
The important part of the structure is the line before the closing curly brackets. This gives a pointer to the next node in the list. This is the only case in C++ where you are allowed to refer to a data type (in this case node) before you have even finished defining it! 
//if add node at the front
void addFront(int v){
Node* newnode = new Node;
newnode->data_ = v;
if(!start_){
 start_ = newnode;
 newnode->next_ = NULL;
}
else{
 newnode->next_ = start_;
 start_ = newnode;
}
}
//if add node at the end
void addEnd(int v){
 NOde* temp, temp2;
 temp = new Node;
 temp->data- = v;
 temp->next_ = NULL;
 if(!start_) start_ = temp;
 else{
  temp2 = start_;
  while(temp2->next_){
    temp2 = temp2->next_;
  }
  temp2->next_ = temp;
 }
}
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment