//************************************************************************************* void SinglyLinkedList::printList() { if(m_debug == true) { cout<<"In function isListEmpty"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(isCircleExist()) { printCircle(); return; } Node* temp = m_head; int index = 1; cout<<endl; while(temp != NULL) { cout<<"|"; cout<< temp->data; cout<<"|"; if(temp->link != NULL) { cout<<" --> "; } index++; temp = temp->link; } cout<<endl; }
//************************************************************************************* int SinglyLinkedList::deletingAtGivenPosition(int position) { if(m_debug == true) { cout<<"In function deletingAtGivenPosition, with position "<<position<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(m_length < position) { //list is not that big } Node* atPositionPtr = m_head; Node* oneBeforeAtPositionPtr = m_head; atPositionPtr = atPositionPtr->link; for(int index = 1 ; index < (position - 1); index++) { oneBeforeAtPositionPtr = oneBeforeAtPositionPtr->link; atPositionPtr = atPositionPtr->link; } oneBeforeAtPositionPtr->link = atPositionPtr->link; int returnValue = atPositionPtr->data; delete atPositionPtr; return returnValue; }
//************************************************************************************* void SinglyLinkedList::printCircle() // circle { if(m_debug == true) { cout<<"In function isListEmpty"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; int index = 0; while(tempNode != NULL) { cout<<"|"; cout<< tempNode->data; cout<<"|"; if(tempNode->link != NULL) { cout<<" --> "; } index++; tempNode = tempNode->link; if(index > MAXLOOPINGLIMIT) { break; } } cout<<endl; }
//************************************************************************************* int SinglyLinkedList::deletingAtend() { if(m_debug == true) { cout<<"In function deletingAtend"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* lastNode = m_head; Node* secondLastNode = m_head; //increment last node before going to loop lastNode = lastNode->link; while(lastNode->link != NULL) { lastNode = lastNode->link; secondLastNode = secondLastNode->link; } int returnValue = lastNode->data; delete lastNode; secondLastNode->link = NULL; return returnValue; }
int removeFirstElement (LIST l){ unsigned int estadoLocal=0; nodo* tempNodo; estadoLocal=isListEmpty(l); if(estadoLocal){ if((l->nodeCount)>1){ tempNodo=l->firstNode; //en temp nodo almaceno el puntero al primer nodo l->firstNode=l->firstNode->next; //pongo omo nuevo primero, al segundo --(l->nodeCount); free(tempNodo->elemento); //libero los espacios asignados free(tempNodo); return 0; } else { tempNodo=l->firstNode; //como esta solo el primero tengo que eliminarlo y poner en null a l l->firstNode=NULL; l->lastNode=NULL; l->nodeCount=0; free(tempNodo->elemento); free(tempNodo); return 0; } }else {return ERROR;} //la lista estaba vacia }
//************************************************************************************* OddEven SinglyLinkedList::lengthOfTheListInOddEven() { if(m_debug == true) { cout<<"In function lengthOfTheListInOddEven"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; while(tempNode != NULL) { if(tempNode->link == NULL) { break; } tempNode = tempNode->link; tempNode = tempNode->link; } if(tempNode == NULL) { return EVEN; } else { return ODD; } }
void LinkedList<NodeType>::removeFromBack( NodeType & dataRemoved) { if( isListEmpty()) { cout << "List is empty." << endl; return; } if( firstNode == lastNode) { ListNode<NodeType>* currentNode = firstNode; firstNode = NULL; lastNode = NULL; dataRemoved = currentNode -> data; delete currentNode; nodeCount--; return; } ListNode<NodeType>* currentNode = firstNode; while( currentNode -> nextNodePtr != lastNode) currentNode = currentNode -> nextNodePtr; lastNode = currentNode; currentNode = currentNode -> nextNodePtr; lastNode -> nextNodePtr = NULL; dataRemoved = currentNode -> data; delete currentNode; nodeCount--; return; }
int deleteNode(LINKED_LIST head, LINKED_LIST nodeToDelete) { if(isListEmpty(head)) return -1; else { LINKED_LIST tempNode=getNode(); int deletedKey; tempNode=head; while(tempNode != NULL && tempNode->next!=nodeToDelete) { tempNode=tempNode->next; } if(tempNode==NULL) { printf("\nERROR3: Node not found\n"); return -1; } deletedKey=tempNode->next->key; if(nodeToDelete->next!=NULL) tempNode->next=nodeToDelete->next; else tempNode->next=NULL; free(nodeToDelete); nodeToDelete=NULL; return deletedKey; } }
//************************************************************************************* void SinglyLinkedList::insertNodeInSortList(int data) { if(m_debug == true) { cout<<"In function insertNodeInSortList"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* slowPtr = m_head; Node* fastPtr = m_head; fastPtr = fastPtr->link; while(fastPtr != NULL) { if(fastPtr->data < data) { fastPtr = fastPtr->link; slowPtr = slowPtr->link; } else { break; } } Node* tempNode = new Node(data); tempNode->link = slowPtr->link; slowPtr->link = tempNode; }
void SinglyLinkedList::reverseListInPair() { if(m_debug == true) { cout<<"In function reverseListInPair"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; while(tempNode != NULL) { if(tempNode->link != NULL) { swap(&(tempNode->data), &((tempNode->link)->data)); } else { return; } tempNode = tempNode->link; if(tempNode->link == NULL) { return; } tempNode = tempNode->link; } }
//************************************************************************************* void SinglyLinkedList::createCircle() { if(m_debug == true) { cout<<"In function createCircle"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* temp = m_head; Node* randomNode; int random = m_length/2; //random int index = 0; while(temp->link != NULL) { if(random == index) { randomNode = temp; } temp = temp->link; index++; } temp->link = randomNode; m_isCircleCreated = true; }
//************************************************************************************* int SinglyLinkedList::middleNode() { if(m_debug == true) { cout<<"In function middleNode"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* slowPtr = m_head; Node* fastPtr = m_head; while(fastPtr->link != NULL) { slowPtr = slowPtr->link; fastPtr = fastPtr->link; if(fastPtr->link == NULL) { break; } fastPtr = fastPtr->link; } return slowPtr->data; }
/***************************************************************************** * * This routine removes a XiaDefaults entry * *****************************************************************************/ HANDEL_SHARED int HANDEL_API xiaRemoveDefault(char *alias) { int status = XIA_SUCCESS; XiaDefaults *prev = NULL; XiaDefaults *current = NULL; XiaDefaults *next = NULL; sprintf(info_string, "Preparing to remove default w/ alias %s", alias); xiaLogDebug("xiaRemoveDefault", info_string); if (isListEmpty(xiaDefaultsHead)) { status = XIA_NO_ALIAS; sprintf(info_string, "Alias %s does not exist", alias); xiaLogError("xiaRemoveDefault", info_string, status); return status; } /* First check if this alias exists already? */ prev = NULL; current = xiaDefaultsHead; next = current->next; while (next != NULL) { if (STREQ(alias, current->alias)) { break; } /* Move to the next element */ prev = current; current = next; next = current->next; } /* Check if we found nothing */ if ((next == NULL) && (!STREQ(current->alias, alias))) { status = XIA_NO_ALIAS; sprintf(info_string,"Alias %s does not exist.", alias); xiaLogError("xiaRemoveDefault", info_string, status); return status; } /* Check if match is the head of the list */ if (current == xiaDefaultsHead) { xiaDefaultsHead = next; } else { prev->next = next; } /* Free up the memory associated with this element */ xiaFreeXiaDefaults(current); return status; }
/* My if len is odd go till middle of the list. else go till len/2 put second half of the list into stack pop out stack element and compare with the first half of the list. */ bool SinglyLinkedList::isListPalindrome() { if(m_debug == true) { cout<<"In function getData"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(m_length == 1) { return true; } Node* fastPtr = m_head; Node* slowPtr = m_head; if(m_length%2 == 0) { int temp = m_length/2; for(int index = 1; index <= temp; index++) { slowPtr = slowPtr->link; } } else { while(fastPtr->link != NULL) { fastPtr = fastPtr->link; if(fastPtr->link == NULL) { break; } fastPtr = fastPtr->link; slowPtr = slowPtr->link; } } //put second half of the list into stack stack<int> tempStack; while(slowPtr != NULL) { tempStack.push(slowPtr->data); slowPtr = slowPtr->link; } //compare data Node* tempNode = m_head; for(int index = 1; index <= tempStack.size(); index++) { if(tempNode->data != tempStack.pop()) { return false; } tempNode = tempNode->link; } return true; }
/* We can do it with the help of hash table. Keep inserting the data into the hash table, the place where you find it is a duplicate just delete that node from the list. */ void SinglyLinkedList::removeDuplicates() { if(m_debug == true) { cout<<"In function removeDuplicates"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } }
void* getFirstElement (LIST l){ unsigned int estado=0; estado=isListEmpty(l); if(estado){ return (l->firstNode->elemento); } else {return NULL;} }
void emptyList(List *list) { if (!isListEmpty(list)) { Node *node = list->first; while (node != NULL) { list->first = node->next; free(node->data); free(node); node = list->first; } } }
//************************************************************************************* void SinglyLinkedList::reverseListWithoutModification() { if(m_debug == true) { cout<<"In function isListEmpty"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } reverseListWithoutModification(m_head); }
void LinkedList<NodeType>::retrieveFromBack( NodeType & retrievedData) { if( isListEmpty()) { cout << "List is empty." << endl; return; } retrievedData = lastNode -> data; return; }
int testMoveNodes(List * one, List * two){ int lengthBeforeMove = listLength(one); moveBetweenLists(one, two); if (isListEmpty(one) != 1 || listLength(two) != lengthBeforeMove){ return 0; } else { return 1; } }
void printAll(SingleLinkedList L){ if (isListEmpty(L)){ cout<<"List kosong"<<endl; } else{ address P = first(L); while(P!=Nil) { cout <<"ID="<<info(P).id<<"Nama="<<info(P).nama<<", Departemen="<<info(P).departemen<<", Gedung="<<info(P).gedung<<endl; P = next(P); } } getch(); }
//************************************************************************************* void SinglyLinkedList::reverseListUsingRecursion() { if(m_debug == true) { cout<<"In function reverseListUsingRecursion"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } m_head = reverseListUsingRecursion(m_head); }
// 获取索引为index的元素,从0开始 ElemType& SqLinearList::getListByIndex(int index) { if (isListEmpty()) { std::cout << "list is empty" << std::endl; return _list->element[0]; // 空情况默认返回第一个元素element[0], 默认值为0 } if (index > _list->size-1) { std::cout << "index is out of range[0," << _list->size << "] " << std::endl; return _list->element[0]; // 默认返回第一个元素element[0], 默认值为0 } return _list->element[index]; }
LINKED_LIST findLastNode(LINKED_LIST head) { if(isListEmpty(head)) return NULL; else { LINKED_LIST tempNode=getNode(); tempNode=head; while(tempNode != NULL && tempNode->next!=NULL) { tempNode=tempNode->next; } return tempNode; } }
void *removeHead(LinkList *list) { void *removedValue; if(!isListEmpty(list)) { removedValue = list->head->data; list->head = list->head->next; if(list->head == NULL) list->tail = NULL; list->ListSize--; } return removedValue; }
LINKED_LIST listSearch(LINKED_LIST head, unsigned int element) { if(isListEmpty(head)) return NULL; else { LINKED_LIST tempNode=getNode(); tempNode=head->next; while(tempNode != NULL && tempNode->key!=element) { tempNode=tempNode->next; } return tempNode; } }
void SqLinearList::traverseList(void) { if (isListEmpty()) { std::cout << "list is empty" << std::endl; return; } std::cout << "_________begin traverse____________" << std::endl; for (int i= 0; i < _list->size; i++) { std::cout << "list[" << i << "]: " << _list->element[i] << " "; } std::cout << std::endl; std::cout << "_________end traverse____________" << std::endl; }
void LinkedList<NodeType>::insertAtFront( const NodeType & insertionData) { if( isListEmpty()) { firstNode = createNewNode( insertionData); lastNode = firstNode; nodeCount++; return; } ListNode<NodeType>* newNode = createNewNode( insertionData); newNode -> nextNodePtr = firstNode; firstNode = newNode; nodeCount++; return; }
void deleteNode(int deleteValue, Node* header){ if (header == NULL || isListEmpty(header)) { printf("Error deleteNode list is NULL"); return; } Node* tmp = header; while (tmp->next != NULL) { if (tmp->next->data == deleteValue) { Node* deletedNode = tmp->next; tmp->next = tmp->next->next; free(deletedNode); return; } tmp = tmp->next; } }
void LinkedList<NodeType>::printList() { if( isListEmpty()) { cout << "List is empty." << endl; return; } ListNode<NodeType>* currentNode = firstNode; while( currentNode != NULL) { cout << currentNode -> data << endl; currentNode = currentNode -> nextNodePtr; } return; }