Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. //list header
  2. #ifndef LIST_H
  3. #define LIST_H
  4. #include <fstream>
  5. #include "node.h"
  6.  
  7.  
  8. template <class T, class E>
  9. class dlist {
  10. private:
  11. int length;
  12. T *head;
  13. T *tail;
  14.  
  15. public:
  16. dlist()
  17. {
  18. head = 0;
  19. }
  20.  
  21.  
  22.  
  23.  
  24. void createDlist(string value, E x)
  25. {
  26. T *cu;
  27. T *temp;
  28. temp = new node(x);
  29.  
  30. temp->data = value;
  31. temp->next = NULL;
  32. if (head == NULL)
  33. {
  34. temp->prev = NULL;
  35. head = temp;
  36. }
  37. else
  38. {
  39. T *cu = head;
  40. while (cu->next != NULL)
  41. cu = cu->next;
  42. cu->next = temp;
  43. temp->prev = cu;
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. void printDlist() {
  53. T *temp = head;
  54. cout << "The linked list is as followed: " << endl;
  55. while (temp != NULL)
  56. {
  57. cout << temp->data << " " << endl;
  58. temp = temp->next;
  59.  
  60. }
  61. cout << endl;
  62. }
  63.  
  64.  
  65.  
  66. };
  67. #endif
  68.  
  69.  
  70.  
  71.  
  72. //node header
  73.  
  74. #ifndef NODE_H
  75. #define NODE_H
  76. #include <string>
  77.  
  78. using namespace std;
  79.  
  80. class node {
  81. public:
  82. string data;
  83. node* next;
  84. node* prev;
  85.  
  86. node(string x) {
  87. data = x;
  88. next = 0;
  89. prev = 0;
  90. }
  91. };
  92.  
  93. #endif
  94.  
  95.  
  96. //main cpp
  97.  
  98. #include "list.h"
  99. #include <iostream>
  100. #include <fstream>
  101. #include <string>
  102. using namespace std;
  103.  
  104. int main() {
  105. dlist <node> list;
  106.  
  107.  
  108. string line;
  109. ifstream myfile("input1.txt");
  110. if (myfile.is_open())
  111. {
  112. while (getline(myfile, line))
  113. {
  114. //cout << line << endl;
  115. list.createDlist(line);
  116. }
  117. list.printDlist();
  118. //myfile.close();
  119. }
  120. //else cout << "unable to open file";
  121. system("pause");
  122. return 0;
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement