Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include "LinkedList.h"
- /* Node constructor
- *
- */
- Node::Node()
- {
- data = 0;
- next = NULL;
- }
- Node::Node(int d)
- {
- data = d;
- next = NULL;
- }
- /* LinkedList constructor
- *
- * This sets the initial values to make an empty linked list
- */
- LinkedList::LinkedList()
- {
- head = NULL;
- // initialize instance variable in the constructor
- }
- /* addHead
- *
- * This function takes one parameter - an int.
- * This creates a node for the linked list and connects the int to the
- * node. Then it adds the node to the head of the linked list.
- */
- void LinkedList::addHead(int f)
- {
- // create a node and put the int in it
- Node *newNode = new Node(f);
- // if the list is empty
- if(head == NULL) head = newNode;
- else
- {
- newNode = head;
- head = newNode;
- }
- // make the head pointer point to the new link
- }
- /*
- * printList
- *
- * This steps down through each of the nodes in a linked list and
- * prints out the information stored in the int to which the node points.
- * Instead of automatically printing to the screen, it prints to the
- * file pointer passed in. If the programmer wants to print to the screen,
- * he/she will pass in stdout.
- */
- // Note: I have provided the string part of the printf so that your output
- // will match what is expected.
- void LinkedList::printList(FILE *fp) const
- {
- for(Node *temp=head; temp!=NULL; temp=temp->getNext())
- {
- int f = temp->getData();
- fprintf(fp, "%d", f);
- }
- fprintf(fp,"\n");
- // for each node, print out the int attached to it
- // assign f to the int of the right node
- // print the int out
- //fprintf(,"%d, ",);
- //fprintf(,"\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement