1. Question:
The Problem Description has a single-chain table. You must delete the minimum value (assuming that the minimum value is unique ). If the deletion is successful, the minimum value is output. If the deletion fails, the value "not exist" is output ".
The Input contains multiple groups of data. Each group has n (0 <= n <100) elements in the first behavior single-chain table, and the second behavior is the elements in the single-chain table.
If the Output is successfully deleted, the minimum value of the Output is deleted. If the deletion fails, the Output is "not exist ".
Sample Input
84 2 6 -3 1 9 14 552 4 1 6 7
Sample Output
-31
2. Code:
# Include <iostream> using namespace std; struct Node {// defines the Node type int data; // data storage Node * next; // next points to the next Node }; class LinkList {private: Node * head; // defines the header Node public: LinkList (int a [], int n); // it constitutes a member function of a single-chain table ~ LinkList (); // release a single-chain table void Delete (); // Delete a single-chain table data member function}; LinkList: LinkList (int a [], int n) /// form parameters, which constitute a single-chain table {int I; head = new Node; // head Node * r, * s; // defines two Node pointers, A single * s is used to store related data, and * r is used to connect each * s to form a single-chain table r = head; // assign the head to r for related operations, avoid modifying head for (I = 0; I <n; I ++) {// I loop to form a single-chain table s = new Node; /// dynamically create the s structure space; s-> data = a [I]; // assign a [I] to s-> data; s-> next = r-> next; // assign r-> next to s-> next. In fact, this step is not required, but for security (this is a pointer) R-> next = s; // an s is formed above. Use r-> next to point to the next node, that is, s r = s; // OK, s has been connected to the linked list, and r has been moved to the node s of the new chain for the next connection node} r-> next = NULL; // the entire chain has been formed, finally, a null value is assigned to the last node, which is used to identify the last node so that an error occurs during linked list operations .} /// In fact, I have granted the LinkList: ~ to the last node when I loop ::~ LinkList () // release the single-chain table {Node * p, * q; // use p to move in the chain table and Use q to release nodes. P = head; while (p) {// move the "head Pointer" and release the linked list one by one until NULL q = p; p = p-> next; /// p points to the next Node delete q; // release Node} void LinkList: Delete () // delete single-chain table data {Node * q, * p, * r, * t; int min; // defines min for comparison. Find the minimum value to operate if (head-> next) {// use head-> next to determine whether the single-chain table is empty chain. if it is not empty chain, run the if statement; otherwise, run else q = head; // OK, if it is not an empty chain, assign the head node to the q node to avoid the head being changed. p = q-> next; /// use the p node as the next node of the q node min = p-> data; // assign the p-> data to min and start to find the minimum value Oh r = q; /// the previous q of p, assigned to r for saving While (p) {// loop to the NULL node, it ends the loop if (p-> data <min) {// compare the data and min size of the p node, select a small value. If p-> data is less than min, run the previous q of r = q; // p and assign it to r to save min = p-> data; /// assign p data to min} q = p; // move q from the previous node of p to p. p = p-> next; /// move the p node to the node pointed to by p-> next (next node of p)} // the smallest value is found through the above loop, how can I delete it? // The previous node of the node whose minimum value has been saved with r, then r-> next is the node whose minimum value is t = r-> next; /// t the node that saves the minimum value r-> next = t-> next; // The previous node of the node that uses the minimum value points to the next node of the minimum value node, then the minimum node is deleted from cout <t-> data <Endl; // output the minimum value of delete t; // the space of the smallest node has not been released. delete it!} Else // less than minimum Woo cout <"not exist" <endl;} int main () {int a [100], I, n; while (cin> n) {// number of data in the input single-link table for (I = 0; I <n; I ++) cin> a [I]; /// input data LinkList L (a, n); // defines the declared Object and references LinkList class L. delete (); // single-chain table, Delete data} return 0 ;}