Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
2
Indexable
Never
#include <iostream>
#include "Data.h"
using namespace std;
MovieList::MovieList()
{
	head = NULL;
}

MovieList::~MovieList()
{
	MovieNode *nodePtr, *nextNode;

	nodePtr = head;
	while (nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;
	}
	cout << "Thank you for using our rental service!" << endl;
}

//RETURN A MOVIE
void MovieList::appendNode(Movie mov)
{
	MovieNode *newNode, *nodePtr;
	// Allocate a new node & store num
	newNode = new MovieNode;
	newNode->movie = mov;
	newNode->next = NULL;
	// If there are no nodes in the list
	// make newNode the first node

    if (!head)
		  head = newNode;
	  else	// Otherwise, insert newNode at end
	  {
		  // Initialize nodePtr to head of list
		  nodePtr = head;
		  // Find the last node in the list
		  while (nodePtr->next)
			nodePtr = nodePtr->next;
	    // Insert newNode as the last node
		  nodePtr->next = newNode;
	  }
	  cout << endl << "You have successfully returned a movie!" << endl;
}


//DISPLAY THE LIST
void MovieList::displayList()
{
	MovieNode *nodePtr;
	if (head == NULL)
		cout << "You haven't inserted any movies yet!" << endl;
	else
	{
		cout << "Here are your inserted movies... " << endl<<endl;
		nodePtr = head;
		while (nodePtr)
		{
			cout << "Movie Code: " << nodePtr->movie.code << endl;
			cout << "Movie Name: " << nodePtr->movie.name << endl;
			nodePtr = nodePtr->next;
			cout<<endl;
		}
	}
	system("Sleep 1");
}

//INSERT A NODE
void MovieList::insertNode(Movie mov)
{
	MovieNode *newNode, *nodePtr, *previousNode;

	// Allocate a new node & store Num
	newNode = new MovieNode;
	newNode->movie = mov;

	// If there are no nodes in the list
	// make newNode the first node
	if (!head)
	{
		head = newNode;
		newNode->next = NULL;
	}
	else	// Otherwise, insert newNode.
	{
		// Initialize nodePtr to head of list
		nodePtr = head;
		previousNode = NULL;

		// Skip all nodes whose value member is less
		// than num.
		//while (nodePtr != NULL && nodePtr->movie < mov)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}
		// If the new mode is to be the 1st in the list,
			// insert it before all other nodes.
		if (previousNode == NULL)
		{
			head = newNode;
			newNode->next = nodePtr;
		}
		else
		{
			previousNode->next = newNode;
			newNode->next = nodePtr;
		}
	}
	cout << endl << "You have successfully inserted a Movie!" << endl;
}





//SHOW DETAILS
void MovieList::showDetails(Movie mov)
{
	MovieNode *nodePtr;
	if(head == NULL)
		cout<<"You have not inserted any movies yet!" <<endl;
	else
	{
		cout<<"Movie Details: "<<endl<<endl;
		nodePtr = head;
		while(nodePtr)
		{
			cout<<"Movie Code: "<< nodePtr->movie.code <<endl;
			cout<<"Movie Name: "<< nodePtr->movie.name <<endl;
			cout<<"Year Released: "<< nodePtr->movie.year <<endl;
			cout<<"Genre: "<< nodePtr->movie.genre <<endl;
			nodePtr = nodePtr->next;
			cout<<endl;
		}
	}
	system("Sleep 4");
}




//DELETE A NODE
void MovieList::deleteNode(Movie mov)
{
	MovieNode *nodePtr, *previousNode;
	int found = 0;

	// If the list is empty, do nothing.
	if (!head)
	{
		cout << "There is no movie inserted." << endl;
		return;
	}

	// Determine if the first node is the one.
	if (head->movie.code == mov.code)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
		cout << "You have successfully rented a movie!" << endl;
		found = 1;
	}
	else
	{
		// Initialize nodePtr to head of list
		nodePtr = head;
		previousNode = NULL;

		// Skip all nodes whose value member is 
		// not equal to num.
		while (nodePtr != NULL && nodePtr->movie.code != mov.code)
		{
				previousNode = nodePtr;
				nodePtr = nodePtr->next;
		}

		// Link the previous node to the node after
		// nodePtr, then delete nodePtr.
		if (nodePtr != NULL)
		 {
			previousNode->next = nodePtr->next;
			delete nodePtr;
			cout << "You have successfully rented a Movie!" << endl;
			found = 1;
		}
	}
	if (found == 0)
		cout << "You haven't inserted a movie yet!" << endl;
}