📄 glist1.cpp
字号:
#include <iostream.h>#include "glist1.h"/* * This is the program for keeping words or datas * with big amount of similar keys. NAMED * GENERAL LIST * Nodes with two link * 1) Sibling ( like brother ) * 2) Child * *//*----------------------------------------------*/GList::GList(){ _DUMMY = new _NODE;}/*----------------------------------------------*/GList::~GList(){ delTree( _DUMMY ); delete _DUMMY; cout << "The tree is deleted..." << endl;}/*----------------------------------------------*//* * This is a function useful for adding one node * in one row * if The node with this data is existed before * then return NULL*/_NODE* GList::returnLastSibling( _NODE* myNode, char myData ){ _NODE* temp = myNode; _NODE* previousNode = NULL;// cout << "returnLastSibling : " ; while( temp != NULL ) { if( temp->data == myData ) { return temp; } // cout << temp->data; previousNode = temp; temp = temp->sibling; } // cout << endl; return previousNode;}/*----------------------------------------------*//* * This is a function for returning one node we * want to find a state...... */_NODE* GList::returnNextState( char firstData, char secondData, char thirdData ){ _NODE* temp1 = NULL; // First row of LIST checking if( _DUMMY->child == NULL ) return NULL; temp1 = findMyNodeInRow( _DUMMY->child, firstData ); // Second row of LIST checking if( temp1 != NULL && temp1->data != firstData ) return NULL; temp1 = findMyNodeInRow( temp1->child, secondData );// Third row of LIST checking if( temp1 != NULL && temp1->data != secondData ) return NULL; temp1 = findMyNodeInRow( temp1->child, thirdData ); if( temp1 != NULL && temp1->data != thirdData ) return NULL; if( temp1 != NULL ) return temp1->child; return NULL;}/*----------------------------------------------*//* * This function is for returning the node with * expected data and if there is no node in this * row with expected data it returns last sibling node */_NODE* GList::findMyNodeInRow( _NODE* myNode, char myData ){ _NODE* temp = NULL; _NODE* temp1 = NULL; temp = myNode; while( temp != NULL ) { if( temp->data == myData ) { return temp; } temp1 = temp; temp = temp->sibling; } return temp1;}/*----------------------------------------------*//* * This is a function for adding one state in * General List*/_NODE* GList::addOneState( char* myData ){ _NODE* tillNow = NULL; if( _DUMMY->child == NULL ) // If list is empty { tillNow = new _NODE; _DUMMY->child = tillNow; tillNow->data = myData[0]; } else // If the list is not empty and search for expected node { tillNow = findMyNodeInRow( _DUMMY->child, myData[0] ); if( tillNow->data != myData[0] ) // If node with myData[0] data previously was not existed { tillNow->sibling = new _NODE; tillNow = tillNow->sibling; tillNow->data = myData[0]; } } // end of first character checking // The variable tillNow is a pointer to expected node till now _NODE* temp = NULL; temp = findMyNodeInRow( tillNow->child, myData[1] ); if( temp == NULL ) // If my node has no child to search its data whether or not it was created before { temp = new _NODE; temp->data = myData[1]; tillNow->child = temp; tillNow = temp; } else if( temp->data != myData[1] && temp->sibling == NULL ) { temp->sibling = new _NODE; tillNow = temp->sibling; tillNow->data = myData[1]; } else if( temp->data != myData[1] && temp->sibling != NULL ) return NULL; // End of second character checking if( tillNow->child == NULL ) { temp = new _NODE; temp->data = myData[2]; tillNow->child = temp; tillNow = tillNow->child; } else { temp = findMyNodeInRow( tillNow->child, myData[2] ); if( temp->data != myData[2] ) { temp->child = new _NODE; temp->child->data = myData[2]; } tillNow = temp; }// Search completed// Starting add new answer for this state if( tillNow->child != NULL ) { temp = findMyNodeInRow( tillNow->child, myData[3]); if( temp->data != myData[3] ) { temp->sibling = new _NODE; temp = temp->sibling; temp->data = myData[3]; tillNow = temp; } } else { tillNow->child = new _NODE; tillNow = tillNow->child; tillNow->data = myData[3]; } int i = 4; while( myData[i] != NULL ) { temp = new _NODE; temp->data = myData[i]; tillNow->child = temp; tillNow = tillNow->child; i++; } return tillNow;}/*----------------------------------------------*//* * This function is useful for addOneNote function * which get two parameter , first a pointer to one * Node( One Node should be added to this node as a child)*//*----------------------------------------------*//* * This is a function for deleting whole list * rather than _DUMMY node*/void GList::delTree( _NODE* myNode ){ if( myNode->sibling != NULL ) { delTree( myNode->sibling ); } if( myNode->child != NULL ) { delTree( myNode->child ); } delete myNode;}/*----------------------------------------------*//* * This is a function for add one element to the * child of one node which passed to this function * */_NODE* GList::addToChild( _NODE* myNode, char myData ){ _NODE* newNode = NULL; if( myNode != NULL && myNode->child != NULL ) /* Checking whether or not the node is valid*/ { _NODE* temp = NULL; temp = returnLastSibling( myNode->child, myData ); if( temp != NULL ) { newNode = new _NODE; newNode->data = myData; temp->sibling = newNode; } } return newNode; }/*0000000000000000000000000000000000000000000*/_NODE* GList::dummyNode(){ return _DUMMY;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -