Single-item Linked List Implementation

Source: Internet
Author: User

Single-item Linked List Implementation

Implement a list class based on a one-way linked list.

Struct node {

Int x;

Int y;

Node (int xx, int yy ){

X = xx;

Y = yy;

}

}

1. When constructing a function, set a head node to point to null. As follows:

2. Insert nodes:

When position is 0, it should have been like this:

However, when this function inserts a knot here, if count = 0, the position in the graph is regarded as NULL, and the following insertion method is still not affected. So this is the advantage of setting the header node. When position = 0, the insert node does not need to be determined by the conditions (this kind of judgment is easy to forget to write, this leads to hard debugging). Here, you only need to find the node before the position inserted through current, and then insert the new node after current, the insert operation can be completed if the next of the new node is current-> next.

 

3. remove node:

To delete the position node, the head is also used to reduce the judgment steps.

 

Code and comments:

1 # include <iostream> 2 using namespace std; 3 enum Error_code {4 success, 5 underflow, 6 overflow 7}; 8 template <class List_entry> 9 struct Node {10 List_entry entry; 11 Node <List_entry> * next; 12}; 13 template <class List_entry> 14 class MyList {15 public: 16 MyList () {17 head = new Node <List_entry>; // set a header node 18 head-> entry = 0; 19 head-> next = NULL; 20 count = 0; // The current linked list is empty (the node after the header is saved in, c Ount refers to the number of nodes we store) 21 curPosition =-1; // The Position of the header is-1 22 current = head; // The current position points to the head of the linked list 23} 24/* The Role Of The header is mainly reflected in insert (int position, const List_entry & entry), remove (...), retrieve (...), 25 replay (...) for details about these functions, see related functions */26 ~ MyList () {27 current = NULL; 28 curPosition =-1; 29 Node <List_entry> * temp = head, * a; 30 while (head! = NULL) {31 head = temp-> next; 32 a = temp; 33 delete a; 34 temp = head; 35} 36 count = 0; 37} 38 // copy constructor and assign value operator overload. Note the difference between deep copy and shallow copy 39 MyList (const MyList <List_entry> & copy) {40 head = new Node <List_entry>; // this start... 41 head-> entry = 0; 42 head-> next = NULL; 43 count = 0; 44 curPosition =-1; 45 current = head; // this end... copy constructor, which is equivalent to constructing a MyList () and then copying the content of the copy linked list to 46 if (copy. count! = 0) {// copy the content of the linked list 47 count = copy. count; 48 Node <List_entry> * a, * B; 49 a = copy. head-> next; 50 B = head; 51 for (int I = 0; I <copy. count; I ++) {52 B-> next = new Node <List_entry>; 53 B = B-> next; 54 B-> entry = a-> entry; 55 B-> next = NULL; 56 a = a-> next; 57} 58} 59} 60 // different from the copy constructor, the premise of this function is that MyList () is already implemented, for example, MyList a, and copy to a 61 void operator = (const MyList <List_entry> & copy) {62 if (! Empty () clear (); // clear 63 if (copy. count! = 0) {// copy the linked list content 64 count = copy. count; 65 Node <List_entry> * a, * B; 66 a = copy. head-> next; 67 B = head; 68 for (int I = 0; I <copy. count; I ++) {69 B-> next = new Node <List_entry>; 70 B = B-> next; 71 B-> entry = a-> entry; 72 B-> next = NULL; 73 a = a-> next; 74} 75} 76} 77 // clear the list and retain the header node when clearing the list, restore data to original status 78 void clear () {79 if (! Empty () {80 Node <List_entry> * temp = head-> next, * a; 81 while (temp! = NULL) {82 head-> next = temp-> next; 83 a = temp; 84 temp = head-> next; 85 delete a; 86 count --; 87} 88 current = head; 89 curPosition =-1; 90} 91} 92 // determine whether the list is empty 93 bool empty () const {94 if (count! = 0) return false; 95 return true; 96} 97 // determine whether the list is full 98 bool full () const {99 return false; 100} 101 // obtain the number of elements in the list 102 int size () const {103 return count; 104} 105 // insert an element with a value of entry at the position, if the position is 0, it is inserted after the head (the position of the head is-1), and so on. // if the position is <0 or position> count, returns underflow107 Error_code insert (int position, const List_entry & entry) {108 if (position <0 | position> count) ret Urn underflow; 109 Node <List_entry> * new_node, * pre, * follow; 110 // a new Node 111 new_node = new Node <List_entry>; 112 new_node-> entry = entry; 113 // setPosition is used to set the current position to the position of position-1, insert new_node114 behind position-1 // when position = 0, insert the position behind the header, and pre points to the header. If there is no head, we need to discuss the situation when position = 0, it is easy to miss the judgment of 115 setPosition (position-1); 116 pre = current; 117 follow = current-> next; 118 // The new node points to follow, and the previous node points to the new node new_node119 new_node-> next = follow; 120 pre-> next = new_node; 121 count ++; 122 return success; 123} 124 // Delete the element at the position, and save the value of this element in the entry 125 // If position <0 or position> = count, returns underflow126 Error_code remove (int position, List_entry & entry) {127 if (position <0 | position> = count) return underflow; 128 Node <List_entry> * pre, * now, * follow; 129 // remove the position node and set the pre point to the previous one, therefore, we should first find the node 130 setPosition (position-1); 131 pre = current; 132 now = current-> next; 133 entry = now-> entry; 134 follow = now-> next; 135 pre-> next = follow; 136 delete now; // delete the position node 137 count --; 138 return success; 139} 140 // obtain the element at the position specified by position and save it in entry 141 // If position <0 or position> = count, underflow142 Error_code retrieve (int position, list_entry & entry) const {143 if (position <0 | position> = count) return underflow; 144 setPosition (position); 145 entry = current-> entry; 146 return success; 147} 148 // replace the element at position with entry149 // If position <0 or position> = count, underflow150 Error_code replace (int position, const List_entry & entry) {151 if (position <0 | position> = count) return underflow; 152 setPosition (position); 153 current-> entry = entry; 154 return success; 155} 156 // use the visit function to traverse all elements in the list. 157 void traverse (void (* visit) (List_entry &)) {158 Node <List_entry> * a = head-> next; 159 for (int I = 0; I <count; I ++) {160 (* visit) (a-> entry); 161 a = a-> next; 162} 163} 164 protected: 165 int count; // record the number of elements in the list: 166 Node <List_entry> * head; // The head pointer of the linked list: 167 mutable int curPosition; // current pointer position number 168 mutable Node <List_entry> * current; // current pointer 169 // set the current pointer position, point to position 170 void setPosition (int position) const {171 if (position <curPosition) {172 curPosition =-1; 173 current = head; 174} 175 (; curPosition <position; curPosition ++) {176 current = current-> next; 177} 178} 179 };

 

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.