Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: jaspwns on Mar 1st, 2013  |  syntax: None  |  size: 3.73 KB  |  views: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. main.cpp---------------------------------------------------------------------------------------
  2. #include "LinkList.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     linkedlist obj;
  10.     obj.pushfront(3);
  11.     obj.pushfront(6);
  12.     obj.pushfront(20);
  13.     obj.popback();
  14.     obj.pushfront(5);
  15.     obj.pushback(12);
  16.     obj.popfront();
  17.     obj.deletel(2);
  18.  
  19.  
  20.     obj.printlistfromfront();
  21.     obj.printlistfromtail();
  22.     obj.size();
  23.  
  24.     cin.get();
  25.     return 0;
  26.  
  27. }
  28.  
  29. Node.h--------------------------------------------------------------------------------------------
  30. #include <cstddef>
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. class Node{
  35. public:
  36.     int item;
  37.     Node *next;
  38.     Node *prev;
  39.  
  40.     friend ostream& operator<< (ostream& print, const Node &value){
  41.     return print << value.item;
  42.     };
  43.  
  44.  
  45.  
  46.  
  47.     Node(int data){
  48.     item = data;
  49.     next=NULL; //set it = to NULL by default so it doesnt hold a random value
  50.     prev =NULL; //set it = to NULL by default so it doesnt hold a random value
  51.     }
  52. };
  53.  
  54.  
  55. LinkList.h------------------------------------------------------------------------------
  56. #include "Node.h"
  57. #include <cstddef>
  58. #include<iostream>
  59. using namespace std;
  60.  
  61. class linkedlist{
  62. public:
  63.  
  64.     linkedlist(){
  65.     front=NULL;
  66.     tail=NULL;
  67.  
  68.     };
  69.  
  70.  
  71.  
  72. void pushfront(int item){{
  73. if (front == NULL){
  74.     front =tail = new Node(item);
  75.     front -> next = NULL;
  76.     tail -> prev = NULL;
  77. }
  78. else //or else
  79. {
  80.     Node *temp;
  81.     temp = new Node(item);
  82.     temp -> next = front;
  83.     front -> prev = temp;
  84.     front = temp;
  85. }}
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void popfront (){
  93. if (front == NULL)
  94.     return;
  95. else
  96. {
  97.     Node *temp;
  98.     temp = front;
  99.     front = front -> next;
  100.     front -> prev = NULL;
  101.     front -> next = (temp-> next)->next;
  102.     delete temp;
  103. }
  104. }
  105.  
  106.  
  107.  
  108. void pushback( int item){
  109.     if (front == NULL){
  110.         front = tail = new Node(item);
  111.         front -> next = NULL;
  112.         tail -> prev = NULL;
  113. }
  114.     else
  115.         {
  116.         Node *temp = new Node(item);
  117.         tail -> next = temp;
  118.         temp -> prev = tail;
  119.         tail = temp;
  120.  
  121. }
  122.     }
  123.  
  124.  
  125.  
  126. void popback(){
  127. Node* temp = tail;
  128. (temp -> prev) -> next = NULL;
  129. tail = temp -> prev;
  130. delete temp;
  131. }
  132.  
  133. int size(){
  134. Node* traverse;
  135. int sizel = 0;
  136. traverse = front;
  137. while (traverse != NULL){
  138.     traverse = traverse -> next;
  139.  
  140.  
  141.     sizel++;
  142. }
  143. cout << "The size of the list is: "<<sizel<<endl;
  144. return sizel;
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151. void deletel(int ith){
  152. Node* traversle;
  153. int sizel = 0;
  154. traversle = front;
  155. while (traversle != NULL){
  156.     traversle = traversle -> next;
  157.     sizel++;
  158. }
  159. if (ith <= sizel){
  160. if (front == NULL && tail == NULL){
  161.     cout<<"there are no nodes to delete.";
  162.     }
  163. if ((front -> next == NULL) && (tail -> prev == NULL)){
  164.         front = tail = NULL;}
  165.     else{
  166.         int i = 0;
  167.         Node *traverse;
  168.         traverse = front;
  169.         while (i < ith ){
  170.             traverse = traverse -> next;
  171.             i++;
  172.             }
  173.             (traverse -> next) -> prev = traverse -> prev;
  174.             (traverse -> prev) -> next = traverse -> next;
  175.  
  176.  
  177.  
  178.         }
  179. }
  180. else{
  181.     cout << "There is no such node" << endl;
  182. }
  183. }
  184.  
  185.  
  186.  
  187.  
  188. void printlistfromfront(){
  189. Node* traverse;
  190. traverse = front;
  191. cout << "Items starting from the front of the list are:\a ";
  192. while (traverse != NULL){
  193.     cout <<" " << *traverse;
  194.     traverse = traverse -> next;
  195. }
  196. cout << endl;
  197.  
  198. }
  199.  
  200.  
  201. void printlistfromtail(){
  202. Node *traversle;
  203. traversle = tail;
  204. cout << "Items from the end of the list, going backwards are:\a ";
  205. while (traversle != NULL){
  206.     cout << " " << *traversle;
  207. traversle = traversle -> prev;
  208. }
  209. cout << endl;
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. private:
  223.     Node *front;
  224.     Node *tail;
  225.  
  226. };