Advertisement
pichumy

Untitled

Jan 31st, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. #include "LinkedList.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 = 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement