fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Data{
  6. int n;
  7. };
  8. struct Node{
  9. Data data;
  10. Node* pnext;
  11. };
  12. struct List{
  13. Node* pHead;
  14. Node* pTail;
  15. };
  16.  
  17. Node* CreateNode(Data data);
  18. Node* CreateNode(int n);
  19. void InitList(List &lst);
  20. bool IsEmpty(const List &lst);
  21. void AddHead(List &lst, Node* pnode);
  22. void AddHead(List &lst, int n);
  23. void AddTail(List &lst,Node* pnode);
  24. void AddTail(List &lst,int n);
  25. int Size(const List &lst);
  26. void PrintList(const List &lst,string);
  27. void Insert(List &lst,Node* pnode,int index);
  28. //void Insert(List &lst,int n,int index);
  29.  
  30.  
  31. int main(){
  32. List lst;
  33. InitList(lst);
  34.  
  35.  
  36. Node* node1=CreateNode(5);
  37.  
  38. AddHead(lst,5);
  39. PrintList(lst,"Add head 1");
  40.  
  41. AddHead(lst,5);
  42. PrintList(lst,"Add head 2");
  43.  
  44. AddHead(lst,5);
  45. PrintList(lst,"Add head 3");
  46.  
  47. AddHead(lst,5);
  48. PrintList(lst,"Add head 4");
  49.  
  50. AddHead(lst,5);
  51. PrintList(lst,"Add head 5");
  52.  
  53. // Insert(lst,node1,0);
  54. // PrintList(lst,"Add index 1");
  55. // Insert(lst,node1,1);
  56. // cout<<"Insert k loi\n";
  57. //
  58. // Insert(lst,node1,0);
  59. // PrintList(lst,"Add index 1");
  60. // Insert(lst,node1,0);
  61. // PrintList(lst,"Add index 1");
  62. }
  63.  
  64. Node* CreateNode(Data _data){
  65. Node* ptemp;
  66. ptemp=new Node;
  67. ptemp->data=_data;
  68. ptemp->pnext=nullptr;
  69. return ptemp;
  70. }
  71.  
  72. Node* CreateNode(int _n){
  73. Data _data;
  74. _data.n=_n;
  75. return CreateNode(_data);
  76.  
  77. }
  78.  
  79. bool IsEmpty(const List &lst){
  80. if(lst.pHead && lst.pTail){
  81. return false;
  82. }
  83. else
  84. return true;
  85. }
  86.  
  87. void InitList(List &lst){
  88. lst.pHead=nullptr;
  89. lst.pTail=nullptr;
  90. }
  91.  
  92. void AddHead(List &lst,Node* pnode){
  93. if(IsEmpty(lst)){
  94. lst.pHead=lst.pTail=pnode;
  95. }
  96. else{
  97. pnode->pnext=lst.pHead;
  98. lst.pHead=pnode;
  99. }
  100. }
  101.  
  102. void AddHead(List &lst,int n){
  103. Node* pnode=CreateNode(n);
  104. if(IsEmpty(lst)){
  105. lst.pHead=lst.pTail=pnode;
  106. }
  107. else{
  108. pnode->pnext=lst.pHead;
  109. lst.pHead=pnode;
  110. }
  111. }
  112.  
  113. void AddTail(List &lst,Node* pnode){
  114. if(IsEmpty(lst)){
  115. lst.pHead=lst.pTail=pnode;
  116. }
  117. else{
  118. lst.pTail->pnext=pnode;
  119. lst.pTail=pnode;
  120. }
  121. }
  122.  
  123. void AddTail(List &lst, int n){
  124. Node* pnode=CreateNode(n);
  125. if(IsEmpty(lst)){
  126. lst.pHead=lst.pTail=pnode;
  127. }
  128. else{
  129. lst.pTail->pnext=pnode;
  130. lst.pTail=pnode;
  131. }
  132. }
  133.  
  134. int Size(const List &lst){
  135. int count=0;
  136. Node* pnode=lst.pHead;
  137. while(pnode){
  138. count++;
  139. pnode=pnode->pnext;
  140. }
  141. return count;
  142. }
  143.  
  144. void PrintList(const List &lst,string mess){
  145. cout<<mess<<endl;
  146. if(IsEmpty(lst)){
  147. cout<<"\nList is Empty!!";
  148. }
  149. else{
  150. Node* pnode=lst.pHead;
  151. while(pnode){
  152. cout<<pnode->data.n<<"\t";
  153. pnode=pnode->pnext;
  154. }
  155. }
  156. cout<<endl;
  157. }
  158.  
  159. void Insert(List &lst,Node* pnode,int index){
  160. int size_list=Size(lst);
  161. if(index>size_list || index<0){
  162. cout<<"\nIndex is invalid(bigger than size list or be negative number)!!";
  163. return;
  164. }
  165.  
  166. if(index==size_list){
  167. AddTail(lst,pnode);
  168. }
  169. else if(index==0){
  170. AddHead(lst,pnode);
  171. }
  172. else{
  173. Node* ptemp=lst.pHead;
  174. for(int i=1;i<=index;i++){
  175. if(i==index){
  176. pnode->pnext=ptemp->pnext;
  177. ptemp->pnext=pnode;
  178. }
  179. ptemp=ptemp->pnext;
  180. }
  181. }
  182. }
  183.  
  184. //void Inset(List &lst,int n,int index){
  185. // int size_list=Size(lst);
  186. // if(index<0 || index>size_list){
  187. // cout<<"\nInvalid index(is bigger than size list or negative number!!)";
  188. // return;
  189. // }
  190. //
  191. // if(index==0){
  192. // AddHead(lst,n);
  193. // }
  194. // else if(index==size_list){
  195. // AddTail(lst,n);
  196. // }
  197. // else{
  198. // Node* pnode=CreateNode(n);
  199. // Node* ptemp=lst.pHead;
  200. // for(int i=1;i<=index;i++){
  201. // if(i==index){
  202. // pnode->pnext=ptemp->pnext;
  203. // ptemp->pnext=pnode;
  204. // }
  205. // ptemp=ptemp->pnext;
  206. // }
  207. // }
  208. //}
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
Success #stdin #stdout 0s 4364KB
stdin
Standard input is empty
stdout
Add head 1
5	
Add head 2
5	5	
Add head 3
5	5	5	
Add head 4
5	5	5	5	
Add head 5
5	5	5	5	5