/*Split from question about a working linked list ~ kenfitlike*/
It is just your code, but painted:
[edit]Code:#include <iostream> using namespace std; struct node { int x; node* next; }; void createList( node* root, int size ){ //Creates a list of any size for(int i = 0; i < size; i++){ root->x = i + 1; root->next = new node; //Creates a new node at the end of the list root = root->next; //Moves to that node root->next = 0; //Sets the end of the list at the current node } //for } void cleanList( node* root ){ //Clears the list from memory if( root != 0 ) { while( root->next != 0 ){ node* temp = root; //Assigns temp to be the current head root = root->next; //Moves the head to the next element delete temp; //Deletes the temporary head } //while } //if } void displayList( node* walker ){ //Displays all the elments of the list while(walker->next != 0 ){ cout << walker->x << endl; walker = walker->next; //Moves to the next element } //while } node* addToList( node* head, int value ){ //Adds a new node to the // beginning, the middle or the end // of the list node* search_ptr = head; //search_ptr points to the first node node* newNode = new node; //Creates new node //If the new value is less than or equal, or greater than the value in the first // node, then: if( value <= search_ptr->x ) { newNode->x = value; //Inserts this new value at the first node newNode->next = head; // and makes the next pointer in newNode // to point to the beginning of the list } else { node* past_node; //This node stores the last node accessed while( search_ptr->next != 0 ){ //While is not the end of the list past_node = search_ptr; //past_node equals the current node search_ptr = search_ptr->next; //Moves to the next node //If the value is less than or equal to the next value in the node, then // inserts the new node in the middle of the list if( value <= search_ptr->x ){ past_node->next = newNode; //Last element in the node points to the // new node newNode->x = value; //The new node gets the value newNode->next = search_ptr; //The next pointer of the new node points // to the next element in the list return head; //Returns the whole list } //if } //while //If we get to the last node in the list and the results from the comparissons // were all false, then it means that this value must be placed at the end of // the list if( head->next == 0 ){ newNode->x = value; //Inserts this new value at the first node newNode->next = head; // and makes the next pointer in newNode } else { newNode->x = value; //The new node gets the value search_ptr->next = newNode; //Search_ptr is now the last element in the // in the list, so we make the next pointer // of the last element to point to the newNode newNode->next = 0; // and newNode becomes the last element in the // list return head; //Returns the whole list } } return newNode; //Returns the address of the new first node } int main(){ node* head; //First node in the list head = new node; //Creates the first element createList( head, 1 ); head = addToList( head, 10 ); head = addToList( head, 6 ); displayList( head ); cleanList( head ); cin.get(); }
I am just testing my code painter.



LinkBack URL
About LinkBacks



forgot the oftopic tags