fork download
  1. //Nick
  2.  
  3. #include <cstddef>
  4. #ifndef DOUBLY_LINKED_LIST
  5. #define DOUBLY_LINKED_LIST
  6.  
  7. template <class T>
  8. class IntDLLNode {
  9.  
  10. friend class IntDLList;
  11.  
  12. public:
  13. IntDLLNode(){next = prev = NULL;}
  14.  
  15. IntDLLNode(const T& el, IntDLLNode *n = NULL, IntDLLNode *p = NULL){
  16. info = el;
  17. next = n;
  18. prev = p;
  19. }
  20.  
  21. // protected:
  22. T info;
  23. IntDLLNode<T> *next, *prev;
  24. };
  25.  
  26. template<class T> class IntDLList {
  27.  
  28. public:
  29.  
  30. IntDLList(){head = tail = NULL;}
  31.  
  32. void addToDLLTail(const T& el);
  33. void addToDLLHead(const T& el);
  34. void deleteNode(const T& el);
  35. bool isInList(const T& el) const;
  36. T deleteFromDLLTail();
  37. T deleteFromDLLHead();
  38. void addSorted(int);
  39. void printList();
  40.  
  41. private:
  42.  
  43. IntDLLNode<T> *head, *tail;
  44.  
  45. };
  46. #endif // DOUBLY_LINKED_LIST
  47.  
  48. #include <cstddef>
  49. #include <iostream>
  50.  
  51. using namespace std;
  52.  
  53. template<class T>
  54. void IntDLList<T>::addToDLLHead(const T& el){
  55. if(head!=NULL){
  56. head = new IntDLLNode<T>(el,head,NULL);
  57. head->prev=head;
  58. }
  59. else head = tail = new IntDLLNode<T>(el);
  60. }
  61.  
  62. template<class T>
  63. void IntDLList<T>::addToDLLTail(const T& el){
  64. if(tail!=NULL){
  65. tail = new IntDLLNode<T>(el,NULL,tail);
  66. tail->prev->next = tail;
  67. }
  68. else head = tail = new IntDLLNode<T>(el);
  69. }
  70. template<class T>
  71. T IntDLList<T>::deleteFromDLLTail(){
  72. if(head!=NULL){
  73. T el = tail->info;
  74. if(head == tail) {
  75. delete tail;
  76. head = tail = NULL;
  77. }
  78. else {
  79.  
  80. tail = tail-> prev;
  81. delete tail->next;
  82. tail->next = NULL;
  83. }
  84.  
  85. return el;
  86. }
  87. else throw(20);
  88. }
  89.  
  90. template<class T>
  91. T IntDLList<T>::deleteFromDLLHead(){
  92. if(head!=NULL){
  93. int el = head->info;
  94. IntDLLNode<T> *tmp = head;
  95.  
  96. if(head == tail){
  97. head = tail = NULL;
  98. }
  99.  
  100. else{head = head->next;}
  101. delete tmp;
  102. return(el);
  103. }
  104. else throw(20);
  105. }
  106.  
  107. template<class T>
  108. void IntDLList<T>::deleteNode(const T& el){
  109. if(head!=NULL){
  110.  
  111. if(head == tail && el == head->info){
  112. delete head;
  113. head = tail = NULL;
  114. }
  115.  
  116. else if(el == head->info){
  117. IntDLLNode<T> *tmp = head;
  118. head = head->next;
  119. delete tmp;
  120. }
  121.  
  122. else{
  123. IntDLLNode<T> *els, *tmp;
  124.  
  125. for(els = head, tmp = head->next;
  126. tmp!=NULL && tmp-> info!= el;
  127. els = els->next, tmp = tmp->next);
  128. if(tmp!=NULL){
  129. els->next = tmp->next;
  130.  
  131. if(tmp == tail)
  132. tail = els;
  133.  
  134. delete tmp;
  135. }
  136. }
  137. }
  138. }
  139.  
  140. template<class T>
  141. bool IntDLList<T>::isInList(const T& el) const{
  142. IntDLLNode<T> *tmp;
  143. for(tmp=head;tmp!=NULL && tmp->info!= el;
  144. tmp = tmp->next);
  145. return(tmp!=NULL);
  146. }
  147.  
  148. template<class T>
  149. void IntDLList<T>::addSorted(int i) {
  150. IntDLLNode<T> *tmp;
  151. if(head==NULL)
  152. head = tail = new IntDLLNode<T>(i);
  153. else if(head!=NULL){
  154. for(tmp=head;tmp!=NULL && tmp->info <= i;){
  155. if(tmp->next->info >= i){
  156. new IntDLLNode<T>(i,tmp,tmp->next);
  157. break;
  158. }
  159. else{ tmp = tmp->next;}
  160. }
  161. }
  162. }
  163.  
  164. template<class T>
  165. void IntDLList<T>::printList(){
  166. IntDLLNode<T> *tmp;
  167. if(head!=NULL){
  168. for(tmp=head; tmp!=NULL; tmp = tmp->next){
  169. cout << tmp->info;
  170. }
  171. }
  172. }
  173.  
  174. void f(){
  175. try{
  176. //int val = list.deleteFromHead();
  177. }
  178. catch(int error_code)
  179.  
  180. {
  181. cerr << "Error: " <<error_code << endl;
  182. switch (error_code){
  183. default: cout << "error!!" << endl;
  184. }
  185. }
  186. }
  187.  
  188. #include <iostream>
  189. #include <stdio.h>
  190. #include <list>
  191.  
  192. using namespace std;
  193.  
  194. int main()
  195. {
  196.  
  197. int choice, choice2, numero;
  198. cout << "Select an option\n" << endl;
  199. cout << "1. Create Simple List" << endl;
  200. cout << "2. Create Sorted List" << endl;
  201. cout << "3. Create FIFO Queue" <<endl;
  202. cout << "4. Create LIFO Queue" <<endl;
  203. cout << "5. Exit Program" << endl;
  204. cin >> choice;
  205.  
  206. IntDLList<int> dll;
  207.  
  208. switch(choice){
  209.  
  210. case 1:
  211. switch(choice2){
  212. //add to head
  213. case 1:
  214. cin >> numero;
  215. dll.addToDLLHead(numero);
  216. //add to tail
  217. case 2:
  218. cin >> numero;
  219. dll.addToDLLTail(numero);
  220. //delete head
  221. case 3:
  222. //display integer at head of list
  223. dll.deleteFromDLLHead();
  224. //delete tail
  225. case 4:
  226. //display integer at tail of list
  227. dll.deleteFromDLLTail();
  228. //delete integer from list
  229. case 5:
  230. cin >> numero;
  231. dll.deleteNode(numero);
  232. case 6:
  233. dll.printList();
  234. case 7: break;
  235. }
  236.  
  237. case 2:
  238. switch(choice2){
  239. //add integer in sorted order
  240. case 1:
  241. cin >> numero;
  242. dll.addSorted(numero);
  243. //delete integer node
  244. case 2:
  245. cin >> numero;
  246. dll.deleteNode(numero);
  247. //print list
  248. case 3: dll.printList();
  249. //end program
  250. case 4: break;
  251. }
  252.  
  253. case 3:
  254. switch(choice2){
  255. //enqueue
  256. case 1:
  257. cin >> numero;
  258. dll.addToDLLTail(numero);
  259. //dequeue
  260. case 2:
  261. cin >> numero;
  262. dll.deleteFromDLLHead();
  263. //print FIFO
  264. case 3: dll.printList();
  265. //end
  266. case 4: break;
  267. }
  268.  
  269. case 4:
  270. switch(choice2){
  271. //add to stack
  272. case 1:
  273. cin >> numero;
  274. dll.addToDLLHead(numero);
  275. //delete from stack
  276. case 2:
  277. dll.deleteFromDLLHead();
  278. //print stack
  279. case 3: dll.printList();
  280. //end program
  281. case 4: break;
  282. }
  283.  
  284. case 5: break;
  285.  
  286. }
  287. }
  288.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘IntDLLNode<int>’:
prog.cpp:56:   instantiated from ‘void IntDLList<T>::addToDLLHead(const T&) [with T = int]’
prog.cpp:215:   instantiated from here
prog.cpp:8: error: template argument required for ‘struct IntDLList’
stdout
Standard output is empty