📄 llist.c
字号:
/*Walter FontanillaCompiler: g++Purpose: To be able to implement a linked structure to perform as a queue.*///including the header file llist.h#include "llist.h"#include <iostream>using namespace std;//Purpose: Constructor to initialize front rear and count.llist::llist(){ //Front Rear and Count equaled to zero. Front = 0; Rear = 0; Count = 0;}//Purpose: Deconstructor while the queue is not empty calls deletefrontllist::~llist(){ //variable int called deleteMSG, used later. int deleteMSG; //Delete the Front node while not isEmpty while(!isEmpty()) deleteFront(deleteMSG);}//Name: addRear//Purpose: Adds a new node at the rear and puts NewNum in the Elem field// of this new node. Count is updated.//How to Call: No parameters required, passed by value.void llist::addRear(el_t NewNum){ //Node called current. Node* curr; // if Front is equal to NULL, then add will occur and increment with Count // else it will go to the Rear. if (Front == NULL) { curr = new Node; Front = curr; Front -> Elem = NewNum; Rear = Front; } else { Rear -> Next = new Node; Rear = Rear -> Next; Rear -> Elem = NewNum; Rear -> Next = NULL; } //at the end of the if else statement, count wil incremented. Count++;}//Name: deleteFront//Purpose: deletes front node. if the Queue is empty, call private QueueError// ("Error: list is empty.")//How to Call: If the list is empty, both Front and Rear become NULL.void llist::deleteFront(el_t& OldNum){ //Node called Second, will be used in the if/else statement. Node* Second; //QueueError will display "The list is empty" if isEmpty is empty // else will decrement the count. if (isEmpty()) { ListError("The list is empty"); } else { OldNum = Front -> Elem; Second = Front -> Next; delete Front; Front = Second; Count--; if (Front == NULL) { Rear = Front; } }}//Name: isEmpty//Purpose: To check and see if the lqueue is empty.//How to call: No parameters needed.bool llist::isEmpty(){ //Easy bool statement,return true or false. if (Front == NULL && Rear == NULL) { return true; } else { return false; }}//Name: displayAll//Parameters: No parameters needed.//Purpose: To display all elements in the list.void llist::displayAll(){ //Node called slot used as a pointer. Node* slot; // display [ empty ] if isEmpty is, basically empty. if(isEmpty()) { cout << "[ empty ] " << endl; //Display message } else { cout << "[ " ; slot = Front; // slot gets Front address //For statment, used just like a while //This will display the element and move the to the next slot. // at the end will increment and end with a display ] for(int i=1; i <= Count; i++) { cout << slot -> Elem<< " "; //Displays slot elem slot = slot ->Next; // Moves to next code } cout << "]" << endl; } }//Name: QueueError//Purpose: To display a message and exit.//Parameters: None.void llist::ListError(char* Mesg){ cout << Mesg << endl; //Just displays a message. exit(1); //Closes program.}//=============================================================================================================//Name: addFront//Purpose: Will add a new node to the front of the list.//Parameters: el_t NewNumvoid llist::addFront(el_t NewNum){ Node* P; Node* Q; if(isEmpty()) { P = new Node; Front = P; Front->Elem = NewNum; Front-> Next = NULL; Rear = Front; } else { Q = Front; P = new Node; Front = P; Front -> Elem = NewNum; Front -> Next = Q; } Count++;}//Name: deleteRear(el_t& OldNum)//Purpose: This will delete a node from the rear of the list.//Parameters:el_t& OldNumvoid llist::deleteRear(el_t& OldNum){ Node* P; //Node pointer P Node* Q; if (isEmpty()) { ListError("Error: list is empty."); //Calls ListError } if(Count ==1) { deleteFront(OldNum);//Calls deleteFront and passes OldNum } else { P=Front; //P becomes Front Q=Front; //Q becomes Front while(Q->Next !=NULL) { P = Q; Q = Q -> Next; } OldNum = Q->Elem; // Old Num gets Front Elem delete Q; Rear = P; Rear->Next = NULL; Count--; if(isEmpty()) //If Front is NULL { Rear = Front; //Rear will become front which is NULL } }}//Name: deleteIth//Purpose: will delete the Ith node. count is updated.//Parameters: int I, el_t& OldNumvoid llist::deleteIth(int I, el_t& OldNum){ Node* P; //pointer P Node* Q; //pointer Q P = Front; //P will be the same as Front. Q = Front; //Q will be the same as Front. if (I > Count || I < 1) { ListError("Error: list is empty."); //Calls ListError } if(I == 1) { deleteFront(OldNum); //Calls deleteFront } //==========================REVISED=========================== if(I == Count) { deleteRear(OldNum); //Deletes rear. } else { for(int y =1; y< I-1; y++) { P = P->Next; //P will become the P->Next node. } Q = P->Next; OldNum = Q->Elem; P->Next = Q->Next; delete Q; Count--; }}//Name: addbeforeith//Purpose: will add right before the Ith node.//Paramemter: int I, el_t newNumvoid llist::addbeforeIth(int I, el_t newNum){ Node* P; Node* Q; Node* R; P = Front; Q = Front; //==================REVISED================= if(I > Count + 1 || I < 1) { cout << Count << endl; ListError("Error: list is empty."); // Calls addFront which passes newnum } if(I == 1) { addFront(newNum); } if(I == Count + 1) { addRear(newNum); } else { for(int y = 1; y< I-1; y++) { P = P->Next; } Q = P->Next; R = new Node; R->Elem = newNum; R->Next = Q; P->Next = R; Count++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -