Advertisement
pichumy

Untitled

Feb 2nd, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. #include "LinkedList2.h"
  5.  
  6. /* Node constructor
  7. *
  8. */
  9. Node::Node()
  10. {
  11. data = 0;
  12. next = NULL;
  13. }
  14.  
  15. Node::Node(int d)
  16. {
  17. data = d;
  18. next = NULL;
  19. }
  20.  
  21. /* LinkedList constructor
  22. *
  23. * This sets the initial values to make an empty linked list
  24. */
  25.  
  26. LinkedList::LinkedList()
  27. {
  28. head = NULL;
  29. // initialize instance variable in the constructor
  30. }
  31.  
  32. /* addHead
  33. *
  34. * This function takes one parameter - an int.
  35. * This creates a node for the linked list and connects the int to the
  36. * node. Then it adds the node to the head of the linked list.
  37. */
  38.  
  39. void LinkedList::addHead(int f)
  40. {
  41. // create a node and put the int in it
  42. Node *newNode = new Node(f);
  43. // if the list is empty
  44. if(head == NULL) head = newNode;
  45. else
  46. {
  47. newNode->setNext(head);
  48. head = newNode;
  49. }
  50.  
  51. // make the head pointer point to the new link
  52. }
  53.  
  54. /*
  55. * printList
  56. *
  57. * This steps down through each of the nodes in a linked list and
  58. * prints out the information stored in the int to which the node points.
  59. * Instead of automatically printing to the screen, it prints to the
  60. * file pointer passed in. If the programmer wants to print to the screen,
  61. * he/she will pass in stdout.
  62. */
  63. // Note: I have provided the string part of the printf so that your output
  64. // will match what is expected.
  65. void LinkedList::printList(FILE *fp) const
  66. {
  67. for(Node *temp=head; temp!=NULL; temp=temp->getNext())
  68. {
  69. int f = temp->getData();
  70. fprintf(fp, "%d", f);
  71. }
  72. fprintf(fp,"\n");
  73. // for each node, print out the int attached to it
  74. // assign f to the int of the right node
  75. // print the int out
  76. //fprintf(,"%d, ",);
  77.  
  78. //fprintf(,"\n");
  79. }
  80.  
  81. /*
  82. * length
  83. *
  84. * This steps down the list to determine how many nodes are in the
  85. * linked list. It returns that value.
  86. */
  87. int LinkedList::length() const
  88. {
  89. Node *temp;
  90. int count = 0;
  91. for(temp=head; temp!=NULL; temp=temp->getNext())
  92. {
  93. count++;
  94. }
  95. return count;
  96. }
  97.  
  98. /*
  99. * deleteFourth
  100. *
  101. * This deletes the fourth node. If there are not already four nodes,
  102. * it does nothing and returns false.
  103. *
  104. * Remember to draw before and after pictures if you are having trouble
  105. * designing the code or debugging it.
  106. */
  107. bool LinkedList::deleteFourth()
  108. {
  109. int count = 1;
  110. Node *hold;
  111. Node *np;
  112. if(this->length() < 4) return false;
  113. for(Node *temp=head; temp!=NULL; temp=temp->getNext())
  114. {
  115. if(count == 3)
  116. {
  117. if((temp->getNext()->getNext()) == NULL)
  118. {
  119. temp->setNext(np);
  120. return true;
  121. }
  122. else
  123. {
  124. hold->setNext(temp->getNext()->getNext());
  125. temp->setNext(hold);
  126. return true;
  127. }
  128. }
  129. }
  130. return false;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement