Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Implement a linked list.
- Write a C++ class (or set of classes) that implements a linked list. The data contained in the nodes are integers.
- The maximum number of nodes in the list is 100. Implement at least the following functions:
- · Constructor and destructor;
- · void insert (int number); - inserts an element with the value number at the beginning of the list;
- · int isEmpty(); - returns 1 if the list is empty, 0 otherwise;
- · int numOfElements(); - returns the number of elements in the list.
- */
- linkedList::linkedList()
- {
- root = NULL;
- max_size = 100; // LL size can't go beyond this
- size = 0; //Initial size
- }
- void linkedList::insert(int number)
- {
- if (size == max_size || 100)
- {
- printf("FUll!\n");
- }
- else
- {
- if (size == 0) // If the size is 0 (empty LL), create a new node with avalue
- {
- root = new node(number, NULL);
- }
- else //Otherwise, point to the next node
- {
- node* newNode = new node(number, root);
- root = newNode;
- }
- size++;//Increments the size by 1 each iteration
- printf("The item has successfully been added to the list!\n");//Notifies the user that the item has been added successfully!
- }
- }
- int linkedList::isEmpty() // Checks if the LL is empty or not
- {
- if (size == 0){
- return 1;
- }
- else
- return 0;
- }
- int linkedList::numOfElements() // Checks to see how many elements are in the LL
- {
- return size;
- }
- void linkedList::printList() // Prints but will not return a value
- {
- node* temp = root;
- if (size == 0)
- return 1;
- else
- {
- printf("%f ", root->data);
- }
- while (temp->next != NULL)
- {
- temp = temp->next; // temp is assigned to the next value from the pointer
- printf("%f ", temp->data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment