// sll.cpp: singly linked list // want: - search, insert, delete, maintain sortedness #include #include #include // for rand() and srand() #include // for time() using namespace std; /** * ----------------------------------------------------------------------------- * types & prototypes * ----------------------------------------------------------------------------- */ struct Node { Node* next; string payload; Node(string pl="", Node* n_ptr=NULL) : payload(pl), next(n_ptr) {}; }; void print_list(Node* ptr); // print all nodes starting from ptr Node* search(string key, Node* head); // returns NULL if not found void del_successor(Node* ptr); // delete the successor node void free_list(Node* ptr); // free nodes from ptr down Node* reverse_sll(Node* head); // reverse a linked list Node* insert_into_sorted_list(Node* head, Node* node_ptr); /** * ----------------------------------------------------------------------------- * main body * ----------------------------------------------------------------------------- */ int main() { Node* head = NULL, *temp; ostringstream oss; srand(static_cast(time(0))); // build a 10-node singly linked list for testing for (int i=0; i<10; i++) { oss.str(""); // clear buffer oss << "Node" << (rand() % 10); temp = new Node(oss.str()); head = insert_into_sorted_list(head, temp); } print_list(head); return 0; } /** * ----------------------------------------------------------------------------- * assume the list is already sorted, insert a new node with the given payload * into the list; return a pointer to the (potentially) new head * assume node_ptr points to an allocated node * ----------------------------------------------------------------------------- */ Node* insert_into_sorted_list(Node* head, Node* node_ptr) { if (head == NULL || node_ptr->payload < head->payload) { node_ptr->next = head; return node_ptr; } Node *prev = head, *temp = head->next; while (temp != NULL && temp->payload < node_ptr->payload) { prev = temp; temp = temp->next; } prev->next = node_ptr; node_ptr->next = temp; return head; } /** * ----------------------------------------------------------------------------- * reverse the list with given head pointer, return pointer to the new head. * ----------------------------------------------------------------------------- */ Node* reverse_sll(Node* head) { Node *prev = NULL, *temp; while (head != NULL) { temp = head->next; head->next = prev; prev = head; head = temp; } return prev; } /** * ----------------------------------------------------------------------------- * free the memory of all nodes starting from ptr down * ----------------------------------------------------------------------------- */ void free_list(Node* ptr) { Node* temp; while (ptr != NULL) { temp = ptr; ptr = ptr->next; delete temp; } } /** * ----------------------------------------------------------------------------- * delete the successor node of the node pointed to by ptr * ----------------------------------------------------------------------------- */ void del_successor(Node* ptr) { if (ptr == NULL || ptr->next == NULL) return; Node* temp = ptr->next; ptr->next = temp->next; delete temp; // always remember this } /** * ----------------------------------------------------------------------------- * search for the first occurrence of key in the list, starting from ptr * return NULL if not found, pointer to containing node is found * ----------------------------------------------------------------------------- */ Node* search(string key, Node* ptr) { while (ptr != NULL && ptr->payload != key) ptr = ptr->next; return ptr; } /** * ----------------------------------------------------------------------------- * search for the first occurrence of name in the list, starting from ptr * ----------------------------------------------------------------------------- */ void print_list(Node* ptr) { while (ptr != NULL) { cout << ptr->payload << " "; ptr = ptr->next; } cout << endl; }