#include #include #include #include #include "link1.h" // This is the first attempt // to code up an object representing // a job in JSSP //---------------------------------------------------------------------- // 10 useful functions with linked lists //---------------------------------------------------------------------- // FUNCTION 1 - Count the number of nodes in the list size_t list_length(Node* head_ptr) { Node *cursor; size_t answer; answer = 0; for (cursor = head_ptr; cursor !=NULL; cursor = cursor->link) answer++; return answer; } // FUNCTION 2 - Insert the head void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr; insert_ptr = new Node; insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; } // FUNCTION 3 - Insert a node void list_insert(Node* previous_ptr, const Node::Item& entry) { Node *insert_ptr; insert_ptr = new Node; insert_ptr->data = entry; insert_ptr->link = previous_ptr->link; previous_ptr->link = insert_ptr; } // FUNCTION 4 - Searching a node having specified target data Node* list_search(Node* head_ptr, const Node::Item& target) { Node *cursor; for (cursor = head_ptr; cursor !=NULL; cursor = cursor->link) if (target==cursor->data) return cursor; return NULL; } // ADDITIONAL FUNCTION - returns the previous element to the one // whose data is looked for! Node* list_prev_locate(Node* head_ptr, const Node::Item& target) { Node *cursor = head_ptr; while (cursor->link!=NULL) { if (target==cursor->link->data) return cursor; cursor = cursor->link; } return NULL; } Node* list_prev_search(Node* head_ptr, Node* target) { Node *cursor = head_ptr; while (cursor->link!=NULL) { if (target==cursor->link) return cursor; cursor = cursor->link; } return NULL; } Node* list_tail_search(Node* head_ptr) { Node *cursor = head_ptr; while (cursor->link!=NULL) { cursor = cursor->link; } return cursor; } // FUNCTION 5 - Find a node of a specified position; returns a pointer to it Node* list_locate(Node* head_ptr, size_t position) { Node *cursor; size_t i; assert(0link; return cursor; } // FUNCTION 6 - copies a linked list void list_copy(Node* source_ptr, Node*& head_ptr, Node*& tail_ptr) { head_ptr = NULL; tail_ptr = NULL; if (source_ptr == NULL) return; list_head_insert(head_ptr, source_ptr->data); tail_ptr = head_ptr; for (source_ptr = source_ptr->link; source_ptr != NULL ; source_ptr=source_ptr->link) { list_insert(tail_ptr, source_ptr->data); tail_ptr = tail_ptr->link; } } // FUNCTION 7 - copies part of a linked list starting from start_node ending in end_node (mine) void list_piece(Node* start_ptr, Node* end_ptr, Node*& head_ptr, Node*& tail_ptr) { Node * current_ptr; int CHECK=0; head_ptr = NULL; tail_ptr = NULL; if (start_ptr == NULL) return; if (end_ptr == NULL) return; //Check that start_ptr points to a node at or before end_ptr for (current_ptr= start_ptr; ( current_ptr != NULL && CHECK != 1 ); current_ptr=current_ptr->link) { if (current_ptr==end_ptr) CHECK=1; //cout<<"# "<data); tail_ptr = head_ptr; for (current_ptr= start_ptr->link; current_ptr != end_ptr->link; current_ptr=current_ptr->link) { list_insert(tail_ptr, current_ptr->data); tail_ptr = tail_ptr->link; } } // FUNCTION 8 - delete the head of the list only void list_head_remove(Node*& head_ptr) { Node *remove_ptr; remove_ptr=head_ptr; head_ptr=head_ptr->link; delete remove_ptr; } // FUNCTION 9 - delete a node from the list void list_node_remove(Node* previous_ptr) { Node *remove_ptr; remove_ptr=previous_ptr->link; // if (remove_ptr->link!=NULL) { previous_ptr->link = remove_ptr->link; // } //else previous_ptr->link = NULL; delete remove_ptr; } // FUNCTION 10 - discard the whole list void list_clear(Node* & head_ptr) { while (head_ptr != NULL) list_head_remove(head_ptr); } void print_list(Node* head_ptr) { Node *cursor = head_ptr; if (cursor==NULL) { cout<<"Empty list "; } else { while (cursor!=NULL) { cout<data<<" "; cursor = cursor->link; } } cout<data<<" "; cursor = cursor->link; } } fout<