Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zach Bowers
- //LinkedList HW5
- //LinkedList.cpp
- #include "LinkedList.h"
- #include <iostream>
- // creating an empty node
- LinkedList ::LinkedList() {
- //dynamically allocating a new node
- headPtr = new Node;
- //headptr of nextptr points to null pointer
- headPtr ->nextPtr= nullptr;
- headPtr->data=' ';
- }
- bool LinkedList ::insertAtFront(char value) {
- //create a new node
- Node *newNode = new Node;
- //fill the new node with data
- newNode->data=value;
- //new node equals what head pointer points to
- newNode->nextPtr=headPtr->nextPtr;
- //connect head pointer of nextptr to new node
- headPtr->nextPtr=newNode;
- return true;
- }
- bool LinkedList:: insertAtBack(char value)
- {
- //if headptr of nextptr equals nullptr
- if(headPtr->nextPtr == nullptr)
- //if empty insert node at front
- return insertAtFront(value);
- //temp node connects to what head pointer is pointing to
- Node *temp = headPtr->nextPtr;
- //create new node
- Node *newNode = new Node;
- //Fill node with data
- newNode->data = value;
- //new node of nextptr equal to nullptr
- newNode->nextPtr = nullptr;
- //loop while temp node of nextptr doesnt equal to nullptr
- while(temp->nextPtr != nullptr)
- {
- //temp equals to temp of nextptr
- temp = temp->nextPtr;
- }
- //temp of nextptr points to new node
- temp->nextPtr = newNode;
- return true;
- }
- bool LinkedList:: deleteAtFront()
- {
- //if list is empty
- if(headPtr->nextPtr == nullptr)
- return false;
- //create temp node
- Node *temp = headPtr->nextPtr;
- //points to first node
- headPtr->nextPtr = temp->nextPtr;
- //delete node
- delete(temp);
- return true;
- }
- bool LinkedList ::insertBeforePosition(char value, int index)
- {
- //empty list
- if(index == 0)
- return false;
- //headptr of nextptr equals nullptr and the index >1
- if(headPtr->nextPtr == nullptr && index>1)
- return false;
- //checks to see if at front of list
- if(index == 1)
- //returns insert at front function
- return insertAtFront(value);
- //create temp node
- Node *temp = headPtr->nextPtr;
- int i=1;
- //new node
- Node *newNode = new Node;
- //fill node with data
- newNode->data = value;
- //connect new node to nullptr
- newNode->nextPtr = nullptr;
- //loop while temp node doesnt equal nullptr
- while(temp != nullptr)
- {
- //checks to see if at back of list
- if(i == index-1)
- {
- //temp node of nextptr equals nullptr
- if(temp->nextPtr == nullptr)
- //return insert at back function
- return insertAtBack(value);
- //new node connects to nextptr
- else{
- //new node of nextptr equal to temp of nextptr
- newNode->nextPtr = temp->nextPtr;
- //connect head ptr to new node
- temp->nextPtr = newNode;
- return true;
- }
- }
- //+1 to i
- i++;
- //temp connects to temp of nextptr
- temp = temp->nextPtr;
- }
- return false;
- }
- bool LinkedList::deleteAtBack()
- {
- //empty list
- if(headPtr->nextPtr == nullptr)
- return false;
- //create temp node
- Node *temp = headPtr->nextPtr;
- //if list has one node
- if(temp->nextPtr == nullptr)
- //delete at front node
- return deleteAtFront();
- //loop while nextptr doesnt point to nullptr
- while(temp->nextPtr->nextPtr!=nullptr)
- {
- //temp equals temp of nextptr
- temp = temp->nextPtr;
- }
- //create del node
- Node *del = temp->nextPtr;
- //temp of nextptr equals nullptr
- temp->nextPtr = nullptr;
- //delete del node
- delete(del);
- return true;
- }
- bool LinkedList::deletePosition(int index) {
- Node *tempNode=headPtr;
- Node *prevNode=headPtr;
- //fill the new node with data
- //loop until the position of the previous node is
- while (tempNode->nextPtr !=nullptr)
- {
- //check if the current node has the same data
- if (tempNode->data==index)
- {
- break;
- }
- prevNode=tempNode;
- tempNode=tempNode->nextPtr;
- }
- //now the new node should be added to point to the next pointer of the temp node
- //connect new node to what head pointer points to
- prevNode->nextPtr=tempNode->nextPtr;
- // connect head pointer to new node
- tempNode->nextPtr= nullptr;
- //delete tempNode
- delete tempNode;
- return true;
- }
- void LinkedList:: clear()
- {
- //headptr equals headptr of nextptr
- headPtr = headPtr->nextPtr;
- //loop while headptr doesnt equal nullptr
- while(headPtr != nullptr)
- {
- //temp node equals headptr
- Node *temp = headPtr;
- //headptr equals headptr of nextptr
- headPtr =headPtr->nextPtr;
- //delete temp node
- delete(temp);
- }
- }
- ostream& operator << ( ostream& out, LinkedList &list)
- {
- Node *ptr=list.headPtr->nextPtr;
- cout <<"headPtr->";
- while(ptr !=nullptr)
- {
- //ptr of nextptr equals nullptr
- if(ptr->nextPtr==nullptr)
- out <<ptr->data<<"->nullptr"<<endl;
- else
- out<<ptr->data<<"->";
- ptr = ptr->nextPtr;
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement