Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class ChainNode{
- friend class Chain;
- public:
- ChainNode(int element, ChainNode* next);
- private:
- int data;
- ChainNode* link;
- };
- class Chain{
- public:
- Chain(): first(NULL){} //empty constructor
- void insertAtBack(const int& value);
- void insertAtFront(const int& value);
- void deletefromBack();
- void deletefromFront();
- void print();
- ~Chain();
- private:
- ChainNode* first;
- };
- int main(){
- Chain myChain;
- myChain.insertAtBack(50);
- myChain.insertAtBack(60);
- myChain.insertAtFront(10);
- myChain.insertAtFront(20);
- myChain.insertAtFront(30);
- myChain.print();
- myChain.deletefromBack();
- myChain.deletefromBack();
- myChain.deletefromBack();
- myChain.deletefromBack();
- myChain.deletefromBack();
- myChain.deletefromBack();
- myChain.print();
- myChain.insertAtBack(50);
- myChain.insertAtBack(60);
- myChain.insertAtFront(10);
- myChain.insertAtFront(20);
- myChain.insertAtFront(30);
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- myChain.deletefromFront();
- myChain.print();
- }
- ChainNode::ChainNode(int element, ChainNode* next): data(0), link(NULL)
- {
- data = element;
- link = next;
- }
- void Chain::insertAtBack(const int& value){
- ChainNode* tmpPtr = new ChainNode(value,0);
- if(first){
- ChainNode* last = first;
- while(last->link != NULL)
- last = last->link;
- last->link = tmpPtr;
- }else{
- first = tmpPtr;
- }
- }
- void Chain::insertAtFront(const int& value){
- ChainNode* tmpPtr = new ChainNode(value,0);
- if(first){
- tmpPtr->link = first;
- first = tmpPtr;
- }else{
- first = tmpPtr;
- }
- }
- void Chain::deletefromBack(){
- int value;
- // get the mry location of the last node
- if (first){
- ChainNode* last = first;
- while(last->link != NULL){
- last = last->link;
- }// end while
- if(first == last){
- value = last->data;
- delete last;
- first = NULL;
- cout << value << " is removed" << " and nothing remained"<< endl;
- }else{
- ChainNode* previous = first;
- while(previous->link != last){
- previous = previous->link;
- }
- previous->link = NULL;
- value = last->data;
- delete last;
- cout << value << " is removed" << endl;
- }// end inner if
- }else{
- cout << "remove unsuccessful: no nodes remained" << endl;
- }// end if
- }
- void Chain::deletefromFront(){
- int value;
- // get the mry location of the last node
- if(first){
- ChainNode* tmpPtr = first;
- first = first->link;
- value = tmpPtr->data;
- delete tmpPtr;
- cout << value << " is removed" << endl;
- }else{
- cout << "remove unsuccessful: no nodes remained" << endl;
- }
- }
- void Chain::print(){
- if(first != NULL){
- ChainNode* tmpPtr = first;
- while(tmpPtr != NULL){
- cout << tmpPtr->data << " ";
- tmpPtr = tmpPtr->link;
- }// end while
- cout << endl;
- }else{
- cout << "nothing" << endl;
- }// end if
- }
- Chain::~Chain(){
- ChainNode* tmpPtr = first;
- while(tmpPtr != NULL){
- first = first->link;
- delete tmpPtr;
- tmpPtr = first;
- }// end while
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement