void SLList::Insert(int contents) { if(head_ == NULL) { InsertHead(contents); } else if(contents < GetHead()) { InsertHead(contents); } else if (head_->next_node() == NULL && head_ != NULL) { InsertTail(contents); } else if(contents > GetTail()) { InsertTail(contents); } else { SLNode* node = new SLNode(contents); SLNode* i = head_; SLNode* j = NULL; while(i->contents() <= contents && i->next_node() != NULL) { j = i; i = i->next_node(); } j->set_next_node(node); node->set_next_node(i); size_++; } }
void DLList::Insert(int new_node) { DLNode *temp = new DLNode(new_node); //creates temp node and makes it point at the new_node information DLNode *iterator = head_; //creates an iterator and makes it point to head_ DLNode *iterator2 = tail_; //second iterator to trail behind the first iterator unsigned int original_size = size_; if(iterator == NULL) //checks if the list is empty { InsertHead(new_node); } while(iterator != NULL && original_size == size_) { if((iterator -> contents()) >= (temp -> contents())) //checks if the iterator is larger than or equal to the new node { if(iterator2 == NULL) { InsertHead(new_node); } else { iterator2 -> set_next_node(temp); //makes the previous node point at the new node temp -> set_next_node(iterator); //makes the new node point at the next node iterator -> set_previous_node(temp); //makes the next node point at the new node temp -> set_previous_node(iterator2); //makes the new node point the previous node size_++; } } else if((iterator -> contents()) < (temp -> contents())) //checks if the iterator is smaller than the new node { if((iterator -> next_node()) == NULL) { InsertTail(new_node); } else { iterator2 = iterator; iterator = iterator -> next_node(); } } } }
int main() { int i; Node *head = NULL; Node *list1 = NULL; for(i = 0; i < 5; i++) { head = InsertHead(head, i); } Print(head); Node *list2 = NULL; list1 = InsertHead(list1, 6); list1 = InsertHead(list1, 5); list1 = InsertHead(list1, 5); list1 = InsertHead(list1, 1); list2 = InsertHead(list2, 7); list2 = InsertHead(list2, 4); list2 = InsertHead(list2, 2); Print(list1); Print(list2); /* Node *newList = MergeLists(list1, list2); */ /* Print(newList); */ Node *newList = RemoveDuplicates(list1); Print(newList); return 0; }
void SLList::Insert(int contents) { //Checks if head is null or contents is < head_->contents(); if (head_ == NULL || head_->contents() > contents) { InsertHead(contents); } else if ( tail_->contents() < contents) { // cout << " got it: " << contents << endl; // cout << ToString(); InsertTail(contents); } else { //creates iterator and newnode SLNode* iterator; iterator = head_; while (iterator->next_node() != NULL && contents > iterator->next_node()->contents()) { iterator = iterator->next_node(); } if (iterator != tail_) { SLNode *newNode = new SLNode(contents); newNode->set_next_node(iterator->next_node()); iterator->set_next_node(newNode); size_ +=1; } else { InsertTail(contents); } } }
void *addItemToList(tList *pList, void *pItem, int(*fcmp)(void *pItList, void *pItNew)) { if(pItem) { if(pList->phead) { pList->pcurr=pList->phead; while(pList->pcurr) { if(fcmp(pList->pcurr->pdata, pItem) > 0) { InsertBefore(pList, pItem); break; } else if(pList->pcurr == pList->ptail) { InsertTail(pList, pItem); break; } GetNext(pList); } } else InsertHead(pList, pItem); } return 0; }
void SLList::Insert(int data) { SLNode* insert = new SLNode(data); //assigns data to new node SLNode* current = head_; //assigns head to current SLNode* prev = current; if(!head_ || data <= current->contents()) //if there is a list or data is less than or equal to head_ { InsertHead(data); } else if(data > tail_->contents()) //if data is greater than tail { InsertTail(data); } else { while(current->contents() < data) //while current is less than data { prev = current; //we assign prev to currents position current = current->next_node(); //then assign current to next position } insert->set_next_node(current); //set node at currents position prev->set_next_node(insert); //set data at proper position size_++; } }
//检查注册 int check_reg(Cache *cache,Parket *ptr){ if(ptr == NULL || cache == NULL){ return -1; } Daily *newDaily=(Daily *)calloc(1,sizeof(Daily)); Node *newNode=(Node *)calloc(1,sizeof(Node)); if(newNode==NULL){ exit(0); } Node *cur=head->next; while(cur!=head){ if(strcmp(cur->name,ptr->name)==0){ strcpy(ptr->usr_return,"exist"); write(cache->fd,ptr,sizeof(Parket)); return 1; } cur=cur->next; } strcpy(newNode->name,ptr->name); strcpy(newNode->pwd,ptr->pwd); newNode->state=0; strcpy(newDaily->name,ptr->name); strcpy(newDaily->client_ip,cache->clientip); strcpy(newDaily->cmd,"注册成功!"); strcpy(ptr->usr_return,"yes"); write(cache->fd,ptr,sizeof(Parket)); InsertdHead(newDaily); InsertHead(newNode); return 0; }
void SLList::InsertTail(int newTail) { if(head_ == NULL) { InsertHead(newTail); } else { SLNode* i = head_; while (i->next_node() != NULL) { i = i->next_node(); } SLNode* node = new SLNode(newTail); i->set_next_node(node); size_++; } }
/** * creates a new dynamic SLNode with the contents of * the contents and attaches as the new tail of the list * @param int contents */ void SLList::InsertTail(int contents) { if (head_ != NULL) { SLNode* temp, *newNode = new SLNode(contents); temp = head_; while (temp->next_node() != NULL) { temp = temp->next_node(); } temp->set_next_node(newNode); tail_ = newNode; size_ += 1; } else { InsertHead(contents); } }
int main(){ int i; node *root = (node*) malloc( sizeof(node) ); root->value = 2; root->next = NULL; DisplayList(root); for(i = 3; i < 8; i++) InsertTail(&root, i); for(i = 1; i >= 0; i--) InsertHead(&root, i); DisplayList(root); ClearList(&root); DisplayList(root); return 0; }